2009-05-04 29 views
3

解決方法(kinda):

原來,這種模擬與.NET的安全性只允許應用程序級訪問。由於COM對象處於系統級別,因此模擬的用戶仍然無法實例化它。我想通過右鍵單擊可執行文件並選擇「運行爲...」,程序運行正常。我發現啓動了系統訪問程序(假設你正在運行它的用戶擁有這些憑據)。現在我正在創建一個外部程序,它將使用此方法啓動此應用程序。是否有可能在VB.NET中複製以下憑據進程?

感謝您的提示:d


我有一個虛擬機上的Windows XP安裝。它是我的域的一部分,但登錄的用戶只是本地用戶。顯然,如果我嘗試訪問網絡共享它會提示輸入用戶名/密碼:

alt text http://i40.tinypic.com/wchl5l.jpg

我在虛擬機上測試了該程序使用的COM對象從另一個程序數據的接口。如果我不模仿,我會得到錯誤,因爲我沒有正確的憑據。

我對這件事做了一些研究,發現一些網站上有一個體面的VB.NET信息。我使用我編寫的代碼的問題是我可以訪問網絡資源,但我無法實例化COM對象。

如果我在嘗試實例化它之前填寫並提交證書提示(上面),它工作正常。這導致我相信WinXP證書提示所做的一定是我不知道的。下面是我用於模擬的代碼:

Public Sub BeginImpersonation() 
    Const LOGON32_PROVIDER_DEFAULT As Integer = 0 
    Const LOGON32_LOGON_INTERACTIVE As Integer = 2 
    Const SecurityImpersonation As Integer = 2 

    Dim win32ErrorNumber As Integer 

    _tokenHandle = IntPtr.Zero 
    _dupeTokenHandle = IntPtr.Zero 

    If Not LogonUser(_username, _domainname, _password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, _tokenHandle) Then 
     win32ErrorNumber = System.Runtime.InteropServices.Marshal.GetLastWin32Error() 
     Throw New ImpersonationException(win32ErrorNumber, GetErrorMessage(win32ErrorNumber), _username, _domainname) 
    End If 

    If Not DuplicateToken(_tokenHandle, SecurityImpersonation, _dupeTokenHandle) Then 
     win32ErrorNumber = System.Runtime.InteropServices.Marshal.GetLastWin32Error() 

     CloseHandle(_tokenHandle) 
     Throw New ImpersonationException(win32ErrorNumber, "Unable to duplicate token!", _username, _domainname) 
    End If 

    Dim newId As New System.Security.Principal.WindowsIdentity(_dupeTokenHandle) 
    _impersonatedUser = newId.Impersonate() 
    _impersonating = True 
End Sub 

我也嘗試向模擬方法發送不同的標誌,但似乎沒有任何工作。這裏有不同的標誌,我發現:

Enum LOGON32_LOGON 
    INTERACTIVE = 2 
    NETWORK = 3 
    BATCH = 4 
    SERVICE = 5 
    UNLOCK = 7 
    NETWORK_CLEARTEXT = 8 
    NEW_CREDENTIALS = 9 
End Enum 
Enum LOGON32_PROVIDER 
    [DEFAULT] = 0 
    WINNT35 = 1 
    WINNT40 = 2 
    WINNT50 = 3 
End Enum 
Enum SECURITY_LEVEL 
    Anonymous = 0 
    Identification = 1 
    Impersonation = 2 
    Delegation = 3 
End Enum 
+0

我有一些代碼,做模擬ImpersonationHelper,並根據測試看來,使用Windows 2008系統的虛擬機,您可以成功使用LogonUser,併爲用戶獲得成功的模擬會話。至少它適用於我的代碼。我的代碼也失敗了舊版Windows系統的vms。 – 2012-04-25 23:38:08

回答

1

我以前也碰到這個,並且採用了兩種不同soloution - 最簡單的是使用第三方應用程序:TqcRunas:http://www.quimeras.com/Products/products.asp,讓你打包需要creentials在加密文件。然而,如果密碼被強制過期則會很痛苦。

,我已經使用了其他的解決辦法是調用一個新的進程與其他憑據:

 Dim myProcessStartInfo As ProcessStartInfo = New ProcessStartInfo 

    With myProcessStartInfo 

     .FileName = "file path and name" 

     .Domain = "domainname" 
     .UserName = "username" 

     'password needs to be a SerureString 
     Using NewPassword As New Security.SecureString 
      With NewPassword 
       For Each c As Char In "password".ToCharArray 
        .AppendChar(c) 
       Next c 
       .MakeReadOnly() 
      End With 
      .Password = NewPassword.Copy 
     End Using 

     'UseShellExecute must be false for impersonated process 
     .UseShellExecute = False 

    End With 

    Using Process As New System.Diagnostics.Process 
     With Process 
      .StartInfo = myProcessStartInfo 
      .Start() 
     End With 
    End Using 
0

有了您的定義,我在代碼通過網絡進行身份驗證使用

LogonUser(_username, _domainname, _password, LOGON32_LOGON_NEW_CREDENTIALS, LOGON32_PROVIDER_WINNT50, _tokenHandle) 

。我正在模擬遠程盒子上的本地用戶,就像你一樣。但是,很久以前,我不記得使用這些值的理由。

+0

感謝您的回覆!不幸的是,我嘗試過這些值無濟於事。必須有SOMETHING(可能小而愚蠢),我忽略了導致這種情況不起作用的某個地方 – Anders 2009-05-04 21:14:53

+0

我現在看到我誤解了 - 它試圖通過COM對象訪問您遇到問題的數據。我還看到你已經提出了一個解決方法 - 並不理想,但正如你所說,因爲模擬是在應用程序級別,所以要做到這一點。 – Don 2009-05-07 16:39:08

0

我做了類似的事情來映射網絡驅動器在機器之間複製文件。我沒有寫代碼,但它幾乎和你的一樣,除了兩件事情:

  • 的模擬方法返回我關閉使用CloseHandle的例行兩個標記後,在我離開我的冒領方法。

  • 在模仿者的頂部,首先發生的是對RevertToSelf的調用,可能是爲了取消以前的任何模仿。

我不知道他們是否會有所作爲,但值得一試。下面是相關聲明:

Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal handle As IntPtr) As Long 
Declare Auto Function RevertToSelf Lib "advapi32.dll"() As Long 
相關問題