【问题标题】:"Win32Exception: The directory name is invalid" Impersonation & UAC“Win32Exception:目录名称无效”模拟和 UAC
【发布时间】:2013-02-27 16:09:30
【问题描述】:

我编写了一个程序,该程序需要提升运行才能调用 WMI 对象的两个方法。如果由管理员组中的用户在“提升模式”下运行,它就像一个魅力。但现在的问题是它需要由每个人运行,而不是每个人都在他们工作的机器上拥有管理权限。所以我决定让程序自己重新启动,模拟“域管理员”组中的用户。

我的代码如下:

 ProcessStartInfo proc = new ProcessStartInfo();
 proc.WorkingDirectory = Environment.CurrentDirectory;
 proc.FileName = System.Windows.Forms.Application.ExecutablePath; ;
 proc.UserName = "USERNAME";
 SecureString secure = new SecureString();
 foreach (var item in "PASSWORD")
 {
     secure.AppendChar(item);
 }
 proc.Password = secure;
 proc.Domain = "DOMAIN";
 proc.UseShellExecute = false;
 proc.Verb = "runas";
 Process.Start(proc);

启用 UAC 时有两个问题。 首先,如果我没有明确地将应该被模拟的用户添加到程序安装文件夹的 ACL 中(用户已经在管理员组中,该组对文件夹和其中包含的所有文件具有完全访问权限)它会抛出一个 System.ComponentModel.Win32Exception: The directory name is invalid。 我仔细检查了目录路径,它实际上是有效的(当用户明确地在 ACL 中时有效)。

现在,如果用户被明确添加并且没有例外,则程序以用户身份运行但没有提升的权限,因此调用不起作用。

我做错了什么?感谢您的帮助。

我知道已经有一些关于类似问题的问题,但没有一个解决方案/答案解决了我的问题,或者它们不够详细并且太长时间处于非活动状态。

【问题讨论】:

  • 如果我理解你想要做什么,这都是绕过 UAC 的尝试。如果这是正确的,那么您正在尝试绕过一些旨在不被绕过的东西。
  • @AbqBill 您确定以低权限运行的程序无法通过使用域管理员的登录以高权限重新启动自身吗?我真的必须创建服务并让服务通过进程间通信执行操作吗?
  • 如果有可能,我会感到惊讶,因为这将是一个潜在的(并且相当微不足道的)攻击媒介。另外,检查 SO 中的 UAC 标签并按 FAQ 排序(这是经常被问到的)。在我看来,您的解决方案(创建一个运行提升的服务)似乎是一个合理的解决方案。
  • @AbqBill 这似乎是可能的,因为如果您查看 metasploit 中包含的“工具”,则有多个程序可以绕过 UAC 或将您从标准用户提升到系统。你的问题是我不想为用户提供这些程序。我想将此功能集成到我的程序中以防止滥用。
  • 但是我想我还是会把我的程序拆分成一个 GUIapp 和一个服务,因为我真的不想把所有的精力都放在让我的程序绕过 UAC 被病毒扫描程序删除或类似的东西。(而且我真的不想在病毒扫描仪规避上付出任何努力......)

标签: c# windows-7 process uac impersonation


【解决方案1】:

我遇到了同样的问题,我无法同时提升和提供凭据。为了使用“runas”动词,您必须设置 UseShellExecute=true,但为了提供凭据,您必须将其设置为 false。我最终做的是测试管理员权限,如果它们不存在,则以具有本地管理员权限的单独用户身份重新启动我的应用程序。然后,我将使用“runas”动词运行 process.start,而无需提供凭据。此时,您的用户将收到 UAC 提示,选择“是”后,该进程将运行提升。我用 VB 写了我的,但想法是一样的,这是我的代码,IsUserAdminGroup 的使用来自:

UAC self-elevation


     Try
    'check if user is administrator
    Dim fInAdminGroup As Boolean = Me.IsUserInAdminGroup
    If (Me.IsUserInAdminGroup = False) Then
    'check if process is running under user you want to elevate
        If Not (System.Environment.UserName = "") Then
    'this process restarts the app as the desired admin user
            Dim path As String = Environment.CurrentDirectory
            Dim filename As String = Application.ExecutablePath
    'create secure string password
            Dim passwordPre As String = ""
            Dim passwordChars As Char() = passwordPre.ToCharArray()
            Dim password As New SecureString()
            For Each c As Char In passwordChars
                password.AppendChar(c)
            Next
            Dim procInfo As New System.Diagnostics.Process
            procInfo.StartInfo.FileName = filename
            procInfo.StartInfo.UserName = ""
            procInfo.StartInfo.Domain = ""
            procInfo.StartInfo.Password = password
            procInfo.StartInfo.UseShellExecute = False
    'load user profile was needed for a legacy application I used that failed without the user hive loaded
            'procInfo.StartInfo.LoadUserProfile = True
            procInfo.StartInfo.CreateNoWindow = True
            procInfo.StartInfo.WorkingDirectory = path
            procInfo.Start()
            Application.Exit()
        Else
            MsgBox("Unable to open, call support.", MsgBoxStyle.Critical, "Run Failed")
            Application.Exit()
        End If
    ElseIf (System.Environment.UserName = "") Then
        Me.Visible = False
        Me.Hide()
    'this process starts the desired app elevated
        Dim procInfo As New System.Diagnostics.Process
        procInfo.StartInfo.FileName = "ptpublisher.exe"
        procInfo.StartInfo.Verb = "runas"
        procInfo.StartInfo.UseShellExecute = True
        procInfo.StartInfo.CreateNoWindow = True
        procInfo.StartInfo.WorkingDirectory = "C:\Program Files (x86)\Primera Technology\PTPublisher"
        procInfo.StartInfo.Arguments = ""
        procInfo.Start()
        Application.Exit()
    End If
Catch ex As Exception
    MsgBox(ex.Message, MsgBoxStyle.Critical, "Run Failed")
    Application.Exit()
End Try

【讨论】:

    猜你喜欢
    • 2010-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-23
    • 2014-03-07
    • 1970-01-01
    • 2013-07-25
    相关资源
    最近更新 更多