BlueBarren BlueBarren - 4 days ago 6
C# Question

Writing a cmd Command in a Console Application

I'm trying to write a C# console application that will launch

runas.exe
through cmd then run another application as that user. I've taken one of the suggestions listed below (and added a little bit) as it seems the most promising.

Process cmd = new Process();
ProcessStartInfo startinfo = new ProcessStartInfo("cmd.exe", @"/K C:\Windows\System32\runas.exe /noprofile /user:DOMAIN\USER'c:\windows\system32\notepad.exe\'")
{
RedirectStandardInput = true,
UseShellExecute = false
};
cmd.StartInfo = startinfo;
cmd.Start();
StreamWriter stdInputWriter = cmd.StandardInput;
stdInputWriter.Write("PASSWORD");
cmd.WaitForExit();


When I launch the application it asks for the password before the command itself causing there to be an error with
runas.exe


enter image description here

I'm pretty sure
UseShellExecute = false
is causing the error but the
StreamWriter
doesn't work without it so I'm not sure what to do.

Answer

I found a solution in the following post. I had to forgo most of the structure I was working with but I can now successfully run notepad as my desired user with the code listed below.

var pass = new SecureString();
pass.AppendChar('p');
pass.AppendChar('a');
pass.AppendChar('s');
pass.AppendChar('s');
pass.AppendChar('w');
pass.AppendChar('o');
pass.AppendChar('r');
pass.AppendChar('d');
var runFileAsUser = new ProcessStartInfo
{
    FileName = "notepad",
    UserName = "username",
    Domain = "domain",
    Password = pass,
    UseShellExecute = false,
    RedirectStandardOutput = true,
    RedirectStandardError = true
};
Process.Start(runFileAsUser);
Comments