如何使用提供的凭据调用 SSPI 的 'AcquireCredentialsHandle'?

How to invoke SSPI's 'AcquireCredentialsHandle' with supplied credentials?

背景

Windows SSPI API 是 Windows 安全服务的一个接口,允许您相互验证客户端和服务器。 API 的主要用途之一是提供 Windows 集成身份验证,即单点登录 - 应用程序能够通过使用用户登录时的凭据自动向服务器验证用户身份。

这个过程的正常流程:

这是使用 API 作为单点登录的一部分时此过程的正常流程。

AcquireCredentialsHandle() 的签名是:

SECURITY_STATUS SEC_Entry AcquireCredentialsHandle(
  _In_  SEC_CHAR       *pszPrincipal,
  _In_  SEC_CHAR       *pszPackage,
  _In_  ULONG          fCredentialUse,
  _In_  PLUID          pvLogonID,
  _In_  PVOID          pAuthData,
  _In_  SEC_GET_KEY_FN pGetKeyFn,
  _In_  PVOID          pvGetKeyArgument,
  _Out_ PCredHandle    phCredential,
  _Out_ PTimeStamp     ptsExpiry
);

在上述典型的 Windows 集成身份验证案例中使用时,通常不提供 pAuthData 参数 - 而是提供空引用。

问题

我希望能够以直接向其提供用户名和密码的方式使用 AcquireCredentialsHandle()。它似乎可以处理这个问题,因为 pAuthData 参数(上面为空)可以是对 SEC_WINNT_AUTH_IDENTITY 结构的引用,它允许您直接指定用户名和密码。

我曾尝试以这种方式调用 AcquireCredentialsHandle(),为它提供一个填写好的 SEC_WINNT_AUTH_IDENTITY 结构以及我的用户名和密码。但是,每次我调用它时,我都会返回成功,即使我使用虚构的用户名或密码也是如此。作为健全性检查,我尝试使用相同的凭据调用 LogonUser(),它们要么成功,要么失败,正如预期的那样,这取决于我是否给了它一个有效的 username/password 组合。

我做错了什么? 为什么 AcquireCredentialsHandle() 总是 return 成功,即使凭据完全不正确?

下面显示了我用来调用 AcquireCredentialsHandle():

的代码的基础知识
public class SuppliedCredential : Credential
{
    public SuppliedCredential( string securityPackage, CredentialUse use, string username, string domain, string password ) :
        base( securityPackage )
    { 
        Init( use, username, domain, password );
    }

    private void Init( CredentialUse use, string username, string domain, string password )
    {
        // Copy off for the call, since this.SecurityPackage is a property.
        string packageName = this.SecurityPackage;

        TimeStamp rawExpiry = new TimeStamp();

        SecurityStatus status = SecurityStatus.InternalError;

        AuthIdentity auth = new AuthIdentity();

        auth.User = username;
        auth.UserLength = username.Length;
        auth.Domain = domain;
        auth.DomainLength = domain.Length;
        auth.Password = password;
        auth.PasswordLength = password.Length;
        auth.Flags = 0x2; // unicode

        this.Handle = new SafeCredentialHandle();

        RuntimeHelpers.PrepareConstrainedRegions();
        try { }
        finally
        {
            status = CredentialNativeMethods.AcquireCredentialsHandle_2(
               "",
               packageName,
               use,
               IntPtr.Zero,
               ref auth,
               IntPtr.Zero,
               IntPtr.Zero,
               ref this.Handle.rawHandle,
               ref rawExpiry
           );
        }

        if( status != SecurityStatus.OK )
        {
            throw new SSPIException( "Failed to call AcquireCredentialHandle", status );
        }

        this.Expiry = rawExpiry.ToDateTime();
    }
}

...

[StructLayout( LayoutKind.Sequential )]
public struct AuthIdentity
{
    [MarshalAs(UnmanagedType.LPWStr)]
    public string User;
    public int UserLength;
    [MarshalAs(UnmanagedType.LPWStr)]
    public string Domain;
    public int DomainLength;
    [MarshalAs(UnmanagedType.LPWStr)]
    public string Password;
    public int PasswordLength;
    public int Flags;
};

...

[ReliabilityContract( Consistency.WillNotCorruptState, Cer.MayFail )]
[DllImport( "Secur32.dll", EntryPoint = "AcquireCredentialsHandle", CharSet = CharSet.Unicode )]
internal static extern SecurityStatus AcquireCredentialsHandle_2(
    string principleName,
    string packageName,
    CredentialUse credentialUse,
    IntPtr loginId,
    ref AuthIdentity packageData,
    IntPtr getKeyFunc,
    IntPtr getKeyData,
    ref RawSspiHandle credentialHandle,
    ref TimeStamp expiry
);

简而言之

AcquireCredentialsHandle() returns 即使凭据是伪造的,也是如此;只有当客户端尝试调用 InitializeSecurityContext() 时,API 才会验证用户名和密码。 AcquireCredentialsHandle() 仅执行参数验证(指针值有效、结构填写正确、参数彼此有意义等)。由于我正确地提供了不正确的参数,AcquireCredentialsHandle() 不在乎。

...

中长

总而言之,客户端应用程序参与 SSPI 身份验证的正常周期如下:

  • 客户端调用某种形式的 AcquireCredentialsHandle()
  • 客户端调用 InitializeSecurityContext(),returns 一些令牌发送到服务器。
  • 服务器获取接收到的令牌并调用AcceptSecurityContext(),返回一些令牌发送回客户端。
  • 客户端收到令牌并调用InitializeSecurityContext()
  • ...循环继续,直到两端之间的身份验证周期完成。

在上述情况下,使用提供的 SEC_WINNT_AUTH_IDENTITY 结构调用 AcquireCredentialsHandle() 获得的凭据(依次填写有效的用户名、域和密码)未在客户端验证直到客户端第一次调用 InitializeSecurityContext(),然后才将其第一个令牌发送到服务器。

为了回答类似的问题,Dave Christiansen(Microsoft 员工?)posted the following 在新闻组 'microsoft.public.platformsdk.security' 于 2003 年 9 月 19 日:

How are you determining that the credentials are those of the local user? SSPI is sometimes tricky this way. What package are you using (Sounds like NTLM, Kerberos, or Negotiate if you're using SEC_WINNT_AUTH_IDENTITY)?

Note that AcquireCredentialsHandle will succeed even if the credentials given are incorrect (e.g. wrong password), because it doesn't actually use them until you call InitializeSecurityContext.