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

标签 c# windows authentication winapi sspi

背景

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

这个过程的正常流程:

  • 用户登录他们的计算机,通常使用用户名和密码。
  • 用户运行一个应用程序,该应用程序使用 SSPI 对用户进行服务身份验证。
  • 该应用程序调用 AcquireCredentialsHandle()获取用户登录时创建的现有凭据的句柄。
  • 应用程序使用 AcquireCredentialsHandle() 返回的句柄参与服务器的 SSPI 身份验证周期,使用 InitializeSecurityContext() API。
  • 应用程序和服务器使用 InitializeSecurityContext()(客户端)和 AcceptSecurityContext() 交换不透明的字节数组(“ token ”) (服务器端)用于验证彼此 token 并继续身份验证周期的 API。
  • 当足够多的 token 来回传递时,身份验证周期在某个时刻完成。
  • 应用程序已代表用户向服务器验证了自己的身份。身份验证完成。

当使用 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 参数(上面为 null)可以是对 SEC_WINNT_AUTH_IDENTITY 的引用结构,允许您直接指定用户名和密码。

我尝试以这种方式调用 AcquireCredentialsHandle(),为其提供一个包含我的用户名和密码的完整 SEC_WINNT_AUTH_IDENTITY 结构。但是,每次我调用它时,我都会返回成功,即使我使用虚构的用户名或密码也是如此。作为健全性检查,我尝试使用相同的凭据调用 LogonUser(),结果如预期的那样成功或失败,具体取决于我是否为其提供了有效的用户名/密码组合。

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

下面显示了我用来调用 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() 即使凭据是伪造的,也会返回 true;只有当客户端尝试调用 InitializeSecurityContext() 时,API 才会验证用户名和密码。 AcquireCredentialsHandle() 仅执行参数验证(指针值有效、结构填写正确、参数彼此有意义等)。由于我正确地提供了不正确的参数,AcquireCredentialsHandle() 没有在意。

...

中长

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

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

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

在回答类似问题时,Dave Christiansen(微软员工?)posted the following在 2003 年 9 月 19 日的新闻组“microsoft.public.platformsdk.security”中:

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.

关于c# - 如何使用提供的凭据调用 SSPI 的 'AcquireCredentialsHandle'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45290882/

相关文章:

C# System.Management 只有 1 个类?

c# - 在 C# 中从 Chrome/Chromium 80+ 解密 Cookie(加密值)- 身份验证标签问题

c++ - 低功耗蓝牙 : setting characteristic to byte array sends wrong values

authentication - 来自 'next-auth/react' 的注册不存在

c# - 如何使用文件路径从 csproj 文件访问 Azure 开发操作存储库中保存的文件?

c# - Firefox 的等待超时 [WatiN]

windows - 如何在 Windows 批处理脚本中将 %USERNAME% 的值转换为小写?

windows - Git Bash - 段错误问题(Windows)

authentication - 使用 JUnit 和 Selenium-RC 以编程方式设置凭据

ios - 解析 PFUser.currentUser 返回 nil - Swift