以不同的用户身份启动.Net流程

Ibr*_*GUN 20 c# impersonation

我想启动一个具有管理员权限的进程.当我运行以下代码时,Process抱怨说它需要管理员权限:

public class ImpersonationHelper : IDisposable
{
    IntPtr m_tokenHandle = new IntPtr(0);
    WindowsImpersonationContext m_impersonatedUser;

    #region Win32 API Declarations

    const int LOGON32_PROVIDER_DEFAULT = 0;
    const int LOGON32_LOGON_INTERACTIVE = 2;    //This parameter causes LogonUser to create a primary token.

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
    int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

    [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
    public extern static bool CloseHandle(IntPtr handle);

    [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public extern static bool DuplicateToken(IntPtr ExistingTokenHandle,
    int SECURITY_IMPERSONATION_LEVEL, ref IntPtr DuplicateTokenHandle);

    #endregion

    /// <summary>
    /// Constructor. Impersonates the requested user. Impersonation lasts until
    /// the instance is disposed.
    /// </summary>
    public ImpersonationHelper(string domain, string user, string password)
    {
        // Call LogonUser to obtain a handle to an access token.
        bool returnValue = LogonUser(user, domain, password,
            LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
            ref m_tokenHandle);
        if (false == returnValue)
        {
            int ret = Marshal.GetLastWin32Error();
            throw new System.ComponentModel.Win32Exception(ret);
        }

        // Impersonate
        m_impersonatedUser = new WindowsIdentity(m_tokenHandle).Impersonate();
    }

    #region IDisposable Pattern

    /// <summary>
    /// Revert to original user and cleanup.
    /// </summary>
    protected virtual void Dispose(bool disposing)
    {
        if (disposing)
        {
            // Revert to original user identity
            if (m_impersonatedUser != null)
                m_impersonatedUser.Undo();
        }

        // Free the tokens.
        if (m_tokenHandle != IntPtr.Zero)
            CloseHandle(m_tokenHandle);
    }

    /// <summary>
    /// Explicit dispose.
    /// </summary>
    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    /// <summary>
    /// Destructor
    /// </summary>
    ~ImpersonationHelper()
    {
        Dispose(false);
    }

    #endregion
}

using (new ImpersonationHelper("xxx.blabla.com", "xxxx", "xxxx"))
{
    if (!string.IsNullOrEmpty(txtFilename.Text))
        Process.Start(txtFilename.Text);
}
Run Code Online (Sandbox Code Playgroud)

HAB*_*JAN 41

你可以尝试这样的事情:以另一个用户的身份开始一个新的流程

代码示例:

System.Diagnostics.Process proc = new System.Diagnostics.Process();
System.Security.SecureString ssPwd = new System.Security.SecureString();
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.FileName = "filename";
proc.StartInfo.Arguments = "args...";
proc.StartInfo.Domain = "domainname";
proc.StartInfo.UserName = "username";
string password = "user entered password";
for (int x = 0; x < password.Length; x++)
{
    ssPwd.AppendChar(password[x]);
}
password = "";
proc.StartInfo.Password = ssPwd;
proc.Start();
Run Code Online (Sandbox Code Playgroud)

  • 它并没有让我感觉更好,因为`string password ="用户输入密码";`仍然存在并将其分配给`""`并不意味着这么多. (3认同)
  • 什么是UAC - Verb runas?http://stackoverflow.com/questions/13838954/how-start-process-with-admin-rights-in-hide-mode (2认同)
  • 错误使用SecureString.你把它存放在`密码`中就打败了它的所有目的 (2认同)
  • 她们不一样。关键是您不应该将该字符串存储在变量中,因为在内存中查看可以很容易地揭示该变量的值。通过逐个字符地附加它,存储的值每次都会被加密,这样偷看就不会泄露密码。 (2认同)
  • @sotn:如果TextBox没有提供一种首先获得它而不通过字符串进行转换的方法,那么SecureString仍然有用吗?我签入了参考源,即使是TextBox.GetCharFromPosition也使用了Text。 (2认同)

sot*_*otn 5

正确使用SecureString和一些额外的东西:

//You should use SecureString like the following
SecureString password = new SecureString();
password.AppendChar('p');
password.AppendChar('a');
password.AppendChar('s');
password.AppendChar('s');

Process process = new Process();
process.StartInfo.UseShellExecute = false;
//Set the working directory if you don't execute something like calc or iisreset but your own exe in which you want to access some files etc..
process.StartInfo.WorkingDirectory = "workingDirectory";
//Full path (e.g. it can be @"C:\Windows\System32\iisreset.exe" OR you can use only file name if the path is included in Environment Variables..)
process.StartInfo.FileName = @"fileName";
process.StartInfo.Domain = "domain";
process.StartInfo.UserName = "userName";
process.StartInfo.Password = password;
process.Start();
Run Code Online (Sandbox Code Playgroud)

编辑:我不知道为什么这个答案被投票低于0,可能需要更多的解释.如果您将在非交互式环境(如Web应用程序)中使用它,并且您希望与用户一起运行进程,那么您可以使用一些选项来使用用户的密码.您可以从存储或代码中读取密码.一个更好的方法; 你可以加密存储它.但是,如果您打算以普通形式使用它(可能暂时或只是为了测试某些东西等),您可以SecureString按我描述的方式使用它.接受的答案没有SecureString以正确的方式使用.从控制台读取密码到一个字符串,然后将其放入一个SecureString简单的错误.接受的答案并不能确保该字符串或其他内容,只会欺骗它.这是我添加这个答案的主要动机.检查链接.

  • 我的例子只是一个POC.我不明白你为什么要做出如此重要的事情呢?无论您对密码进行硬编码(字符串或字符串为char),都可以从反编译的二进制文件和内存中轻松读取.在您的代码在内存中运行的某个时刻,它会指示将哪些内容附加到SecureString.您可以轻松地在WinDbg中放置一个断点并读取传入的内容.我的字符串最终将被垃圾收集,并且不会有任何痕迹.你的角色也在记忆中,把它们放在一起就更难了.它们也将在以后被垃圾收集. (6认同)
  • "...正确使用安全字符串..."*将密码硬编码为一系列字符*....我很确定"正确"用法涉及某处加密版本的密码,尽管即使如此因为必须知道密钥是不确定的. (4认同)
  • 不确定在代码中硬编码密码有多聪明.如果它没有硬编码,你从哪里得到它?这与我的例子有什么不同? (3认同)
  • 我不确定你是否知道但SecureString并不是一个真正的安全字符串.它只是一种减少时间窗口的方法,在这个窗口中,有人可以检查您的内存并成功获取敏感数据.这不是防弹的,也不是故意的. (2认同)
  • @HABJAN 垃圾收集并不一定意味着“没有任何痕迹”。您无法在代码中再次引用它,但内存中肯定会留下痕迹,除非它被其他东西覆盖。包含密钥和密码等敏感信息的普通(不安全)字符串应始终存在最短的时间,并在使用后逐个字符地擦除干净。 (2认同)