I execute net user command from C# and map the network to the local computer.
Below is my code:
string netUse = #"net";
string DomainUser = "Domain\User";
string parameters = "use \"" + ServerLocation + "\" " + server.Password +
#" /USER:" + DomainUser + #" /PERSISTENT:NO";
ProcessStartInfo psi = new ProcessStartInfo(netUse, parameters);
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
Process p = Process.Start(psi);
I found that security sensor (Falcon) detected access with a plain password:
net.exe
ComandLine: "net" use "\ServerLocation\d$" Password /USER:
User /Persistent:NO
My question is:
Is it possible to hide password?
Related
I am tryin got execute a msdeploy.exe command using cmd from visual studio with c# as scripting language
string filename = #"C:\Deploy\Test\Test.zip";
string servername = #"PADEVSPTAPP";
string compname = #"IIS Web Application Name";
string appvalue = #"Test";
string strCmdText;
strCmdText = "msdeploy.exe -verb:sync -source:package=" + filename + " -dest=auto,computerName=" + servername + " -setParam=name=" + compname + ",value=" + appvalue + " -allowUntrusted";
//System.Diagnostics.Process.Start("CMD.exe", strCmdText);
try
{
System.Diagnostics.ProcessStartInfo procStartInfo =
new System.Diagnostics.ProcessStartInfo("cmd", "/c " + strCmdText);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
string result = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
Console.WriteLine(result);
}
catch (Exception objException)
{
Console.WriteLine(objException.ToString());
}
the string outcome is
msdeploy.exe -verb:sync -source:package="C:\\Deploy\\Test\\Test.zip"
-dest=auto,computerName="PADEVSPTAPP" -setParam=name="IIS Web Application Name",value="Test" -allowUntrusted
but this does not work due to \\ in the command.
How should i execute this command.
I even tried with powershell script ,which also did not work
string PS_script = #"$msdeploy = ""C:\\Program Files\\IIS\\Microsoft Web Deploy V3\\msdeploy.exe""
$package = """;
PS_script = PS_script + Row.deployfile;
PS_script = PS_script + #"""
$compname = ""PADEVSPTAPP""
$appname = ""IIS Web Application Name""
$appvalue = """;
PS_script = PS_script + changetype[0];
PS_script = PS_script + #"""
$md = $(""`""{0}`"" -verb:sync -source:package=`""{1}`"" -dest=auto,ComputerName=`""{2}`"" -setParam=name=`""{3}`"",value=`""{4}`"" -allowUntrusted"" -f $msdeploy, $package, $compname, $appname, $appvalue)
cmd.exe /C ""`""$md`""""";
I have no clue where I am going wrong.
You are using an equals sign where it should be colon.
It's supposed to be -dest: and not -dest=
Same with setParam, it's supposed to be -setParam: not -setParam=
I suspect you don't actually have double backslashes \\ in your string it will just look like that if you inspect via the debugger - I suspect thats whats throwing you off.
Since you have spaces in your compname variable you need double quotes in your arguments string (probably around all your variables would be a good idea).
Also try running msdeploy.exe directly instead of via cmd.exe /c.
I assumed your msdeploy.exe is located in C:\Program Files (x86)\IIS\Microsoft Web Deploy V3
The string outcome is:
-verb:sync -source:package="C:\Deploy\Test\Test.zip" -dest:auto,computerName="PADEVSPTAPP" -setParam:name="IIS Web Application Name",value="Test" -allowUntrusted
Put it all together:
string filename = #"C:\Deploy\Test\Test.zip";
string servername = #"PADEVSPTAPP";
string compname = #"IIS Web Application Name";
string appvalue = #"Test";
string strCmdText;
strCmdText = "-verb:sync -source:package=\"" + filename + "\" -dest:auto,computerName=\"" + servername + "\" -setParam:name=\"" + compname + "\",value=\"" + appvalue + "\" -allowUntrusted";
//System.Diagnostics.Process.Start("CMD.exe", strCmdText);
try
{
System.Diagnostics.ProcessStartInfo procStartInfo =
new System.Diagnostics.ProcessStartInfo(#"C:\Program Files (x86)\IIS\Microsoft Web Deploy V3\msdeploy.exe");
procStartInfo.Arguments = strCmdText;
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
string result = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
Console.WriteLine(result);
}
catch (Exception objException)
{
Console.WriteLine(objException.ToString());
}
BONUS INFO
If you need a more bulletproof way of determining where msdeploy.exe is located maybe have a look at these links:
https://gist.github.com/SergeyAxenov/15cf008531e6d0741533
How to find out what version of webdeploy/msdeploy is currently installed?
I tried to disconnect/connect my modem adapter, named as "конект", but it doesn't work, because adapter's name contains Russian letters. How to force it to work? Please help.
Connect("конект", "", "", true);
public static void Connect(string adapter, string login, string pass, bool discon)
{
string cmd = "";
if (discon)
{
cmd = "rasdial " + '"' + adapter + '"' + #" /disconnect";
}
else
{
cmd = "rasdial " + '"' + adapter + '"' + " " + login + " " + pass;
}
Cmd(cmd);
}
public static void Cmd(string URL)
{
ProcessStartInfo startInfo = new ProcessStartInfo("CMD.exe");
Process p = new Process();
startInfo.RedirectStandardInput = true;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.CreateNoWindow = true;
p = Process.Start(startInfo);
p.StandardInput.WriteLine(URL);
p.StandardInput.WriteLine(#"EXIT");
p.WaitForExit();
p.Close();
}
[I know that need just rename adapter with English letters and code will work, but I want to know how to force it work with Russian letters]
p.StandardInput.WriteLine(URL);
ProcessStartInfo is missing a StandardInputEncoding property. That makes it likely that a "URL" string that contains Cyrillic characters is going to get mangled if this code runs on machine that doesn't have a cyrillic code page as the default system code page.
You really want to avoid using input redirection here, it just isn't necessary. Use the /c command option for cmd.exe so that you can pass the command line directly:
startInfo.Arguments = "/c " + URL;
p = Process.Start(startInfo);
Fwiw, not necessary to use cmd.exe either, just run rasdial.exe directly.
I have a remote server name (windows), username and password.
Using C# .Net, I want to run a command on the remote server and get back the console output
Is there a way to do it in C#?
I was able to run the command using WMI with the following code (partial) but with no luck of getting the console output. I could only get back the Process ID.
ObjectGetOptions objectGetOptions = new ObjectGetOptions();
ManagementPath managementPath = new ManagementPath("Win32_Process");
ManagementClass processClass = new ManagementClass(scope, managementPath,objectGetOptions);
ManagementBaseObject inParams = processClass.GetMethodParameters("Create");
inParams["CommandLine"] = "cmd.exe /c "+ mycommand;
ManagementBaseObject outParams = processClass.InvokeMethod("Create", inParams, null);
Any Ideas?
This function is what I came up with after some research. Hope it helps someone else.
public string executeCommand(string serverName, string username, string password, string domain=null, string command)
{
try
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
if (null != username)
{
if (null != domain)
{
startInfo.Arguments = "/C \"psexec.exe \\\\" + serverName + " -u " + domain+"\\"+username + " -p " + password + " " + command + "\"";
}
else
{
startInfo.Arguments = "/C \"psexec.exe \\\\" + serverName + " -u " + username + " -p " + password + " " + command + "\"";
}
}
else
{
startInfo.Arguments = "/C \"utils\\psexec.exe "+serverName+" "+ command + "\"";
}
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
if (process.ExitCode == 0 && null != process && process.HasExited)
{
return process.StandardOutput.ReadToEnd();
}
else
{
return "Error running the command : "+command;
}
}
catch (Exception ex)
{
throw ex;
}
}
You can try executing a command with PsTools. One of many features they offer is PsExec. It allows you to run a command on a remote server. It should also return the results into a console (on local PC where it was run from).
I'm trying to create a RAM Directory via imdisk in C#. Since the cmd command is something like: imdisk -a -s 512M -m X: -p "/fs:ntfs /q /y"
I looked up how to process cmd commands with C# and found several hints regarding ProcessStartInfo(). This class works almost the way I intend it to, but since imdisk needs administrator priviliges I'm kinda stuck. Even though the code block is executed without exceptions, I don't see any new devices within the Windows Explorer.
try
{
string initializeDisk = "imdisk -a ";
string imdiskSize = "-s 1024M ";
string mountPoint = "-m "+ MountPoint + " ";
string formatHdd = "-p '/fs:ntfs /q /y' ";
SecureString password = new SecureString();
password.AppendChar('0');
password.AppendChar('8');
password.AppendChar('1');
password.AppendChar('5');
ProcessStartInfo procStartInfo = new ProcessStartInfo();
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
procStartInfo.RedirectStandardError = true;
procStartInfo.FileName = "cmd";
procStartInfo.Verb = "runas";
procStartInfo.UserName = "Admin";
procStartInfo.Password = password;
procStartInfo.Arguments = initializeDisk + imdiskSize + mountPoint + formatHdd;
Process.Start(procStartInfo);
catch (Exception objException)
{
Console.WriteLine(objException);
}
I hope someone can give me a little hint, right now I'm out of ideas.
Well I solved my problem in a different way. Somehow it seems that imdisk didn't format the new RamDisk the way it should and therefor no disk were created. As soon as I deleted the formatting option the disk is created and needs to be formatted. Therefore I started another process and used the cmd command "format Drive:"
For anyone who is interested, my solution is as follows:
class RamDisk
{
public const string MountPoint = "X:";
public void createRamDisk()
{
try
{
string initializeDisk = "imdisk -a ";
string imdiskSize = "-s 1024M ";
string mountPoint = "-m "+ MountPoint + " ";
ProcessStartInfo procStartInfo = new ProcessStartInfo();
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
procStartInfo.FileName = "cmd";
procStartInfo.Arguments = "/C " + initializeDisk + imdiskSize + mountPoint;
Process.Start(procStartInfo);
formatRAMDisk();
}
catch (Exception objException)
{
Console.WriteLine("There was an Error, while trying to create a ramdisk! Do you have imdisk installed?");
Console.WriteLine(objException);
}
}
/**
* since the format option with imdisk doesn't seem to work
* use the fomat X: command via cmd
*
* as I would say in german:
* "Von hinten durch die Brust ins Auge"
* **/
private void formatRAMDisk(){
string cmdFormatHDD = "format " + MountPoint + "/Q /FS:NTFS";
SecureString password = new SecureString();
password.AppendChar('0');
password.AppendChar('8');
password.AppendChar('1');
password.AppendChar('5');
ProcessStartInfo formatRAMDiskProcess = new ProcessStartInfo();
formatRAMDiskProcess.UseShellExecute = false;
formatRAMDiskProcess.CreateNoWindow = true;
formatRAMDiskProcess.RedirectStandardInput = true;
formatRAMDiskProcess.FileName = "cmd";
formatRAMDiskProcess.Verb = "runas";
formatRAMDiskProcess.UserName = "Administrator";
formatRAMDiskProcess.Password = password;
formatRAMDiskProcess.Arguments = "/C " + cmdFormatHDD;
Process process = Process.Start(formatRAMDiskProcess);
sendCMDInput(process);
}
private void sendCMDInput(Process process)
{
StreamWriter inputWriter = process.StandardInput;
inputWriter.WriteLine("J");
inputWriter.Flush();
inputWriter.WriteLine("RAMDisk for valueable data");
inputWriter.Flush();
}
public string getMountPoint()
{
return MountPoint;
}
}
Doesn't cmd.exe need to have the /C command line option passed through to run a command passed through as an argument? May well be that cmd.exe is just ignoring what you're passing through in procStartInfo.Arguments because you haven't prepended "/C " onto the front of the Arguments.
I realize that in Windows 7, it is not possible to save different credentials for the same host, but I need some workaround.
Can I provide the username and password manually in the code? Store them in a temp .rdp file?
Process rdcProcess = new Process();
rdcProcess.StartInfo.FileName = Environment.ExpandEnvironmentVariables(#"%SystemRoot%\system32\cmdkey.exe");
rdcProcess.StartInfo.Arguments = "/generic:TERMSRV/192.168.0.217 /user:" + "username" + " /pass:" + "password";
rdcProcess.Start();
rdcProcess.StartInfo.FileName = Environment.ExpandEnvironmentVariables(#"%SystemRoot%\system32\mstsc.exe");
rdcProcess.StartInfo.Arguments = "/v " + "192.168.0.217"; // ip or name of computer to connect
rdcProcess.Start();
The above code initiates a connection with .217 and I am not being prompted to provide a password.
Thanks for help.
If you want to use powershell you could add the credentials using
cmdkey /generic:DOMAIN/"computername or IP" /user:"username" /pass:"password"
Then call RDP connection using
Start-Process -FilePath "$env:windir\system32\mstsc.exe" -ArgumentList "/v:computer name/IP" -Wait
If you want to delete the credentials run
cmdkey /delete:DOMAIN/"Computer name or IP"
Remember to remove ""
This is an updated version from Krzysiek's post.
var rdcProcess = new Process
{
StartInfo =
{
FileName = Environment.ExpandEnvironmentVariables(#"%SystemRoot%\system32\cmdkey.exe"),
Arguments = String.Format(#"/generic:TERMSRV/{0} /user:{1} /pass:{2}",
fp.ipAddress,
(String.IsNullOrEmpty(fp.accountDomain)) ? fp.accountUserName : fp.accountDomain + "\\" + fp.accountUserName,
fp.accountPassword),
WindowStyle = ProcessWindowStyle.Hidden
}
};
rdcProcess.Start();
rdcProcess.StartInfo.FileName = Environment.ExpandEnvironmentVariables(#"%SystemRoot%\system32\mstsc.exe");
rdcProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
rdcProcess.StartInfo.Arguments = String.Format("/f /v {0}", fp.ipAddress); // ip or name of computer to connect
rdcProcess.Start();
While trying to figure out how to allow users into our network, without giving them the keys to the castle, I enabled Remote Desktop Access for a few members of my team. Thinking more about this, I quickly remembered a project several years ago while working for the Department of Defense. That project required us to "lock down" access to only necessary personnel and limited access to the programs on the servers. After spending some time on Microsoft's KnowledgeBase, we realized that we could create desktop "shortcuts" for those employees that made the RDP connection, logged them in and limited their access to one specific application on that server.
#echo off
cmdkey /generic:TERMSRV/"*IP or Server Name*" /user:%username%
start mstsc /v:*IP or Server Name*
cmdkey /delete:TERMSRV/"*IP or Server Name*"
quit
The accepted answer solves the problem, but has the side effect of leaving the credentials in the users credential store. I wound up creating an IDisposable so I can use the credentials in a using statement.
using (new RDPCredentials(Host, UserPrincipalName, Password))
{
/*Do the RDP work here*/
}
internal class RDPCredentials : IDisposable
{
private string Host { get; }
public RDPCredentials(string Host, string UserName, string Password)
{
var cmdkey = new Process
{
StartInfo =
{
FileName = Environment.ExpandEnvironmentVariables(#"%SystemRoot%\system32\cmdkey.exe"),
Arguments = $#"/list",
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = false,
RedirectStandardOutput = true
}
};
cmdkey.Start();
cmdkey.WaitForExit();
if (!cmdkey.StandardOutput.ReadToEnd().Contains($#"TERMSRV/{Host}"))
{
this.Host = Host;
cmdkey = new Process
{
StartInfo =
{
FileName = Environment.ExpandEnvironmentVariables(#"%SystemRoot%\system32\cmdkey.exe"),
Arguments = $#"/generic:TERMSRV/{Host} /user:{UserName} /pass:{Password}",
WindowStyle = ProcessWindowStyle.Hidden
}
};
cmdkey.Start();
}
}
public void Dispose()
{
if (Host != null)
{
var cmdkey = new Process
{
StartInfo =
{
FileName = Environment.ExpandEnvironmentVariables(#"%SystemRoot%\system32\cmdkey.exe"),
Arguments = $#"/delete:TERMSRV/{Host}",
WindowStyle = ProcessWindowStyle.Hidden
}
};
cmdkey.Start();
}
}
}
most of the answers are incorrect, it still request password and this because execute different processes on the same process instance.
using command line works perfectly:
string command = "/c cmdkey.exe /generic:" + ip
+ " /user:" + user + " /pass:" + password + " & mstsc.exe /v " + ip;
ProcessStartInfo info = new ProcessStartInfo("cmd.exe", command);
info.WindowStyle = ProcessWindowStyle.Hidden;
info.CreateNoWindow = true;
Process proc = new Process();
proc.StartInfo = info;
proc.Start();