public NTStatus AcceptSecurityContext(ref object context, byte[] inputToken, out byte[] outputToken) { outputToken = null; if (!AuthenticationMessageUtils.IsSignatureValid(inputToken)) { return(NTStatus.SEC_E_INVALID_TOKEN); } MessageTypeName messageType = AuthenticationMessageUtils.GetMessageType(inputToken); if (messageType == MessageTypeName.Negotiate) { NegotiateMessage negotiateMessage; try { negotiateMessage = new NegotiateMessage(inputToken); } catch { return(NTStatus.SEC_E_INVALID_TOKEN); } ChallengeMessage challengeMessage; NTStatus status = GetChallengeMessage(out context, negotiateMessage, out challengeMessage); outputToken = challengeMessage.GetBytes(); return(status); } else if (messageType == MessageTypeName.Authenticate) { AuthenticateMessage authenticateMessage; try { authenticateMessage = new AuthenticateMessage(inputToken); } catch { return(NTStatus.SEC_E_INVALID_TOKEN); } return(Authenticate(context, authenticateMessage)); } else { return(NTStatus.SEC_E_INVALID_TOKEN); } }
public override NTStatus Authenticate(object context, AuthenticateMessage message) { AuthContext authContext = context as AuthContext; if (authContext == null) { // There are two possible reasons for authContext to be null: // 1. We have a bug in our implementation, let's assume that's not the case, // according to [MS-SMB2] 3.3.5.5.1 we aren't allowed to return SEC_E_INVALID_HANDLE anyway. // 2. The client sent AuthenticateMessage without sending NegotiateMessage first, // in this case the correct response is SEC_E_INVALID_TOKEN. return(NTStatus.SEC_E_INVALID_TOKEN); } authContext.DomainName = message.DomainName; authContext.UserName = message.UserName; authContext.WorkStation = message.WorkStation; if (message.Version != null) { authContext.OSVersion = message.Version.ToString(); } if ((message.NegotiateFlags & NegotiateFlags.Anonymous) > 0) { if (this.EnableGuestLogin) { authContext.IsGuest = true; return(NTStatus.STATUS_SUCCESS); } else { return(NTStatus.STATUS_LOGON_FAILURE); } } if (!m_loginCounter.HasRemainingLoginAttempts(message.UserName.ToLower())) { return(NTStatus.STATUS_ACCOUNT_LOCKED_OUT); } string password = m_GetUserPassword(message.UserName); if (password == null) { if (this.EnableGuestLogin) { authContext.IsGuest = true; return(NTStatus.STATUS_SUCCESS); } else { if (m_loginCounter.HasRemainingLoginAttempts(message.UserName.ToLower(), true)) { return(NTStatus.STATUS_LOGON_FAILURE); } else { return(NTStatus.STATUS_ACCOUNT_LOCKED_OUT); } } } bool success; byte[] serverChallenge = authContext.ServerChallenge; byte[] sessionBaseKey; byte[] keyExchangeKey = null; if ((message.NegotiateFlags & NegotiateFlags.ExtendedSessionSecurity) > 0) { if (AuthenticationMessageUtils.IsNTLMv1ExtendedSessionSecurity(message.LmChallengeResponse)) { // NTLM v1 Extended Session Security: success = AuthenticateV1Extended(password, serverChallenge, message.LmChallengeResponse, message.NtChallengeResponse); if (success) { // https://msdn.microsoft.com/en-us/library/cc236699.aspx sessionBaseKey = new MD4().GetByteHashFromBytes(NTLMCryptography.NTOWFv1(password)); byte[] lmowf = NTLMCryptography.LMOWFv1(password); keyExchangeKey = NTLMCryptography.KXKey(sessionBaseKey, message.NegotiateFlags, message.LmChallengeResponse, serverChallenge, lmowf); } } else { // NTLM v2: success = AuthenticateV2(message.DomainName, message.UserName, password, serverChallenge, message.LmChallengeResponse, message.NtChallengeResponse); if (success) { // https://msdn.microsoft.com/en-us/library/cc236700.aspx byte[] responseKeyNT = NTLMCryptography.NTOWFv2(password, message.UserName, message.DomainName); byte[] ntProofStr = ByteReader.ReadBytes(message.NtChallengeResponse, 0, 16); sessionBaseKey = new HMACMD5(responseKeyNT).ComputeHash(ntProofStr); keyExchangeKey = sessionBaseKey; } } } else { success = AuthenticateV1(password, serverChallenge, message.LmChallengeResponse, message.NtChallengeResponse); if (success) { // https://msdn.microsoft.com/en-us/library/cc236699.aspx sessionBaseKey = new MD4().GetByteHashFromBytes(NTLMCryptography.NTOWFv1(password)); byte[] lmowf = NTLMCryptography.LMOWFv1(password); keyExchangeKey = NTLMCryptography.KXKey(sessionBaseKey, message.NegotiateFlags, message.LmChallengeResponse, serverChallenge, lmowf); } } if (success) { // https://msdn.microsoft.com/en-us/library/cc236676.aspx // https://blogs.msdn.microsoft.com/openspecification/2010/04/19/ntlm-keys-and-sundry-stuff/ if ((message.NegotiateFlags & NegotiateFlags.KeyExchange) > 0) { authContext.SessionKey = RC4.Decrypt(keyExchangeKey, message.EncryptedRandomSessionKey); } else { authContext.SessionKey = keyExchangeKey; } return(NTStatus.STATUS_SUCCESS); } else { if (m_loginCounter.HasRemainingLoginAttempts(message.UserName.ToLower(), true)) { return(NTStatus.STATUS_LOGON_FAILURE); } else { return(NTStatus.STATUS_ACCOUNT_LOCKED_OUT); } } }
public abstract NTStatus Authenticate(object context, AuthenticateMessage authenticateMessage);