run mmc.exe services.msc " from iis - c#

http://www.lansweeper.com/ has features that let user do some actions such as Remote Computer management and etc from web application. I need to deploy it in my Asp.Net c# web application. I do it by the code blow when I build my project it works but when I trying to run it from IIS it doesn't works, just nothing happens.
Any help and idea please. No error encountered
protected void lnkComputermanagement_Click(object sender, EventArgs e)
{
try
{
long InfoID = Convert.ToInt64(txtInfoID.Text);
IBAPanel.NetworkSupport.WMIInfo WMIInfo = new IBAPanel.NetworkSupport.WMIInfo();
WMIInfo = WMIInfo.Get(Convert.ToInt32(InfoID));
string ComName = WMIInfo.ComputerName1;
string Pass = WMIInfo.Password;
if (WMIInfo.UserName != "" && WMIInfo.ComputerName1 != "" && WMIInfo.Password != "")
{
string Usname = WMIInfo.UserName.Substring(2);
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 = Usname;
myprocess.StartInfo.Password = MakeSecureString(Pass);
myprocess.StartInfo.Arguments = "/C mmc.exe compmgmt.msc /computer:" + ComName;
myprocess.Start();
myprocess.Close();
}
Response.Redirect("content.aspx?lan=fa&gid=524&InfoID=" + InfoID);
ActionJquery();
}
catch (Exception ex)
{
System.Text.StringBuilder cstext1 = new System.Text.StringBuilder();
cstext1.Append(" $(document).ready(function () {");
cstext1.Append("alert(" + ex.Message + ") ");
cstext1.Append(" }); ");
Page.ClientScript.RegisterStartupScript(typeof(Page), "", cstext1.ToString(), true);
}
}

You are starting the command "mmc.exe" on the webserver and not on the client browsing the website. If you look at the taskmanager on the webserver you will see multiple mmc.exe processes that are running.

To manage Windows Services from Web you need special permission and you can't go through Services.msc, you need to use WMI (Windows management instruments) and you need to give the application pool higher permission but the best way is to impersionate your piece of code where you fetch for sevices information.
A good open source project with full WMI usage is Services+ on codeplex:
Services+ On CodePlex
Thanks

Related

Win C#: Install/Run app as administrator without UAC prompt

I have a requirement where application (app1) needs to install / run another application (app2) on user system in non production hours without UAC prompt. I think I can do it with windows service which will elevate my application 1 (app1) which will install my app 2. I worte below code in my console app which will called by windows service :
SecureString ss = ConvertStr.ConvertToSecureString("****");
DirectoryInfo d = new DirectoryInfo(userDownloadFolder);
string[] extensions = new[] { ".bat", ".exe" };
FileInfo[] Files =
d.GetFiles()
.Where(f => extensions.Contains(f.Extension.ToLower()))
.ToArray();
foreach (FileInfo file in Files)
{
if (File.Exists(d.FullName + file.Name))
{
try
{
var proc = new Process();
proc.StartInfo.FileName = d.FullName + file.Name;
proc.StartInfo.Domain = "****";
proc.StartInfo.UserName = "**********";
proc.StartInfo.Password = ss;
proc.StartInfo.Verb = "runas";
proc.StartInfo.UseShellExecute = false;
proc.Start();
proc.WaitForExit();
var exitCode = proc.ExitCode;
proc.Close();
}
catch (Exception ex)
{
Logger.WriteToFile(ex.Message);
}
}
else
{
Logger.WriteToFile("File does not exists");
}
Console application runs but it gives Access is denied. But when I tried to install /run by manually right clicking app with user Id & password in UAC prompt it works. Any idea any other way to implement this.

Show the output of Powershell script in a WPF application

I have a WPF application that executes a Powershell script and then displays a message in a textbox in the application. The Powershell script is executed like this:
string scriptPath = folderPath + "/EXECUTE.ps1";
string powershellPath = #"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe";
string outputLog = folderPath + "output.log";
bool is64 = IntPtr.Size == 8;
var ENV = "Get-ItemProperty HKLM:\\Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\* "
+ (is64 ? ",HKLM:\\Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\*" : "")
+ " | Select-Object DisplayName";
ProcessStartInfo startInstall = new ProcessStartInfo(powershellPath, ENV);
startInstall.UseShellExecute = false;
startInstall.Arguments = scriptPath;
startInstall.EnvironmentVariables.Add("RedirectStandardOutput", "true");
startInstall.EnvironmentVariables.Add("RedirectStandardError", "true");
startInstall.EnvironmentVariables.Add("UseShellExecute", "false");
startInstall.EnvironmentVariables.Add("CreateNoWindow", "true");
Process Install = Process.Start(startInstall);
Install.Close();
Console.WriteLine("Script executed successfully");
Console.WriteLine("Output available at: " + outputLog);
The last two lines in Console.WriteLine are printed out in a textbox. I was wondering, is there a way that I can show the output of the Powershell terminal window inside of this textbox named txtboxExecutionResult? I am not executing any Powershell command from my application, just starting and executing the EXECUTE.ps1 file from the application. I'd really appreciate any help to point me in some direction.
I had a similar issue once with reading output from another application and I solved it by reading the standard output of the process.
Process process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = false;
process.StartInfo.CreateNoWindow = false;
process.StartInfo.FileName = "example.exe";
process.StartInfo.Arguments = args;
process.Start();
output = await process.StandardOutput.ReadToEndAsync();

.Net 5 WorkerService Run Other APP Not Work Well

I setting my WorkerService appsettings.json like below
"AppRun": {
"App1": {
"AppName": "CheckDataForm",
"AppPath": "D:\\2021-Project\\Project\\CheckDataForm-MSSQL\\CheckDataForm\\bin\\Debug\\net5.0-windows\\CheckDataForm.exe"
},
"App2": {
"AppName": "notepad++",
"AppPath": "C:\\Program Files\\Notepad++\\notepad++.exe"
},
and
call the app like this:
//i is foreach count
var AppName = _config["AppRun:App"+i+":AppName"];
var AppPath = _config["AppRun:App" + i + ":AppPath"];
//check file exist
var fileExist = System.IO.File.Exists(AppPath);
if ( !String.IsNullOrEmpty (AppName) && !String.IsNullOrEmpty(AppPath) &&
fileExist )
{
//find APP
var processApp = Process.GetProcessesByName(AppName);
//can't find app
if (processApp.Length <=0 )
{
try
{
Process proc = new Process();
proc.StartInfo.FileName = AppPath;
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.Verb = "runas";
proc.Start();
}
catch (Exception ex)
{
var error = ex;
}
}
}
the Process can Start notepad++ , but can't start CheckDataForm .net core windowsForm app, and no exception ,
(I have added to RegistryKey , .netframework 4.8 Ver work well , but .net core 5 Ver have no any Responds and no error)
but I can Manual to run CheckDataForm , and work well,
have any idea to fix it, thank you
If I make assumption that you are not able to load reference when running .net core application then you have to add following line overthere.
Process proc = new Process();
proc.StartInfo.FileName = AppPath;
proc.StartInfo.WorkingDirectory = System.IO.Path.GetDirectoryName(AppPath);
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.Verb = "runas";
proc.Start();
You have to set WorkingDirectory.

Running .cmd file by C#

I am using the mentioned code to run a cmd file. It is working properly in my local machine. However when I am running in a remote machine windows security warning is coming. How can i bypass that security warning. Any help?
string[] newFilePath = Directory.GetFiles(workingDir, "*.cmd");
foreach (var n in newFilePath) {
finishedOld = false;
Process p = new Process();
p.StartInfo.FileName = string.Format("\"" + n + "\"");
p.Start();
p.WaitForExit();
p.Dispose();
finishedOld = true;
}

Installing a MSI as administrator silently

I am trying to create an application which will install the msi from the c# windows application, here i wanna take the input from user for UserName, Domain and password so that i can run the application in that user account. in the below code if i only give startInfo.Verb = "runas" its working but i want to provide the user name and password of admin and run it. can you guyz help me out.
private void InstallProbe()
{
try
{
bool gdfg= IsRunAsAdmin();
//processObj.InitializeProcess(txtUserName.Text, txtPassword.Text);
string installcmd = "/c msiexec /i \"{0}\" /quiet TARGETDIR=\"{1}\" HOST=\"{2}\" PORT=\"{3}\" USEHTTPS=\"{4}\" STEALTHMODE=\"{5}\" ORGCODE=\"{6}\"";
installcmd = string.Format(installcmd, txtFilePath.Text, #"%PROGRAMFILES%\ProHance Mate", "services.jamochatech.com", "8080", false, 0, "PHSALE");
string uname, domain = string.Empty;
//RunCommand("cmd", installcmd, processObj);
if (txtUserName.Text.IndexOf("\\") > 0)
{
string[] strarr = txtUserName.Text.Split('\\');
uname = strarr[1];
domain = strarr[0];
}
else
{
uname = txtUserName.Text;
domain = ".";
}
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
//startInfo.Verb = "runas";
startInfo.Domain = domain;
startInfo.UserName = uname;
startInfo.Password = ToSecureString(txtPassword.Text);
startInfo.FileName = "cmd";
startInfo.Arguments = installcmd;
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.LoadUserProfile = true;
MessageBox.Show(installcmd);
process.StartInfo = startInfo;
process.Start();
process.WaitForExit(60000);
}
catch (Exception ex)
{
MessageBox.Show("Exception occured while installing the ProHance Mate " + ex.Message);
}
}
Disregarding the MSI context, you are simply trying to launch a new process (msiexec.exe) under a specific user context. Check the thread below and others alike.
In Windows: How do you programatically launch a process in administrator mode under another user context?

Categories

Resources