| 1 |
# Copyright: (c) 2020, Jordan Borean (@jborean93) <[email protected]> |
| 2 |
# MIT License (see LICENSE or https://opensource.org/licenses/MIT) |
| 3 |
|
| 4 |
import base64 |
| 5 |
import os |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
import spnego._ntlm_raw.crypto as crypto |
| 10 |
from spnego._ntlm_raw.messages import FileTime, NegotiateFlags, TargetInfo |
| 11 |
from spnego._text import to_text |
| 12 |
|
| 13 |
from .._ntlm_raw import ( |
| 14 |
TEST_CLIENT_CHALLENGE, |
| 15 |
TEST_NTLMV1_CLIENT_CHALLENGE_FLAGS, |
| 16 |
TEST_NTLMV1_FLAGS, |
| 17 |
TEST_PASSWD, |
| 18 |
TEST_RANDOM_SESSION_KEY, |
| 19 |
TEST_SERVER_CHALLENGE, |
| 20 |
TEST_TIME, |
| 21 |
TEST_USER, |
| 22 |
TEST_USER_DOM, |
| 23 |
) |
| 24 |
|
| 25 |
|
| 26 |
def test_crc32(): |
| 27 |
actual = crypto.crc32(b"123456789") |
| 28 |
|
| 29 |
# http://reveng.sourceforge.net/crc-catalogue/17plus.htm#crc.cat.crc-32 |
| 30 |
assert actual == b"\x26\x39\xF4\xCB" |
| 31 |
|
| 32 |
|
| 33 |
def test_lmowfv1(): |
| 34 |
# https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/a724e8df-2b0a-4a36-aef4-2d2b56fd3db7 |
| 35 |
actual = crypto.lmowfv1(TEST_PASSWD) |
| 36 |
|
| 37 |
assert actual == b"\xE5\x2C\xAC\x67\x41\x9A\x9A\x22\x4A\x3B\x10\x8F\x3F\xA6\xCB\x6D" |
| 38 |
|
| 39 |
|
| 40 |
def test_lmowfv1_hash(): |
| 41 |
lm_hash = os.urandom(16) |
| 42 |
nt_hash = os.urandom(16) |
| 43 |
ntlm_hash = to_text(b"%s:%s" % (base64.b16encode(lm_hash), base64.b16encode(nt_hash))) |
| 44 |
|
| 45 |
actual = crypto.lmowfv1(ntlm_hash) |
| 46 |
|
| 47 |
assert actual == lm_hash |
| 48 |
|
| 49 |
|
| 50 |
def test_ntowfv1(): |
| 51 |
# https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/0fb94d19-16d2-481d-9121-112defbaac0b |
| 52 |
actual = crypto.ntowfv1(TEST_PASSWD) |
| 53 |
|
| 54 |
assert actual == b"\xA4\xF4\x9C\x40\x65\x10\xBD\xCA\xB6\x82\x4E\xE7\xC3\x0F\xD8\x52" |
| 55 |
|
| 56 |
|
| 57 |
def test_ntowfv1_hash(): |
| 58 |
lm_hash = os.urandom(16) |
| 59 |
nt_hash = os.urandom(16) |
| 60 |
ntlm_hash = to_text(b"%s:%s" % (base64.b16encode(lm_hash), base64.b16encode(nt_hash))) |
| 61 |
|
| 62 |
actual = crypto.ntowfv1(ntlm_hash) |
| 63 |
|
| 64 |
assert actual == nt_hash |
| 65 |
|
| 66 |
|
| 67 |
def test_compute_response_v1_no_session_security(): |
| 68 |
actual_nt, actual_lm, actual_kek = crypto.compute_response_v1( |
| 69 |
TEST_NTLMV1_FLAGS, |
| 70 |
crypto.ntowfv1(TEST_PASSWD), |
| 71 |
crypto.lmowfv1(TEST_PASSWD), |
| 72 |
TEST_SERVER_CHALLENGE, |
| 73 |
TEST_CLIENT_CHALLENGE, |
| 74 |
) |
| 75 |
|
| 76 |
# https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/a9dc740e-e12f-4fdd-8f2b-61a471731a14 |
| 77 |
assert ( |
| 78 |
actual_nt == b"\x67\xC4\x30\x11\xF3\x02\x98\xA2\xAD\x35\xEC\xE6\x4F\x16\x33\x1C" |
| 79 |
b"\x44\xBD\xBE\xD9\x27\x84\x1F\x94" |
| 80 |
) |
| 81 |
|
| 82 |
# https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/859a18d1-d7b2-4e98-a261-5b38cdf4b11d |
| 83 |
assert actual_lm == actual_nt |
| 84 |
|
| 85 |
# https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/0b344a44-7cd8-4ab5-b07c-ff2b3d8c15f4 |
| 86 |
assert actual_kek == b"\xD8\x72\x62\xB0\xCD\xE4\xB1\xCB\x74\x99\xBE\xCC\xCD\xF1\x07\x84" |
| 87 |
|
| 88 |
# https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/2d7f9599-f849-4550-9579-91aeea8078b0 |
| 89 |
actual_enc_key = crypto.rc4k(actual_kek, TEST_RANDOM_SESSION_KEY) |
| 90 |
assert actual_enc_key == b"\x51\x88\x22\xB1\xB3\xF3\x50\xC8\x95\x86\x82\xEC\xBB\x3E\x3C\xB7" |
| 91 |
|
| 92 |
|
| 93 |
def test_compute_response_v1_no_session_security_non_nt_key(): |
| 94 |
flags = TEST_NTLMV1_FLAGS | NegotiateFlags.non_nt_session_key |
| 95 |
actual_nt, actual_lm, actual_kek = crypto.compute_response_v1( |
| 96 |
flags, |
| 97 |
crypto.ntowfv1(TEST_PASSWD), |
| 98 |
crypto.lmowfv1(TEST_PASSWD), |
| 99 |
TEST_SERVER_CHALLENGE, |
| 100 |
TEST_CLIENT_CHALLENGE, |
| 101 |
no_lm_response=False, |
| 102 |
) |
| 103 |
|
| 104 |
# https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/a9dc740e-e12f-4fdd-8f2b-61a471731a14 |
| 105 |
assert ( |
| 106 |
actual_nt == b"\x67\xC4\x30\x11\xF3\x02\x98\xA2\xAD\x35\xEC\xE6\x4F\x16\x33\x1C" |
| 107 |
b"\x44\xBD\xBE\xD9\x27\x84\x1F\x94" |
| 108 |
) |
| 109 |
|
| 110 |
# https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/859a18d1-d7b2-4e98-a261-5b38cdf4b11d |
| 111 |
assert ( |
| 112 |
actual_lm == b"\x98\xDE\xF7\xB8\x7F\x88\xAA\x5D\xAF\xE2\xDF\x77\x96\x88\xA1\x72" |
| 113 |
b"\xDE\xF1\x1C\x7D\x5C\xCD\xEF\x13" |
| 114 |
) |
| 115 |
|
| 116 |
assert actual_kek == b"\xE5\x2C\xAC\x67\x41\x9A\x9A\x22\x00\x00\x00\x00\x00\x00\x00\x00" |
| 117 |
|
| 118 |
# https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/2d7f9599-f849-4550-9579-91aeea8078b0 |
| 119 |
actual_enc_key = crypto.rc4k(actual_kek, TEST_RANDOM_SESSION_KEY) |
| 120 |
assert actual_enc_key == b"\x74\x52\xCA\x55\xC2\x25\xA1\xCA\x04\xB4\x8F\xAE\x32\xCF\x56\xFC" |
| 121 |
|
| 122 |
|
| 123 |
def test_compute_response_v1_no_session_security_lm_key(): |
| 124 |
flags = TEST_NTLMV1_FLAGS | NegotiateFlags.lm_key |
| 125 |
actual_nt, actual_lm, actual_kek = crypto.compute_response_v1( |
| 126 |
flags, |
| 127 |
crypto.ntowfv1(TEST_PASSWD), |
| 128 |
crypto.lmowfv1(TEST_PASSWD), |
| 129 |
TEST_SERVER_CHALLENGE, |
| 130 |
TEST_CLIENT_CHALLENGE, |
| 131 |
no_lm_response=False, |
| 132 |
) |
| 133 |
|
| 134 |
# https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/a9dc740e-e12f-4fdd-8f2b-61a471731a14 |
| 135 |
assert ( |
| 136 |
actual_nt == b"\x67\xC4\x30\x11\xF3\x02\x98\xA2\xAD\x35\xEC\xE6\x4F\x16\x33\x1C" |
| 137 |
b"\x44\xBD\xBE\xD9\x27\x84\x1F\x94" |
| 138 |
) |
| 139 |
|
| 140 |
# https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/859a18d1-d7b2-4e98-a261-5b38cdf4b11d |
| 141 |
assert ( |
| 142 |
actual_lm == b"\x98\xDE\xF7\xB8\x7F\x88\xAA\x5D\xAF\xE2\xDF\x77\x96\x88\xA1\x72" |
| 143 |
b"\xDE\xF1\x1C\x7D\x5C\xCD\xEF\x13" |
| 144 |
) |
| 145 |
assert actual_kek == b"\xB0\x9E\x37\x9F\x7F\xBE\xCB\x1E\xAF\x0A\xFD\xCB\x03\x83\xC8\xA0" |
| 146 |
|
| 147 |
# https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/2d7f9599-f849-4550-9579-91aeea8078b0 |
| 148 |
actual_enc_key = crypto.rc4k(actual_kek, TEST_RANDOM_SESSION_KEY) |
| 149 |
assert actual_enc_key == b"\x4C\xD7\xBB\x57\xD6\x97\xEF\x9B\x54\x9F\x02\xB8\xF9\xB3\x78\x64" |
| 150 |
|
| 151 |
|
| 152 |
def test_compute_response_v1_session_security(): |
| 153 |
actual_nt, actual_lm, actual_kek = crypto.compute_response_v1( |
| 154 |
TEST_NTLMV1_CLIENT_CHALLENGE_FLAGS, |
| 155 |
crypto.ntowfv1(TEST_PASSWD), |
| 156 |
crypto.lmowfv1(TEST_PASSWD), |
| 157 |
TEST_SERVER_CHALLENGE, |
| 158 |
TEST_CLIENT_CHALLENGE, |
| 159 |
) |
| 160 |
|
| 161 |
# https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/becc1601-9c97-4553-aef7-7053d3db5883 |
| 162 |
assert ( |
| 163 |
actual_nt == b"\x75\x37\xF8\x03\xAE\x36\x71\x28\xCA\x45\x82\x04\xBD\xE7\xCA\xF8" |
| 164 |
b"\x1E\x97\xED\x26\x83\x26\x72\x32" |
| 165 |
) |
| 166 |
|
| 167 |
# https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/63725b2b-21fc-4977-9b56-2b3e65f7be76 |
| 168 |
assert actual_lm == TEST_CLIENT_CHALLENGE + b"\x00" * 16 |
| 169 |
|
| 170 |
# https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/321bb3da-c27a-4a5d-91f9-4f23705e4029 |
| 171 |
assert actual_kek == b"\xEB\x93\x42\x9A\x8B\xD9\x52\xF8\xB8\x9C\x55\xB8\x7F\x47\x5E\xDC" |
| 172 |
|
| 173 |
|
| 174 |
def test_ntowfv2(): |
| 175 |
actual = crypto.ntowfv2(TEST_USER, crypto.ntowfv1(TEST_PASSWD), TEST_USER_DOM) |
| 176 |
|
| 177 |
# https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/7795bd0e-fd5e-43ec-bd9c-994704d8ee26 |
| 178 |
assert actual == b"\x0C\x86\x8A\x40\x3B\xFD\x7A\x93\xA3\x00\x1E\xF2\x2E\xF0\x2E\x3F" |
| 179 |
|
| 180 |
|
| 181 |
def test_compute_response_v2(): |
| 182 |
# https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/946f54bd-76b5-4b18-ace8-6e8c992d5847 |
| 183 |
time = FileTime.unpack(TEST_TIME) |
| 184 |
av_pairs = TargetInfo.unpack( |
| 185 |
b"\x02\x00\x0C\x00\x44\x00\x6F\x00\x6D\x00\x61\x00\x69\x00\x6E\x00" |
| 186 |
b"\x01\x00\x0C\x00\x53\x00\x65\x00\x72\x00\x76\x00\x65\x00\x72\x00" |
| 187 |
b"\x00\x00\x00\x00" |
| 188 |
) |
| 189 |
|
| 190 |
actual_nt, actual_lm, actual_kek = crypto.compute_response_v2( |
| 191 |
crypto.ntowfv2(TEST_USER, crypto.ntowfv1(TEST_PASSWD), TEST_USER_DOM), |
| 192 |
TEST_SERVER_CHALLENGE, |
| 193 |
TEST_CLIENT_CHALLENGE, |
| 194 |
time, |
| 195 |
av_pairs, |
| 196 |
) |
| 197 |
|
| 198 |
# https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/946f54bd-76b5-4b18-ace8-6e8c992d5847 |
| 199 |
temp = ( |
| 200 |
b"\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" |
| 201 |
b"\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\x00\x00\x00\x00\x02\x00\x0C\x00" |
| 202 |
b"\x44\x00\x6F\x00\x6D\x00\x61\x00\x69\x00\x6E\x00\x01\x00\x0C\x00" |
| 203 |
b"\x53\x00\x65\x00\x72\x00\x76\x00\x65\x00\x72\x00\x00\x00\x00\x00" |
| 204 |
b"\x00\x00\x00\x00" |
| 205 |
) |
| 206 |
|
| 207 |
# https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/fa2bc0f0-9efa-40d7-a165-adfccd7f6da7 |
| 208 |
assert actual_nt == b"\x68\xCD\x0A\xB8\x51\xE5\x1C\x96\xAA\xBC\x92\x7B\xEB\xEF\x6A\x1C" + temp |
| 209 |
|
| 210 |
# https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/7e2b35f9-fe90-49fb-8c9d-30639a899160 |
| 211 |
assert ( |
| 212 |
actual_lm == b"\x86\xC3\x50\x97\xAC\x9C\xEC\x10\x25\x54\x76\x4A\x57\xCC\xCC\x19" |
| 213 |
b"\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" |
| 214 |
) |
| 215 |
|
| 216 |
# https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/54973495-20d2-49e8-9925-c399a403ed4a |
| 217 |
assert actual_kek == b"\x8D\xE4\x0C\xCA\xDB\xC1\x4A\x82\xF1\x5C\xB0\xAD\x0D\xE9\x5C\xA3" |
| 218 |
|
| 219 |
# https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/4a84eb20-870a-421e-984f-29a842cd6504 |
| 220 |
actual_enc_key = crypto.rc4k(actual_kek, TEST_RANDOM_SESSION_KEY) |
| 221 |
assert actual_enc_key == b"\xC5\xDA\xD2\x54\x4F\xC9\x79\x90\x94\xCE\x1C\xE9\x0B\xC9\xD0\x3E" |
| 222 |
|
| 223 |
|
| 224 |
@pytest.mark.parametrize( |
| 225 |
"flags, usage, expected", |
| 226 |
[ |
| 227 |
# https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/9cdb7bb2-17e6-409c-99cc-04590db064d4 |
| 228 |
(NegotiateFlags.key_128, "initiate", b"\x59\xF6\x00\x97\x3C\xC4\x96\x0A\x25\x48\x0A\x7C\x19\x6E\x4C\x58"), |
| 229 |
(NegotiateFlags.key_128, "accept", b"\x93\x55\xF3\xA9\x57\xC1\x58\x3D\x25\xC4\xC2\xF1\x1E\x40\x39\x0E"), |
| 230 |
(NegotiateFlags.key_56, "initiate", b"\xA5\xF7\x25\x3C\x10\x65\xE8\xD3\xD6\x86\x42\x04\x0E\x71\xCF\xE0"), |
| 231 |
(NegotiateFlags.key_56, "accept", b"\x58\x3E\x2F\x98\x95\x9B\x38\x5C\xD1\x58\xF3\x73\x4B\x5F\x5D\x3F"), |
| 232 |
(0, "initiate", b"\x42\xF9\x64\xA4\x71\x09\x1A\x02\xFF\x4A\x77\x45\x53\x66\xE4\xE5"), |
| 233 |
(0, "accept", b"\xC5\xD3\x85\x3B\x40\x6B\x7C\x12\x41\xC5\x95\xF0\xCE\x07\x50\xE2"), |
| 234 |
], |
| 235 |
ids=[ |
| 236 |
"initiate-128", |
| 237 |
"accept-128", |
| 238 |
"initiate-56", |
| 239 |
"accept-56", |
| 240 |
"initiate-40", |
| 241 |
"accept-40", |
| 242 |
], |
| 243 |
) |
| 244 |
def test_seal_key_ess(flags, usage, expected): |
| 245 |
actual = crypto.sealkey(NegotiateFlags.extended_session_security | flags, TEST_RANDOM_SESSION_KEY, usage) |
| 246 |
|
| 247 |
assert actual == expected |
| 248 |
|
| 249 |
|
| 250 |
@pytest.mark.parametrize( |
| 251 |
"flags, expected", |
| 252 |
[ |
| 253 |
(NegotiateFlags.lm_key | NegotiateFlags.key_56, b"\x55\x55\x55\x55\x55\x55\x55\xA0"), |
| 254 |
(NegotiateFlags.datagram | NegotiateFlags.key_56, b"\x55\x55\x55\x55\x55\x55\x55\xA0"), |
| 255 |
(NegotiateFlags.lm_key, b"\x55\x55\x55\x55\x55\xE5\x38\xB0"), |
| 256 |
(NegotiateFlags.datagram, b"\x55\x55\x55\x55\x55\xE5\x38\xB0"), |
| 257 |
], |
| 258 |
) |
| 259 |
def test_seal_key_lm_key_or_datagram(flags, expected): |
| 260 |
actual = crypto.sealkey(flags, TEST_RANDOM_SESSION_KEY, "initiate") |
| 261 |
|
| 262 |
print(base64.b16encode(actual).decode("ascii")) |
| 263 |
|
| 264 |
assert actual == expected |
| 265 |
|
| 266 |
|
| 267 |
def test_seal_key_no_flags(): |
| 268 |
actual = crypto.sealkey(0, TEST_RANDOM_SESSION_KEY, "initiate") |
| 269 |
|
| 270 |
assert actual == TEST_RANDOM_SESSION_KEY |
| 271 |
|
| 272 |
|
| 273 |
def test_signkey_no_ess(): |
| 274 |
actual = crypto.signkey(0, b"\x01", "initiate") |
| 275 |
|
| 276 |
# https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/524cdccb-563e-4793-92b0-7bc321fce096 |
| 277 |
# If extended session security is not negotiated then no signing keys are available. |
| 278 |
assert not actual |
| 279 |
|
| 280 |
|
| 281 |
def test_signkey_client(): |
| 282 |
actual = crypto.signkey(NegotiateFlags.extended_session_security, TEST_RANDOM_SESSION_KEY, "initiate") |
| 283 |
|
| 284 |
# https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/9cdb7bb2-17e6-409c-99cc-04590db064d4 |
| 285 |
assert actual == b"\x47\x88\xDC\x86\x1B\x47\x82\xF3\x5D\x43\xFD\x98\xFE\x1A\x2D\x39" |
| 286 |
|
| 287 |
|
| 288 |
def test_signkey_server(): |
| 289 |
actual = crypto.signkey(NegotiateFlags.extended_session_security, TEST_RANDOM_SESSION_KEY, "accept") |
| 290 |
|
| 291 |
assert actual == b"\xD0\x4D\x6F\x10\x74\x10\x41\xD1\xD2\x46\xD6\x41\x88\xD7\xA8\xAD" |