| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Copyright: (c) 2020, Jordan Borean (@jborean93) <[email protected]> |
| 3 |
# MIT License (see LICENSE or https://opensource.org/licenses/MIT) |
| 4 |
|
| 5 |
import base64 |
| 6 |
import os |
| 7 |
import re |
| 8 |
import socket |
| 9 |
|
| 10 |
import pytest |
| 11 |
|
| 12 |
import spnego |
| 13 |
import spnego._ntlm as ntlm |
| 14 |
import spnego.channel_bindings |
| 15 |
import spnego.iov |
| 16 |
from spnego._credential import CredentialCache |
| 17 |
from spnego._ntlm_raw.messages import ( |
| 18 |
Authenticate, |
| 19 |
AvId, |
| 20 |
Challenge, |
| 21 |
FileTime, |
| 22 |
Negotiate, |
| 23 |
NegotiateFlags, |
| 24 |
TargetInfo, |
| 25 |
Version, |
| 26 |
) |
| 27 |
from spnego._text import to_bytes, to_text |
| 28 |
from spnego.exceptions import ( |
| 29 |
BadBindingsError, |
| 30 |
BadMICError, |
| 31 |
InvalidTokenError, |
| 32 |
NoContextError, |
| 33 |
OperationNotAvailableError, |
| 34 |
SpnegoError, |
| 35 |
UnsupportedQop, |
| 36 |
) |
| 37 |
|
| 38 |
from .conftest import get_data |
| 39 |
|
| 40 |
|
| 41 |
def test_get_credential_file_no_env_var(): |
| 42 |
actual = ntlm._get_credential_file() |
| 43 |
assert actual is None |
| 44 |
|
| 45 |
|
| 46 |
def test_get_credential_file_env_var_missing_file(tmpdir, monkeypatch): |
| 47 |
tmp_creds = os.path.join(to_text(tmpdir), "pÿspᴞӛgӫ TÈ$" ".creds") |
| 48 |
|
| 49 |
monkeypatch.setenv("NTLM_USER_FILE", tmp_creds) |
| 50 |
|
| 51 |
actual = ntlm._get_credential_file() |
| 52 |
assert actual is None |
| 53 |
|
| 54 |
|
| 55 |
def test_get_credential_file(tmpdir, monkeypatch): |
| 56 |
tmp_creds = os.path.join(to_text(tmpdir), "pÿspᴞӛgӫ TÈ$" ".creds") |
| 57 |
with open(tmp_creds, mode="wb") as fd: |
| 58 |
fd.write(b"data") |
| 59 |
|
| 60 |
monkeypatch.setenv("NTLM_USER_FILE", tmp_creds) |
| 61 |
|
| 62 |
actual = ntlm._get_credential_file() |
| 63 |
assert actual == to_text(tmp_creds) |
| 64 |
|
| 65 |
|
| 66 |
@pytest.mark.parametrize( |
| 67 |
"line, username, domain, lm_hash, nt_hash, input", |
| 68 |
[ |
| 69 |
( |
| 70 |
"domain:Username:password", |
| 71 |
"username", |
| 72 |
"domain", |
| 73 |
"E52CAC67419A9A224A3B108F3FA6CB6D", |
| 74 |
"8846F7EAEE8FB117AD06BDD830B7586C", |
| 75 |
CredentialCache(username="domain\\username"), |
| 76 |
), |
| 77 |
( |
| 78 |
"domain:Username:password\ndomain:other:pass2", |
| 79 |
"Username", |
| 80 |
"domain", |
| 81 |
"E52CAC67419A9A224A3B108F3FA6CB6D", |
| 82 |
"8846F7EAEE8FB117AD06BDD830B7586C", |
| 83 |
None, |
| 84 |
), |
| 85 |
( |
| 86 |
"fake\ndomain:username:password", |
| 87 |
"username", |
| 88 |
"domain", # newline or garbage data in file won't fail |
| 89 |
"E52CAC67419A9A224A3B108F3FA6CB6D", |
| 90 |
"8846F7EAEE8FB117AD06BDD830B7586C", |
| 91 |
CredentialCache("domain\\username"), |
| 92 |
), |
| 93 |
( |
| 94 |
":[email protected]:password", |
| 95 |
"[email protected]", |
| 96 |
None, |
| 97 |
"E52CAC67419A9A224A3B108F3FA6CB6D", |
| 98 |
"8846F7EAEE8FB117AD06BDD830B7586C", |
| 99 |
CredentialCache("[email protected]"), |
| 100 |
), |
| 101 |
( |
| 102 |
"testuser:1000:278623D830DABE161104594F8C2EF12B:C3C6F4FD8A02A6C1268F1A8074B6E7E0:[U]:LCT-1589398321", |
| 103 |
"testuser", |
| 104 |
None, |
| 105 |
"278623D830DABE161104594F8C2EF12B", |
| 106 |
"C3C6F4FD8A02A6C1268F1A8074B6E7E0", |
| 107 |
CredentialCache("testuser"), |
| 108 |
), |
| 109 |
( |
| 110 |
"TESTDOM\\testuser:1000:4588C64B89437893AAD3B435B51404EE:65202355FA01AEF26B89B19E00F52679:[U]:LCT-1589398321", |
| 111 |
"testuser", |
| 112 |
"testdom", |
| 113 |
"4588C64B89437893AAD3B435B51404EE", |
| 114 |
"65202355FA01AEF26B89B19E00F52679", |
| 115 |
CredentialCache("testdom\\testuser"), |
| 116 |
), |
| 117 |
( |
| 118 |
"TESTDOM\\testuser:1000:4588C64B89437893AAD3B435B51404EE:65202355FA01AEF26B89B19E00F52679:[U]:LCT-1589398321", |
| 119 |
"testuser", |
| 120 |
"testdom", |
| 121 |
"4588C64B89437893AAD3B435B51404EE", |
| 122 |
"65202355FA01AEF26B89B19E00F52679", |
| 123 |
CredentialCache("testdom\\testuser"), |
| 124 |
), |
| 125 |
( |
| 126 |
"[email protected]:1000:00000000000000000000000000000000:8ADB9B997580D69E69CAA2BBB68F4697:[U]:LCT-1589398321", |
| 127 |
"[email protected]", |
| 128 |
None, |
| 129 |
"00000000000000000000000000000000", |
| 130 |
"8ADB9B997580D69E69CAA2BBB68F4697", |
| 131 |
CredentialCache("[email protected]"), |
| 132 |
), |
| 133 |
], |
| 134 |
) |
| 135 |
def test_get_credential_from_file(line, username, domain, lm_hash, nt_hash, input, tmpdir, monkeypatch): |
| 136 |
tmp_creds = os.path.join(to_text(tmpdir), "pÿspᴞӛgӫ TÈ$" ".creds") |
| 137 |
monkeypatch.setenv("NTLM_USER_FILE", tmp_creds) |
| 138 |
with open(tmp_creds, mode="wb") as fd: |
| 139 |
fd.write(to_bytes(line)) |
| 140 |
|
| 141 |
actual = ntlm._NTLMCredential(input) |
| 142 |
assert actual.username == username |
| 143 |
assert actual.domain == domain |
| 144 |
assert actual.lm_hash == base64.b16decode(lm_hash) |
| 145 |
assert actual.nt_hash == base64.b16decode(nt_hash) |
| 146 |
|
| 147 |
|
| 148 |
def test_get_credential_from_file_no_matches(tmpdir, monkeypatch): |
| 149 |
tmp_creds = os.path.join(to_text(tmpdir), "pÿspᴞӛgӫ TÈ$" ".creds") |
| 150 |
monkeypatch.setenv("NTLM_USER_FILE", tmp_creds) |
| 151 |
with open(tmp_creds, mode="wb") as fd: |
| 152 |
fd.write(b"domain:username:password") |
| 153 |
|
| 154 |
with pytest.raises( |
| 155 |
SpnegoError, match="Failed to find any matching credential in NTLM_USER_FILE " "credential store." |
| 156 |
): |
| 157 |
ntlm._NTLMCredential(CredentialCache("fake\\username")) |
| 158 |
|
| 159 |
|
| 160 |
@pytest.mark.parametrize("level", [-1, 6]) |
| 161 |
def test_invalid_lm_compat_level(level, monkeypatch): |
| 162 |
monkeypatch.setenv("LM_COMPAT_LEVEL", str(level)) |
| 163 |
|
| 164 |
expected = "Invalid LM_COMPAT_LEVEL %s, must be between 0 and 5" % level |
| 165 |
with pytest.raises(SpnegoError, match=re.escape(expected)): |
| 166 |
ntlm.NTLMProxy("user", "pass") |
| 167 |
|
| 168 |
|
| 169 |
@pytest.mark.parametrize("usage", ["initiate", "accept"]) |
| 170 |
def test_context_no_store(usage): |
| 171 |
with pytest.raises( |
| 172 |
OperationNotAvailableError, match="Retrieving NTLM store without NTLM_USER_FILE set to a " "filepath" |
| 173 |
): |
| 174 |
ntlm.NTLMProxy(CredentialCache(), usage=usage) |
| 175 |
|
| 176 |
|
| 177 |
def test_iov_available(): |
| 178 |
assert ntlm.NTLMProxy.iov_available() is True |
| 179 |
|
| 180 |
|
| 181 |
def test_ntlm_invalid_usage(): |
| 182 |
with pytest.raises(ValueError, match="Invalid usage 'test', must be initiate or accept"): |
| 183 |
ntlm.NTLMProxy("user", "pass", usage="test") |
| 184 |
|
| 185 |
|
| 186 |
def test_ntlm_invalid_protocol(): |
| 187 |
with pytest.raises(ValueError, match="Invalid protocol 'fake', must be ntlm, kerberos, negotiate, or credssp"): |
| 188 |
ntlm.NTLMProxy("user", "pass", protocol="fake") |
| 189 |
|
| 190 |
|
| 191 |
def test_ntlm_query_message_sizes_fail(): |
| 192 |
n = ntlm.NTLMProxy("user", "pass") |
| 193 |
with pytest.raises(NoContextError, match="Cannot get message sizes until context has been established"): |
| 194 |
n.query_message_sizes() |
| 195 |
|
| 196 |
|
| 197 |
def test_ntlm_wrap_qop_invalid(): |
| 198 |
n = ntlm.NTLMProxy("user", "pass") |
| 199 |
with pytest.raises(UnsupportedQop, match="Unsupported QoP value 1 specified for NTLM"): |
| 200 |
n.wrap(b"data", qop=1) |
| 201 |
|
| 202 |
|
| 203 |
def test_ntlm_wrap_no_sign_or_seal(): |
| 204 |
n = ntlm.NTLMProxy("user", "pass") |
| 205 |
with pytest.raises(OperationNotAvailableError, match="NTLM wrap without integrity or confidentiality"): |
| 206 |
n.wrap(b"data") |
| 207 |
|
| 208 |
|
| 209 |
def test_ntlm_wrap_no_context(): |
| 210 |
n = ntlm.NTLMProxy("user", "pass") |
| 211 |
n._context_attr = spnego.ContextReq.confidentiality | spnego.ContextReq.integrity |
| 212 |
with pytest.raises(NoContextError, match="Cannot wrap until context has been established"): |
| 213 |
n.wrap(b"data") |
| 214 |
|
| 215 |
|
| 216 |
def test_ntlm_wrap_winrm_no_context(): |
| 217 |
n = ntlm.NTLMProxy("user", "pass") |
| 218 |
n._context_attr = spnego.ContextReq.confidentiality | spnego.ContextReq.integrity |
| 219 |
with pytest.raises(NoContextError, match="Cannot wrap until context has been established"): |
| 220 |
n.wrap_winrm(b"data") |
| 221 |
|
| 222 |
|
| 223 |
def test_ntlm_unwrap_no_context(): |
| 224 |
n = ntlm.NTLMProxy("user", "pass") |
| 225 |
with pytest.raises(NoContextError, match="Cannot unwrap until context has been established"): |
| 226 |
n.unwrap(b"data") |
| 227 |
|
| 228 |
|
| 229 |
def test_ntlm_unwrap_winrm_no_context(): |
| 230 |
n = ntlm.NTLMProxy("user", "pass") |
| 231 |
with pytest.raises(NoContextError, match="Cannot unwrap until context has been established"): |
| 232 |
n.unwrap_winrm(b"header", b"data") |
| 233 |
|
| 234 |
|
| 235 |
def test_ntlm_wrap_iov_with_qop_fail(): |
| 236 |
n = ntlm.NTLMProxy("user", "pass") |
| 237 |
with pytest.raises(UnsupportedQop, match="Unsupported QoP value 1 specified for NTLM"): |
| 238 |
n.wrap_iov([], qop=1) |
| 239 |
|
| 240 |
|
| 241 |
def test_ntlm_wrap_iov_no_sign_or_seal(): |
| 242 |
n = ntlm.NTLMProxy("user", "pass") |
| 243 |
with pytest.raises(OperationNotAvailableError, match="NTLM wrap without integrity or confidentiality"): |
| 244 |
n.wrap_iov([]) |
| 245 |
|
| 246 |
|
| 247 |
def test_ntlm_wrap_iov_no_context(): |
| 248 |
n = ntlm.NTLMProxy("user", "pass") |
| 249 |
n._context_attr = spnego.ContextReq.confidentiality | spnego.ContextReq.integrity |
| 250 |
with pytest.raises(NoContextError, match="Cannot wrap until context has been established"): |
| 251 |
n.wrap_iov([]) |
| 252 |
|
| 253 |
|
| 254 |
def test_ntlm_wrap_iov_no_header(): |
| 255 |
n = ntlm.NTLMProxy("user", "pass") |
| 256 |
n._context_attr = spnego.ContextReq.confidentiality | spnego.ContextReq.integrity |
| 257 |
n._sign_key_out = n._handle_out = 1 # type: ignore |
| 258 |
|
| 259 |
with pytest.raises(InvalidTokenError, match="wrap_iov no IOV header buffer present"): |
| 260 |
n.wrap_iov([b""]) |
| 261 |
|
| 262 |
|
| 263 |
def test_ntlm_wrap_iov_no_data(): |
| 264 |
n = ntlm.NTLMProxy("user", "pass") |
| 265 |
n._context_attr = spnego.ContextReq.confidentiality | spnego.ContextReq.integrity |
| 266 |
n._sign_key_out = n._handle_out = 1 # type: ignore |
| 267 |
|
| 268 |
with pytest.raises(InvalidTokenError, match="wrap_iov no IOV data buffer present"): |
| 269 |
n.wrap_iov([spnego.iov.BufferType.header]) |
| 270 |
|
| 271 |
|
| 272 |
def test_ntlm_wrap_iov_multiple_header(): |
| 273 |
n = ntlm.NTLMProxy("user", "pass") |
| 274 |
n._context_attr = spnego.ContextReq.confidentiality | spnego.ContextReq.integrity |
| 275 |
n._sign_key_out = n._handle_out = 1 # type: ignore |
| 276 |
|
| 277 |
with pytest.raises(InvalidTokenError, match="wrap_iov must only be used with 1 header IOV buffer"): |
| 278 |
n.wrap_iov([spnego.iov.BufferType.header, b"", spnego.iov.BufferType.header]) |
| 279 |
|
| 280 |
|
| 281 |
def test_ntlm_wrap_iov_multiple_data(): |
| 282 |
n = ntlm.NTLMProxy("user", "pass") |
| 283 |
n._context_attr = spnego.ContextReq.confidentiality | spnego.ContextReq.integrity |
| 284 |
n._sign_key_out = n._handle_out = 1 # type: ignore |
| 285 |
|
| 286 |
with pytest.raises(InvalidTokenError, match="wrap_iov must only be used with 1 data IOV buffer"): |
| 287 |
n.wrap_iov([spnego.iov.BufferType.header, b"", b""]) |
| 288 |
|
| 289 |
|
| 290 |
def test_ntlm_wrap_iov_invalid_type(): |
| 291 |
n = ntlm.NTLMProxy("user", "pass") |
| 292 |
n._context_attr = spnego.ContextReq.confidentiality | spnego.ContextReq.integrity |
| 293 |
n._sign_key_out = n._handle_out = 1 # type: ignore |
| 294 |
|
| 295 |
with pytest.raises(InvalidTokenError, match="wrap_iov unsupported IOV buffer type padding"): |
| 296 |
n.wrap_iov([spnego.iov.BufferType.header, b"", spnego.iov.BufferType.padding]) |
| 297 |
|
| 298 |
|
| 299 |
def test_ntlm_wrap_iov_data_not_bytes(): |
| 300 |
n = ntlm.NTLMProxy("user", "pass") |
| 301 |
n._context_attr = spnego.ContextReq.confidentiality | spnego.ContextReq.integrity |
| 302 |
n._sign_key_out = n._handle_out = 1 # type: ignore |
| 303 |
|
| 304 |
with pytest.raises(InvalidTokenError, match="wrap_iov IOV data buffer at \\[1\\] must be bytes"): |
| 305 |
n.wrap_iov([spnego.iov.BufferType.header, (spnego.iov.BufferType.data, 1)]) |
| 306 |
|
| 307 |
|
| 308 |
def test_ntlm_wrap_iov_signonly_not_bytes(): |
| 309 |
n = ntlm.NTLMProxy("user", "pass") |
| 310 |
n._context_attr = spnego.ContextReq.confidentiality | spnego.ContextReq.integrity |
| 311 |
n._sign_key_out = n._handle_out = 1 # type: ignore |
| 312 |
|
| 313 |
with pytest.raises(InvalidTokenError, match="wrap_iov IOV sign_only buffer at \\[1\\] must be bytes"): |
| 314 |
n.wrap_iov([spnego.iov.BufferType.header, (spnego.iov.BufferType.sign_only, 1)]) |
| 315 |
|
| 316 |
|
| 317 |
def test_ntlm_unwrap_iov_no_sign_or_seal(): |
| 318 |
n = ntlm.NTLMProxy("user", "pass") |
| 319 |
with pytest.raises(OperationNotAvailableError, match="NTLM unwrap without integrity or confidentiality"): |
| 320 |
n.unwrap_iov([]) |
| 321 |
|
| 322 |
|
| 323 |
def test_ntlm_unwrap_iov_no_context(): |
| 324 |
n = ntlm.NTLMProxy("user", "pass") |
| 325 |
n._context_attr = spnego.ContextReq.confidentiality | spnego.ContextReq.integrity |
| 326 |
with pytest.raises(NoContextError, match="Cannot unwrap until context has been established"): |
| 327 |
n.unwrap_iov([]) |
| 328 |
|
| 329 |
|
| 330 |
def test_ntlm_unwrap_iov_no_header(): |
| 331 |
n = ntlm.NTLMProxy("user", "pass") |
| 332 |
n._context_attr = spnego.ContextReq.confidentiality | spnego.ContextReq.integrity |
| 333 |
n._sign_key_in = n._handle_in = 1 # type: ignore |
| 334 |
|
| 335 |
with pytest.raises(InvalidTokenError, match="unwrap_iov no IOV header buffer present"): |
| 336 |
n.unwrap_iov([b""]) |
| 337 |
|
| 338 |
|
| 339 |
def test_ntlm_unwrap_iov_no_data(): |
| 340 |
n = ntlm.NTLMProxy("user", "pass") |
| 341 |
n._context_attr = spnego.ContextReq.confidentiality | spnego.ContextReq.integrity |
| 342 |
n._sign_key_in = n._handle_in = 1 # type: ignore |
| 343 |
|
| 344 |
with pytest.raises(InvalidTokenError, match="unwrap_iov no IOV data buffer present"): |
| 345 |
n.unwrap_iov([(spnego.iov.BufferType.header, b"")]) |
| 346 |
|
| 347 |
|
| 348 |
def test_ntlm_unwrap_iov_multiple_header(): |
| 349 |
n = ntlm.NTLMProxy("user", "pass") |
| 350 |
n._context_attr = spnego.ContextReq.confidentiality | spnego.ContextReq.integrity |
| 351 |
n._sign_key_in = n._handle_in = 1 # type: ignore |
| 352 |
|
| 353 |
with pytest.raises(InvalidTokenError, match="unwrap_iov must only be used with 1 header IOV buffer"): |
| 354 |
n.unwrap_iov([(spnego.iov.BufferType.header, b""), b"", (spnego.iov.BufferType.header, b"")]) |
| 355 |
|
| 356 |
|
| 357 |
def test_ntlm_unwrap_iov_multiple_data(): |
| 358 |
n = ntlm.NTLMProxy("user", "pass") |
| 359 |
n._context_attr = spnego.ContextReq.confidentiality | spnego.ContextReq.integrity |
| 360 |
n._sign_key_in = n._handle_in = 1 # type: ignore |
| 361 |
|
| 362 |
with pytest.raises(InvalidTokenError, match="unwrap_iov must only be used with 1 data IOV buffer"): |
| 363 |
n.unwrap_iov([(spnego.iov.BufferType.header, b""), b"", b""]) |
| 364 |
|
| 365 |
|
| 366 |
def test_ntlm_unwrap_iov_invalid_type(): |
| 367 |
n = ntlm.NTLMProxy("user", "pass") |
| 368 |
n._context_attr = spnego.ContextReq.confidentiality | spnego.ContextReq.integrity |
| 369 |
n._sign_key_in = n._handle_in = 1 # type: ignore |
| 370 |
|
| 371 |
with pytest.raises(InvalidTokenError, match="unwrap_iov unsupported IOV buffer type padding"): |
| 372 |
n.unwrap_iov([(spnego.iov.BufferType.header, b""), b"", (spnego.iov.BufferType.padding, b"")]) |
| 373 |
|
| 374 |
|
| 375 |
def test_ntlm_unwrap_iov_data_not_bytes(): |
| 376 |
n = ntlm.NTLMProxy("user", "pass") |
| 377 |
n._context_attr = spnego.ContextReq.confidentiality | spnego.ContextReq.integrity |
| 378 |
n._sign_key_in = n._handle_in = 1 # type: ignore |
| 379 |
|
| 380 |
with pytest.raises(InvalidTokenError, match="unwrap_iov IOV data buffer at \\[1\\] must be bytes"): |
| 381 |
n.unwrap_iov([(spnego.iov.BufferType.header, b""), (spnego.iov.BufferType.data, 1)]) |
| 382 |
|
| 383 |
|
| 384 |
def test_ntlm_unwrap_iov_data_readonly_not_bytes(): |
| 385 |
n = ntlm.NTLMProxy("user", "pass") |
| 386 |
n._context_attr = spnego.ContextReq.confidentiality | spnego.ContextReq.integrity |
| 387 |
n._sign_key_in = n._handle_in = 1 # type: ignore |
| 388 |
|
| 389 |
with pytest.raises(InvalidTokenError, match="unwrap_iov IOV data_readonly buffer at \\[1\\] must be bytes"): |
| 390 |
n.unwrap_iov([(spnego.iov.BufferType.header, b""), (spnego.iov.BufferType.data_readonly, 1)]) |
| 391 |
|
| 392 |
|
| 393 |
def test_ntlm_sign_qop_invalid(): |
| 394 |
n = ntlm.NTLMProxy("user", "pass") |
| 395 |
with pytest.raises(UnsupportedQop, match="Unsupported QoP value 1 specified for NTLM"): |
| 396 |
n.sign(b"data", qop=1) |
| 397 |
|
| 398 |
|
| 399 |
def test_ntlm_sign_no_context(): |
| 400 |
n = ntlm.NTLMProxy("user", "pass") |
| 401 |
with pytest.raises(NoContextError, match="Cannot sign until context has been established"): |
| 402 |
n.sign(b"data") |
| 403 |
|
| 404 |
|
| 405 |
def test_ntlm_verify_no_context(): |
| 406 |
n = ntlm.NTLMProxy("user", "pass") |
| 407 |
with pytest.raises(NoContextError, match="Cannot verify until context has been established"): |
| 408 |
n.verify(b"data", b"mic") |
| 409 |
|
| 410 |
|
| 411 |
def test_ntlm_no_encoding_flags(): |
| 412 |
negotiate = memoryview(bytearray(get_data("ntlm_negotiate"))) |
| 413 |
negotiate[12:16] = b"\x00\x00\x00\x00" |
| 414 |
|
| 415 |
n = ntlm.NTLMProxy("user", "pass") |
| 416 |
with pytest.raises( |
| 417 |
SpnegoError, |
| 418 |
match="Neither NEGOTIATE_OEM or NEGOTIATE_UNICODE flags were set, cannot derive " "encoding for text fields", |
| 419 |
): |
| 420 |
n._step_accept_negotiate(negotiate.tobytes()) |
| 421 |
|
| 422 |
|
| 423 |
@pytest.mark.parametrize( |
| 424 |
"client_opt, present", |
| 425 |
[ |
| 426 |
(spnego.NegotiateOptions.use_ntlm, False), |
| 427 |
(spnego.NegotiateOptions.use_ntlm, True), |
| 428 |
(spnego.NegotiateOptions.use_gssapi, True), |
| 429 |
(spnego.NegotiateOptions.use_sspi, True), |
| 430 |
], |
| 431 |
) |
| 432 |
def test_ntlm_bad_bindings(client_opt, present, ntlm_cred): |
| 433 |
if client_opt & spnego.NegotiateOptions.use_gssapi: |
| 434 |
if "ntlm" not in spnego._gss.GSSAPIProxy.available_protocols(): |
| 435 |
pytest.skip("Test requires NTLM to be available through GSSAPI") |
| 436 |
|
| 437 |
elif client_opt & spnego.NegotiateOptions.use_sspi: |
| 438 |
if "ntlm" not in spnego._sspi.SSPIProxy.available_protocols(): |
| 439 |
pytest.skip("Test requires NTLM to be available through SSPI") |
| 440 |
|
| 441 |
initiator_cbt = None |
| 442 |
if present: |
| 443 |
initiator_cbt = spnego.channel_bindings.GssChannelBindings(application_data=b"tls-host-data:bad") |
| 444 |
|
| 445 |
c = spnego.client( |
| 446 |
ntlm_cred[0], |
| 447 |
ntlm_cred[1], |
| 448 |
hostname=socket.gethostname(), |
| 449 |
options=client_opt, |
| 450 |
protocol="ntlm", |
| 451 |
channel_bindings=initiator_cbt, |
| 452 |
) |
| 453 |
|
| 454 |
acceptor_cbt = spnego.channel_bindings.GssChannelBindings(application_data=b"tls-host-data:test") |
| 455 |
s = spnego.server(options=spnego.NegotiateOptions.use_ntlm, protocol="ntlm", channel_bindings=acceptor_cbt) |
| 456 |
|
| 457 |
auth = c.step(s.step(c.step())) |
| 458 |
|
| 459 |
if present: |
| 460 |
expected = "Acceptor bindings do not match initiator bindings" |
| 461 |
|
| 462 |
else: |
| 463 |
expected = "Acceptor bindings specified but not present in initiator response" |
| 464 |
|
| 465 |
with pytest.raises(BadBindingsError, match=expected): |
| 466 |
s.step(auth) |
| 467 |
|
| 468 |
|
| 469 |
def test_ntlm_bad_mic(ntlm_cred): |
| 470 |
c = spnego.client( |
| 471 |
ntlm_cred[0], |
| 472 |
ntlm_cred[1], |
| 473 |
hostname=socket.gethostname(), |
| 474 |
options=spnego.NegotiateOptions.use_ntlm, |
| 475 |
protocol="ntlm", |
| 476 |
) |
| 477 |
s = spnego.server(options=spnego.NegotiateOptions.use_ntlm, protocol="ntlm") |
| 478 |
|
| 479 |
auth = memoryview(bytearray(c.step(s.step(c.step())) or b"")) |
| 480 |
auth[64:80] = b"\x01" * 16 |
| 481 |
|
| 482 |
with pytest.raises(InvalidTokenError, match="Invalid MIC in NTLM authentication message"): |
| 483 |
s.step(auth.tobytes()) |
| 484 |
|
| 485 |
|
| 486 |
@pytest.mark.parametrize( |
| 487 |
"env_var, expected", |
| 488 |
[ |
| 489 |
(None, to_text(socket.gethostname()).upper()), |
| 490 |
("", None), |
| 491 |
("custom", "custom"), |
| 492 |
], |
| 493 |
) |
| 494 |
def test_ntlm_workstation_override(env_var, expected, ntlm_cred, monkeypatch): |
| 495 |
if env_var is not None: |
| 496 |
monkeypatch.setenv("NETBIOS_COMPUTER_NAME", env_var) |
| 497 |
|
| 498 |
c = spnego.client( |
| 499 |
ntlm_cred[0], |
| 500 |
ntlm_cred[1], |
| 501 |
hostname=socket.gethostname(), |
| 502 |
options=spnego.NegotiateOptions.use_ntlm, |
| 503 |
protocol="ntlm", |
| 504 |
) |
| 505 |
|
| 506 |
b_negotiate = c.step() |
| 507 |
assert b_negotiate is not None |
| 508 |
negotiate = Negotiate.unpack(b_negotiate) |
| 509 |
|
| 510 |
flags = ( |
| 511 |
negotiate.flags |
| 512 |
| NegotiateFlags.request_target |
| 513 |
| NegotiateFlags.ntlm |
| 514 |
| NegotiateFlags.always_sign |
| 515 |
| NegotiateFlags.target_info |
| 516 |
| NegotiateFlags.target_type_server |
| 517 |
) |
| 518 |
|
| 519 |
server_challenge = os.urandom(8) |
| 520 |
target_name = to_text(socket.gethostname()).upper() |
| 521 |
|
| 522 |
target_info = TargetInfo() |
| 523 |
target_info[AvId.nb_computer_name] = target_name |
| 524 |
target_info[AvId.nb_domain_name] = "WORKSTATION" |
| 525 |
target_info[AvId.dns_computer_name] = to_text(socket.getfqdn()) |
| 526 |
target_info[AvId.timestamp] = FileTime.now() |
| 527 |
|
| 528 |
version = Version(10, 0, 0, 1) |
| 529 |
challenge = Challenge(flags, server_challenge, target_name=target_name, target_info=target_info, version=version) |
| 530 |
|
| 531 |
b_auth = c.step(challenge.pack()) |
| 532 |
assert b_auth is not None |
| 533 |
auth = Authenticate.unpack(b_auth) |
| 534 |
|
| 535 |
assert auth.workstation == expected |
| 536 |
|
| 537 |
|
| 538 |
@pytest.mark.parametrize( |
| 539 |
"include_time, expected", |
| 540 |
[ |
| 541 |
# If the challenge didn't contain the time then the client should generate it's own otherwise it uses the challenge |
| 542 |
# time. |
| 543 |
(True, 0), |
| 544 |
(False, 1), |
| 545 |
], |
| 546 |
) |
| 547 |
def test_ntlm_custom_time(include_time, expected, ntlm_cred, mocker, monkeypatch): |
| 548 |
c = spnego.client( |
| 549 |
ntlm_cred[0], |
| 550 |
ntlm_cred[1], |
| 551 |
hostname=socket.gethostname(), |
| 552 |
options=spnego.NegotiateOptions.use_ntlm, |
| 553 |
protocol="ntlm", |
| 554 |
) |
| 555 |
|
| 556 |
b_negotiate = c.step() |
| 557 |
assert b_negotiate is not None |
| 558 |
negotiate = Negotiate.unpack(b_negotiate) |
| 559 |
|
| 560 |
flags = ( |
| 561 |
negotiate.flags |
| 562 |
| NegotiateFlags.request_target |
| 563 |
| NegotiateFlags.ntlm |
| 564 |
| NegotiateFlags.always_sign |
| 565 |
| NegotiateFlags.target_info |
| 566 |
| NegotiateFlags.target_type_server |
| 567 |
) |
| 568 |
|
| 569 |
server_challenge = os.urandom(8) |
| 570 |
target_name = to_text(socket.gethostname()).upper() |
| 571 |
|
| 572 |
target_info = TargetInfo() |
| 573 |
target_info[AvId.nb_computer_name] = target_name |
| 574 |
target_info[AvId.nb_domain_name] = "WORKSTATION" |
| 575 |
target_info[AvId.dns_computer_name] = to_text(socket.getfqdn()) |
| 576 |
|
| 577 |
if include_time: |
| 578 |
target_info[AvId.timestamp] = FileTime.now() |
| 579 |
|
| 580 |
challenge = Challenge(flags, server_challenge, target_name=target_name, target_info=target_info) |
| 581 |
|
| 582 |
mock_now = mocker.MagicMock() |
| 583 |
mock_now.side_effect = FileTime.now |
| 584 |
monkeypatch.setattr(FileTime, "now", mock_now) |
| 585 |
|
| 586 |
c.step(challenge.pack()) |
| 587 |
assert c.complete |
| 588 |
assert mock_now.call_count == expected |
| 589 |
|
| 590 |
|
| 591 |
def test_ntlm_no_key_exch(ntlm_cred): |
| 592 |
c = spnego.client( |
| 593 |
ntlm_cred[0], |
| 594 |
ntlm_cred[1], |
| 595 |
hostname=socket.gethostname(), |
| 596 |
options=spnego.NegotiateOptions.use_ntlm, |
| 597 |
protocol="ntlm", |
| 598 |
) |
| 599 |
s = spnego.server(options=spnego.NegotiateOptions.use_ntlm, protocol="ntlm") |
| 600 |
|
| 601 |
c._context_req &= ~0x40000000 # NTLMSSP_NEGOTIATE_KEY_EXCH |
| 602 |
|
| 603 |
auth = c.step(s.step(c.step())) |
| 604 |
s.step(auth) |
| 605 |
|
| 606 |
# Make sure EncryptedRandomSessionKeyFields was set to 0 (no KEY_EXCH). |
| 607 |
assert auth is not None |
| 608 |
assert auth[52:54] == b"\x00\x00" |
| 609 |
|
| 610 |
plaintext = os.urandom(32) |
| 611 |
|
| 612 |
c_wrap_result = c.wrap(plaintext) |
| 613 |
assert c_wrap_result.encrypted |
| 614 |
assert c_wrap_result.data != plaintext |
| 615 |
|
| 616 |
s_unwrap_result = s.unwrap(c_wrap_result.data) |
| 617 |
assert s_unwrap_result.data == plaintext |
| 618 |
assert s_unwrap_result.encrypted |
| 619 |
|
| 620 |
plaintext = os.urandom(17) |
| 621 |
|
| 622 |
s_wrap_result = s.wrap(plaintext) |
| 623 |
assert s_wrap_result.encrypted |
| 624 |
assert s_wrap_result.data != plaintext |
| 625 |
|
| 626 |
c_unwrap_result = c.unwrap(s_wrap_result.data) |
| 627 |
assert c_unwrap_result.data == plaintext |
| 628 |
assert c_unwrap_result.encrypted |
| 629 |
|
| 630 |
plaintext = os.urandom(3) |
| 631 |
c_sig = c.sign(plaintext) |
| 632 |
s.verify(plaintext, c_sig) |
| 633 |
|
| 634 |
plaintext = os.urandom(9) |
| 635 |
s_sig = s.sign(plaintext) |
| 636 |
c.verify(plaintext, s_sig) |
| 637 |
|
| 638 |
|
| 639 |
def test_ntlm_lm_request(ntlm_cred, monkeypatch): |
| 640 |
monkeypatch.setenv("LM_COMPAT_LEVEL", "0") |
| 641 |
c = spnego.client( |
| 642 |
ntlm_cred[0], |
| 643 |
ntlm_cred[1], |
| 644 |
hostname=socket.gethostname(), |
| 645 |
options=spnego.NegotiateOptions.use_ntlm, |
| 646 |
protocol="ntlm", |
| 647 |
) |
| 648 |
s = spnego.server(options=spnego.NegotiateOptions.use_ntlm, protocol="ntlm") |
| 649 |
|
| 650 |
auth = memoryview(bytearray(c.step(s.step(c.step())) or b"")) |
| 651 |
auth[20:28] = b"\x00" * 8 |
| 652 |
|
| 653 |
s.step(auth.tobytes()) |
| 654 |
|
| 655 |
assert c.complete |
| 656 |
assert s.complete |
| 657 |
|
| 658 |
|
| 659 |
def test_ntlm_no_lm_allowed(ntlm_cred, monkeypatch): |
| 660 |
monkeypatch.setenv("LM_COMPAT_LEVEL", "0") |
| 661 |
c = spnego.client( |
| 662 |
ntlm_cred[0], |
| 663 |
ntlm_cred[1], |
| 664 |
hostname=socket.gethostname(), |
| 665 |
options=spnego.NegotiateOptions.use_ntlm, |
| 666 |
protocol="ntlm", |
| 667 |
) |
| 668 |
|
| 669 |
monkeypatch.setenv("LM_COMPAT_LEVEL", "4") |
| 670 |
s = spnego.server(options=spnego.NegotiateOptions.use_ntlm, protocol="ntlm") |
| 671 |
|
| 672 |
auth = memoryview(bytearray(c.step(s.step(c.step())) or b"")) |
| 673 |
auth[20:28] = b"\x00" * 8 |
| 674 |
|
| 675 |
with pytest.raises(InvalidTokenError, match="Acceptor settings are set to reject LM responses"): |
| 676 |
s.step(auth) |
| 677 |
|
| 678 |
|
| 679 |
def test_ntlm_nt_v1_request(ntlm_cred, monkeypatch): |
| 680 |
monkeypatch.setenv("LM_COMPAT_LEVEL", "0") |
| 681 |
c = spnego.client( |
| 682 |
ntlm_cred[0], |
| 683 |
ntlm_cred[1], |
| 684 |
hostname=socket.gethostname(), |
| 685 |
options=spnego.NegotiateOptions.use_ntlm, |
| 686 |
protocol="ntlm", |
| 687 |
) |
| 688 |
|
| 689 |
monkeypatch.setenv("LM_COMPAT_LEVEL", "4") |
| 690 |
s = spnego.server(options=spnego.NegotiateOptions.use_ntlm, protocol="ntlm") |
| 691 |
|
| 692 |
auth = c.step(s.step(c.step())) |
| 693 |
|
| 694 |
s.step(auth) |
| 695 |
|
| 696 |
assert c.complete |
| 697 |
assert s.complete |
| 698 |
|
| 699 |
|
| 700 |
def test_ntlm_no_nt_v1_allowed(ntlm_cred, monkeypatch): |
| 701 |
monkeypatch.setenv("LM_COMPAT_LEVEL", "0") |
| 702 |
c = spnego.client( |
| 703 |
ntlm_cred[0], |
| 704 |
ntlm_cred[1], |
| 705 |
hostname=socket.gethostname(), |
| 706 |
options=spnego.NegotiateOptions.use_ntlm, |
| 707 |
protocol="ntlm", |
| 708 |
) |
| 709 |
|
| 710 |
monkeypatch.setenv("LM_COMPAT_LEVEL", "5") |
| 711 |
s = spnego.server(options=spnego.NegotiateOptions.use_ntlm, protocol="ntlm") |
| 712 |
|
| 713 |
auth = c.step(s.step(c.step())) |
| 714 |
|
| 715 |
with pytest.raises(InvalidTokenError, match="Acceptor settings are set to reject NTv1 responses"): |
| 716 |
s.step(auth) |
| 717 |
|
| 718 |
|
| 719 |
@pytest.mark.parametrize( |
| 720 |
"client_opt", |
| 721 |
[ |
| 722 |
spnego.NegotiateOptions.use_ntlm, |
| 723 |
spnego.NegotiateOptions.use_gssapi, |
| 724 |
spnego.NegotiateOptions.use_sspi, |
| 725 |
], |
| 726 |
) |
| 727 |
def test_ntlm_invalid_password(client_opt, ntlm_cred): |
| 728 |
if client_opt & spnego.NegotiateOptions.use_gssapi: |
| 729 |
if "ntlm" not in spnego._gss.GSSAPIProxy.available_protocols(): |
| 730 |
pytest.skip("Test requires NTLM to be available through GSSAPI") |
| 731 |
|
| 732 |
elif client_opt & spnego.NegotiateOptions.use_sspi: |
| 733 |
if "ntlm" not in spnego._sspi.SSPIProxy.available_protocols(): |
| 734 |
pytest.skip("Test requires NTLM to be available through SSPI") |
| 735 |
|
| 736 |
c = spnego.client(ntlm_cred[0], "Invalid", hostname=socket.gethostname(), options=client_opt, protocol="ntlm") |
| 737 |
s = spnego.server(options=spnego.NegotiateOptions.use_ntlm, protocol="ntlm") |
| 738 |
|
| 739 |
auth = c.step(s.step(c.step())) |
| 740 |
|
| 741 |
with pytest.raises(InvalidTokenError, match="Invalid NTLM response from initiator"): |
| 742 |
s.step(auth) |
| 743 |
|
| 744 |
|
| 745 |
@pytest.mark.parametrize( |
| 746 |
"client_opt", |
| 747 |
[ |
| 748 |
spnego.NegotiateOptions.use_ntlm, |
| 749 |
spnego.NegotiateOptions.use_gssapi, |
| 750 |
spnego.NegotiateOptions.use_sspi, |
| 751 |
], |
| 752 |
) |
| 753 |
def test_ntlm_verify_fail(client_opt, ntlm_cred): |
| 754 |
if client_opt & spnego.NegotiateOptions.use_gssapi: |
| 755 |
if "ntlm" not in spnego._gss.GSSAPIProxy.available_protocols(): |
| 756 |
pytest.skip("Test requires NTLM to be available through GSSAPI") |
| 757 |
|
| 758 |
elif client_opt & spnego.NegotiateOptions.use_sspi: |
| 759 |
if "ntlm" not in spnego._sspi.SSPIProxy.available_protocols(): |
| 760 |
pytest.skip("Test requires NTLM to be available through SSPI") |
| 761 |
|
| 762 |
c = spnego.client(ntlm_cred[0], ntlm_cred[1], hostname=socket.gethostname(), options=client_opt, protocol="ntlm") |
| 763 |
s = spnego.server(options=spnego.NegotiateOptions.use_ntlm, protocol="ntlm") |
| 764 |
|
| 765 |
s.step(c.step(s.step(c.step()))) |
| 766 |
|
| 767 |
c.sign(b"data") |
| 768 |
sig = c.sign(b"data 2") |
| 769 |
|
| 770 |
with pytest.raises(BadMICError, match="Invalid Message integrity Check"): |
| 771 |
s.verify(b"data", sig) |
| 772 |
|
| 773 |
|
| 774 |
def test_ntlm_anon_response(ntlm_cred): |
| 775 |
c = spnego.client(ntlm_cred[0], ntlm_cred[1], options=spnego.NegotiateOptions.use_ntlm, protocol="ntlm") |
| 776 |
s = spnego.server(options=spnego.NegotiateOptions.use_ntlm, protocol="ntlm") |
| 777 |
|
| 778 |
auth = Authenticate.unpack(c.step(s.step(c.step())) or b"") |
| 779 |
anon_auth = Authenticate(flags=auth.flags, lm_challenge_response=b"\x00", nt_challenge_response=b"").pack() |
| 780 |
|
| 781 |
with pytest.raises(OperationNotAvailableError, match="Anonymous user authentication not implemented"): |
| 782 |
s.step(anon_auth) |
| 783 |
|
| 784 |
|
| 785 |
def test_ntlm_iov_wrapping(ntlm_cred): |
| 786 |
c = spnego.client( |
| 787 |
ntlm_cred[0], |
| 788 |
ntlm_cred[1], |
| 789 |
hostname=socket.gethostname(), |
| 790 |
protocol="ntlm", |
| 791 |
options=spnego.NegotiateOptions.use_ntlm, |
| 792 |
) |
| 793 |
s = spnego.server(protocol="ntlm") |
| 794 |
|
| 795 |
s.step(c.step(s.step(c.step()))) |
| 796 |
|
| 797 |
res1 = c.wrap_iov( |
| 798 |
[ |
| 799 |
(spnego.iov.BufferType.sign_only, b"sign 1"), |
| 800 |
b"data", |
| 801 |
(spnego.iov.BufferType.data_readonly, b"sign 2"), |
| 802 |
spnego.iov.BufferType.header, |
| 803 |
] |
| 804 |
) |
| 805 |
assert isinstance(res1, spnego.IOVWrapResult) |
| 806 |
assert res1.encrypted |
| 807 |
assert len(res1.buffers) == 4 |
| 808 |
assert res1.buffers[0].data == b"sign 1" |
| 809 |
assert res1.buffers[1].data != b"data" |
| 810 |
assert res1.buffers[2].data == b"sign 2" |
| 811 |
assert len(res1.buffers[3].data or b"") == 16 |
| 812 |
|
| 813 |
res2 = s.unwrap_iov(res1.buffers) |
| 814 |
assert isinstance(res2, spnego.IOVUnwrapResult) |
| 815 |
assert res2.encrypted |
| 816 |
assert res2.qop == 0 |
| 817 |
assert len(res2.buffers) == 4 |
| 818 |
assert res2.buffers[0].data == b"sign 1" |
| 819 |
assert res2.buffers[1].data == b"data" |
| 820 |
assert res2.buffers[2].data == b"sign 2" |
| 821 |
assert res2.buffers[3].data == res1.buffers[3].data |
| 822 |
|
| 823 |
res3 = s.wrap_iov( |
| 824 |
[ |
| 825 |
(spnego.iov.BufferType.sign_only, b"sign 1"), |
| 826 |
b"data", |
| 827 |
(spnego.iov.BufferType.data_readonly, b"sign 2"), |
| 828 |
spnego.iov.BufferType.header, |
| 829 |
] |
| 830 |
) |
| 831 |
assert isinstance(res3, spnego.IOVWrapResult) |
| 832 |
assert res3.encrypted |
| 833 |
assert len(res3.buffers) == 4 |
| 834 |
assert res3.buffers[0].data == b"sign 1" |
| 835 |
assert res3.buffers[1].data != b"data" |
| 836 |
assert res3.buffers[2].data == b"sign 2" |
| 837 |
assert len(res3.buffers[3].data or b"") == 16 |
| 838 |
|
| 839 |
res4 = c.unwrap_iov(res3.buffers) |
| 840 |
assert isinstance(res4, spnego.IOVUnwrapResult) |
| 841 |
assert res4.encrypted |
| 842 |
assert res4.qop == 0 |
| 843 |
assert len(res4.buffers) == 4 |
| 844 |
assert res4.buffers[0].data == b"sign 1" |
| 845 |
assert res4.buffers[1].data == b"data" |
| 846 |
assert res4.buffers[2].data == b"sign 2" |
| 847 |
assert res4.buffers[3].data == res3.buffers[3].data |
| 848 |
|
| 849 |
|
| 850 |
def test_ntlm_iov_unwrapping_as_stream(ntlm_cred): |
| 851 |
c = spnego.client( |
| 852 |
ntlm_cred[0], |
| 853 |
ntlm_cred[1], |
| 854 |
hostname=socket.gethostname(), |
| 855 |
protocol="ntlm", |
| 856 |
options=spnego.NegotiateOptions.use_ntlm, |
| 857 |
) |
| 858 |
s = spnego.server(protocol="ntlm") |
| 859 |
|
| 860 |
s.step(c.step(s.step(c.step()))) |
| 861 |
|
| 862 |
res1 = c.wrap_iov( |
| 863 |
[ |
| 864 |
b"data", |
| 865 |
spnego.iov.BufferType.header, |
| 866 |
] |
| 867 |
) |
| 868 |
assert isinstance(res1, spnego.IOVWrapResult) |
| 869 |
assert res1.encrypted |
| 870 |
assert len(res1.buffers) == 2 |
| 871 |
assert res1.buffers[0].data != b"data" |
| 872 |
assert len(res1.buffers[1].data or b"") == 16 |
| 873 |
|
| 874 |
msg = (res1.buffers[1].data or b"") + (res1.buffers[0].data or b"") |
| 875 |
res2 = s.unwrap_iov( |
| 876 |
[ |
| 877 |
(spnego.iov.BufferType.stream, msg), |
| 878 |
spnego.iov.BufferType.data, |
| 879 |
] |
| 880 |
) |
| 881 |
assert isinstance(res2, spnego.IOVUnwrapResult) |
| 882 |
assert res2.encrypted |
| 883 |
assert res2.qop == 0 |
| 884 |
assert len(res2.buffers) == 2 |
| 885 |
assert res2.buffers[1].data == b"data" |
| 886 |
|
| 887 |
res3 = s.wrap_iov( |
| 888 |
[ |
| 889 |
spnego.iov.BufferType.header, |
| 890 |
b"data", |
| 891 |
] |
| 892 |
) |
| 893 |
assert isinstance(res3, spnego.IOVWrapResult) |
| 894 |
assert res3.encrypted |
| 895 |
assert len(res3.buffers) == 2 |
| 896 |
assert len(res3.buffers[0].data or b"") == 16 |
| 897 |
assert res3.buffers[1].data != b"data" |
| 898 |
|
| 899 |
msg = (res3.buffers[0].data or b"") + (res3.buffers[1].data or b"") |
| 900 |
res4 = c.unwrap_iov( |
| 901 |
[ |
| 902 |
(spnego.iov.BufferType.stream, msg), |
| 903 |
spnego.iov.BufferType.data, |
| 904 |
] |
| 905 |
) |
| 906 |
assert isinstance(res4, spnego.IOVUnwrapResult) |
| 907 |
assert res4.encrypted |
| 908 |
assert res4.qop == 0 |
| 909 |
assert len(res4.buffers) == 2 |
| 910 |
assert res4.buffers[1].data == b"data" |