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 input = new NegotiateMessage(inputToken);
                ChallengeMessage output;
                NTStatus         status = GetChallengeMessage(out context, input, out output);
                outputToken = output.GetBytes();
                return(status);
            }
            else if (messageType == MessageTypeName.Authenticate)
            {
                AuthenticateMessage message = new AuthenticateMessage(inputToken);
                return(Authenticate(context, message));
            }
            else
            {
                return(NTStatus.SEC_E_INVALID_TOKEN);
            }
        }
예제 #2
0
        public override NTStatus Authenticate(object context, byte[] authenticateMessageBytes)
        {
            AuthenticateMessage message;

            try
            {
                message = new AuthenticateMessage(authenticateMessageBytes);
            }
            catch
            {
                return(NTStatus.SEC_E_INVALID_TOKEN);
            }

            if (!(context is AuthContext authContext))
            {
                // 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 (!EnableGuestLogin)
                {
                    return(NTStatus.STATUS_LOGON_FAILURE);
                }
                authContext.IsGuest = true;
                return(NTStatus.STATUS_SUCCESS);
            }

            if (!m_loginCounter.HasRemainingLoginAttempts(message.UserName.ToLower()))
            {
                return(NTStatus.STATUS_ACCOUNT_LOCKED_OUT);
            }

            string password = m_GetUserPassword(message.UserName);

            if (password == null)
            {
                if (EnableGuestLogin)
                {
                    authContext.IsGuest = true;
                    return(NTStatus.STATUS_SUCCESS);
                }

                if (m_loginCounter.HasRemainingLoginAttempts(message.UserName.ToLower(), true))
                {
                    return(NTStatus.STATUS_LOGON_FAILURE);
                }

                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);
                        using HMACMD5 hmacmd5 = new HMACMD5(responseKeyNT);
                        sessionBaseKey        = hmacmd5.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);
            }

            if (m_loginCounter.HasRemainingLoginAttempts(message.UserName.ToLower(), true))
            {
                return(NTStatus.STATUS_LOGON_FAILURE);
            }

            return(NTStatus.STATUS_ACCOUNT_LOCKED_OUT);
        }
 public abstract NTStatus Authenticate(object context, AuthenticateMessage authenticateMessage);