2011-10-26 87 views
3

我创建了一个启动两种类型进程的进程处理程序: 用管理员用户名和密码提升的进程处理程序 另一个运行正常,没有任何用户名和密码输入。从高架子进程获取错误和标准输出

我struggeling弄清楚如何从高架过程获取输出。启动进程的应用程序不需要管理员凭据运行,管理员凭据将输入到单独加密的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, "<REPLACED>")); 
     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; 
} 

我试着用模仿类为好,有和没有加入到过程中的管理员凭据启动进程。假冒者类没有做任何事情,但告诉我,我没有acccess沉绵我冒充管理员...

我从这里冒领类:

http://freshclickmedia.co.uk/2008/11/programmatic-impersonation-in-c/

所以,如何在不升高的过程中从升级过程中获得标准和错误输出?

+0

您可以使用'Named Pipes':http://msdn.microsoft.com/en-us/library/bb546085.aspx – SepehrM

回答

1

无法除非黑客攻击的系统和/或利用一些bug和/或写一些内核级代码(即驱动程序)来规避这些安全措施......

只是想想这种可能性将意味着 - 因为总是在一个系统中的一些提升进程可能通过这样的手段来操纵......所以,答案是否定的海拔将变得毫无意义......

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

想想另一种不需要这种访问的设计...

+0

通过重定向输出到一个文件,然后你在想什么?通过将其添加到运行的命令或过程对象本身中? – Verzada

+0

通过添加'> C:\ MyLog.txt'到命令行的末尾... – Yahia

+0

尝试过了,没有工作。 虽然,我正在思考启动应用程序的其他方式。作为普通用户启动后,是否可以提升应用程序?还是必须重新启动? – Verzada

相关问题