2010-06-07 58 views
5

我使用了常見的模擬代碼,它工作得很好,直到我在域插入隨機「dggdgsdg」 - 它仍然工作...冒充在錯誤的域用戶不會拋出異常

if (LogonUser(Username, Domain, Password, Logon32LogonInteractive, Logon32ProviderDefault, ref existingTokenHandle) && 
    DuplicateToken(existingTokenHandle, (int)SecurityImpersonationLevel.SecurityDelegation, ref duplicateTokenHandle)) 
    { 
      Identity = new WindowsIdentity(duplicateTokenHandle); 
      ImpersonationContext = Identity.Impersonate(); 
    } 
    else 
    { 
      throw new Win32Exception(Marshal.GetLastWin32Error()); 
    } 

我在我的域中使用了一些TestUser,並且它工作正常。然後,我將域名轉換爲隨機廢話「werwerhrg」,並在我的域名上模擬了TestUser!爲什麼?我期望拋出異常,爲什麼它在工作?

private const int Logon32LogonInteractive = 2; 
private const int Logon32ProviderDefault = 0; 

public enum SecurityImpersonationLevel 
     { 
      SecurityAnonymous = 0, 
      SecurityIdentification = 1, 
      SecurityImpersonation = 2, 
      SecurityDelegation = 3 
     } 
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)] 
private static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken); 

[DllImport("kernel32.dll", CharSet = CharSet.Auto)] 
private extern static bool CloseHandle(IntPtr handle); 

[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
private static extern bool DuplicateToken(IntPtr existingTokenHandle, int securityImpersonationLevel, ref IntPtr duplicateTokenHandle); 
+1

通常使用域認證,我看到的是用戶名/域名合併。因此,您可以指定MyDomain \ TestUser或TestUser @ MyDomain。嘗試指定你的用戶名,看看會發生什麼。 我猜你的機器是目標域的成員,並以某種方式嘗試使用該域上的用戶而不是指定的域。 – 2010-06-07 19:24:21

+0

@Tim,我試過了,沒有運氣。如果我將'domain \ testUser'指定爲用戶名,我會收到一個異常'未知用戶名或密碼錯誤'。 – Rita 2010-06-07 19:47:49

+1

發佈您的P/Invoke聲明。 – 2010-06-07 19:56:45

回答

4

我相信答案在於如何進行身份驗證。 LogonUser將嘗試將您登錄到執行的本地計算機上。如果此計算機位於域中,那麼您的密碼將針對AD控制器進行檢查並進行驗證。

如果您提供了一個域,但它無法找到它將對其自己的本地用戶羣進行身份驗證作爲回退。本地它將使用NTLM(當客戶端和服務器是同一臺機器時使用NTLM)。 NTLM驗證密碼哈希和用戶名,似乎不打擾域名(ref doc)。

如果您使用UPN format instead and set domain to null,那麼如果該域不存在,並且獲得您想要的結果,則會出現錯誤。

這與我在兩臺機器上創建密碼爲B的用戶A相似,然後這些用戶可以使用本地權限訪問對方機器,而無需登錄。

所有這些都是您爲什麼應該遠離域名本地帳戶(至少使用相同的用戶名,將它們作爲前綴)的一個很好的理由,以及爲什麼非域名機器不應該能夠看到對方一個網絡。如果你可以代替NTLM,爲什麼你應該使用Kerberos。

+0

非常感謝! – Rita 2010-06-07 22:34:38