在 vb.net 中以不同用户身份运行新进程

发布于 2024-07-12 04:46:38 字数 1103 浏览 18 评论 0原文

我目前正在使用一种自行开发的方法以 Vista 中的不同用户身份运行一个进程,而且我无法摆脱这种感觉,即它是黑客式的且不太理想(此外,它会导致 UAC 崩溃,使我的电脑崩溃)。应用程序出现安全异常,并迫使我完全禁用 UAC)。 我的进程由两个项目(因此两个 EXE 文件)组成 - 一个“界面”和一个“启动存根” - 进程如下:

  1. 用户有一个启动“Interface.exe notepad.exe”的快捷方式
  2. Interface.exe 的形式为询问他们想要使用的凭据
  3. Interace.exe 使用 ProcessStartInfo 创建 LaunchStub.exe (LS) 的实例,因为新用户
  4. LS 使用 ProcessStartInfo(将 ShellExecute 设置为 true)来启动请求的文件,并且因为它已经作为请求的用户运行,新进程也是如此。

我采用两步流程的原因是,我希望用户能够右键单击操作系统具有默认操作的任何文件(.EXE、.SQL、.MSC 等)并启动它,并且仅限 ProcessStartInfo支持启用“UseShellExecute”,但该开关阻止我使用新的凭据,因此我一次只能执行一个操作。

这会导致一些问题 - 首先,用户必须已经存在于计算机上,这意味着他们之前必须在本地登录。 如果该用户没有本地配置文件,则请求的应用程序有时会启动,但我会收到注册表和配置文件异常,因为应用程序期望尚不存在的东西(例如注册表中的 HKCU 配置单元,用户不存在该配置单元)因为他们从未登录过)。

我知道我应该能够将我的应用程序的权限“提升”给他们请求的用户,启动我的新进程,然后撤消提升,但我无法为此找到一个好的代码示例,并且我不确定它是否允许以完全不同的用户身份运行。 这一切有意义吗? 我只是忍不住觉得有更好的方法来做到这一点。


更新:我刚刚尝试了一些模拟代码我在网上找到了,但没有结果。 当与 ProcessStartInfo 结合使用时,它似乎仍然使用我当前的登录名(而不是我提供的登录名)启动进程,即使我已使用提供的凭据激活了模拟。

I'm currently using a homegrown method to run a process as a different user in Vista, and I can't escape the feeling that's it hack-ish and less than ideal (in addition to the fact that it craps out UAC, crashing my app with a security exception, and forcing me to disable UAC altogether). My process consists of two projects (so two EXE files) - an "interface" and a "launch stub" - and here's the process:

  1. User has a shortcut that launches "Interface.exe notepad.exe"
  2. Interface.exe has a form that asks for the credentials they'd like to use
  3. Interace.exe uses ProcessStartInfo to create an instance of LaunchStub.exe (LS) as the new user
  4. LS uses ProcessStartInfo (with ShellExecute set to true) to launch the requested file, and since it's already running as the requested user, so is the new process.

The reason I have a two-step process is that I want users to be able to right-click on any file the OS has a default action for (.EXE, .SQL, .MSC, etc) and launch it, and ProcessStartInfo only supports that with "UseShellExecute" enabled, but that switch prevents me from using new credentials, so I can only do one at a time.

This causes a few problems - first, the user has to already exist on the computer, meaning they have to have logged in locally before. If there's no local profile for that user, the requested app will sometimes launch, but I get registry and profile exceptions because the application expects things to exist that don't yet (like an HKCU hive in the registry, which the user doesn't have because they've never logged in).

I know I should be able to just "Elevate" the rights of my application to the user they're requesting, launch my new process, and then undo the elevation, but I'm unable to find a good code sample for that, and I'm not sure that it would allow running as a completely different user. Does this all make sense? I just can't help feel like there's a better way to do this.


UPDATE: I just tried some Impersonation code I found online, but to no avail. When used in conjunction with ProcessStartInfo, it still seems to launch processes using my current login, not the one I've provided, even though I've activated impersonation using the provided credentials.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

兮颜 2024-07-19 04:46:38

您很可能必须使用 Win32 API 创建自己的“shell”函数。

使用 CreateProcessWithLogonW API,您可以在不同的凭据下创建新进程,并可选择加载用户配置文件信息。

在下面的代码片段中,如果您将

  • 用户名 - 替换为您的用户名
  • 域 - 替换为您的域或“vbNullString”
  • 密码 - 替换为您的密码
  • 参数 4 - 将 0 替换为“登录与配置文件”以加载指定的用户配置文件。

有关详细信息,请参阅 CreateProcessWithLogonW API 的文档具体细节。 走这条路,您可以完全控制并承担启动应用程序的全部责任。

同样,这只是一个示例,您可能需要稍微尝试一下才能让它执行您想要的操作。


Imports System.Runtime.InteropServices

Public Module modShell

    <StructLayout(LayoutKind.Sequential)> _
    Public Structure STARTUPINFO
        Public cb As Integer
        Public lpReserved As String
        Public lpDesktop As String
        Public lpTitle As String
        Public dwX As Integer
        Public dwY As Integer
        Public dwXSize As Integer
        Public dwYSize As Integer
        Public dwXCountChars As Integer
        Public dwYCountChars As Integer
        Public dwFillAttribute As Integer
        Public dwFlags As Integer
        Public wShowWindow As Short
        Public cbReserved2 As Short
        Public lpReserved2 As Integer
        Public hStdInput As Integer
        Public hStdOutput As Integer
        Public hStdError As Integer
    End Structure

    <StructLayout(LayoutKind.Sequential)> _
    Public Structure PROCESS_INFORMATION
        Public hProcess As IntPtr
        Public hThread As IntPtr
        Public dwProcessId As Integer
        Public dwThreadId As Integer
    End Structure

    Public Declare Unicode Function CreateProcessWithLogonW Lib "Advapi32" (ByVal lpUsername As String, ByVal lpDomain As String, ByVal lpPassword As String, ByVal dwLogonFlags As Int32, ByVal lpApplicationName As String, ByVal lpCommandLine As String, ByVal dwCreationFlags As Int32, ByVal lpEnvironment As IntPtr, ByVal lpCurrentDirectory As String, ByRef si As STARTUPINFO, ByRef pi As PROCESS_INFORMATION) As Integer
    Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As IntPtr) As Integer

    Public Const LOGON_WITH_PROFILE As Int32 = &H1

    Public Const NORMAL_PRIORITY_CLASS As Int32 = &H20&

    Public Const STARTF_USESHOWWINDOW As Int32 = &H1
    Public Const SW_HIDE As Int16 = 0
    Public Const SW_SHOW As Int16 = 5

    Public Function Shell(ByVal strCmdLine As String, ByVal strCurrentDirectory As String) As Boolean

        Dim pi As PROCESS_INFORMATION
        Dim si As New STARTUPINFO

        si.cb = Marshal.SizeOf(si)
        si.dwFlags = STARTF_USESHOWWINDOW
        si.wShowWindow = SW_SHOW

        Dim result As Integer = CreateProcessWithLogonW("username", "domain", "password", 0, vbNullString, strCmdLine, NORMAL_PRIORITY_CLASS, IntPtr.Zero, strCurrentDirectory, si, pi)

        If result <> 0 Then
            Call CloseHandle(pi.hThread)
            Call CloseHandle(pi.hProcess)
        Else
            Return False
        End If

        Return True

    End Function

End Module

Chances are that you have to create your own "shell" function using the Win32 API.

Using the CreateProcessWithLogonW API you can create new processes under different credentials and optionally load user profile information.

In the code snippet below if you replace

  • username - with your username
  • domain - with your domain or "vbNullString"
  • password - with your password
  • parameter 4 - replace 0 with 'LOGON WITH PROFILE' to load the specified users profile.

See the documentation for the CreateProcessWithLogonW API for further specifics. Going this route you have full control and full responsibility for launching the application.

Again this is just a sample and you may have to play with it a little to get it to do what you want.


Imports System.Runtime.InteropServices

Public Module modShell

    <StructLayout(LayoutKind.Sequential)> _
    Public Structure STARTUPINFO
        Public cb As Integer
        Public lpReserved As String
        Public lpDesktop As String
        Public lpTitle As String
        Public dwX As Integer
        Public dwY As Integer
        Public dwXSize As Integer
        Public dwYSize As Integer
        Public dwXCountChars As Integer
        Public dwYCountChars As Integer
        Public dwFillAttribute As Integer
        Public dwFlags As Integer
        Public wShowWindow As Short
        Public cbReserved2 As Short
        Public lpReserved2 As Integer
        Public hStdInput As Integer
        Public hStdOutput As Integer
        Public hStdError As Integer
    End Structure

    <StructLayout(LayoutKind.Sequential)> _
    Public Structure PROCESS_INFORMATION
        Public hProcess As IntPtr
        Public hThread As IntPtr
        Public dwProcessId As Integer
        Public dwThreadId As Integer
    End Structure

    Public Declare Unicode Function CreateProcessWithLogonW Lib "Advapi32" (ByVal lpUsername As String, ByVal lpDomain As String, ByVal lpPassword As String, ByVal dwLogonFlags As Int32, ByVal lpApplicationName As String, ByVal lpCommandLine As String, ByVal dwCreationFlags As Int32, ByVal lpEnvironment As IntPtr, ByVal lpCurrentDirectory As String, ByRef si As STARTUPINFO, ByRef pi As PROCESS_INFORMATION) As Integer
    Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As IntPtr) As Integer

    Public Const LOGON_WITH_PROFILE As Int32 = &H1

    Public Const NORMAL_PRIORITY_CLASS As Int32 = &H20&

    Public Const STARTF_USESHOWWINDOW As Int32 = &H1
    Public Const SW_HIDE As Int16 = 0
    Public Const SW_SHOW As Int16 = 5

    Public Function Shell(ByVal strCmdLine As String, ByVal strCurrentDirectory As String) As Boolean

        Dim pi As PROCESS_INFORMATION
        Dim si As New STARTUPINFO

        si.cb = Marshal.SizeOf(si)
        si.dwFlags = STARTF_USESHOWWINDOW
        si.wShowWindow = SW_SHOW

        Dim result As Integer = CreateProcessWithLogonW("username", "domain", "password", 0, vbNullString, strCmdLine, NORMAL_PRIORITY_CLASS, IntPtr.Zero, strCurrentDirectory, si, pi)

        If result <> 0 Then
            Call CloseHandle(pi.hThread)
            Call CloseHandle(pi.hProcess)
        Else
            Return False
        End If

        Return True

    End Function

End Module

别理我 2024-07-19 04:46:38

您可以尝试从您的应用程序运行 runas。 一些示例和选项此处

You may try to run runas from your app. Some examples and options here.

暮年 2024-07-19 04:46:38

尝试这个模块:

Module Impersonation

#Region "API Structures"
    <StructLayout(LayoutKind.Sequential)> _
      Public Structure PROCESS_INFORMATION
        Dim hProcess As System.IntPtr
        Dim hThread As System.IntPtr
        Dim dwProcessId As Integer
        Dim dwThreadId As Integer
    End Structure

    <StructLayout(LayoutKind.Sequential)> _
     Public Structure STARTUPINFO
        Dim cb As Integer
        Dim lpReserved As System.IntPtr
        Dim lpDesktop As System.IntPtr
        Dim lpTitle As System.IntPtr
        Dim dwX As Integer
        Dim dwY As Integer
        Dim dwXSize As Integer
        Dim dwYSize As Integer
        Dim dwXCountChars As Integer
        Dim dwYCountChars As Integer
        Dim dwFillAttribute As Integer
        Dim dwFlags As Integer
        Dim wShowWindow As Short
        Dim cbReserved2 As Short
        Dim lpReserved2 As System.IntPtr
        Dim hStdInput As System.IntPtr
        Dim hStdOutput As System.IntPtr
        Dim hStdError As System.IntPtr
    End Structure
#End Region

#Region "API Constants"
    Private Const LOGON_NETCREDENTIALS_ONLY As Integer = &H2
    Private Const NORMAL_PRIORITY_CLASS As Integer = &H20
    Private Const CREATE_DEFAULT_ERROR_MODE As Integer = &H4000000
    Private Const CREATE_NEW_CONSOLE As Integer = &H10
    Private Const CREATE_NEW_PROCESS_GROUP As Integer = &H200
    Private Const LOGON_WITH_PROFILE As Integer = &H1
#End Region

#Region "API Functions"
    Private Declare Unicode Function CreateProcessWithLogon Lib "Advapi32" Alias "CreateProcessWithLogonW" _
        (ByVal lpUsername As String, _
         ByVal lpDomain As String, _
         ByVal lpPassword As String, _
         ByVal dwLogonFlags As Integer, _
         ByVal lpApplicationName As String, _
         ByVal lpCommandLine As String, _
         ByVal dwCreationFlags As Integer, _
         ByVal lpEnvironment As System.IntPtr, _
         ByVal lpCurrentDirectory As System.IntPtr, _
         ByRef lpStartupInfo As STARTUPINFO, _
         ByRef lpProcessInfo As PROCESS_INFORMATION) As Integer

    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As System.IntPtr) As Integer

#End Region

    Public Sub RunProgram(ByVal UserName As String, ByVal Password As String, ByVal Domain As String, ByVal Application As String, ByVal CommandLine As String)

        Dim siStartup As STARTUPINFO
        Dim piProcess As PROCESS_INFORMATION
        Dim intReturn As Integer

        If CommandLine Is Nothing OrElse CommandLine = "" Then CommandLine = String.Empty

        siStartup.cb = Marshal.SizeOf(siStartup)
        siStartup.dwFlags = 0

        intReturn = CreateProcessWithLogon(UserName, Domain, Password, LOGON_WITH_PROFILE, Application, CommandLine, _
        NORMAL_PRIORITY_CLASS Or CREATE_DEFAULT_ERROR_MODE Or CREATE_NEW_CONSOLE Or CREATE_NEW_PROCESS_GROUP, _
        IntPtr.Zero, IntPtr.Zero, siStartup, piProcess)

        If intReturn = 0 Then
            Throw New System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error())
        End If

        CloseHandle(piProcess.hProcess)
        CloseHandle(piProcess.hThread)

    End Sub

End Module

使用 Runprogram() 以 user/pw y 之类的方式启动您的程序。 程序仅指.exe,参数写入“命令​​行”

Try this Module:

Module Impersonation

#Region "API Structures"
    <StructLayout(LayoutKind.Sequential)> _
      Public Structure PROCESS_INFORMATION
        Dim hProcess As System.IntPtr
        Dim hThread As System.IntPtr
        Dim dwProcessId As Integer
        Dim dwThreadId As Integer
    End Structure

    <StructLayout(LayoutKind.Sequential)> _
     Public Structure STARTUPINFO
        Dim cb As Integer
        Dim lpReserved As System.IntPtr
        Dim lpDesktop As System.IntPtr
        Dim lpTitle As System.IntPtr
        Dim dwX As Integer
        Dim dwY As Integer
        Dim dwXSize As Integer
        Dim dwYSize As Integer
        Dim dwXCountChars As Integer
        Dim dwYCountChars As Integer
        Dim dwFillAttribute As Integer
        Dim dwFlags As Integer
        Dim wShowWindow As Short
        Dim cbReserved2 As Short
        Dim lpReserved2 As System.IntPtr
        Dim hStdInput As System.IntPtr
        Dim hStdOutput As System.IntPtr
        Dim hStdError As System.IntPtr
    End Structure
#End Region

#Region "API Constants"
    Private Const LOGON_NETCREDENTIALS_ONLY As Integer = &H2
    Private Const NORMAL_PRIORITY_CLASS As Integer = &H20
    Private Const CREATE_DEFAULT_ERROR_MODE As Integer = &H4000000
    Private Const CREATE_NEW_CONSOLE As Integer = &H10
    Private Const CREATE_NEW_PROCESS_GROUP As Integer = &H200
    Private Const LOGON_WITH_PROFILE As Integer = &H1
#End Region

#Region "API Functions"
    Private Declare Unicode Function CreateProcessWithLogon Lib "Advapi32" Alias "CreateProcessWithLogonW" _
        (ByVal lpUsername As String, _
         ByVal lpDomain As String, _
         ByVal lpPassword As String, _
         ByVal dwLogonFlags As Integer, _
         ByVal lpApplicationName As String, _
         ByVal lpCommandLine As String, _
         ByVal dwCreationFlags As Integer, _
         ByVal lpEnvironment As System.IntPtr, _
         ByVal lpCurrentDirectory As System.IntPtr, _
         ByRef lpStartupInfo As STARTUPINFO, _
         ByRef lpProcessInfo As PROCESS_INFORMATION) As Integer

    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As System.IntPtr) As Integer

#End Region

    Public Sub RunProgram(ByVal UserName As String, ByVal Password As String, ByVal Domain As String, ByVal Application As String, ByVal CommandLine As String)

        Dim siStartup As STARTUPINFO
        Dim piProcess As PROCESS_INFORMATION
        Dim intReturn As Integer

        If CommandLine Is Nothing OrElse CommandLine = "" Then CommandLine = String.Empty

        siStartup.cb = Marshal.SizeOf(siStartup)
        siStartup.dwFlags = 0

        intReturn = CreateProcessWithLogon(UserName, Domain, Password, LOGON_WITH_PROFILE, Application, CommandLine, _
        NORMAL_PRIORITY_CLASS Or CREATE_DEFAULT_ERROR_MODE Or CREATE_NEW_CONSOLE Or CREATE_NEW_PROCESS_GROUP, _
        IntPtr.Zero, IntPtr.Zero, siStartup, piProcess)

        If intReturn = 0 Then
            Throw New System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error())
        End If

        CloseHandle(piProcess.hProcess)
        CloseHandle(piProcess.hThread)

    End Sub

End Module

Use Runprogram() To start your Program with user/pw y like. Programm means the .exe only, parameters ar written into "commandline"

隔纱相望 2024-07-19 04:46:38

如果您想使用与当前正在运行的进程不同的凭据启动应用程序,可以使用 .Net Process 类。

this.Process = new Process();

this.Process.StartInfo.Arguments = "Arguments";
this.Process.StartInfo.FileName = "C:\your.exe";
this.Process.StartInfo.UserName = "UserName";
string password = "some password";

this.Process.StartInfo.Password.Clear();
foreach (char c in password)
{
    this.Process.StartInfo.Password.AppendChar(c);
}


//allow the process to raise events
this.Process.EnableRaisingEvents = true;
this.Process.StartInfo.ErrorDialog = false;
//Method for handling the exit event
this.Process.Exited += new EventHandler(ApplicationProcess_Exited);

//Set the application directory as the current working directory
Environment.CurrentDirectory = System.IO.Directory.GetParent("C:\").ToString();

if (this.Process.Start())
{
    // Do something on start
}

If you want to start an application with different credentials than the current running process, you can use the .Net Process class.

this.Process = new Process();

this.Process.StartInfo.Arguments = "Arguments";
this.Process.StartInfo.FileName = "C:\your.exe";
this.Process.StartInfo.UserName = "UserName";
string password = "some password";

this.Process.StartInfo.Password.Clear();
foreach (char c in password)
{
    this.Process.StartInfo.Password.AppendChar(c);
}


//allow the process to raise events
this.Process.EnableRaisingEvents = true;
this.Process.StartInfo.ErrorDialog = false;
//Method for handling the exit event
this.Process.Exited += new EventHandler(ApplicationProcess_Exited);

//Set the application directory as the current working directory
Environment.CurrentDirectory = System.IO.Directory.GetParent("C:\").ToString();

if (this.Process.Start())
{
    // Do something on start
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文