pur*_*ang 8 .net c# privileges command-line process
这是我的代码:
try
{
ProcessStartInfo procStartInfo = new ProcessStartInfo(
"cmd.exe",
"/c " + command);
procStartInfo.UseShellExecute = true;
procStartInfo.CreateNoWindow = true;
procStartInfo.Verb = "runas";
procStartInfo.Arguments = "/env /user:" + "Administrator" + " cmd" + command;
///command contains the command to be executed in cmd
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Run Code Online (Sandbox Code Playgroud)
我想保留
procStartInfo.UseShellExecute = true
procStartInfo.RedirectStandardInput = false;
Run Code Online (Sandbox Code Playgroud)
是否可以在不使用的情况下执行命令process.standardinput?我尝试执行命令我已经传入参数,但命令没有执行.
正如@mtijn所说,你有很多事情要做,以后你也会压倒一切.你还需要确保你正确地逃避了事情.
假设您要运行以下命令提升:
dir c:\
Run Code Online (Sandbox Code Playgroud)
首先,如果您只是通过Process.Start()一个窗口运行此命令,则会立即打开和关闭,因为没有任何东西可以保持窗口打开.它处理命令并退出.要保持窗口打开,我们可以将命令包装在单独的命令窗口中,并使用/K开关使其保持运行:
cmd /K "dir c:\"
Run Code Online (Sandbox Code Playgroud)
为了运行该命令,我们可以runas.exe像你一样使用,除了我们需要更多地逃避一些事情.根据帮助文档(runas /?),我们传递给的命令中的任何引号都runas需要使用反斜杠进行转义.不幸的是,使用上面的命令执行此操作会给我们一个双重反斜杠,这会使cmd解析器混淆,因此也需要进行转义.所以上面的命令将最终成为:
cmd /K \"dir c:\\\"
Run Code Online (Sandbox Code Playgroud)
最后,使用您提供的语法,我们可以将所有内容包装到一个runas命令中,并将上面的命令括在另一组引号中:
runas /env /user:Administrator "cmd /K \"dir c:\\\""
Run Code Online (Sandbox Code Playgroud)
从命令提示符运行上面的命令,以确保它按预期方式工作.
鉴于所有这些,最终代码变得更容易组装:
//Assuming that we want to run the following command:
//dir c:\
//The command that we want to run
string subCommand = @"dir";
//The arguments to the command that we want to run
string subCommandArgs = @"c:\";
//I am wrapping everything in a CMD /K command so that I can see the output and so that it stays up after executing
//Note: arguments in the sub command need to have their backslashes escaped which is taken care of below
string subCommandFinal = @"cmd /K \""" + subCommand.Replace(@"\", @"\\") + " " + subCommandArgs.Replace(@"\", @"\\") + @"\""";
//Run the runas command directly
ProcessStartInfo procStartInfo = new ProcessStartInfo("runas.exe");
procStartInfo.UseShellExecute = true;
procStartInfo.CreateNoWindow = true;
//Create our arguments
string finalArgs = @"/env /user:Administrator """ + subCommandFinal + @"""";
procStartInfo.Arguments = finalArgs;
//command contains the command to be executed in cmd
using (System.Diagnostics.Process proc = new System.Diagnostics.Process())
{
proc.StartInfo = procStartInfo;
proc.Start();
}
Run Code Online (Sandbox Code Playgroud)
为什么要使用参数初始化流程对象,然后覆盖这些参数?顺便说一句:您设置参数的最后一位将“command”连接到“cmd”,这没有多大意义,可能是它失败的地方(看起来您缺少一个空格)。
此外,您当前正在使用标准命令行,您可能想考虑使用runas 工具。您还可以从命令行调用 runas。
另外,为什么要从命令行运行“命令”?为什么不使用当时提供的管理员权限直接从 Process.Start 启动它呢?这是一些伪代码:
Process p = Process.Start(new ProcessStartInfo()
{
FileName = <your executable>,
Arguments = <any arguments>,
UserName = "Administrator",
Password = <password>,
UseShellExecute = false,
WorkingDirectory = <directory of your executable>
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
42093 次 |
| 最近记录: |