user4604440
user4604440

Reputation:

run exe as system account

I am trying to run my c# exe as an system account. How can I do that. I've tried <requestedExecutionlevel level="requireAdministrator"> and <requestedExecutionlevel level="requireSystem"> The administrator is working but the second one is not working.

Please help me how can I do this.

Upvotes: 6

Views: 13722

Answers (3)

Boso Yolo
Boso Yolo

Reputation: 35

I know i am waaaay (7 years) late but this is what i found works. This peace of code basically runs the program specified as system user.

ProcessStartInfo startInfo = new ProcessStartInfo
    {
        FileName = "schtasks.exe",
        Arguments = $"/Create /RU \"NT AUTHORITY\\SYSTEM\" /RL HIGHEST /SC ONCE /TN MyTask /TR \"{program} {programArgs}\" /ST {DateTime.Now.AddSeconds(5):HH:mm:ss} /F",
        Verb = "runas", // Run the process as an administrator
        CreateNoWindow = false,
        UseShellExecute = false
    };

    Process process = Process.Start(startInfo);
    process.WaitForExit();
    Console.WriteLine(process.ExitCode);

    startInfo = new ProcessStartInfo
    {
        FileName = "schtasks.exe",
        Arguments = $"/Run /TN MyTask",
        Verb = "runas", // Run the process as an administrator
        CreateNoWindow = true,
        UseShellExecute = false
    };

    process = Process.Start(startInfo);
    process.WaitForExit();
    Console.WriteLine(process.ExitCode);

    startInfo = new ProcessStartInfo
    {
        FileName = "schtasks.exe",
        Arguments = $"/Delete /TN MyTask /F",
        Verb = "runas", // Run the process as an administrator
        CreateNoWindow = true,
        UseShellExecute = false
    };
    process = Process.Start(startInfo);
    process.WaitForExit();
    Console.WriteLine(process.ExitCode);

    Console.ReadLine();

This code will execute any program with any args as nt authority\system. Keep in mind it is incompatible with window display (will allways run in the background). We can verify if this works by defining a cmd to run, that creates a file with the output of "whoami":

string program = Environment.GetEnvironmentVariable("ComSpec"); // Path to cmd.exe
string programArgs = $"/C whoami > \"C:\\Users\\{Enviroment.UserName}\\Desktop\\text.txt\"";

You can now find your text.txt file on your desktop with the text nt authority\system in it

Upvotes: 3

Jcl
Jcl

Reputation: 28272

It's not directly possible. There are hacky ways if you are an administrator (like getting the security token from an already running service or something), but I far from recommend using those.

The point of the SYSTEM account is precisely that: that it's only run directly by the system.

If you want an easy-hacky-way without third-party tools (like psexec), you could set up a ONEEVENT scheduled task (with schtasks, which is part of the OS), which can indeed run with the system account. This would still need two processes (although it could be the same exe with different command line parameters for the task and for setting it up), but it'd work.

Upvotes: 2

Scott Chamberlain
Scott Chamberlain

Reputation: 127603

To the best of my knowledge you can not force your app to run as SYSTEM. Either your app must be a service and the service is setup to run as System or you must use a tool like PsExec to launch your executable as system.

psexec.exe -i -s YourProgram.exe

When using requestedExecutionlevel the only 3 valid options are

  • requireAdministrator - prompt for UAC always (even if the user is not an administrator).
  • asInvoker - never prompt for UAC.
  • highestAvailable - prompt for UAC if the user is a member of the Administrators group but do not prompt and run as a normal user if the user is not a member of the group.

Upvotes: 9

Related Questions