Example #1
0
    def test_authenticate_message_ntlm_v1_non_unicode(self, session_key,
                                                      version_function):
        test_challenge_message = ChallengeMessage(ntlmv1_challenge_message)
        test_challenge_message.negotiate_flags -= NegotiateFlags.NTLMSSP_NEGOTIATE_UNICODE
        test_challenge_message.negotiate_flags |= NegotiateFlags.NTLMSSP_NEGOTIATE_OEM

        # Not a Microsoft example, using pre-computed value
        expected = HexToByte('4e 54 4c 4d 53 53 50 00 03 00 00 00 18 00 18 00'
                             '62 00 00 00 18 00 18 00 7a 00 00 00 06 00 06 00'
                             '48 00 00 00 04 00 04 00 4e 00 00 00 10 00 10 00'
                             '52 00 00 00 10 00 10 00 92 00 00 00 32 82 02 e2'
                             '05 01 28 0a 00 00 00 0f 44 6f 6d 61 69 6e 55 73'
                             '65 72 43 00 4f 00 4d 00 50 00 55 00 54 00 45 00'
                             '52 00 98 de f7 b8 7f 88 aa 5d af e2 df 77 96 88'
                             'a1 72 de f1 1c 7d 5c cd ef 13 67 c4 30 11 f3 02'
                             '98 a2 ad 35 ec e6 4f 16 33 1c 44 bd be d9 27 84'
                             '1f 94 51 88 22 b1 b3 f3 50 c8 95 86 82 ec bb 3e'
                             '3c b7')
        actual = AuthenticateMessage(user_name, password, domain_name,
                                     workstation_name, test_challenge_message,
                                     1, None)
        actual.add_mic(None, test_challenge_message)
        actual = actual.get_data()

        assert actual == expected
Example #2
0
    def test_authenticate_message_with_cbt(self, random_function,
                                           session_key_function,
                                           timestamp_function):
        test_challenge_message = ChallengeMessage(ntlmv2_challenge_message)
        test_server_cert_hash = 'E3CA49271E5089CC48CE82109F1324F41DBEDDC29A777410C738F7868C4FF405'

        # Not a Microsoft example, using pre-computed value
        expected = HexToByte('4e 54 4c 4d 53 53 50 00 03 00 00 00 18 00 18 00'
                             '7c 00 00 00 68 00 68 00 94 00 00 00 0c 00 0c 00'
                             '48 00 00 00 08 00 08 00 54 00 00 00 20 00 20 00'
                             '5c 00 00 00 10 00 10 00 fc 00 00 00 31 82 8a e2'
                             '06 01 b1 1d 00 00 00 0f 44 00 6f 00 6d 00 61 00'
                             '69 00 6e 00 55 00 73 00 65 00 72 00 43 00 00 00'
                             '4f 00 00 00 4d 00 00 00 50 00 00 00 55 00 00 00'
                             '54 00 00 00 45 00 00 00 52 00 00 00 86 c3 50 97'
                             'ac 9c ec 10 25 54 76 4a 57 cc cc 19 aa aa aa aa'
                             'aa aa aa aa 04 10 c4 7a cf 19 97 89 de 7f 20 11'
                             '95 7a ea 50 01 01 00 00 00 00 00 00 00 00 00 00'
                             '00 00 00 00 aa aa aa aa aa aa aa aa 00 00 00 00'
                             '02 00 0c 00 44 00 6f 00 6d 00 61 00 69 00 6e 00'
                             '01 00 0c 00 53 00 65 00 72 00 76 00 65 00 72 00'
                             '0a 00 10 00 6e a1 9d f0 66 da 46 22 05 1f 9c 4f'
                             '92 c6 df 74 00 00 00 00 00 00 00 00 e5 69 95 1d'
                             '15 d4 73 5f 49 e1 4c f9 a7 d3 e6 72')
        actual = AuthenticateMessage(user_name, password, domain_name,
                                     workstation_name, test_challenge_message,
                                     3, test_server_cert_hash)
        actual.add_mic(None, test_challenge_message)
        actual = actual.get_data()

        assert actual == expected
Example #3
0
    def test_authenticate_message_ntlm_v1(self, monkeypatch):
        monkeypatch.setattr('ntlm_auth.messages.get_random_export_session_key',
                            lambda: b"\x55" * 16)
        monkeypatch.setattr('ntlm_auth.messages.get_version',
                            lambda s: b"\x05\x01\x28\x0A\x00\x00\x00\x0F")

        test_challenge_message = ChallengeMessage(
            b"\x4e\x54\x4c\x4d\x53\x53\x50\x00"
            b"\x02\x00\x00\x00\x0c\x00\x0c\x00"
            b"\x38\x00\x00\x00\x33\x82\x02\xe2"
            b"\x01\x23\x45\x67\x89\xab\xcd\xef"
            b"\x06\x00\x70\x17\x00\x00\x00\x0f"
            b"\x53\x00\x65\x00\x72\x00\x76\x00"
            b"\x65\x00\x72\x00")
        # Need to override the flags in the challenge message to match the
        # expectation, these flags are inconsequential and are done manualy for
        # sanity
        test_challenge_message.negotiate_flags -= \
            NegotiateFlags.NTLMSSP_TARGET_TYPE_SERVER
        test_challenge_message.negotiate_flags |= \
            NegotiateFlags.NTLMSSP_REQUEST_TARGET
        test_challenge_message.negotiate_flags |= \
            NegotiateFlags.NTLMSSP_NEGOTIATE_TARGET_INFO

        expected = b"\x4e\x54\x4c\x4d\x53\x53\x50\x00" \
                   b"\x03\x00\x00\x00\x18\x00\x18\x00" \
                   b"\x6c\x00\x00\x00\x18\x00\x18\x00" \
                   b"\x84\x00\x00\x00\x0c\x00\x0c\x00" \
                   b"\x48\x00\x00\x00\x08\x00\x08\x00" \
                   b"\x54\x00\x00\x00\x10\x00\x10\x00" \
                   b"\x5c\x00\x00\x00\x10\x00\x10\x00" \
                   b"\x9c\x00\x00\x00\x35\x82\x80\xe2" \
                   b"\x05\x01\x28\x0a\x00\x00\x00\x0f" \
                   b"\x44\x00\x6f\x00\x6d\x00\x61\x00" \
                   b"\x69\x00\x6e\x00\x55\x00\x73\x00" \
                   b"\x65\x00\x72\x00\x43\x00\x4f\x00" \
                   b"\x4d\x00\x50\x00\x55\x00\x54\x00" \
                   b"\x45\x00\x52\x00\x98\xde\xf7\xb8" \
                   b"\x7f\x88\xaa\x5d\xaf\xe2\xdf\x77" \
                   b"\x96\x88\xa1\x72\xde\xf1\x1c\x7d" \
                   b"\x5c\xcd\xef\x13\x67\xc4\x30\x11" \
                   b"\xf3\x02\x98\xa2\xad\x35\xec\xe6" \
                   b"\x4f\x16\x33\x1c\x44\xbd\xbe\xd9" \
                   b"\x27\x84\x1f\x94\x51\x88\x22\xb1" \
                   b"\xb3\xf3\x50\xc8\x95\x86\x82\xec" \
                   b"\xbb\x3e\x3c\xb7"

        actual = AuthenticateMessage("User", "Password", "Domain", "COMPUTER",
                                     test_challenge_message, 1, None)
        actual.add_mic(None, test_challenge_message)
        actual = actual.get_data()
        assert actual == expected
Example #4
0
    def test_authenticate_message_ntlm_v1_with_ess(self, monkeypatch):
        monkeypatch.setattr('os.urandom', lambda s: b"\xaa" * 8)
        monkeypatch.setattr('ntlm_auth.messages.get_version',
                            lambda s: b"\x05\x01\x28\x0A\x00\x00\x00\x0F")

        test_challenge_message = ChallengeMessage(
            b"\x4e\x54\x4c\x4d\x53\x53\x50\x00"
            b"\x02\x00\x00\x00\x0c\x00\x0c\x00"
            b"\x38\x00\x00\x00\x33\x82\x0a\x82"
            b"\x01\x23\x45\x67\x89\xab\xcd\xef"
            b"\x00\x00\x00\x00\x00\x00\x00\x00"
            b"\x00\x00\x00\x00\x00\x00\x00\x00"
            b"\x06\x00\x70\x17\x00\x00\x00\x0f"
            b"\x53\x00\x65\x00\x72\x00\x76\x00"
            b"\x65\x00\x72\x00")
        # Need to override the flags in the challenge message to match the
        # expectation, these flags are inconsequential and are done manualy
        # for sanity
        test_challenge_message.negotiate_flags -= \
            NegotiateFlags.NTLMSSP_TARGET_TYPE_SERVER
        test_challenge_message.negotiate_flags |= \
            NegotiateFlags.NTLMSSP_REQUEST_TARGET

        expected = b"\x4e\x54\x4c\x4d\x53\x53\x50\x00" \
                   b"\x03\x00\x00\x00\x18\x00\x18\x00" \
                   b"\x6c\x00\x00\x00\x18\x00\x18\x00" \
                   b"\x84\x00\x00\x00\x0c\x00\x0c\x00" \
                   b"\x48\x00\x00\x00\x08\x00\x08\x00" \
                   b"\x54\x00\x00\x00\x10\x00\x10\x00" \
                   b"\x5c\x00\x00\x00\x00\x00\x00\x00" \
                   b"\x9c\x00\x00\x00\x35\x82\x08\x82" \
                   b"\x05\x01\x28\x0a\x00\x00\x00\x0f" \
                   b"\x44\x00\x6f\x00\x6d\x00\x61\x00" \
                   b"\x69\x00\x6e\x00\x55\x00\x73\x00" \
                   b"\x65\x00\x72\x00\x43\x00\x4f\x00" \
                   b"\x4d\x00\x50\x00\x55\x00\x54\x00" \
                   b"\x45\x00\x52\x00\xaa\xaa\xaa\xaa" \
                   b"\xaa\xaa\xaa\xaa\x00\x00\x00\x00" \
                   b"\x00\x00\x00\x00\x00\x00\x00\x00" \
                   b"\x00\x00\x00\x00\x75\x37\xf8\x03" \
                   b"\xae\x36\x71\x28\xca\x45\x82\x04" \
                   b"\xbd\xe7\xca\xf8\x1e\x97\xed\x26" \
                   b"\x83\x26\x72\x32"

        actual = AuthenticateMessage("User", "Password", "Domain", "COMPUTER",
                                     test_challenge_message, 1, None)
        actual.add_mic(None, test_challenge_message)
        actual = actual.get_data()
        assert actual == expected
Example #5
0
    def test_authenticate_message_ntlm_v1(self, session_key, version_function):
        test_challenge_message = ChallengeMessage(ntlmv1_challenge_message)
        # Need to override the flags in the challenge message to match the expectation, these flags are inconsequential and are done manualy for sanity
        test_challenge_message.negotiate_flags -= NegotiateFlags.NTLMSSP_TARGET_TYPE_SERVER
        test_challenge_message.negotiate_flags |= NegotiateFlags.NTLMSSP_REQUEST_TARGET
        test_challenge_message.negotiate_flags |= NegotiateFlags.NTLMSSP_NEGOTIATE_TARGET_INFO

        expected = ntlmv1_authenticate_message

        actual = AuthenticateMessage(user_name, password, domain_name,
                                     "COMPUTER", test_challenge_message, 1,
                                     None)
        actual.add_mic(None, test_challenge_message)
        actual = actual.get_data()

        assert actual == expected
Example #6
0
    def test_authenticate_message_ntlm_v1_non_unicode(self, monkeypatch):
        monkeypatch.setattr('ntlm_auth.messages.get_random_export_session_key',
                            lambda: b"\x55" * 16)
        monkeypatch.setattr('ntlm_auth.messages.get_version',
                            lambda s: b"\x05\x01\x28\x0A\x00\x00\x00\x0F")

        test_challenge_message = ChallengeMessage(
            b"\x4e\x54\x4c\x4d\x53\x53\x50\x00"
            b"\x02\x00\x00\x00\x0c\x00\x0c\x00"
            b"\x38\x00\x00\x00\x33\x82\x02\xe2"
            b"\x01\x23\x45\x67\x89\xab\xcd\xef"
            b"\x06\x00\x70\x17\x00\x00\x00\x0f"
            b"\x53\x00\x65\x00\x72\x00\x76\x00"
            b"\x65\x00\x72\x00")
        test_challenge_message.negotiate_flags -= \
            NegotiateFlags.NTLMSSP_NEGOTIATE_UNICODE
        test_challenge_message.negotiate_flags |= \
            NegotiateFlags.NTLMSSP_NEGOTIATE_OEM

        # Not a Microsoft example, using pre-computed value
        expected = b"\x4e\x54\x4c\x4d\x53\x53\x50\x00" \
                   b"\x03\x00\x00\x00\x18\x00\x18\x00" \
                   b"\x5a\x00\x00\x00\x18\x00\x18\x00" \
                   b"\x72\x00\x00\x00\x06\x00\x06\x00" \
                   b"\x48\x00\x00\x00\x04\x00\x04\x00" \
                   b"\x4e\x00\x00\x00\x08\x00\x08\x00" \
                   b"\x52\x00\x00\x00\x10\x00\x10\x00" \
                   b"\x8a\x00\x00\x00\x32\x82\x02\xe2" \
                   b"\x05\x01\x28\x0a\x00\x00\x00\x0f" \
                   b"\x44\x6f\x6d\x61\x69\x6e\x55\x73" \
                   b"\x65\x72\x43\x4f\x4d\x50\x55\x54" \
                   b"\x45\x52\x98\xde\xf7\xb8\x7f\x88" \
                   b"\xaa\x5d\xaf\xe2\xdf\x77\x96\x88" \
                   b"\xa1\x72\xde\xf1\x1c\x7d\x5c\xcd" \
                   b"\xef\x13\x67\xc4\x30\x11\xf3\x02" \
                   b"\x98\xa2\xad\x35\xec\xe6\x4f\x16" \
                   b"\x33\x1c\x44\xbd\xbe\xd9\x27\x84" \
                   b"\x1f\x94\x51\x88\x22\xb1\xb3\xf3" \
                   b"\x50\xc8\x95\x86\x82\xec\xbb\x3e" \
                   b"\x3c\xb7"
        actual = AuthenticateMessage("User", "Password", "Domain", "COMPUTER",
                                     test_challenge_message, 1, None)
        actual.add_mic(None, test_challenge_message)
        actual = actual.get_data()
        assert actual == expected
Example #7
0
    def test_authenticate_without_domain_workstation(self, session_key,
                                                     version_function):
        test_challenge_message = ChallengeMessage(ntlmv1_challenge_message)

        # Not a Microsoft example, using pre-computed value
        expected = HexToByte('4e 54 4c 4d 53 53 50 00 03 00 00 00 18 00 18 00'
                             '50 00 00 00 18 00 18 00 68 00 00 00 00 00 00 00'
                             '48 00 00 00 08 00 08 00 48 00 00 00 00 00 00 00'
                             '50 00 00 00 10 00 10 00 80 00 00 00 31 82 02 e2'
                             '05 01 28 0a 00 00 00 0f 55 00 73 00 65 00 72 00'
                             '98 de f7 b8 7f 88 aa 5d af e2 df 77 96 88 a1 72'
                             'de f1 1c 7d 5c cd ef 13 67 c4 30 11 f3 02 98 a2'
                             'ad 35 ec e6 4f 16 33 1c 44 bd be d9 27 84 1f 94'
                             '51 88 22 b1 b3 f3 50 c8 95 86 82 ec bb 3e 3c b7')

        actual = AuthenticateMessage(user_name, password, None, None,
                                     test_challenge_message, 1, None)
        actual.add_mic(None, test_challenge_message)
        actual = actual.get_data()

        assert actual == expected
Example #8
0
    def test_authenticate_without_domain_workstation(self, monkeypatch):
        monkeypatch.setattr('ntlm_auth.messages.get_random_export_session_key',
                            lambda: b"\x55" * 16)
        monkeypatch.setattr('ntlm_auth.messages.get_version',
                            lambda s: b"\x05\x01\x28\x0A\x00\x00\x00\x0F")

        test_challenge_message = ChallengeMessage(
            b"\x4e\x54\x4c\x4d\x53\x53\x50\x00"
            b"\x02\x00\x00\x00\x0c\x00\x0c\x00"
            b"\x38\x00\x00\x00\x33\x82\x02\xe2"
            b"\x01\x23\x45\x67\x89\xab\xcd\xef"
            b"\x06\x00\x70\x17\x00\x00\x00\x0f"
            b"\x53\x00\x65\x00\x72\x00\x76\x00"
            b"\x65\x00\x72\x00")

        # Not a Microsoft example, using pre-computed value
        expected = b"\x4e\x54\x4c\x4d\x53\x53\x50\x00" \
                   b"\x03\x00\x00\x00\x18\x00\x18\x00" \
                   b"\x50\x00\x00\x00\x18\x00\x18\x00" \
                   b"\x68\x00\x00\x00\x00\x00\x00\x00" \
                   b"\x48\x00\x00\x00\x08\x00\x08\x00" \
                   b"\x48\x00\x00\x00\x00\x00\x00\x00" \
                   b"\x50\x00\x00\x00\x10\x00\x10\x00" \
                   b"\x80\x00\x00\x00\x31\x82\x02\xe2" \
                   b"\x05\x01\x28\x0a\x00\x00\x00\x0f" \
                   b"\x55\x00\x73\x00\x65\x00\x72\x00" \
                   b"\x98\xde\xf7\xb8\x7f\x88\xaa\x5d" \
                   b"\xaf\xe2\xdf\x77\x96\x88\xa1\x72" \
                   b"\xde\xf1\x1c\x7d\x5c\xcd\xef\x13" \
                   b"\x67\xc4\x30\x11\xf3\x02\x98\xa2" \
                   b"\xad\x35\xec\xe6\x4f\x16\x33\x1c" \
                   b"\x44\xbd\xbe\xd9\x27\x84\x1f\x94" \
                   b"\x51\x88\x22\xb1\xb3\xf3\x50\xc8" \
                   b"\x95\x86\x82\xec\xbb\x3e\x3c\xb7"

        actual = AuthenticateMessage("User", "Password", None, None,
                                     test_challenge_message, 1, None)
        actual.add_mic(None, test_challenge_message)
        actual = actual.get_data()
        assert actual == expected
Example #9
0
    def test_authenticate_message_with_mic(self, random_function,
                                           session_key_function):
        test_challenge_message = ChallengeMessage(ntlmv2_challenge_message)
        test_challenge_message.target_info[
            TargetInfo.MSV_AV_TIMESTAMP] = mock_timestamp()
        test_server_cert_hash = 'E3CA49271E5089CC48CE82109F1324F41DBEDDC29A777410C738F7868C4FF405'
        test_negotiate_message = NegotiateMessage(ntlmv2_negotiate_flags,
                                                  domain_name,
                                                  workstation_name)

        # Not a Microsoft example, using pre-computed value
        expected = HexToByte('4e 54 4c 4d 53 53 50 00 03 00 00 00 18 00 18 00'
                             '8c 00 00 00 7c 00 7c 00 a4 00 00 00 0c 00 0c 00'
                             '58 00 00 00 08 00 08 00 64 00 00 00 20 00 20 00'
                             '6c 00 00 00 10 00 10 00 20 01 00 00 31 82 8a e2'
                             '06 01 b1 1d 00 00 00 0f 77 ff c5 e6 db 55 87 0e'
                             '65 8d 7c ff 33 cd 90 2e 44 00 6f 00 6d 00 61 00'
                             '69 00 6e 00 55 00 73 00 65 00 72 00 43 00 00 00'
                             '4f 00 00 00 4d 00 00 00 50 00 00 00 55 00 00 00'
                             '54 00 00 00 45 00 00 00 52 00 00 00 00 00 00 00'
                             '00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00'
                             '00 00 00 00 a1 3d 03 8a d0 ca 02 64 33 89 7c 33'
                             '5e 0f 56 df 01 01 00 00 00 00 00 00 00 00 00 00'
                             '00 00 00 00 aa aa aa aa aa aa aa aa 00 00 00 00'
                             '02 00 0c 00 44 00 6f 00 6d 00 61 00 69 00 6e 00'
                             '01 00 0c 00 53 00 65 00 72 00 76 00 65 00 72 00'
                             '07 00 08 00 00 00 00 00 00 00 00 00 06 00 04 00'
                             '02 00 00 00 0a 00 10 00 6e a1 9d f0 66 da 46 22'
                             '05 1f 9c 4f 92 c6 df 74 00 00 00 00 00 00 00 00'
                             '1d 08 89 d1 a5 ee ed 21 91 9e 1a b8 27 c3 0b 17')

        actual = AuthenticateMessage(user_name, password, domain_name,
                                     workstation_name, test_challenge_message,
                                     3, test_server_cert_hash)
        actual.add_mic(test_negotiate_message, test_challenge_message)
        actual = actual.get_data()

        assert actual == expected
Example #10
0
class Ntlm(object):
    def __init__(self, ntlm_compatibility=3):
        """
        Initialises the NTLM context to use when sending and receiving messages
        to and from the server. You should be using this object as it supports
        NTLMv2 authenticate and it easier to use than before. It also brings in
        the ability to use signing and sealing with session_security and
        generate a MIC structure.

        :param ntlm_compatibility: (Default 3)
            The Lan Manager Compatibility Level to use with the auth message
            This is set by an Administrator in the registry key
            'HKLM\SYSTEM\CurrentControlSet\Control\Lsa\LmCompatibilityLevel'
            The values correspond to the following;
                0 : LM and NTLMv1
                1 : LM, NTLMv1 and NTLMv1 with Extended Session Security
                2 : NTLMv1 and NTLMv1 with Extended Session Security
                3-5 : NTLMv2 Only
            Note: Values 3 to 5 are no different from a client perspective

        Attributes:
            negotiate_flags: A NEGOTIATE structure that contains a set of bit
                flags. These flags are the options the client supports and are
                sent in the negotiate_message
            ntlm_compatibility: The Lan Manager Compatibility Level, same as
                the input if supplied
            negotiate_message: A NegotiateMessage object that is sent to the
                server
            challenge_message: A ChallengeMessage object that has been created
                from the server response
            authenticate_message: An AuthenticateMessage object that is sent to
                the server based on the ChallengeMessage
            session_security: A SessionSecurity structure that can be used to
                sign and seal messages sent after the authentication challenge
        """
        self.ntlm_compatibility = ntlm_compatibility

        # Setting up our flags so the challenge message returns the target info
        # block if supported
        self.negotiate_flags = NegotiateFlags.NTLMSSP_NEGOTIATE_TARGET_INFO | \
            NegotiateFlags.NTLMSSP_NEGOTIATE_128 | \
            NegotiateFlags.NTLMSSP_NEGOTIATE_56 | \
            NegotiateFlags.NTLMSSP_NEGOTIATE_UNICODE | \
            NegotiateFlags.NTLMSSP_NEGOTIATE_VERSION | \
            NegotiateFlags.NTLMSSP_NEGOTIATE_KEY_EXCH | \
            NegotiateFlags.NTLMSSP_NEGOTIATE_ALWAYS_SIGN | \
            NegotiateFlags.NTLMSSP_NEGOTIATE_SIGN | \
            NegotiateFlags.NTLMSSP_NEGOTIATE_SEAL

        # Setting the message types based on the ntlm_compatibility level
        self._set_ntlm_compatibility_flags(self.ntlm_compatibility)

        self.negotiate_message = None
        self.challenge_message = None
        self.authenticate_message = None
        self.session_security = None

    def create_negotiate_message(self, domain_name=None, workstation=None):
        """
        Create an NTLM NEGOTIATE_MESSAGE

        :param domain_name: The domain name of the user account we are
            authenticating with, default is None
        :param worksation: The workstation we are using to authenticate with,
            default is None
        :return: A base64 encoded string of the NEGOTIATE_MESSAGE
        """
        self.negotiate_message = NegotiateMessage(self.negotiate_flags,
                                                  domain_name, workstation)

        return base64.b64encode(self.negotiate_message.get_data())

    def parse_challenge_message(self, msg2):
        """
        Parse the NTLM CHALLENGE_MESSAGE from the server and add it to the Ntlm
        context fields

        :param msg2: A base64 encoded string of the CHALLENGE_MESSAGE
        """
        msg2 = base64.b64decode(msg2)
        self.challenge_message = ChallengeMessage(msg2)

    def create_authenticate_message(self,
                                    user_name,
                                    password,
                                    domain_name=None,
                                    workstation=None,
                                    server_certificate_hash=None):
        """
        Create an NTLM AUTHENTICATE_MESSAGE based on the Ntlm context and the
        previous messages sent and received

        :param user_name: The user name of the user we are trying to
            authenticate with
        :param password: The password of the user we are trying to authenticate
            with
        :param domain_name: The domain name of the user account we are
            authenticated with, default is None
        :param workstation: The workstation we are using to authenticate with,
            default is None
        :param server_certificate_hash: The SHA256 hash string of the server
            certificate (DER encoded) NTLM is authenticating to. Used for
            Channel Binding Tokens. If nothing is supplied then the CBT hash
            will not be sent. See messages.py AuthenticateMessage for more
            details
        :return: A base64 encoded string of the AUTHENTICATE_MESSAGE
        """
        self.authenticate_message = \
            AuthenticateMessage(user_name, password, domain_name, workstation,
                                self.challenge_message,
                                self.ntlm_compatibility,
                                server_certificate_hash)
        self.authenticate_message.add_mic(self.negotiate_message,
                                          self.challenge_message)

        # Setups up the session_security context used to sign and seal messages
        # if wanted
        if self.negotiate_flags & NegotiateFlags.NTLMSSP_NEGOTIATE_SEAL or \
                self.negotiate_flags & NegotiateFlags.NTLMSSP_NEGOTIATE_SIGN:
            flags = self.authenticate_message.negotiate_flags
            flag_bytes = struct.unpack("<I", flags)[0]
            self.session_security = \
                SessionSecurity(flag_bytes,
                                self.authenticate_message.exported_session_key)

        return base64.b64encode(self.authenticate_message.get_data())

    def _set_ntlm_compatibility_flags(self, ntlm_compatibility):
        if (ntlm_compatibility >= 0) and (ntlm_compatibility <= 5):
            if ntlm_compatibility == 0:
                self.negotiate_flags |= \
                    NegotiateFlags.NTLMSSP_NEGOTIATE_NTLM | \
                    NegotiateFlags.NTLMSSP_NEGOTIATE_LM_KEY
            elif ntlm_compatibility == 1:
                self.negotiate_flags |= \
                    NegotiateFlags.NTLMSSP_NEGOTIATE_NTLM | \
                    NegotiateFlags.NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY
            else:
                self.negotiate_flags |= \
                    NegotiateFlags.NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY
        else:
            raise Exception("Unknown ntlm_compatibility level - "
                            "expecting value between 0 and 5")
Example #11
0
class NtlmContext(object):

    def __init__(self, username, password, domain=None, workstation=None,
                 cbt_data=None, ntlm_compatibility=3):
        r"""
        Initialises a NTLM context to use when authenticating using the NTLM
        protocol.
        Initialises the NTLM context to use when sending and receiving messages
        to and from the server. You should be using this object as it supports
        NTLMv2 authenticate and it easier to use than before. It also brings in
        the ability to use signing and sealing with session_security and
        generate a MIC structure.

        :param username: The username to authenticate with
        :param password: The password for the username
        :param domain: The domain part of the username (None if n/a)
        :param workstation: The localworkstation (None if n/a)
        :param cbt_data: A GssChannelBindingsStruct or None to bind channel
            data with the auth process
        :param ntlm_compatibility: (Default 3)
            The Lan Manager Compatibility Level to use with the auth message
            This is set by an Administrator in the registry key
            'HKLM\SYSTEM\CurrentControlSet\Control\Lsa\LmCompatibilityLevel'
            The values correspond to the following;
                0 : LM and NTLMv1
                1 : LM, NTLMv1 and NTLMv1 with Extended Session Security
                2 : NTLMv1 and NTLMv1 with Extended Session Security
                3-5 : NTLMv2 Only
            Note: Values 3 to 5 are no different from a client perspective
        """
        self.username = username
        self.password = password
        self.domain = domain
        self.workstation = workstation
        self.cbt_data = cbt_data
        self._server_certificate_hash = None  # deprecated for backwards compat
        self.ntlm_compatibility = ntlm_compatibility
        self.complete = False

        # Setting up our flags so the challenge message returns the target info
        # block if supported
        self.negotiate_flags = NegotiateFlags.NTLMSSP_NEGOTIATE_TARGET_INFO | \
            NegotiateFlags.NTLMSSP_NEGOTIATE_128 | \
            NegotiateFlags.NTLMSSP_NEGOTIATE_56 | \
            NegotiateFlags.NTLMSSP_NEGOTIATE_UNICODE | \
            NegotiateFlags.NTLMSSP_NEGOTIATE_VERSION | \
            NegotiateFlags.NTLMSSP_NEGOTIATE_KEY_EXCH | \
            NegotiateFlags.NTLMSSP_NEGOTIATE_ALWAYS_SIGN | \
            NegotiateFlags.NTLMSSP_NEGOTIATE_SIGN | \
            NegotiateFlags.NTLMSSP_NEGOTIATE_SEAL

        # Setting the message types based on the ntlm_compatibility level
        self._set_ntlm_compatibility_flags(self.ntlm_compatibility)

        self._negotiate_message = None
        self._challenge_message = None
        self._authenticate_message = None
        self._session_security = None

    @property
    def mic_present(self):
        if self._authenticate_message:
            return bool(self._authenticate_message.mic)

        return False

    @property
    def session_key(self):
        if self._authenticate_message:
            return self._authenticate_message.exported_session_key

    def reset_rc4_state(self, outgoing=True):
        """ Resets the signing cipher for the incoming or outgoing cipher. For SPNEGO for calculating mechListMIC. """
        if self._session_security:
            self._session_security.reset_rc4_state(outgoing=outgoing)

    def step(self, input_token=None):
        if self._negotiate_message is None:
            self._negotiate_message = NegotiateMessage(self.negotiate_flags,
                                                       self.domain,
                                                       self.workstation)
            return self._negotiate_message.get_data()
        else:
            self._challenge_message = ChallengeMessage(input_token)
            self._authenticate_message = AuthenticateMessage(
                self.username, self.password, self.domain, self.workstation,
                self._challenge_message, self.ntlm_compatibility,
                server_certificate_hash=self._server_certificate_hash,
                cbt_data=self.cbt_data
            )
            self._authenticate_message.add_mic(self._negotiate_message,
                                               self._challenge_message)

            flag_bytes = self._authenticate_message.negotiate_flags
            flags = struct.unpack("<I", flag_bytes)[0]
            if flags & NegotiateFlags.NTLMSSP_NEGOTIATE_SEAL or \
                    flags & NegotiateFlags.NTLMSSP_NEGOTIATE_SIGN:
                self._session_security = SessionSecurity(
                    flags, self.session_key
                )

            self.complete = True
            return self._authenticate_message.get_data()

    def sign(self, data):
        return self._session_security.get_signature(data)

    def verify(self, data, signature):
        self._session_security.verify_signature(data, signature)

    def wrap(self, data):
        if self._session_security is None:
            raise NoAuthContextError("Cannot wrap data as no security context "
                                     "has been established")

        data, header = self._session_security.wrap(data)
        return header + data

    def unwrap(self, data):
        if self._session_security is None:
            raise NoAuthContextError("Cannot unwrap data as no security "
                                     "context has been established")
        header = data[0:16]
        data = data[16:]
        message = self._session_security.unwrap(data, header)
        return message

    def _set_ntlm_compatibility_flags(self, ntlm_compatibility):
        if (ntlm_compatibility >= 0) and (ntlm_compatibility <= 5):
            if ntlm_compatibility == 0:
                self.negotiate_flags |= \
                    NegotiateFlags.NTLMSSP_NEGOTIATE_NTLM | \
                    NegotiateFlags.NTLMSSP_NEGOTIATE_LM_KEY
            elif ntlm_compatibility == 1:
                self.negotiate_flags |= \
                    NegotiateFlags.NTLMSSP_NEGOTIATE_NTLM | \
                    NegotiateFlags.NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY
            else:
                self.negotiate_flags |= \
                    NegotiateFlags.NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY
        else:
            raise Exception("Unknown ntlm_compatibility level - "
                            "expecting value between 0 and 5")
Example #12
0
    def test_authenticate_message_with_mic(self, monkeypatch):
        monkeypatch.setattr('os.urandom', lambda s: b"\xaa" * 8)
        monkeypatch.setattr('ntlm_auth.messages.get_random_export_session_key',
                            lambda: b"\x55" * 16)

        test_challenge_message = ChallengeMessage(
            b"\x4e\x54\x4c\x4d\x53\x53\x50\x00"
            b"\x02\x00\x00\x00\x03\x00\x0c\x00"
            b"\x38\x00\x00\x00\x33\x82\x8a\xe2"
            b"\x01\x23\x45\x67\x89\xab\xcd\xef"
            b"\x00\x00\x00\x00\x00\x00\x00\x00"
            b"\x24\x00\x24\x00\x44\x00\x00\x00"
            b"\x06\x00\x70\x17\x00\x00\x00\x0f"
            b"\x53\x00\x65\x00\x72\x00\x76\x00"
            b"\x65\x00\x72\x00\x02\x00\x0c\x00"
            b"\x44\x00\x6f\x00\x6d\x00\x61\x00"
            b"\x69\x00\x6e\x00\x01\x00\x0c\x00"
            b"\x53\x00\x65\x00\x72\x00\x76\x00"
            b"\x65\x00\x72\x00\x00\x00\x00\x00")
        test_challenge_message.target_info[AvId.MSV_AV_TIMESTAMP] = b"\x00" * 8
        test_server_cert_hash = \
            'E3CA49271E5089CC48CE82109F1324F41DBEDDC29A777410C738F7868C4FF405'
        test_negotiate_message = NegotiateMessage(3800728115, "Domain",
                                                  "COMPUTER")

        # Not a Microsoft example, using pre-computed value
        expected = b"\x4E\x54\x4C\x4D\x53\x53\x50\x00" \
                   b"\x03\x00\x00\x00\x18\x00\x18\x00" \
                   b"\x7C\x00\x00\x00\x7C\x00\x7C\x00" \
                   b"\x94\x00\x00\x00\x0C\x00\x0C\x00" \
                   b"\x58\x00\x00\x00\x08\x00\x08\x00" \
                   b"\x64\x00\x00\x00\x10\x00\x10\x00" \
                   b"\x6C\x00\x00\x00\x10\x00\x10\x00" \
                   b"\x10\x01\x00\x00\x31\x82\x8A\xE2" \
                   b"\x06\x01\xB1\x1D\x00\x00\x00\x0F" \
                   b"\xD2\xA1\x45\xDE\xA4\x25\x3E\x19" \
                   b"\x10\xFE\x0F\x5B\x7A\x0D\x2A\x90" \
                   b"\x44\x00\x6F\x00\x6D\x00\x61\x00" \
                   b"\x69\x00\x6E\x00\x55\x00\x73\x00" \
                   b"\x65\x00\x72\x00\x43\x00\x4F\x00" \
                   b"\x4D\x00\x50\x00\x55\x00\x54\x00" \
                   b"\x45\x00\x52\x00\x00\x00\x00\x00" \
                   b"\x00\x00\x00\x00\x00\x00\x00\x00" \
                   b"\x00\x00\x00\x00\x00\x00\x00\x00" \
                   b"\x00\x00\x00\x00\xA1\x3D\x03\x8A" \
                   b"\xD0\xCA\x02\x64\x33\x89\x7C\x33" \
                   b"\x5E\x0F\x56\xDF\x01\x01\x00\x00" \
                   b"\x00\x00\x00\x00\x00\x00\x00\x00" \
                   b"\x00\x00\x00\x00\xAA\xAA\xAA\xAA" \
                   b"\xAA\xAA\xAA\xAA\x00\x00\x00\x00" \
                   b"\x02\x00\x0C\x00\x44\x00\x6F\x00" \
                   b"\x6D\x00\x61\x00\x69\x00\x6E\x00" \
                   b"\x01\x00\x0C\x00\x53\x00\x65\x00" \
                   b"\x72\x00\x76\x00\x65\x00\x72\x00" \
                   b"\x07\x00\x08\x00\x00\x00\x00\x00" \
                   b"\x00\x00\x00\x00\x06\x00\x04\x00" \
                   b"\x02\x00\x00\x00\x0A\x00\x10\x00" \
                   b"\x6E\xA1\x9D\xF0\x66\xDA\x46\x22" \
                   b"\x05\x1F\x9C\x4F\x92\xC6\xDF\x74" \
                   b"\x00\x00\x00\x00\x00\x00\x00\x00" \
                   b"\x1D\x08\x89\xD1\xA5\xEE\xED\x21" \
                   b"\x91\x9E\x1A\xB8\x27\xC3\x0B\x17"

        actual = AuthenticateMessage("User", "Password", "Domain", "COMPUTER",
                                     test_challenge_message, 3,
                                     test_server_cert_hash)
        actual.add_mic(test_negotiate_message, test_challenge_message)
        actual = actual.get_data()
        assert actual == expected
Example #13
0
    def test_authenticate_message_with_cbt(self, monkeypatch):
        monkeypatch.setattr('os.urandom', lambda s: b"\xaa" * 8)
        monkeypatch.setattr('ntlm_auth.messages.get_random_export_session_key',
                            lambda: b"\x55" * 16)
        monkeypatch.setattr('ntlm_auth.compute_response.get_windows_timestamp',
                            lambda: b"\x00" * 8)

        test_challenge_message = ChallengeMessage(
            b"\x4e\x54\x4c\x4d\x53\x53\x50\x00"
            b"\x02\x00\x00\x00\x03\x00\x0c\x00"
            b"\x38\x00\x00\x00\x33\x82\x8a\xe2"
            b"\x01\x23\x45\x67\x89\xab\xcd\xef"
            b"\x00\x00\x00\x00\x00\x00\x00\x00"
            b"\x24\x00\x24\x00\x44\x00\x00\x00"
            b"\x06\x00\x70\x17\x00\x00\x00\x0f"
            b"\x53\x00\x65\x00\x72\x00\x76\x00"
            b"\x65\x00\x72\x00\x02\x00\x0c\x00"
            b"\x44\x00\x6f\x00\x6d\x00\x61\x00"
            b"\x69\x00\x6e\x00\x01\x00\x0c\x00"
            b"\x53\x00\x65\x00\x72\x00\x76\x00"
            b"\x65\x00\x72\x00\x00\x00\x00\x00")
        test_server_cert_hash = \
            'E3CA49271E5089CC48CE82109F1324F41DBEDDC29A777410C738F7868C4FF405'

        # Not a Microsoft example, using pre-computed value
        expected = b"\x4e\x54\x4c\x4d\x53\x53\x50\x00" \
                   b"\x03\x00\x00\x00\x18\x00\x18\x00" \
                   b"\x6c\x00\x00\x00\x68\x00\x68\x00" \
                   b"\x84\x00\x00\x00\x0c\x00\x0c\x00" \
                   b"\x48\x00\x00\x00\x08\x00\x08\x00" \
                   b"\x54\x00\x00\x00\x10\x00\x10\x00" \
                   b"\x5c\x00\x00\x00\x10\x00\x10\x00" \
                   b"\xec\x00\x00\x00\x31\x82\x8a\xe2" \
                   b"\x06\x01\xb1\x1d\x00\x00\x00\x0f" \
                   b"\x44\x00\x6f\x00\x6d\x00\x61\x00" \
                   b"\x69\x00\x6e\x00\x55\x00\x73\x00" \
                   b"\x65\x00\x72\x00\x43\x00\x4f\x00" \
                   b"\x4d\x00\x50\x00\x55\x00\x54\x00" \
                   b"\x45\x00\x52\x00\x86\xc3\x50\x97" \
                   b"\xac\x9c\xec\x10\x25\x54\x76\x4a" \
                   b"\x57\xcc\xcc\x19\xaa\xaa\xaa\xaa" \
                   b"\xaa\xaa\xaa\xaa\x04\x10\xc4\x7a" \
                   b"\xcf\x19\x97\x89\xde\x7f\x20\x11" \
                   b"\x95\x7a\xea\x50\x01\x01\x00\x00" \
                   b"\x00\x00\x00\x00\x00\x00\x00\x00" \
                   b"\x00\x00\x00\x00\xaa\xaa\xaa\xaa" \
                   b"\xaa\xaa\xaa\xaa\x00\x00\x00\x00" \
                   b"\x02\x00\x0c\x00\x44\x00\x6f\x00" \
                   b"\x6d\x00\x61\x00\x69\x00\x6e\x00" \
                   b"\x01\x00\x0c\x00\x53\x00\x65\x00" \
                   b"\x72\x00\x76\x00\x65\x00\x72\x00" \
                   b"\x0a\x00\x10\x00\x6e\xa1\x9d\xf0" \
                   b"\x66\xda\x46\x22\x05\x1f\x9c\x4f" \
                   b"\x92\xc6\xdf\x74\x00\x00\x00\x00" \
                   b"\x00\x00\x00\x00\xe5\x69\x95\x1d" \
                   b"\x15\xd4\x73\x5f\x49\xe1\x4c\xf9" \
                   b"\xa7\xd3\xe6\x72"
        actual = AuthenticateMessage("User", "Password", "Domain", "COMPUTER",
                                     test_challenge_message, 3,
                                     test_server_cert_hash)
        actual.add_mic(None, test_challenge_message)
        actual = actual.get_data()
        assert actual == expected
Example #14
0
    def test_authenticate_message_ntlm_v2(self, monkeypatch):
        monkeypatch.setattr('os.urandom', lambda s: b"\xaa" * 8)
        monkeypatch.setattr('ntlm_auth.messages.get_version',
                            lambda s: b"\x05\x01\x28\x0A\x00\x00\x00\x0F")
        monkeypatch.setattr('ntlm_auth.messages.get_random_export_session_key',
                            lambda: b"\x55" * 16)
        monkeypatch.setattr('ntlm_auth.compute_response.get_windows_timestamp',
                            lambda: b"\x00" * 8)

        test_challenge_message = ChallengeMessage(
            b"\x4e\x54\x4c\x4d\x53\x53\x50\x00"
            b"\x02\x00\x00\x00\x03\x00\x0c\x00"
            b"\x38\x00\x00\x00\x33\x82\x8a\xe2"
            b"\x01\x23\x45\x67\x89\xab\xcd\xef"
            b"\x00\x00\x00\x00\x00\x00\x00\x00"
            b"\x24\x00\x24\x00\x44\x00\x00\x00"
            b"\x06\x00\x70\x17\x00\x00\x00\x0f"
            b"\x53\x00\x65\x00\x72\x00\x76\x00"
            b"\x65\x00\x72\x00\x02\x00\x0c\x00"
            b"\x44\x00\x6f\x00\x6d\x00\x61\x00"
            b"\x69\x00\x6e\x00\x01\x00\x0c\x00"
            b"\x53\x00\x65\x00\x72\x00\x76\x00"
            b"\x65\x00\x72\x00\x00\x00\x00\x00")

        # Need to override the flags in the challenge message to match the
        # expectation, these flags are inconsequential and are done manualy for
        # sanity
        test_challenge_message.negotiate_flags -= \
            NegotiateFlags.NTLMSSP_TARGET_TYPE_SERVER
        test_challenge_message.negotiate_flags |= \
            NegotiateFlags.NTLMSSP_REQUEST_TARGET

        expected = b"\x4e\x54\x4c\x4d\x53\x53\x50\x00" \
                   b"\x03\x00\x00\x00\x18\x00\x18\x00" \
                   b"\x6c\x00\x00\x00\x54\x00\x54\x00" \
                   b"\x84\x00\x00\x00\x0c\x00\x0c\x00" \
                   b"\x48\x00\x00\x00\x08\x00\x08\x00" \
                   b"\x54\x00\x00\x00\x10\x00\x10\x00" \
                   b"\x5c\x00\x00\x00\x10\x00\x10\x00" \
                   b"\xd8\x00\x00\x00\x35\x82\x88\xe2" \
                   b"\x05\x01\x28\x0a\x00\x00\x00\x0f" \
                   b"\x44\x00\x6f\x00\x6d\x00\x61\x00" \
                   b"\x69\x00\x6e\x00\x55\x00\x73\x00" \
                   b"\x65\x00\x72\x00\x43\x00\x4f\x00" \
                   b"\x4d\x00\x50\x00\x55\x00\x54\x00" \
                   b"\x45\x00\x52\x00\x86\xc3\x50\x97" \
                   b"\xac\x9c\xec\x10\x25\x54\x76\x4a" \
                   b"\x57\xcc\xcc\x19\xaa\xaa\xaa\xaa" \
                   b"\xaa\xaa\xaa\xaa\x68\xcd\x0a\xb8" \
                   b"\x51\xe5\x1c\x96\xaa\xbc\x92\x7b" \
                   b"\xeb\xef\x6a\x1c\x01\x01\x00\x00" \
                   b"\x00\x00\x00\x00\x00\x00\x00\x00" \
                   b"\x00\x00\x00\x00\xaa\xaa\xaa\xaa" \
                   b"\xaa\xaa\xaa\xaa\x00\x00\x00\x00" \
                   b"\x02\x00\x0c\x00\x44\x00\x6f\x00" \
                   b"\x6d\x00\x61\x00\x69\x00\x6e\x00" \
                   b"\x01\x00\x0c\x00\x53\x00\x65\x00" \
                   b"\x72\x00\x76\x00\x65\x00\x72\x00" \
                   b"\x00\x00\x00\x00\x00\x00\x00\x00" \
                   b"\xc5\xda\xd2\x54\x4f\xc9\x79\x90" \
                   b"\x94\xce\x1c\xe9\x0b\xc9\xd0\x3e"

        actual = AuthenticateMessage("User", "Password", "Domain", "COMPUTER",
                                     test_challenge_message, 3, None)
        actual.add_mic(None, test_challenge_message)
        actual = actual.get_data()
        assert actual == expected
Example #15
0
class Ntlm(object):
    """
    Initialises the NTLM context to use when sending and receiving messages to and from the server. You should be
    using this object as it supports NTLMv2 authenticate and it easier to use than before. It also brings in the
    ability to use signing and sealing with session_security and generate a MIC structure.

    :param ntlm_compatibility: The Lan Manager Compatibility Level to use withe the auth message - Default 3
                                This is set by an Administrator in the registry key
                                'HKLM\SYSTEM\CurrentControlSet\Control\Lsa\LmCompatibilityLevel'
                                The values correspond to the following;
                                    0 : LM and NTLMv1
                                    1 : LM, NTLMv1 and NTLMv1 with Extended Session Security
                                    2 : NTLMv1 and NTLMv1 with Extended Session Security
                                    3-5 : NTLMv2 Only
                                Note: Values 3 to 5 are no different as the client supports the same types

    Attributes:
        negotiate_flags: A NEGOTIATE structure that contains a set of bit flags. These flags are the options the client supports and are sent in the negotiate_message
        ntlm_compatibility: The Lan Manager Compatibility Level, same as the input if supplied
        negotiate_message: A NegotiateMessage object that is sent to the server
        challenge_message: A ChallengeMessage object that has been created from the server response
        authenticate_message: An AuthenticateMessage object that is sent to the server based on the ChallengeMessage
        session_security: A SessionSecurity structure that can be used to sign and seal messages sent after the authentication challenge
    """
    def __init__(self, ntlm_compatibility=3):
        self.ntlm_compatibility = ntlm_compatibility

        # Setting up our flags so the challenge message returns the target info block if supported
        self.negotiate_flags = NegotiateFlags.NTLMSSP_NEGOTIATE_TARGET_INFO | \
                               NegotiateFlags.NTLMSSP_NEGOTIATE_128 | \
                               NegotiateFlags.NTLMSSP_NEGOTIATE_56 | \
                               NegotiateFlags.NTLMSSP_NEGOTIATE_UNICODE | \
                               NegotiateFlags.NTLMSSP_NEGOTIATE_VERSION | \
                               NegotiateFlags.NTLMSSP_NEGOTIATE_KEY_EXCH | \
                               NegotiateFlags.NTLMSSP_NEGOTIATE_ALWAYS_SIGN | \
                               NegotiateFlags.NTLMSSP_NEGOTIATE_SIGN | \
                               NegotiateFlags.NTLMSSP_NEGOTIATE_SEAL

        # Setting the message types based on the ntlm_compatibility level
        self._set_ntlm_compatibility_flags(self.ntlm_compatibility)

        self.negotiate_message = None
        self.challenge_message = None
        self.authenticate_message = None
        self.session_security = None


    def create_negotiate_message(self, domain_name=None, workstation=None):
        """
        Create an NTLM NEGOTIATE_MESSAGE

        :param domain_name: The domain name of the user account we are authenticating with, default is None
        :param worksation: The workstation we are using to authenticate with, default is None
        :return: A base64 encoded string of the NEGOTIATE_MESSAGE
        """
        self.negotiate_message = NegotiateMessage(self.negotiate_flags, domain_name, workstation)

        return base64.b64encode(self.negotiate_message.get_data())

    def parse_challenge_message(self, msg2):
        """
        Parse the NTLM CHALLENGE_MESSAGE from the server and add it to the Ntlm context fields

        :param msg2: A base64 encoded string of the CHALLENGE_MESSAGE
        """
        msg2 = base64.b64decode(msg2)
        self.challenge_message = ChallengeMessage(msg2)

    def create_authenticate_message(self, user_name, password, domain_name=None, workstation=None, server_certificate_hash=None):
        """
        Create an NTLM AUTHENTICATE_MESSAGE based on the Ntlm context and the previous messages sent and received

        :param user_name: The user name of the user we are trying to authenticate with
        :param password: The password of the user we are trying to authenticate with
        :param domain_name: The domain name of the user account we are authenticated with, default is None
        :param workstation: The workstation we are using to authenticate with, default is None
        :param server_certificate_hash: The SHA256 hash string of the server certificate (DER encoded) NTLM is authenticating to. Used for Channel
                                        Binding Tokens. If nothing is supplied then the CBT hash will not be sent. See messages.py AuthenticateMessage
                                        for more details
        :return: A base64 encoded string of the AUTHENTICATE_MESSAGE
        """
        self.authenticate_message = AuthenticateMessage(user_name, password, domain_name, workstation,
                                                        self.challenge_message, self.ntlm_compatibility,
                                                        server_certificate_hash)
        self.authenticate_message.add_mic(self.negotiate_message, self.challenge_message)

        # Setups up the session_security context used to sign and seal messages if wanted
        if self.negotiate_flags & NegotiateFlags.NTLMSSP_NEGOTIATE_SEAL or self.negotiate_flags & NegotiateFlags.NTLMSSP_NEGOTIATE_SIGN:
            self.session_security = SessionSecurity(struct.unpack("<I", self.authenticate_message.negotiate_flags)[0],
                                                    self.authenticate_message.exported_session_key)

        return base64.b64encode(self.authenticate_message.get_data())

    def _set_ntlm_compatibility_flags(self, ntlm_compatibility):
        if (ntlm_compatibility >= 0) and (ntlm_compatibility <= 5):
            if ntlm_compatibility == 0:
                self.negotiate_flags |= NegotiateFlags.NTLMSSP_NEGOTIATE_NTLM | \
                                        NegotiateFlags.NTLMSSP_NEGOTIATE_LM_KEY
            elif ntlm_compatibility == 1:
                self.negotiate_flags |= NegotiateFlags.NTLMSSP_NEGOTIATE_NTLM | \
                                        NegotiateFlags.NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY
            else:
                self.negotiate_flags |= NegotiateFlags.NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY
        else:
            raise Exception("Unknown ntlm_compatibility level - expecting value between 0 and 5")
Example #16
0
    def test_authenticate_message_with_mic(self, monkeypatch):
        monkeypatch.setattr('os.urandom', lambda s: b"\xaa" * 8)
        monkeypatch.setattr('ntlm_auth.messages.get_random_export_session_key',
                            lambda: b"\x55" * 16)

        test_challenge_message = ChallengeMessage(
            b"\x4e\x54\x4c\x4d\x53\x53\x50\x00"
            b"\x02\x00\x00\x00\x03\x00\x0c\x00"
            b"\x38\x00\x00\x00\x33\x82\x8a\xe2"
            b"\x01\x23\x45\x67\x89\xab\xcd\xef"
            b"\x00\x00\x00\x00\x00\x00\x00\x00"
            b"\x24\x00\x24\x00\x44\x00\x00\x00"
            b"\x06\x00\x70\x17\x00\x00\x00\x0f"
            b"\x53\x00\x65\x00\x72\x00\x76\x00"
            b"\x65\x00\x72\x00\x02\x00\x0c\x00"
            b"\x44\x00\x6f\x00\x6d\x00\x61\x00"
            b"\x69\x00\x6e\x00\x01\x00\x0c\x00"
            b"\x53\x00\x65\x00\x72\x00\x76\x00"
            b"\x65\x00\x72\x00\x00\x00\x00\x00")
        test_challenge_message.target_info[AvId.MSV_AV_TIMESTAMP] = b"\x00" * 8
        test_server_cert_hash = \
            'E3CA49271E5089CC48CE82109F1324F41DBEDDC29A777410C738F7868C4FF405'
        test_negotiate_message = NegotiateMessage(3800728115, "Domain",
                                                  "COMPUTER")

        # Not a Microsoft example, using pre-computed value
        expected = b"\x4e\x54\x4c\x4d\x53\x53\x50\x00" \
                   b"\x03\x00\x00\x00\x18\x00\x18\x00" \
                   b"\x7c\x00\x00\x00\x7c\x00\x7c\x00" \
                   b"\x94\x00\x00\x00\x0c\x00\x0c\x00" \
                   b"\x58\x00\x00\x00\x08\x00\x08\x00" \
                   b"\x64\x00\x00\x00\x10\x00\x10\x00" \
                   b"\x6c\x00\x00\x00\x10\x00\x10\x00" \
                   b"\x10\x01\x00\x00\x31\x82\x8a\xe2" \
                   b"\x06\x01\xb1\x1d\x00\x00\x00\x0f" \
                   b"\x8b\x69\xf5\x92\xb2\xd7\x8f\xd7" \
                   b"\x3a\x3a\x49\xdb\xfe\x19\x61\xbc" \
                   b"\x44\x00\x6f\x00\x6d\x00\x61\x00" \
                   b"\x69\x00\x6e\x00\x55\x00\x73\x00" \
                   b"\x65\x00\x72\x00\x43\x00\x4f\x00" \
                   b"\x4d\x00\x50\x00\x55\x00\x54\x00" \
                   b"\x45\x00\x52\x00\x00\x00\x00\x00" \
                   b"\x00\x00\x00\x00\x00\x00\x00\x00" \
                   b"\x00\x00\x00\x00\x00\x00\x00\x00" \
                   b"\x00\x00\x00\x00\xa1\x3d\x03\x8a" \
                   b"\xd0\xca\x02\x64\x33\x89\x7c\x33" \
                   b"\x5e\x0f\x56\xdf\x01\x01\x00\x00" \
                   b"\x00\x00\x00\x00\x00\x00\x00\x00" \
                   b"\x00\x00\x00\x00\xaa\xaa\xaa\xaa" \
                   b"\xaa\xaa\xaa\xaa\x00\x00\x00\x00" \
                   b"\x02\x00\x0c\x00\x44\x00\x6f\x00" \
                   b"\x6d\x00\x61\x00\x69\x00\x6e\x00" \
                   b"\x01\x00\x0c\x00\x53\x00\x65\x00" \
                   b"\x72\x00\x76\x00\x65\x00\x72\x00" \
                   b"\x07\x00\x08\x00\x00\x00\x00\x00" \
                   b"\x00\x00\x00\x00\x06\x00\x04\x00" \
                   b"\x02\x00\x00\x00\x0a\x00\x10\x00" \
                   b"\x6e\xa1\x9d\xf0\x66\xda\x46\x22" \
                   b"\x05\x1f\x9c\x4f\x92\xc6\xdf\x74" \
                   b"\x00\x00\x00\x00\x00\x00\x00\x00" \
                   b"\x1d\x08\x89\xd1\xa5\xee\xed\x21" \
                   b"\x91\x9e\x1a\xb8\x27\xc3\x0b\x17"

        actual = AuthenticateMessage("User", "Password", "Domain", "COMPUTER",
                                     test_challenge_message, 3,
                                     test_server_cert_hash)
        actual.add_mic(test_negotiate_message, test_challenge_message)
        actual = actual.get_data()
        assert actual == expected