【问题标题】:Start a .Net Process as a Different User以不同用户身份启动 .Net 进程
【发布时间】:2011-06-05 04:16:24
【问题描述】:

我想以管理员权限启动进程。当我运行下面的代码时,进程抱怨说它需要管理员权限:

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);
}

【问题讨论】:

    标签: c# impersonation


    【解决方案1】:

    你能试试这样的吗:Start a new Process as another user

    代码示例:

    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();
    

    【讨论】:

    • UAC - 动词 runas 怎么样? stackoverflow.com/questions/13838954/…
    • SecureString 的错误使用。您将其存储在 password 中,从而破坏了它的所有目的
    • 它实际上并没有让我感觉更好,因为string password = "user entered password"; 仍然存在并且将其分配给"" 并没有多大意义。
    • 他们不一样。关键是您不应该将该字符串存储在变量中,因为在内存中偷看可以很容易地揭示该变量的值。通过逐个字符地附加它,存储的值每次都会被加密,这样偷看就不会泄露密码。
    • @sotn:如果 TextBox 没有提供一种在不通过字符串传输的情况下首先获取它的方法,那么 SecureString 到底有多大用处?即使TextBox.GetCharFromPosition 使用Text,我在参考源中检查了。
    【解决方案2】:

    正确使用 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();
    

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

    【讨论】:

    • 不确定在代码中硬编码密码有多聪明。如果它不是硬编码的,你从哪里得到它?这与我的示例有何不同?
    • 我的例子只是一个 POC。我不明白你为什么要大肆宣传它。无论您以何种方式硬编码您的密码(字符串或 char by char),它也可以很容易地从反编译的二进制文件和内存中读取。当您的代码在内存中运行时,它会指示要附加到 SecureString 的内容。您可以轻松地在 WinDbg 中放置断点并读取传入的内容。我的字符串最终将被垃圾收集,并且不会留下任何痕迹。你的字符也在内存中,只是更难把它们放在一起。稍后它们也会被垃圾收集。
    • 我不确定您是否知道,但 SecureString 并不是真正的安全字符串。这只是减少某人可以检查您的内存并成功获取敏感数据的时间窗口的一种方法。这不是防弹的,也不是故意的。
    • "...正确使用安全字符串..." 将密码硬编码为一系列字符 ....我很确定“正确”使用在某处涉及密码的加密版本,尽管那是不确定的,因为必须知道密钥。
    • @HABJAN 收集的垃圾并不一定意味着“没有它的踪迹”。您无法在代码中再次引用它,但肯定会在内存中留下痕迹,除非它被其他东西覆盖。包含密钥和密码等敏感信息的正常(不安全)字符串应始终存在最短时间,并在使用后逐个字符擦除。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-14
    • 2013-02-04
    • 1970-01-01
    • 2013-09-18
    • 2010-09-26
    • 2021-10-11
    相关资源
    最近更新 更多