C# Start Program with ADMIN rights from other program [duplicate] - c#

This question already has answers here:
Elevating privileges doesn't work with UseShellExecute=false
(3 answers)
Closed 1 year ago.
I´ve written a notify program, which communicats with an windows service on WebSocket. This notify program does not have admin rights but with click on button starts an other program which starts and stops the service. This program should run with admin rights, it worked very fine for some time but somehow not any more.
Thats my code snippet:
using (var process = new Process())
{
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = $".\\ServiceControl.exe";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.Arguments = $"-s {ServiceName} -start";
process.StartInfo.Verb = "runas";
process.Start();
}
The exception is the following:
System.ComponentModel.Win32Exception: "Der angeforderte Vorgang erfordert erhöhte Rechte"
Back then it showed a window where I could accept admin rights but does not any more.

Try adding this line
process.StartInfo.UseShellExecute = true;
on
using (var process = new Process())
{
process.StartInfo.UseShellExecute = true;
process.StartInfo.FileName = $".\\ServiceControl.exe";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.Arguments = $"-s {ServiceName} -start";
process.StartInfo.Verb = "runas";
process.Start();
}

Related

ASP.Net run exe on server from an API call

I have a WebAPI that works great but need to add the ability to call to an EXE on the server to run some tasks with Video, the server is our machine and running IIS to host the WebAPI.
I have tried it working with Process() and the calls make it to the cmd.exe file I have written but the issue is that the user is IUSER and this won't work as the Video processing needs to use system hardware so needs to be the current logged in Windows User.
I don't want to give the IUSER this privilege for obvious security reasons so I am looking for another way to call and pass data to the an EXE or background task (Short running <3seconds) and for that process to reply with the results.
It's all on the server which we have full control over.
Current Code:
string exeLoc = Environment.ExpandEnvironmentVariables(baseEXELocation);
using var process = new Process();
process.StartInfo.FileName = exeLoc;
process.StartInfo.Arguments = $"{command}";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.OutputDataReceived += (sender, data) =>
{
if (!String.IsNullOrEmpty(data.Data))
{
Console.WriteLine(data.Data);
consoleData += data.Data;
}
};
process.Start();
process.BeginOutputReadLine();
var exited = process.WaitForExit(1000 * 10); // (optional) wait up to 10 seconds
Thanks
You could try to use the impersonation in iis:
<identity impersonate="true" />
https://support.microsoft.com/en-us/help/306158/how-to-implement-impersonation-in-an-asp-net-application
or assign the administrator permission to the application pool by using application pool advance setting -> identity to the custom account.
or You can try using the verb runas in Process.Start to execute the exe file as an Administrator.
ProcessStartInfo proc = new ProcessStartInfo();
proc.WindowStyle = ProcessWindowStyle.Normal;
proc.FileName = myExePath;
proc.CreateNoWindow = false;
proc.UseShellExecute = false;
proc.Verb = "runas"; //this is how you pass this verb

Why does Kaspersky anti-virus prevent my program from capturing the output of its child process?

I have a C# program that launches a child process and captures its output in a string. This works on most Windows machines (Windows 7 and newer), but when Kaspersky anti-virus is present, Process.StandardOutput.ReadToEnd() returns null. There is no error code or exception. The child process is a trusted console application. The process takes 5 or 6 seconds to run.
The code for launching the child process is as follows:
ProcessStartInfo psi = new ProcessStartInfo();
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.FileName = "icao.exe";
psi.Arguments = im_path + "image.jpg";
Process p = new Process();
p.StartInfo = psi;
p.Start();
string output = p.StandardOutput.ReadToEnd();
string error = p.StandardError.ReadToEnd();
MessageBox.Show(error);
p.WaitForExit();
int exitCode = p.ExitCode;
MessageBox.Show(exitCode+"");
Why does output end up being null when Kaspersky is present?
My guess is that Kaspersky's heuristics are seeing that your program wants to execute another exe. Because nothing is telling Kaspersky that this is ok, it flags your program as possible malware, because it wants to interface with other programs that are developed by other companies. If you are able to I would try white listing your program with Kaspersky and see if that solves your issue.

How to run command line with admin right in C# [duplicate]

This question already has answers here:
Elevating process privilege programmatically?
(6 answers)
Execute several elevated commands in shell (or similar)
(1 answer)
How to run multiple lines in cmd as administrator using C#?
(2 answers)
How do I make a console app always run as an administrator?
(3 answers)
Closed 5 years ago.
I have referenced some other topics and it comes up the following coding, yet it doesn't work. I allow it creates command window and use "/k" argument keeping the window opened so that I can trace its output.
However, I can see the warning from the window that the command needs admin rights to execute. How can I execute it in admin rights?
public void ClearArpCache () {
ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd.exe", "/user:Administrator \" cmd /k arp -d \""); // same as "netsh interface ip delete arpcache"
processStartInfo.RedirectStandardOutput = true;
//processStartInfo.CreateNoWindow = true;
//processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
processStartInfo.UseShellExecute = false;
processStartInfo.StandardOutputEncoding = Encoding.Default;
processStartInfo.Verb = "runas";
Process.Start (processStartInfo);
UnityEngine.Debug.Log ("ARP table cache cleared.");
}
EDITED:
Try changing from "cmd.exe" to "runas.exe"
public void ClearArpCache () {
ProcessStartInfo processStartInfo = new ProcessStartInfo("runas.exe", "/user:Administrator \" cmd /k arp -d \""); // same as "netsh interface ip delete arpcache"
processStartInfo.RedirectStandardOutput = true;
//processStartInfo.CreateNoWindow = true;
//processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
processStartInfo.UseShellExecute = false;
processStartInfo.StandardOutputEncoding = Encoding.Default;
Process.Start (processStartInfo);
UnityEngine.Debug.Log ("ARP table cache cleared.");
}
Just clarified why it failed using "runas". In order to use "runas" in Verb, UseShellExecute must be firstly turned true.
When executing following function, it will pop a window asking for admin rights, just click 'Yes' to begin the execution. Though it is beyond the scope, if possible I want to skip the pop-up window as well.
public void ClearArpCache () {
ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd.exe", "/k arp -d"); // same as "netsh interface ip delete arpcache"
processStartInfo.CreateNoWindow = true;
processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
processStartInfo.UseShellExecute = true;
processStartInfo.Verb = "runas";
Process.Start (processStartInfo);
UnityEngine.Debug.Log ("ARP table cache cleared.");
}

connect compmgmnt.msc to remote host using c#

how can i use compmgmnt.msc to remote host using c#.
i found this...
execute this command in "cmd.exe" it is working and ask you for The password:
/user:administrator \"mmc.exe compmgmt.msc /computer:IZADI-PC\
but i need to know how can i use this command in c#.
i also need to pass the password to this command using c#.
i have username and password of the remote computer and i wanna do everything programatically.
I also visited :
http://www.lansweeper.com/forum/yaf_postst5116_Runas-Custom-Actions.aspx
Process.Start with different credentials with UAC on
Thank you in advanced!!!
anyone write sample code to excecute /user:administrator \"mmc.exe compmgmt.msc /computer:IZADI-PC\ in c#
ProcessStartInfo startInfo = new ProcessStartInfo();
Process myprocess = new Process();
myprocess.StartInfo.CreateNoWindow = true;
myprocess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
myprocess.StartInfo.UseShellExecute = false;
myprocess.StartInfo.Verb = "runas";
myprocess.StartInfo.FileName = "cmd.exe";
myprocess.StartInfo.UserName = "administrator";
myprocess.StartInfo.Password = MakeSecureString("123456");
myprocess.StartInfo.Arguments = "/k mmc.exe compmgmt.msc /computer:PC135-PC";
myprocess.Start();
myprocess.Close();

getting osk.exe to run from C#

I have VS2010, C# program that is setup to build as x86. I have two PCs where they are running. Both are Win 7 Prof, SP1, 32 bits. Both VS2010s are running at Admin level. Within my project I try to execute the line:
Process.Start("c:\\Windows\\System32\\osk.exe"); //win 7 on-screen keyboard
From debug mode-run, on one system it runs fine, on the other, an exception is thrown:
The specified executable is not a valid application for this OS platform.
I have the user control setting in Win 7--User Accounts to "never notify" as suggested from other sites, that did not work.
I have tried: (same result, fail)
Process process = new Process();
process.StartInfo.UseShellExecute = false; //have tried it true also
process.StartInfo.WorkingDirectory = "c:\\";
process.StartInfo.FileName = "c:\\WINDOWS\\system32\\osk.exe";
process.StartInfo.Verb = "runas";
process.Start();
Any ideas what needs to be changed (or do)?
Will this work for you? I altered it a bit using the System.Diagnostics.ProcessStartInfo Class
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.WorkingDirectory = #"c:\WINDOWS\system32\";
startInfo.FileName = "osk.exe";
startInfo.Verb = "runas";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
try
{
using(Process process = Process.Start(startInfo))
{
process.WaitForExit();
}
}
catch (Exception)
{
//throw;
}

Categories

Resources