Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run elevated process

I am trying to run a cmd command with the following code:

ProcessStartInfo cmd = new ProcessStartInfo("cmd.exe");
cmd.RedirectStandardInput = true;
cmd.RedirectStandardOutput = true;
cmd.RedirectStandardError = true;
cmd.UseShellExecute = false;
cmd.CreateNoWindow = true;
cmd.WindowStyle = ProcessWindowStyle.Hidden;
Process exec = Process.Start(cmd);
exec.StandardInput.WriteLine("sc create \"BaliService\" binPath= \"{0}\\BaliService.exe\"", Directory.GetCurrentDirectory());

This command requires admin privelages, if I run cmd as administrator and type the command it works perfectly but not when I run this app as admin. I have added

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

to a manifest file which prompts uac each time I open the exe.

I have seen multiple questions on this and they all seem to suggest any processes run under an elevated app will have the same rights but this isn't working for me.

I have tried cmd.Verb = "runas"; but no dice.

like image 565
Bali C Avatar asked Dec 31 '11 21:12

Bali C


People also ask

What does elevated mean in Task Manager?

Start Task Manager and switch to the Details tab. The new Task Manager has a column called "Elevated" which directly informs you which processes are running as administrator.

How do I run as elevated user?

In the search results window, under Programs, right-click on the program cmd.exe. In the pop-up menu, select Run As Administrator. If a User Access Control window appears, log in with a Windows user account that has full Administrator access rights. An Elevated Command Prompt window should now open.

How do I run a program with elevated permissions?

To run apps elevated from the taskbar, use these steps: Right-click the app in the taskbar. Right-click the app name. Select the Run as administrator option.


2 Answers

You need to set UseShellExecute to true for the Verb to be respected and it must be set to 'false' to redirect standard output. You can't do both.

I'm pretty sure Windows also won't allow you to redirect standard input/output/error across the admin/non-admin security boundary. You'll have to find a different way to get output from the program running as admin.

I didn't read this article, but this may give you more information: http://www.codeproject.com/KB/vista-security/UAC__The_Definitive_Guide.aspx

like image 72
shf301 Avatar answered Oct 03 '22 21:10

shf301


Did you try assigning administrative credentials to your ProcessStartInfo object?

SecureString password = new SecureString();
password.AppendChar('p');
password.AppendChar('w');
cmd.UserName = "admin";
cmd.Password = password;
like image 23
Abbas Avatar answered Sep 30 '22 21:09

Abbas