Debian Codegraph @

src/spnego/_ntlm_raw/crypto.py

raw ◀ ▶
1 Debian patch touch this file
1 # Copyright: (c) 2020, Jordan Borean (@jborean93) <[email protected]>
2 # MIT License (see LICENSE or https://opensource.org/licenses/MIT)
3
4 """NTLM Cryptographic Operations
5
6 Implements the various cryptographic operations that are defined in `MS-NLMP Crypto Operations`_. Some crypto
7 operations aren't implemented due to it being a simple operation in Python.
8
9 .. _MS-NLMP Crypto Operations
10 https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/26c42637-9549-46ae-be2e-90f6f1360193
11 """
12
13 import base64
14 import binascii
15 import hashlib
16 import hmac
17 import io
18 import re
19 import struct
20 import typing
21
22 from cryptography.hazmat.backends import default_backend
•23 from cryptography.hazmat.primitives.ciphers import Cipher
24
25 from spnego._ntlm_raw.des import DES
26 from spnego._ntlm_raw.md4 import md4
27 from spnego._ntlm_raw.messages import (
28 FileTime,
29 NegotiateFlags,
30 NTClientChallengeV2,
31 TargetInfo,
32 )
33
•34 try:
35 # cryptography 43.0.0 and later moved ARC4 to decrepit
36 from cryptography.hazmat.decrepit.ciphers import algorithms
37 except ImportError:
38 from cryptography.hazmat.primitives.ciphers import ( # type: ignore[no-redef]
39 algorithms,
40 )
41
42 # A user does not need to specify their actual plaintext password they can specify the LM and NT hash (from lmowfv1 and
43 # ntowfv2) in the form 'lm_hash_hex:nt_hash_hex'. This is still considered a plaintext pass as we can use it to build
44 # the LM and NT response but it's only usable for NTLM.
45 _NTLM_HASH_PATTERN = re.compile(r"^[a-fA-F0-9]{32}:[a-fA-F0-9]{32}$")
46
47
48 class RC4Handle:
49 """RC4 class to wrap the underlying crypto function."""
50
51 def __init__(self, key: bytes) -> None:
52 self._key = key
53
54 arc4 = algorithms.ARC4(self._key)
55 cipher = Cipher(arc4, mode=None, backend=default_backend())
56 self._handle = cipher.encryptor()
57
58 def update(self, b_data: bytes) -> bytes:
59 """Update the RC4 stream and return the encrypted/decrypted bytes."""
60 return self._handle.update(b_data)
61
62 def reset(self) -> None:
63 """Reset's the cipher stream back to the original state."""
64 arc4 = algorithms.ARC4(self._key)
65 cipher = Cipher(arc4, mode=None, backend=default_backend())
66 self._handle = cipher.encryptor()
67
68
69 def is_ntlm_hash(password: str) -> bool:
70 return bool(_NTLM_HASH_PATTERN.match(password))
71
72
73 def compute_response_v1(
74 flags: int,
75 nt_hash: bytes,
76 lm_hash: bytes,
77 server_challenge: bytes,
78 client_challenge: bytes,
79 no_lm_response: bool = True,
80 ) -> typing.Tuple[bytes, bytes, bytes]:
81 """Compute NT and LM Response for NTLMv1.
82
83 Computes the NT and LM Response for NTLMv1 messages. The response is dependent on the flags that were negotiated
84 between the client and server.
85
86 The pseudo-code for this function as documented under `NTLM v1 Authentication`_ is::
87
88 Define ComputeResponse(NegFlg, ResponseKeyNT, ResponseKeyLM, CHALLENGE_MESSAGE.ServerChallenge,
89 ClientChallenge, Time, ServerName) As
90
91 If (User is set to "" AND Passwd is set to "")
92 -- Special case for anonymous authentication
93 Set NtChallengeResponseLen to 0
94 Set NtChallengeResponseMaxLen to 0
95 Set NtChallengeResponseBufferOffset to 0
96
97 Set LmChallengeResponse to Z(1)
98 ElseIf
99 If (NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY flag is set in NegFlg)
100 Set NtChallengeResponse to DESL(ResponseKeyNT,
101 MD5(ConcatenationOf(CHALLENGE_MESSAGE.ServerChallenge, ClientChallenge))[0..7])
102
103 Set LmChallengeResponse to ConcatenationOf{ClientChallenge, Z(16)}
104 Else
105 Set NtChallengeResponse to DESL(ResponseKeyNT, CHALLENGE_MESSAGE.ServerChallenge)
106
107 If (NoLMResponseNTLMv1 is TRUE)
108 Set LmChallengeResponse to NtChallengeResponse
109 Else
110 Set LmChallengeResponse to DESL(ResponseKeyLM, CHALLENGE_MESSAGE.ServerChallenge)
111 EndIf
112 EndIf
113 EndIf
114
115 Set SessionBaseKey to MD4(NTOWF)
116
117 Args:
118 flags: The negotiated flags between the initiator and acceptor.
119 nt_hash: The response key computed by :meth:`ntowfv1`.
120 lm_hash: The response key computed by :meth:`lmowfv1`.
121 server_challenge: The 8 byte nonce generated by the acceptor.
122 client_challenge: The 8 byte nonce generated by the initiator.
123 no_lm_response: Whether to compute (True) the `LmChallengeResponse` or not (False) when extended session
124 security was not negotiated.
125
126 Returns:
127 Tuple[bytes, bytes, bytes]: Returns the NTChallengeResponse, LMChallengeResponse and KeyExchangeKey.
128
129 .. _NTLM v1 Authentication:
130 https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/464551a8-9fc4-428e-b3d3-bc5bfb2e73a5
131 """
132 if flags & NegotiateFlags.extended_session_security:
133 nt_response = desl(nt_hash, md5(server_challenge + client_challenge[:8]))
134 lm_response = client_challenge + (b"\x00" * 16)
135
136 else:
137 nt_response = lm_response = desl(nt_hash, server_challenge)
138
139 if not no_lm_response:
140 lm_response = desl(lm_hash, server_challenge)
141
142 session_base_key = md4(nt_hash)
143 key_exchange_key = kxkey(flags, session_base_key, lm_hash, lm_response, server_challenge)
144
145 return nt_response, lm_response, key_exchange_key
146
147
148 def compute_response_v2(
149 nt_hash: bytes,
150 server_challenge: bytes,
151 client_challenge: bytes,
152 time: FileTime,
153 av_pairs: TargetInfo,
154 ) -> typing.Tuple[bytes, bytes, bytes]:
155 """Compute NT and LM Response for NTLMv2.
156
157 Computes the NT and LM Response for NTLMv2 messages. The response is dependent on the flags that were negotiated
158 between the client and server.
159
160 The pseudo-code for this function as documented under `NTLM v2 Authentication`_ is::
161
162 Define ComputeResponse(NegFlg, ResponseKeyNT, ResponseKeyLM, CHALLENGE_MESSAGE.ServerChallenge,
163 ClientChallenge, Time, ServerName) As
164
165 If (User is set to "" && Passwd is set to "")
166 -- Special case for anonymous authentication
167 Set NtChallengeResponseLen to 0
168 Set NtChallengeResponseMaxLen to 0
169 Set NtChallengeResponseBufferOffset to 0
170
171 Set LmChallengeResponse to Z(1)
172 Else
173 Set temp to ConcatenationOf(Responserversion, HiResponserversion, Z(6), Time, ClientChallenge, Z(4),
174 ServerName, Z(4))
175
176 Set NTProofStr to HMAC_MD5(ResponseKeyNT, ConcatenationOf(CHALLENGE_MESSAGE.ServerChallenge,temp))
177
178 Set NtChallengeResponse to ConcatenationOf(NTProofStr, temp)
179
180 Set LmChallengeResponse to ConcatenationOf(
181 HMAC_MD5(ResponseKeyLM, ConcatenationOf(CHALLENGE_MESSAGE.ServerChallenge, ClientChallenge)),
182 ClientChallenge)
183 EndIf
184
185 Set SessionBaseKey to HMAC_MD5(ResponseKeyNT, NTProofStr)
186
187 Args:
188 nt_hash: The response key computed by :meth:`ntwofv2`. The `ResponseKeyLM` is the same value so we only
189 pass in the 1 key.
190 server_challenge: The 8 byte nonce generated by the acceptor.
191 client_challenge: The 8 byte nonce generated by the initiator.
192 time: The FileTime to place in the NT hash.
193 av_pairs: The TargetInfo AvPairs fields that are placed in the Authenticate message.
194
195 Returns:
196 Tuple[bytes, bytes, bytes]: Returns the NTChallengeResponse, LMChallengeResponse and KeyExchangeKey.
197
198 .. _NTLM v2 Authentication:
199 https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/5e550938-91d4-459f-b67d-75d70009e3f3
200 """
201 temp = NTClientChallengeV2(time_stamp=time, client_challenge=client_challenge, av_pairs=av_pairs)
202 b_temp = temp.pack() + b"\x00\x00\x00\x00"
203 nt_proof_str = hmac_md5(nt_hash, server_challenge + b_temp)
204
205 nt_response = nt_proof_str + b_temp
206 lm_response = hmac_md5(nt_hash, server_challenge + client_challenge) + client_challenge
207 session_base_key = hmac_md5(nt_hash, nt_proof_str)
208
209 return nt_response, lm_response, session_base_key # Is KeyExchangeKey in NTLMv2.
210
211
212 def crc32(m: bytes) -> bytes:
213 """Simple wrapper function to generate a CRC32 checksum."""
214 # We bitand to ensure the value is the same across all Python versions.
215 return struct.pack("<I", binascii.crc32(m) & 0xFFFFFFFF)
216
217
218 def des(k: bytes, d: bytes) -> bytes:
219 """DES encryption.
220
221 Indicates the encryption of an 8-byte data item `d` with the 7-byte key `k` using the Data Encryption Standard
222 (DES) algorithm in Electronic Codebook (ECB) mode. The result is 8 bytes in length ([FIPS46-2]).
223
224 Args:
225 k: The 7-byte key to use in the DES cipher.
226 d: The 8-byte data block to encrypt.
227
228 Returns:
229 bytes: The encrypted data block.
230 """
231 return DES(DES.key56_to_key64(k)).encrypt(d)
232
233
234 def desl(k: bytes, d: bytes) -> bytes:
235 """Encryption using the DES Long algorithm.
236
237 Indicates the encryption of an 8-byte data item `d` with the 16-byte key `k` using the Data Encryption
238 Standard Long (DESL) algorithm. The result is 24 bytes in length.
239
240 `DESL(K, D)` as by MS-NLMP `DESL`_ is computed as follows::
241
242 ConcatenationOf(
243 DES(K[0..6], D),
244 DES(K[7..13], D),
245 DES(ConcatenationOf(K[14..15], Z(5)), D),
246 );
247
248 Args:
249 k: The key to use for the DES cipher, will be truncated to 16 bytes and then padded to 21 bytes.
250 d: The value to run through the DESL algorithm, will be truncated to 8 bytes.
251
252 Returns:
253 bytes: The output of the DESL algorithm.
254
255 .. _DESL:
256 https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/26c42637-9549-46ae-be2e-90f6f1360193
257 """
258 k = k[:16].ljust(21, b"\x00") # Key needs to be stripped at 16 characters and then padded to 21 chars.
259 d = d[:8].ljust(8, b"\x00") # Data need to be at most 8 bytes long.
260
261 b_value = io.BytesIO()
262
263 b_value.write(des(k[:7], d))
264 b_value.write(des(k[7:14], d))
265 b_value.write(des(k[14:], d))
266
267 return b_value.getvalue()
268
269
270 def hmac_md5(key: bytes, data: bytes) -> bytes:
271 """Simple wrapper function for a HMAC MD5 digest."""
272 return hmac.new(key, data, digestmod=hashlib.md5).digest()
273
274
275 def kxkey(
276 flags: int,
277 session_base_key: bytes,
278 lmowf: bytes,
279 lm_response: bytes,
280 server_challenge: bytes,
281 ) -> bytes:
282 """NTLM KXKEY function.
283
284 The MS-NLMP `KXKEY`_ function used to derive the key exchange key for a security context. This is only for NTLMv1
285 contexts as NTLMv2 just re-uses the session base key.
286
287 Args:
288 flags: The negotiate flags in the Challenge msg.
289 session_base_key: The session base key from :meth:`compute_response_v1`.
290 lmowf: The LM hash from :meth:`lmowfv1`.
291 lm_response: The lm response from :meth:`compute_response_v1`.
292 server_challenge: The server challenge in the Challenge msg.
293
294 Returns:
295 bytes: The derived key exchange key.
296
297 .. _KXKEY:
298 https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/d86303b5-b29e-4fb9-b119-77579c761370
299 """
300 if flags & NegotiateFlags.extended_session_security:
301 return hmac_md5(session_base_key, server_challenge + lm_response[:8])
302
303 elif flags & NegotiateFlags.lm_key:
304 b_data = lm_response[:8]
305 return des(lmowf[:7], b_data) + des(lmowf[7:8] + b"\xBD\xBD\xBD\xBD\xBD\xBD", b_data)
306
307 elif flags & NegotiateFlags.non_nt_session_key:
308 return lmowf[:8] + b"\x00" * 8
309
310 else:
311 return session_base_key
312
313
314 def lmowfv1(password: str) -> bytes:
315 """NTLMv1 LMOWFv1 function
316
317 The Lan Manager v1 one way function as documented under `NTLM v1 Authentication`_.
318
319 The pseudo-code for this function is::
320
321 Define LMOWFv1(Passwd, User, UserDom) as
322 ConcatenationOf(
323 DES(UpperCase(Passwd)[0..6], "KGS!@#$%"),
324 DES(UpperCase(Passwd)[7..13], "KGS!@#$%"),
325 );
326
327 Args:
328 password: The password for the user.
329
330 Returns:
331 bytes: The LMv1 one way hash of the user's password.
332
333 .. _NTLM v1 Authentication:
334 https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/464551a8-9fc4-428e-b3d3-bc5bfb2e73a5
335 """
336 if is_ntlm_hash(password):
337 return base64.b16decode(password.split(":")[0].upper())
338
339 # Fix the password to upper case and pad the length to exactly 14 bytes. While it is true LM only authentication
340 # will fail if the password exceeds 14 bytes typically it is used in conjunction with the NTv1 hash which has no
341 # such restrictions.
342 b_password = password.upper().encode("utf-8").ljust(14, b"\x00")[:14]
343
344 b_hash = io.BytesIO()
345 for start, end in [(0, 7), (7, 14)]:
346 b_hash.write(des(b_password[start:end], b"KGS!@#$%"))
347
348 return b_hash.getvalue()
349
350
351 def md5(m: bytes) -> bytes:
352 """Simple wrapper to generate a MD5 checksum."""
353 return hashlib.md5(m).digest()
354
355
356 def ntowfv1(password: str) -> bytes:
357 """NTLMv1 NTOWFv1 function
358
359 The NT v1 one way function as documented under `NTLM v1 Authentication`_.
360
361 The pseudo-code for this function is::
362
363 Define NTOWFv1(Passwd, User, UserDom) as MD4(UNICODE(Passwd))
364
365 Args:
366 password: The password for the user.
367
368 Returns:
369 bytes: The NTv1 one way hash of the user's password.
370
371 .. _NTLM v1 Authentication:
372 https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/464551a8-9fc4-428e-b3d3-bc5bfb2e73a5
373 """
374 if is_ntlm_hash(password):
375 return base64.b16decode(password.split(":")[1].upper())
376
377 return md4(password.encode("utf-16-le"))
378
379
380 def ntowfv2(username: str, nt_hash: bytes, domain_name: typing.Optional[str]) -> bytes:
381 """NTLMv2 NTOWFv2 function
382
383 The NT v2 one way function as documented under `NTLM v2 Authentication`_.
384
385 The pseudo-code for this function is::
386
387 Define NTOWFv2(Passwd, User, UserDom) as
388
389 HMAC_MD5(MD4(UNICODE(Passwd)), UNICODE(ConcatenationOf(Uppercase(User), UserDom)))
390
391 Args:
392 username: The username.
393 nt_hash: The NT hash from :meth:`ntowfv1`.
394 domain_name: The optional domain name of the user.
395
396 Returns:
397 bytes: The NTv2 one way has of the user's credentials.
398
399 .. _NTLM v2 Authentication:
400 https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/5e550938-91d4-459f-b67d-75d70009e3f3
401 """
402 b_user = (username.upper() + (domain_name or "")).encode("utf-16-le")
403 return hmac_md5(nt_hash, b_user)
404
405
406 def rc4(h: RC4Handle, d: bytes) -> bytes:
407 """RC4 encryption of the data specified using the handle generated by rc4init."""
408 return h.update(d)
409
410
411 def rc4k(k: bytes, d: bytes) -> bytes:
412 """RC4 encryption with an explicit key.
413
414 Indicates the encryption of data item `d` with the key `k` using the `RC4`_ algorithm.
415
416 Args:
417 k: The key to use for the RC4 cipher.
418 d: The data to encrypt.
419
420 Returns:
421 bytes: The RC4 encrypted bytes.
422
423 .. _RC4K:
424 https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/26c42637-9549-46ae-be2e-90f6f1360193
425 """
426 return rc4init(k).update(d)
427
428
429 def rc4init(k: bytes) -> RC4Handle:
430 """Initialization of the RC4 handle using the key specified."""
431 return RC4Handle(k)
432
433
434 def sealkey(flags: int, session_key: bytes, usage: str) -> bytes:
435 """NTLM SEALKEY function.
436
437 The MS-NLMP `SEALKEY`_ function used to generate the sealing keys for a security context.
438
439 The pseudo-code for this function as documented under `SEALKEY`_ is::
440
441 Define SEALKEY(NegFlg, ExportedSessionKey, Mode) as
442
443 If (NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY flag is set in NegFlg)
444
445 If ( NTLMSSP_NEGOTIATE_128 is set in NegFlg)
446 Set SealKey to ExportedSessionKey
447
448 ElseIf ( NTLMSSP_NEGOTIATE_56 flag is set in NegFlg)
449 Set SealKey to ExportedSessionKey[0..6]
450
451 Else
452 Set SealKey to ExportedSessionKey[0..4]
453
454 Endif
455
456 If (Mode equals "Client")
457 Set SealKey to MD5(ConcatenationOf(SealKey,
458 "session key to client-to-server sealing key magic constant"))
459
460 Else
461 Set SealKey to MD5(ConcatenationOf(SealKey,
462 "session key to server-to-client sealing key magic constant"))
463
464 Endif
465 ElseIf ((NTLMSSP_NEGOTIATE_LM_KEY is set in NegFlg) or ((NTLMSSP_NEGOTIATE_DATAGRAM is set in NegFlg) and
466 (NTLMRevisionCurrent >= NTLMSSP_REVISION_W2K3)))
467
468 If (NTLMSSP_NEGOTIATE_56 flag is set in NegFlg)
469 Set SealKey to ConcatenationOf(ExportedSessionKey[0..6], 0xA0)
470
471 Else
472 Set SealKey to ConcatenationOf(ExportedSessionKey[0..4], 0xE5, 0x38, 0xB0)
473
474 EndIf
475
476 Else
477 Set SealKey to ExportedSessionKey
478 Endif
479 EndDefine
480
481 Args:
482 flags: The negotiated flags between the initiator and acceptor.
483 session_key: The derived session key.
484 usage: Whether the sealing key is for the 'initiate' or 'accept' context.
485
486 Returns:
487 bytes: The derived sealing key.
488
489 .. _SEALKEY:
490 https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/bf39181d-e95d-40d7-a740-ab4ec3dc363d
491 """
492 if flags & NegotiateFlags.extended_session_security:
493 if flags & NegotiateFlags.key_128:
494 seal_key = session_key
495
496 elif flags & NegotiateFlags.key_56:
497 seal_key = session_key[:7]
498
499 else:
500 seal_key = session_key[:5]
501
502 direction = b"client-to-server" if usage == "initiate" else b"server-to-client"
503
504 return md5(seal_key + b"session key to %s sealing key magic constant\x00" % direction)
505
506 elif flags & NegotiateFlags.lm_key or flags & NegotiateFlags.datagram:
507 if flags & NegotiateFlags.key_56:
508 return session_key[:7] + b"\xA0"
509
510 else:
511 return session_key[:5] + b"\xE5\x38\xB0"
512
513 else:
514 return session_key
515
516
517 def signkey(flags: int, session_key: bytes, usage: str) -> bytes:
518 """NTLM SIGNKEY function.
519
520 The MS-NLMP `SIGNKEY`_ function used to generate the signing keys for a security context.
521
522 The pseudo-code for this function as documented under `SIGNKEY`_ is::
523
524 Define SIGNKEY(NegFlg, ExportedSessionKey, Mode) as
525
526 If (NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY flag is set in NegFlg)
527 If (Mode equals "Client")
528 Set SignKey to MD5(ConcatenationOf(ExportedSessionKey,
529 "session key to client-to-server signing key magic constant"))
530
531 Else
532 Set SignKey to MD5(ConcatenationOf(ExportedSessionKey,
533 "session key to server-to-client signing key magic constant"))
534
535 Endif
536 Else
537 Set SignKey to NIL
538
539 Endif
540 EndDefine
541
542 Args:
543 flags: The negotiated flags between the initiator and acceptor.
544 session_key: The derived session key.
545 usage: Whether the signing key is for the 'initiate' or 'accept' context.
546
547 Returns:
548 bytes: The derived singing key.
549
550 .. _SIGNKEY:
551 https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/524cdccb-563e-4793-92b0-7bc321fce096
552 """
553 if flags & NegotiateFlags.extended_session_security == 0:
554 return b""
555
556 direction = b"client-to-server" if usage == "initiate" else b"server-to-client"
557
558 return md5(session_key + b"session key to %s signing key magic constant\x00" % direction)