7

我目前使用本地方法在Vista中以不同用戶的身份運行進程,但我無法逃避這種感覺,這是它的破解和不理想(除了這個事實,它摧毀UAC,崩潰我的應用程序與安全異常,並迫使我完全禁用UAC)。我的過程包括兩個項目(於是兩個EXE文件) - 一個「接口」和「發射短截線」 - 而這裏的過程:在vb.net中以不同用戶身份運行新進程

  1. 用戶已啓動「Interface.exe的notepad.exe」
  2. 快捷方式
  3. Interface.exe有要求提供憑證的形式,他們想用
  4. Interace.exe使用的ProcessStartInfo創建LaunchStub.exe(LS)的實例作爲新用戶
  5. LS使用的ProcessStartInfo(帶ShellExecute設置爲true)啓動所請求的文件,並且由於它已經以請求的用戶身份運行,所以新進程也是如此。

我有兩個步驟的過程的原因是我希望用戶能夠右鍵單擊任何文件操作系統有(.EXE,.SQL,.MSC等)的默認行爲和啓動它,並且ProcessStartInfo僅在啓用「UseShellExecute」的情況下支持該功能,但該開關阻止我使用新憑據,所以我一次只能執行一次。

這會導致一些問題 - 首先,用戶必須已經存在於計算機上,這意味着他們必須先在本地登錄。如果該用戶沒有本地配置文件,則所請求的應用程序有時會啓動,但我得到註冊表和配置文件例外,因爲應用程序希望存在尚未存在的東西(例如註冊表中的HKCU配置單元,而用戶沒有因爲他們從未登錄過)。

我知道我應該可以將我的應用程序的權限「提升」給他們請求的用戶,啓動我的新進程,然後撤消提升,但我無法找到一個好的代碼示例爲此,我不確定它會允許以完全不同的用戶身份運行。這一切都有意義嗎?我只是不禁覺得有更好的方法來做到這一點。


更新:我只是想some Impersonation code我在網上找到,但無濟於事。當與ProcessStartInfo結合使用時,儘管我已經使用提供的憑證激活了模擬,但似乎仍然使用我當前的登錄名啓動進程,而不是我提供的登錄名。

回答

3

很可能您必須使用Win32 API創建自己的「shell」函數。

使用CreateProcessWithLogonW API,您可以在不同憑據下創建新進程,並可以選擇加載用戶配置文件信息。

在下面的代碼片段,如果你更換

  • 用戶名 - 與您的用戶名
  • 域 - 與您的域名或 「vbNullString」
  • 密碼 - 與您的密碼
  • 參數4 - 取代0,'LOGON WITH PROFILE'加載指定的用戶配置文件。

有關更多詳細信息,請參閱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 

+0

我會試試看看會發生什麼。如果我可以做到這一點,而不必使用第二個EXE作爲存根,那麼我會很高興,如果UAC啓用時不會發生,我會處理提示。我嘗試過之後我會成爲bakc。 – SqlRyan 2009-01-26 20:08:08

+0

@SqlRyan,並與啓用UAC工作? – Jet 2013-03-31 13:59:57

1

您可以嘗試從您的應用運行runas。一些示例和選項here

0

試試這個模塊:

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()開始與用戶程序/ PWÿ喜歡。程序僅指.exe,將參數寫入「命令​​行」

0

如果要使用不同於當前正在運行的進程的憑據啓動應用程序,則可以使用.Net 進程類。

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 
} 
相關問題