Allow System Account to start impersonated process - c#

I am working on a project where I have a Windows service running under Local System Account.
What I want to do is to start another process (C++ application) that should write files to disk.
If I just use Process.Start, then the target application also runs under Local System Account, and as far as I know I can not simply write files anywhere.
For this reason I'm trying to make the target application run as a different user.
ProcessStartInfo psi = new ProcessStartInfo(#"C:\Path\to\Application.exe", parameters);
psi.UseShellExecute = false;
psi.UserName = "Username";
System.Security.SecureString sec = new System.Security.SecureString();
foreach (Char c Password)
sec.AppendChar(c);
psi.Password = sec;
psi.Domain = ".";
psi.LoadUserProfile = true;
Process.Start(psi);
But I am getting a Win32 Exception telling me the "Acces is denied".
Does anybody know how I can achieve my goal?

Check the file system permissions. Does the user you are using have execute permissions for the specified application?

Related

C# WPF, How to hide hard coded password?

I have an internal only Helpdesk program that has admin access to servers on the private network to pull logs, reboot hung servers, and perform other various admin tasks..
Rather than giving admin access to a significant number level 1 helpdesk users across hundreds of servers, my program runs under a single admin account, which access to this program is protected under a AD group, and launched only on a single Windows server via Citrix published app. So access to run the app is protected, but the password for the account with server access is hardcoded in the code using securestring.
I am not an expert level programmer. Just an advanced sysadmin with enough knowledge to do more things to accomplish my needs.
I know securestring is not recommended anymore, but i dont have the advanced knowledge to implement another password encryption solution without some help.
This is the code being used in my C# WPF app..
secureString pass = new NetworkCredential("", "hardcodedpassword").SecurePassword;
strCmdText = "command to be run here"
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = strCmdText;
process.StartInfo.UserName = "Helpdesk1";
process.StartInfo.Password = pass;
process.StartInfo.Domain = "Domain1";
process.StartInfo.ErrorDialog = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardError = false;
process.StartInfo.RedirectStandardOutput = false;
process.StartInfo.CreateNoWindow = true;
process.Start();
process.WaitForExit();
pass.Dispose();
I also have some users who are not very technical, but need to perform some limited tasks, like logging off stuck users from servers where you need admin access. That is why i do not want to assign Admin to these individual AD accounts. Having them access a single tool with the required access running on a single windows server solves that problem compared to giving all these users direct server admin access. So bottom line is I just want to be able to at least hide password in the code at least at the minimum, fully understanding its not the best approach. Thank you for the help!

C# / .Net: Run CMD as admin with provided credentials

i searched for this some hours today but i only find solutions that wont work.
Maybe it is impossible but let's give it a try:
I'm in a company and I will write some code so that a user can run the software whenever he need it. The software needs administrator-permissions. For example I've wrote some code to start the cmd as admin and create a folder at c:/Windows (you'll need admin-permission for that). The credentials for the admin account are right (we use Microsoft AD) but I only get "Access denied" in the cmd.
Does anyone know whether it is possible to get admin permission with hard coded credentials?
Note: Don't talk about security risks, the cmd is not the target software but it should demonstrate the problem.
My code:
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = #"/Kmkdir C:\Windows\_Test";
p.StartInfo.UserName = "admin";
System.Security.SecureString sPW = new System.Security.SecureString();
sPW.AppendChar('a');
sPW.AppendChar('b');
sPW.AppendChar('c');
p.StartInfo.Password = sPW;
p.StartInfo.UseShellExecute = false;
p.Start();
You are still getting an error because all admin accounts in newer windows versions (since Vista) technically are standard user accounts. The way administrative tasks are performed is through the User Account Control (UAC). It allows you to elevate permissions as administrator to perform administrative tasks. So yes, you are executing the process using an administrator account, but you did not elevate the process. To do so, add this parameter:
p.StartInfo.Verb = "runas";
p.StartInfo.UseShellExecute = true;
You can remove all other parameters regarding authentication, since all the authentication is handled by UAC. If for some reason you wish not to use UAC, then you probably will have to disable it, which is not recommended in most cases.

Azure - fail running external EXE

I am running a WebApi on Azure, in one of the request I need to run an external EXE, and return by its running result.
It works perfect on my local machine.
the exe reads a file, process it, and write the result to a new file to the local storage.
I have writing privileges (done writing of file and directory before)
I doubled checked the paths and the existence of the resources
all the paths look ok - example: D:\home\\site\wwwroot\myexe.exe
here is the call:
ProcessStartInfo si = new ProcessStartInfo();
si.WindowStyle = ProcessWindowStyle.Hidden;
si.UseShellExecute = false;
si.CreateNoWindow = true;
si.FileName = _pathToExe;
si.Arguments = _prathToArguments;
Process p = new Process();
p.StartInfo = si;
p.Start();
p.WaitForExit();
when debugging it I noticed that the process "completes" right away, and doesn't preform his work.
What am I missing? Am I allowed to run in this way an exe Azure? or should I use Work Role?
Thanks!!

How to run an elevated command as impersonated user within a domain

We have a lot of clients within our domain that are not constant connected with our network. Our users are working on small (sometimes disconnected) local networks with different types of network printers (usually provided by us). Within this small network there are also people NOT enlisted in our domain (partners, employers,..) that need these printers. Some of these users are local administrators but most are not.
My goal is to (dynamically) create an installer for each printer so the user may run this installer either from our network, usb, cd,.. so the local tcp/ip port is created, the printerdriver gets installed and the printer gets added.
My problem lies in obtaining sufficient rights to perform the installation.
(1) IF (and only IF!) the local user is an administrator, the installer should launch an elevated app to handel the installation.
(2) IF the user is a member of our domain but he is NOT a local administrator, the installer should use a local administrator account that was added by our policies.
I know how to run an elevated process which brings up the UAC for confirmation and i know how to impersonate another user..
But when trying to Elevate a command while impersonating i never get to see an elevation confirmation.. Which is logical and normal.
Any tips or tricks? Anyone?
What'ya know.. it seems to be possible after all. I first used an impersonation that fired the elevation prompt which didn't work. It's actually even more easy.
Here are the stripped down basics:
[FirstApp]
var str = "%My Administrator Password%";
var pwd = new System.Security.SecureString();
foreach(char c in str) pwd.AppendChar(c);
ProcessStartInfo psi = new ProcessStartInfo();
psi.UseShellExecute = false;
psi.WorkingDirectory = #"C:\path";
psi.FileName = #"C:\path\SecondApp.exe";
psi.UserName = "%My Administrator UserName%";
psi.Domain = "%My Administrator Domain%";
psi.Password = pwd;
psi.Verb = "runas";
var proc = new Process();
proc.StartInfo = psi;
proc.Start();
[SecondApp.exe]
ProcessStartInfo psi = new ProcessStartInfo();
psi.UseShellExecute = true;
psi.WorkingDirectory = #"C:\path";
psi.FileName = #"C:\path\ElevatedApp.exe";
psi.Verb = "runas";
var proc = new Process();
proc.StartInfo = psi;
proc.Start();
Works for me. The simple user that fires FirstApp.exe see's the elevation prompt that get's executed as an elevated administrator.
Credit go to J. Robbins : http://www.wintellect.com/blogs/jrobbins/elevate-a-process-at-the-command-line-in-vista

how to start a process as user then elevate in c# ASP.NET

I am working on a web application that install software on a server.
I can run the install by hand if I log in a user that is apart of a specific group then run install msi as administrator.
This specific group is apart of the local administrators group.
My app pool is running as Network_Service.
Do I impersonate then use the runAs verb? but then I need to know the u/p as user and u/p of Administrator .. I think
I am using the System.Diagnostics.Process classes.
System.Diagnostics.ProcessStartInfo oInfo = new System.Diagnostics.ProcessStartInfo(str);
oInfo.UseShellExecute = false;
oInfo.ErrorDialog = false;
oInfo.CreateNoWindow = false;
oInfo.RedirectStandardOutput = true;
Process p = System.Diagnostics.Process.Start(oInfo);
System.IO.StreamReader oReader2 = p.StandardOutput;
string sRes = oReader2.ReadToEnd();
oReader2.Close();
return sRes;
You need to set the UserName and Password properties to the login credentials of an Administrator account.
You can't change the user context of a running process later on. I suggest u use windows authentication and impersonation to be sure the web request is executed as the authenticated user and besides that you don't have to care about the user credentials.

Categories

Resources