从提升的子进程获取错误和标准输出

我创建了一个进程处理程序,它启动两种类型的进程:一个用管理员用户名和密码提升,另一个没有任何用户名和密码输入正常运行。

我很想知道如何从升级过程中获得输出。 启动该过程的应用程序不需要运行管理员凭据,管理员凭据将输入单独的加密xml文件,应用程序在脚本和需要管理员凭据的其他位置使用该文件。

由于应用程序是与普通用户一起运行的,因此访问应用程序已启动的已提升的进程似乎是不可能的。 我可以启动一个进程,我可以很容易地检查它是否已完成它所要求的内容,但我无法将其操作读取到字符串或日志。

public bool CreateProcessWithAdminRights(string filePath, string commandlineArgument, bool log) { if (!string.IsNullOrEmpty(filePath) && !string.IsNullOrEmpty(commandlineArgument) && _user.UserDataExsists()) { var securePassword = GetSecureString(_user.Password); ToolsProvider.Logger.Debug("Creating process with the following filepath: {0} and commandline argument: {1}", filePath, commandlineArgument.Replace(_user.Password, "")); ToolsProvider.Logger.Info("Creating Process with admin rights for {0} against {1}", _user.Name ); _proc = new Process { StartInfo = { FileName = @filePath, Arguments = commandlineArgument, ErrorDialog = false, RedirectStandardInput = false, RedirectStandardOutput = _log, RedirectStandardError = _log, UseShellExecute = false, CreateNoWindow = true, WindowStyle = ProcessWindowStyle.Hidden, UserName = _user.Name, Password = securePassword, Domain = _user.Domain } }; _proc.ErrorDataReceived += ErrorDataReceived; _proc.OutputDataReceived += OutputDataReceived; return true; } return false; } 

该过程使用以下方式开始:

 private bool StartProcess() { if (_proc != null) { try { _proc.Start(); _proc.BeginErrorReadLine(); _proc.BeginOutputReadLine(); _proc.WaitForExit(); _proc.CancelOutputRead(); _proc.CancelErrorRead(); if (_standardOutput.Length > 2) { // use writeline, the builder itself will add the DEBUG / info tag ToolsProvider.Logger.WriteLine(_standardOutput.ToString()); } if (_errorBuilder.Length > 2) { // use writeline, the builder itself will add the DEBUG / info tag ToolsProvider.Logger.WriteLine(_errorBuilder.ToString()); } return true; } catch (Win32Exception ex) { ToolsProvider.Logger.Error( "Missing file while trying to run an action: " + _proc.StartInfo.FileName, ex.Message); } } ToolsProvider.Logger.Error(""); return false; } 

我已尝试使用Impersonator类启动该过程,无论是否添加了管理员凭据。 模仿者没有做任何事情,只是告诉我我没有进行任何操作,尽管我冒充管理员……

我从这里得到了Impersonator课程:

Programmatic impersonation in C#

那么,如何在未提升的流程中从提升的流程获得标准和错误输出?

你不能通过攻击系统和/或利用一些bug和/或编写一些内核级代码(即驱动程序)来规避这些安全措施……

试想一下这种可能性意味着什么 – boost会变得毫无意义,因为系统中总会有一些升级的过程可以通过这种方式来操纵……所以答案是否定的……

您应该能够做的是将输出重定向到一个文件(例如> C:\MyLog.txt ),然后再读取该文件…

考虑一种不需要这种访问的不同设计……