| 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 logging |
| 6 |
import os |
| 7 |
import socket |
| 8 |
import typing |
| 9 |
|
| 10 |
from spnego._context import ( |
| 11 |
IOV, |
| 12 |
ContextProxy, |
| 13 |
ContextReq, |
| 14 |
IOVUnwrapResult, |
| 15 |
IOVWrapResult, |
| 16 |
SecPkgContextSizes, |
| 17 |
UnwrapResult, |
| 18 |
WinRMWrapResult, |
| 19 |
WrapResult, |
| 20 |
split_username, |
| 21 |
) |
| 22 |
from spnego._credential import ( |
| 23 |
Credential, |
| 24 |
CredentialCache, |
| 25 |
NTLMHash, |
| 26 |
Password, |
| 27 |
unify_credentials, |
| 28 |
) |
| 29 |
from spnego._ntlm_raw.crypto import ( |
| 30 |
RC4Handle, |
| 31 |
compute_response_v1, |
| 32 |
compute_response_v2, |
| 33 |
hmac_md5, |
| 34 |
lmowfv1, |
| 35 |
md5, |
| 36 |
ntowfv1, |
| 37 |
ntowfv2, |
| 38 |
rc4init, |
| 39 |
rc4k, |
| 40 |
sealkey, |
| 41 |
signkey, |
| 42 |
) |
| 43 |
from spnego._ntlm_raw.messages import ( |
| 44 |
Authenticate, |
| 45 |
AvFlags, |
| 46 |
AvId, |
| 47 |
Challenge, |
| 48 |
FileTime, |
| 49 |
Negotiate, |
| 50 |
NegotiateFlags, |
| 51 |
NTClientChallengeV2, |
| 52 |
TargetInfo, |
| 53 |
Version, |
| 54 |
) |
| 55 |
from spnego._ntlm_raw.security import seal, sign |
| 56 |
from spnego._text import to_text |
| 57 |
from spnego.channel_bindings import GssChannelBindings |
| 58 |
from spnego.exceptions import ( |
| 59 |
BadBindingsError, |
| 60 |
BadMICError, |
| 61 |
ErrorCode, |
| 62 |
InvalidTokenError, |
| 63 |
NegotiateOptions, |
| 64 |
NoContextError, |
| 65 |
OperationNotAvailableError, |
| 66 |
SpnegoError, |
| 67 |
UnsupportedQop, |
| 68 |
) |
| 69 |
from spnego.iov import BufferType, IOVResBuffer |
| 70 |
|
| 71 |
log = logging.getLogger(__name__) |
| 72 |
|
| 73 |
|
| 74 |
def _get_credential_file() -> typing.Optional[str]: |
| 75 |
"""Get the path to the NTLM credential store. |
| 76 |
|
| 77 |
Returns the path to the NTLM credential store specified by the environment variable `NTLM_USER_FILE`. |
| 78 |
|
| 79 |
Returns: |
| 80 |
Optional[bytes]: The path to the NTLM credential file or None if not set or found. |
| 81 |
""" |
| 82 |
user_file_path = os.environ.get("NTLM_USER_FILE", None) |
| 83 |
if not user_file_path: |
| 84 |
return None |
| 85 |
|
| 86 |
file_path = to_text(user_file_path, encoding="utf-8") |
| 87 |
if os.path.isfile(file_path): |
| 88 |
return file_path |
| 89 |
|
| 90 |
return None |
| 91 |
|
| 92 |
|
| 93 |
def _get_credential( |
| 94 |
store: typing.Optional[str], |
| 95 |
domain: typing.Optional[str] = None, |
| 96 |
username: typing.Optional[str] = None, |
| 97 |
) -> typing.Tuple[typing.Optional[str], typing.Optional[str], bytes, bytes]: |
| 98 |
"""Look up NTLM credentials from the common flat file. |
| 99 |
|
| 100 |
Retrieves the LM and NT hash for use with authentication or validating a credential from an initiator. |
| 101 |
|
| 102 |
Each line in the store can be in the Heimdal format `DOMAIN:USER:PASSWORD` like:: |
| 103 |
|
| 104 |
testdom:testuser:Password01 |
| 105 |
:[email protected]:Password01 |
| 106 |
|
| 107 |
Or it can use the `smbpasswd`_ file format `USERNAME:UID:LM_HASH:NT_HASH:ACCT_FLAGS:TIMESTAMP` like:: |
| 108 |
|
| 109 |
testuser:1000:278623D830DABE161104594F8C2EF12B:C3C6F4FD8A02A6C1268F1A8074B6E7E0:[U]:LCT-1589398321 |
| 110 |
TESTDOM\testuser:1000:4588C64B89437893AAD3B435B51404EE:65202355FA01AEF26B89B19E00F52679:[U]:LCT-1589398321 |
| 111 |
[email protected]:1000:00000000000000000000000000000000:8ADB9B997580D69E69CAA2BBB68F4697:[U]:LCT-1589398321 |
| 112 |
|
| 113 |
While only the `USERNAME`, `LM_HASH`, and `NT_HASH` fields are used, the colons are still required to differentiate |
| 114 |
between the 2 formats. See `ntlm hash generator`_ for ways to generate the `LM_HASH` and `NT_HASH`. |
| 115 |
|
| 116 |
The username is case insensitive but the format of the domain and user part must match up with the value used as |
| 117 |
the username specified by the caller. |
| 118 |
|
| 119 |
While each line can use a different format, it is recommended to stick to 1 throughout the file. |
| 120 |
|
| 121 |
The same env var and format can also be read with gss-ntlmssp. |
| 122 |
|
| 123 |
Args: |
| 124 |
store: The credential store to lookup the credential from. |
| 125 |
domain: The domain for the user to get the credentials for. Should be `None` for a user in the UPN form. |
| 126 |
username: The username to get the credentials for. If omitted then the first entry in the store is used. |
| 127 |
|
| 128 |
Returns: |
| 129 |
Tuple[str, str, bytes, bytes]: The domain, username, LM, and NT hash of the user specified. |
| 130 |
|
| 131 |
.. _smbpasswd: |
| 132 |
https://www.samba.org/samba/docs/current/man-html/smbpasswd.5.html |
| 133 |
|
| 134 |
.. _ntlm hash generator: |
| 135 |
https://asecuritysite.com/encryption/lmhash |
| 136 |
""" |
| 137 |
if not store: |
| 138 |
raise OperationNotAvailableError(context_msg="Retrieving NTLM store without NTLM_USER_FILE set to a filepath") |
| 139 |
|
| 140 |
domain = domain or "" |
| 141 |
|
| 142 |
def store_lines( |
| 143 |
text: str, |
| 144 |
) -> typing.Iterator[typing.Tuple[str, str, typing.Optional[str], typing.Optional[bytes], typing.Optional[bytes]]]: |
| 145 |
for line in text.splitlines(): |
| 146 |
line_split = line.split(":") |
| 147 |
|
| 148 |
if len(line_split) == 3: |
| 149 |
yield line_split[0], line_split[1], line_split[2], None, None |
| 150 |
|
| 151 |
elif len(line_split) == 6: |
| 152 |
domain_entry, user_entry = split_username(line_split[0]) |
| 153 |
lm_entry = base64.b16decode(line_split[2].upper()) |
| 154 |
nt_entry = base64.b16decode(line_split[3].upper()) |
| 155 |
|
| 156 |
yield domain_entry or "", user_entry or "", None, lm_entry, nt_entry |
| 157 |
|
| 158 |
with open(store, mode="rb") as fd: |
| 159 |
cred_text = fd.read().decode() |
| 160 |
|
| 161 |
for line_domain, line_user, line_password, lm_hash, nt_hash in store_lines(cred_text): |
| 162 |
if not username or (username.upper() == line_user.upper() and domain.upper() == line_domain.upper()): |
| 163 |
# The Heimdal format uses the password so if the LM or NT hash isn't set generate it ourselves. |
| 164 |
if not lm_hash: |
| 165 |
lm_hash = lmowfv1(line_password or "") |
| 166 |
if not nt_hash: |
| 167 |
nt_hash = ntowfv1(line_password or "") |
| 168 |
|
| 169 |
# Favour the explicit username/password value, otherwise use what was in the credential file. |
| 170 |
if not username: |
| 171 |
username = line_user |
| 172 |
|
| 173 |
if not domain: |
| 174 |
domain = line_domain or None |
| 175 |
|
| 176 |
return domain, username, lm_hash, nt_hash |
| 177 |
|
| 178 |
else: |
| 179 |
raise SpnegoError( |
| 180 |
ErrorCode.failure, |
| 181 |
context_msg="Failed to find any matching credential in " "NTLM_USER_FILE credential store.", |
| 182 |
) |
| 183 |
|
| 184 |
|
| 185 |
def _get_workstation() -> typing.Optional[str]: |
| 186 |
"""Get the current workstation name. |
| 187 |
|
| 188 |
This gets the current workstation name that respects `NETBIOS_COMPUTER_NAME`. The env var is used by the library |
| 189 |
that gss-ntlmssp calls and makes sure that this Python implementation is a closer in its behaviour. |
| 190 |
|
| 191 |
Returns: |
| 192 |
Optional[str]: The workstation to supply in the NTLM authentication message or None. |
| 193 |
""" |
| 194 |
if "NETBIOS_COMPUTER_NAME" in os.environ: |
| 195 |
workstation = os.environ["NETBIOS_COMPUTER_NAME"] |
| 196 |
|
| 197 |
else: |
| 198 |
workstation = socket.gethostname().upper() |
| 199 |
|
| 200 |
# An empty workstation should be None so we don't set it in the message. |
| 201 |
return to_text(workstation) if workstation else None |
| 202 |
|
| 203 |
|
| 204 |
class _NTLMCredential: |
| 205 |
def __init__( |
| 206 |
self, |
| 207 |
credential: typing.Optional[Credential] = None, |
| 208 |
) -> None: |
| 209 |
self._raw_username: typing.Optional[str] = None |
| 210 |
self._store: typing.Optional[str] |
| 211 |
if isinstance(credential, Password): |
| 212 |
self._store = "explicit" |
| 213 |
self._raw_username = credential.username |
| 214 |
self.domain, self.username = split_username(credential.username) |
| 215 |
self.lm_hash = lmowfv1(credential.password) |
| 216 |
self.nt_hash = ntowfv1(credential.password) |
| 217 |
|
| 218 |
elif isinstance(credential, NTLMHash): |
| 219 |
self._store = "explicit" |
| 220 |
self._raw_username = credential.username |
| 221 |
self.domain, self.username = split_username(credential.username) |
| 222 |
self.lm_hash = base64.b16decode(credential.lm_hash.upper()) if credential.lm_hash else b"\x00" * 16 |
| 223 |
self.nt_hash = base64.b16decode(credential.nt_hash.upper()) if credential.nt_hash else b"\x00" * 16 |
| 224 |
|
| 225 |
else: |
| 226 |
domain = username = None |
| 227 |
if isinstance(credential, CredentialCache): |
| 228 |
self._raw_username = credential.username |
| 229 |
domain, username = split_username(credential.username) |
| 230 |
|
| 231 |
self._store = _get_credential_file() |
| 232 |
self.domain, self.username, self.lm_hash, self.nt_hash = _get_credential(self._store, domain, username) |
| 233 |
|
| 234 |
|
| 235 |
class NTLMProxy(ContextProxy): |
| 236 |
"""A context wrapper for a Python managed NTLM context. |
| 237 |
|
| 238 |
This is a context that can be used on Linux to generate NTLM without any system dependencies. |
| 239 |
""" |
| 240 |
|
| 241 |
def __init__( |
| 242 |
self, |
| 243 |
username: typing.Optional[typing.Union[str, Credential, typing.List[Credential]]] = None, |
| 244 |
password: typing.Optional[str] = None, |
| 245 |
hostname: typing.Optional[str] = None, |
| 246 |
service: typing.Optional[str] = None, |
| 247 |
channel_bindings: typing.Optional[GssChannelBindings] = None, |
| 248 |
context_req: ContextReq = ContextReq.default, |
| 249 |
usage: str = "initiate", |
| 250 |
protocol: str = "ntlm", |
| 251 |
options: NegotiateOptions = NegotiateOptions.none, |
| 252 |
**kwargs: typing.Any, |
| 253 |
) -> None: |
| 254 |
credentials = unify_credentials(username, password, required_protocol="ntlm") |
| 255 |
super(NTLMProxy, self).__init__( |
| 256 |
credentials, hostname, service, channel_bindings, context_req, usage, protocol, options |
| 257 |
) |
| 258 |
|
| 259 |
self._complete = False |
| 260 |
self._credential: typing.Optional[_NTLMCredential] = None |
| 261 |
|
| 262 |
# Set the default flags, these might change depending on the LM_COMPAT_LEVEL set. |
| 263 |
self._context_req = ( |
| 264 |
self._context_req |
| 265 |
| NegotiateFlags.key_128 |
| 266 |
| NegotiateFlags.key_56 |
| 267 |
| NegotiateFlags.key_exch |
| 268 |
| NegotiateFlags.extended_session_security |
| 269 |
| NegotiateFlags.always_sign |
| 270 |
| NegotiateFlags.ntlm |
| 271 |
| NegotiateFlags.lm_key |
| 272 |
| NegotiateFlags.request_target |
| 273 |
| NegotiateFlags.oem |
| 274 |
| NegotiateFlags.unicode |
| 275 |
) |
| 276 |
|
| 277 |
# gss-ntlmssp uses the env var 'LM_COMPAT_LEVEL' to control the NTLM compatibility level. To try and make our |
| 278 |
# NTLM implementation similar in functionality we will also use that behaviour. |
| 279 |
# https://github.com/gssapi/gss-ntlmssp/blob/e498737a96e8832a2cb9141ab1fe51e129185a48/src/gss_ntlmssp.c#L159-L170 |
| 280 |
# See the below policy link for more details on what these mean, for now 3 is the sane behaviour. |
| 281 |
# https://docs.microsoft.com/en-us/windows/security/threat-protection/security-policy-settings/network-security-lan-manager-authentication-level |
| 282 |
lm_compat_level = int(os.environ.get("LM_COMPAT_LEVEL", 3)) |
| 283 |
if lm_compat_level < 0 or lm_compat_level > 5: |
| 284 |
raise SpnegoError( |
| 285 |
ErrorCode.failure, context_msg="Invalid LM_COMPAT_LEVEL %d, must be between 0 and 5" % lm_compat_level |
| 286 |
) |
| 287 |
|
| 288 |
if lm_compat_level == 0: |
| 289 |
self._context_req &= ~NegotiateFlags.extended_session_security |
| 290 |
|
| 291 |
if self.usage == "initiate": |
| 292 |
self._credential = _NTLMCredential(next(c for c in credentials if "ntlm" in c.supported_protocols)) |
| 293 |
|
| 294 |
self._lm = lm_compat_level < 2 |
| 295 |
self._nt_v1 = lm_compat_level < 3 |
| 296 |
self._nt_v2 = lm_compat_level > 2 |
| 297 |
|
| 298 |
if lm_compat_level > 1: |
| 299 |
self._context_req &= ~NegotiateFlags.lm_key |
| 300 |
|
| 301 |
else: |
| 302 |
self._lm = lm_compat_level < 4 |
| 303 |
self._nt_v1 = lm_compat_level < 5 |
| 304 |
self._nt_v2 = True |
| 305 |
|
| 306 |
# Make sure that the credential file is set and exists |
| 307 |
if not _get_credential_file(): |
| 308 |
raise OperationNotAvailableError( |
| 309 |
context_msg="Retrieving NTLM store without NTLM_USER_FILE set to a " "filepath" |
| 310 |
) |
| 311 |
|
| 312 |
self._temp_negotiate: typing.Optional[Negotiate] = None |
| 313 |
self._temp_challenge: typing.Optional[Challenge] = None |
| 314 |
self._mic_required = False |
| 315 |
|
| 316 |
# Crypto state for signing and sealing. |
| 317 |
self._session_key: typing.Optional[bytes] = None |
| 318 |
self._sign_key_out: typing.Optional[bytes] = None |
| 319 |
self._sign_key_in: typing.Optional[bytes] = None |
| 320 |
self._handle_out: typing.Optional[RC4Handle] = None |
| 321 |
self._handle_in: typing.Optional[RC4Handle] = None |
| 322 |
self.__seq_num_in = 0 |
| 323 |
self.__seq_num_out = 0 |
| 324 |
|
| 325 |
@classmethod |
| 326 |
def available_protocols(cls, options: typing.Optional[NegotiateOptions] = None) -> typing.List[str]: |
| 327 |
return ["ntlm"] |
| 328 |
|
| 329 |
@classmethod |
| 330 |
def iov_available(cls) -> bool: |
| 331 |
return True |
| 332 |
|
| 333 |
@property |
| 334 |
def client_principal(self) -> typing.Optional[str]: |
| 335 |
if self.usage == "accept" and self.complete and self._credential: |
| 336 |
domain_part = self._credential.domain + "\\" if self._credential.domain else "" |
| 337 |
return "%s%s" % (domain_part, self._credential.username) |
| 338 |
|
| 339 |
return None |
| 340 |
|
| 341 |
@property |
| 342 |
def complete(self) -> bool: |
| 343 |
return self._complete |
| 344 |
|
| 345 |
@property |
| 346 |
def negotiated_protocol(self) -> typing.Optional[str]: |
| 347 |
return "ntlm" |
| 348 |
|
| 349 |
@property |
| 350 |
def session_key(self) -> bytes: |
| 351 |
return self._session_key or b"" |
| 352 |
|
| 353 |
def new_context(self) -> "NTLMProxy": |
| 354 |
cred: typing.Optional[NTLMHash] = None |
| 355 |
if self._credential and self.usage == "initiate": |
| 356 |
cred = NTLMHash( |
| 357 |
username=self._credential._raw_username or "", |
| 358 |
lm_hash=base64.b16encode(self._credential.lm_hash).decode(), |
| 359 |
nt_hash=base64.b16encode(self._credential.nt_hash).decode(), |
| 360 |
) |
| 361 |
|
| 362 |
return NTLMProxy( |
| 363 |
username=cred, |
| 364 |
hostname=self._hostname, |
| 365 |
service=self._service, |
| 366 |
channel_bindings=self.channel_bindings, |
| 367 |
context_req=self.context_req, |
| 368 |
usage=self.usage, |
| 369 |
protocol=self.protocol, |
| 370 |
options=self.options, |
| 371 |
) |
| 372 |
|
| 373 |
def step( |
| 374 |
self, |
| 375 |
in_token: typing.Optional[bytes] = None, |
| 376 |
*, |
| 377 |
channel_bindings: typing.Optional[GssChannelBindings] = None, |
| 378 |
) -> typing.Optional[bytes]: |
| 379 |
if not self._is_wrapped: |
| 380 |
log.debug("NTLM step input: %s", base64.b64encode(in_token or b"").decode()) |
| 381 |
|
| 382 |
out_token = getattr(self, "_step_%s" % self.usage)( |
| 383 |
in_token=in_token, |
| 384 |
channel_bindings=channel_bindings, |
| 385 |
) |
| 386 |
|
| 387 |
if not self._is_wrapped: |
| 388 |
log.debug("NTLM step output: %s", base64.b64encode(out_token or b"").decode()) |
| 389 |
|
| 390 |
if self._complete: |
| 391 |
# Clear out any temp data we still have stored. |
| 392 |
self._temp_negotiate = None |
| 393 |
self._temp_challenge = None |
| 394 |
|
| 395 |
in_usage = "accept" if self.usage == "initiate" else "initiate" |
| 396 |
session_key = self._session_key or b"" |
| 397 |
# if tmp_key := os.environ.get("TESTING", None): |
| 398 |
# session_key = self._session_key = base64.b16decode(tmp_key) |
| 399 |
|
| 400 |
self._sign_key_out = signkey(self._context_attr, session_key, self.usage) |
| 401 |
self._sign_key_in = signkey(self._context_attr, session_key, in_usage) |
| 402 |
|
| 403 |
# Found a vague reference in MS-NLMP that states if NTLMv2 authentication was not used then only 1 key is |
| 404 |
# used for sealing. This seems to reference when NTLMSSP_NEGOTIATE_EXTENDED_SESSION_SECURITY is not set and |
| 405 |
# not NTLMv2 messages itself. |
| 406 |
# https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/d1c86e81-eb66-47fd-8a6f-970050121347 |
| 407 |
if self._context_attr & NegotiateFlags.extended_session_security: |
| 408 |
self._handle_out = rc4init(sealkey(self._context_attr, session_key, self.usage)) |
| 409 |
self._handle_in = rc4init(sealkey(self._context_attr, session_key, in_usage)) |
| 410 |
else: |
| 411 |
self._handle_out = self._handle_in = rc4init(sealkey(self._context_attr, session_key, self.usage)) |
| 412 |
|
| 413 |
return out_token |
| 414 |
|
| 415 |
def _step_initiate( |
| 416 |
self, |
| 417 |
in_token: typing.Optional[bytes] = None, |
| 418 |
*, |
| 419 |
channel_bindings: typing.Optional[GssChannelBindings] = None, |
| 420 |
) -> bytes: |
| 421 |
if not self._temp_negotiate: |
| 422 |
# SSPI always sends the version even if it's optional. There have |
| 423 |
# been reports that some NTLM servers expect this to produce a |
| 424 |
# valid challenge/authentication token later in the exchange. |
| 425 |
# https://github.com/jborean93/smbprotocol/issues/216 |
| 426 |
self._temp_negotiate = Negotiate(self._context_req, version=Version.get_current()) |
| 427 |
return self._temp_negotiate.pack() |
| 428 |
|
| 429 |
in_token = in_token or b"" |
| 430 |
challenge = Challenge.unpack(in_token) |
| 431 |
|
| 432 |
# Lots of mypy failures with self._credential, cast it to non Optional as it will always be set here. |
| 433 |
credential = typing.cast(_NTLMCredential, self._credential) |
| 434 |
auth_kwargs: typing.Dict[str, typing.Any] = { |
| 435 |
"domain_name": credential.domain, |
| 436 |
"username": credential.username, |
| 437 |
} |
| 438 |
|
| 439 |
if challenge.flags & NegotiateFlags.version: |
| 440 |
auth_kwargs["version"] = Version.get_current() |
| 441 |
auth_kwargs["workstation"] = _get_workstation() |
| 442 |
|
| 443 |
nt_challenge, lm_challenge, key_exchange_key = self._compute_response( |
| 444 |
challenge, |
| 445 |
credential, |
| 446 |
channel_bindings=channel_bindings or self.channel_bindings, |
| 447 |
) |
| 448 |
|
| 449 |
if challenge.flags & NegotiateFlags.key_exch: |
| 450 |
# This is only documented on the server side for MS-NLMP but is also valid for the client. The actual |
| 451 |
# session key is the KeyExchangeKey like normal unless sign or seal is negotiated. |
| 452 |
|
| 453 |
if challenge.flags & NegotiateFlags.sign or challenge.flags & NegotiateFlags.seal: |
| 454 |
self._session_key = os.urandom(16) |
| 455 |
auth_kwargs["encrypted_session_key"] = rc4k(key_exchange_key, self._session_key) |
| 456 |
|
| 457 |
else: |
| 458 |
self._session_key = key_exchange_key |
| 459 |
auth_kwargs["encrypted_session_key"] = b"\x00" # Must be set to some value but this can be anything. |
| 460 |
|
| 461 |
else: |
| 462 |
self._session_key = key_exchange_key |
| 463 |
|
| 464 |
authenticate = Authenticate(challenge.flags, lm_challenge, nt_challenge, **auth_kwargs) |
| 465 |
|
| 466 |
if self._mic_required: |
| 467 |
authenticate.mic = self._calculate_mic( |
| 468 |
self._session_key, self._temp_negotiate.pack(), in_token, authenticate.pack() |
| 469 |
) |
| 470 |
|
| 471 |
self._context_attr = authenticate.flags |
| 472 |
self._complete = True |
| 473 |
|
| 474 |
return authenticate.pack() |
| 475 |
|
| 476 |
def _step_accept( |
| 477 |
self, |
| 478 |
in_token: bytes, |
| 479 |
*, |
| 480 |
channel_bindings: typing.Optional[GssChannelBindings] = None, |
| 481 |
) -> typing.Optional[bytes]: |
| 482 |
if not self._temp_negotiate: |
| 483 |
return self._step_accept_negotiate(in_token) |
| 484 |
|
| 485 |
else: |
| 486 |
self._step_accept_authenticate( |
| 487 |
in_token, |
| 488 |
channel_bindings=channel_bindings or self.channel_bindings, |
| 489 |
) |
| 490 |
return None |
| 491 |
|
| 492 |
def _step_accept_negotiate(self, token: bytes) -> bytes: |
| 493 |
"""Process the Negotiate message from the initiator.""" |
| 494 |
negotiate = Negotiate.unpack(token) |
| 495 |
|
| 496 |
flags = ( |
| 497 |
negotiate.flags |
| 498 |
| NegotiateFlags.request_target |
| 499 |
| NegotiateFlags.ntlm |
| 500 |
| NegotiateFlags.always_sign |
| 501 |
| NegotiateFlags.target_info |
| 502 |
| NegotiateFlags.target_type_server |
| 503 |
) |
| 504 |
|
| 505 |
# Make sure either UNICODE or OEM is set, not both. |
| 506 |
if flags & NegotiateFlags.unicode: |
| 507 |
flags &= ~NegotiateFlags.oem |
| 508 |
elif flags & NegotiateFlags.oem == 0: |
| 509 |
raise SpnegoError( |
| 510 |
ErrorCode.failure, |
| 511 |
context_msg="Neither NEGOTIATE_OEM or NEGOTIATE_UNICODE flags were " |
| 512 |
"set, cannot derive encoding for text fields", |
| 513 |
) |
| 514 |
|
| 515 |
if flags & NegotiateFlags.extended_session_security: |
| 516 |
flags &= ~NegotiateFlags.lm_key |
| 517 |
|
| 518 |
server_challenge = os.urandom(8) |
| 519 |
target_name = to_text(socket.gethostname()).upper() |
| 520 |
|
| 521 |
target_info = TargetInfo() |
| 522 |
target_info[AvId.nb_computer_name] = target_name |
| 523 |
target_info[AvId.nb_domain_name] = "WORKSTATION" |
| 524 |
target_info[AvId.dns_computer_name] = to_text(socket.getfqdn()) |
| 525 |
target_info[AvId.timestamp] = FileTime.now() |
| 526 |
|
| 527 |
challenge = Challenge(flags, server_challenge, target_name=target_name, target_info=target_info) |
| 528 |
|
| 529 |
self._temp_negotiate = negotiate |
| 530 |
self._temp_challenge = challenge |
| 531 |
|
| 532 |
return challenge.pack() |
| 533 |
|
| 534 |
def _step_accept_authenticate(self, token: bytes, channel_bindings: typing.Optional[GssChannelBindings]) -> None: |
| 535 |
"""Process the Authenticate message from the initiator.""" |
| 536 |
negotiate = typing.cast(Negotiate, self._temp_negotiate) |
| 537 |
challenge = typing.cast(Challenge, self._temp_challenge) |
| 538 |
server_challenge = challenge.server_challenge |
| 539 |
auth = Authenticate.unpack(token) |
| 540 |
|
| 541 |
# TODO: Add anonymous user support. |
| 542 |
if not auth.user_name or ( |
| 543 |
not auth.nt_challenge_response and (not auth.lm_challenge_response or auth.lm_challenge_response == b"\x00") |
| 544 |
): |
| 545 |
raise OperationNotAvailableError(context_msg="Anonymous user authentication not implemented") |
| 546 |
|
| 547 |
username = auth.user_name |
| 548 |
if auth.domain_name: |
| 549 |
username = f"{auth.domain_name}\\{username}" |
| 550 |
self._credential = _NTLMCredential(CredentialCache(username=username)) |
| 551 |
expected_mic = None |
| 552 |
|
| 553 |
if auth.nt_challenge_response and len(auth.nt_challenge_response) > 24: |
| 554 |
nt_hash = ntowfv2(auth.user_name, self._credential.nt_hash, auth.domain_name) |
| 555 |
|
| 556 |
nt_challenge = NTClientChallengeV2.unpack(auth.nt_challenge_response[16:]) |
| 557 |
time = nt_challenge.time_stamp |
| 558 |
client_challenge = nt_challenge.challenge_from_client |
| 559 |
target_info = nt_challenge.av_pairs |
| 560 |
|
| 561 |
expected_nt, expected_lm, key_exchange_key = compute_response_v2( |
| 562 |
nt_hash, server_challenge, client_challenge, time, target_info |
| 563 |
) |
| 564 |
|
| 565 |
if channel_bindings: |
| 566 |
if AvId.channel_bindings not in target_info: |
| 567 |
raise BadBindingsError( |
| 568 |
context_msg="Acceptor bindings specified but not present in initiator " "response" |
| 569 |
) |
| 570 |
|
| 571 |
expected_bindings = target_info[AvId.channel_bindings] |
| 572 |
actual_bindings = md5(channel_bindings.pack()) |
| 573 |
if expected_bindings not in [actual_bindings, b"\x00" * 16]: |
| 574 |
raise BadBindingsError(context_msg="Acceptor bindings do not match initiator bindings") |
| 575 |
|
| 576 |
if target_info.get(AvId.flags, 0) & AvFlags.mic: |
| 577 |
expected_mic = auth.mic |
| 578 |
|
| 579 |
else: |
| 580 |
if not self._nt_v1: |
| 581 |
raise InvalidTokenError(context_msg="Acceptor settings are set to reject NTv1 responses") |
| 582 |
|
| 583 |
elif not auth.nt_challenge_response and not self._lm: |
| 584 |
raise InvalidTokenError(context_msg="Acceptor settings are set to reject LM responses") |
| 585 |
|
| 586 |
client_challenge = b"\x00" * 8 |
| 587 |
if auth.flags & NegotiateFlags.extended_session_security: |
| 588 |
client_challenge = (auth.lm_challenge_response or b"\x00" * 8)[:8] |
| 589 |
|
| 590 |
expected_nt, expected_lm, key_exchange_key = compute_response_v1( |
| 591 |
auth.flags, |
| 592 |
self._credential.nt_hash, |
| 593 |
self._credential.lm_hash, |
| 594 |
server_challenge, |
| 595 |
client_challenge, |
| 596 |
no_lm_response=not self._lm, |
| 597 |
) |
| 598 |
|
| 599 |
auth_success = False |
| 600 |
|
| 601 |
if auth.nt_challenge_response: |
| 602 |
auth_success = auth.nt_challenge_response == expected_nt |
| 603 |
|
| 604 |
elif auth.lm_challenge_response: |
| 605 |
auth_success = auth.lm_challenge_response == expected_lm |
| 606 |
|
| 607 |
if not auth_success: |
| 608 |
raise InvalidTokenError(context_msg="Invalid NTLM response from initiator") |
| 609 |
|
| 610 |
if auth.flags & NegotiateFlags.key_exch and ( |
| 611 |
auth.flags & NegotiateFlags.sign or auth.flags & NegotiateFlags.seal |
| 612 |
): |
| 613 |
self._session_key = rc4k(key_exchange_key, auth.encrypted_random_session_key or b"") |
| 614 |
|
| 615 |
else: |
| 616 |
self._session_key = key_exchange_key |
| 617 |
|
| 618 |
if expected_mic: |
| 619 |
auth.mic = b"\x00" * 16 |
| 620 |
actual_mic = self._calculate_mic(self.session_key, negotiate.pack(), challenge.pack(), auth.pack()) |
| 621 |
|
| 622 |
if actual_mic != expected_mic: |
| 623 |
raise InvalidTokenError(context_msg="Invalid MIC in NTLM authentication message") |
| 624 |
|
| 625 |
self._context_attr = auth.flags |
| 626 |
self._complete = True |
| 627 |
|
| 628 |
def query_message_sizes(self) -> SecPkgContextSizes: |
| 629 |
if not self.complete: |
| 630 |
raise NoContextError(context_msg="Cannot get message sizes until context has been established") |
| 631 |
|
| 632 |
return SecPkgContextSizes(header=16) |
| 633 |
|
| 634 |
def wrap(self, data: bytes, encrypt: bool = True, qop: typing.Optional[int] = None) -> WrapResult: |
| 635 |
if qop: |
| 636 |
raise UnsupportedQop(context_msg="Unsupported QoP value %s specified for NTLM" % qop) |
| 637 |
|
| 638 |
if self.context_attr & ContextReq.integrity == 0 and self.context_attr & ContextReq.confidentiality == 0: |
| 639 |
raise OperationNotAvailableError(context_msg="NTLM wrap without integrity or confidentiality") |
| 640 |
|
| 641 |
if not self._handle_out or self._sign_key_out is None: |
| 642 |
raise NoContextError(context_msg="Cannot wrap until context has been established") |
| 643 |
|
| 644 |
msg, signature = seal( |
| 645 |
self._context_attr, |
| 646 |
self._handle_out, |
| 647 |
self._sign_key_out, |
| 648 |
self._seq_num_out, |
| 649 |
data, |
| 650 |
) |
| 651 |
|
| 652 |
return WrapResult(data=signature + msg, encrypted=True) |
| 653 |
|
| 654 |
def wrap_iov( |
| 655 |
self, |
| 656 |
iov: typing.Iterable[IOV], |
| 657 |
encrypt: bool = True, |
| 658 |
qop: typing.Optional[int] = None, |
| 659 |
) -> IOVWrapResult: |
| 660 |
if qop: |
| 661 |
raise UnsupportedQop(context_msg="Unsupported QoP value %s specified for NTLM" % qop) |
| 662 |
|
| 663 |
if self.context_attr & ContextReq.integrity == 0 and self.context_attr & ContextReq.confidentiality == 0: |
| 664 |
raise OperationNotAvailableError(context_msg="NTLM wrap without integrity or confidentiality") |
| 665 |
|
| 666 |
if not self._handle_out or self._sign_key_out is None: |
| 667 |
raise NoContextError(context_msg="Cannot wrap until context has been established") |
| 668 |
|
| 669 |
header_idx = -1 |
| 670 |
data_idx = -1 |
| 671 |
signature_input = [] |
| 672 |
buffers = self._build_iov_list(iov, lambda b: b) |
| 673 |
res: typing.List[IOVResBuffer] = [] |
| 674 |
|
| 675 |
for idx, buffer in enumerate(buffers): |
| 676 |
data: bytes |
| 677 |
if buffer.type == BufferType.header: |
| 678 |
if header_idx != -1: |
| 679 |
raise InvalidTokenError(context_msg="wrap_iov must only be used with 1 header IOV buffer.") |
| 680 |
header_idx = idx |
| 681 |
data = b"" |
| 682 |
|
| 683 |
elif buffer.type == BufferType.data: |
| 684 |
if data_idx != -1: |
| 685 |
raise InvalidTokenError(context_msg="wrap_iov must only be used with 1 data IOV buffer.") |
| 686 |
|
| 687 |
if not isinstance(buffer.data, bytes): |
| 688 |
raise InvalidTokenError(context_msg=f"wrap_iov IOV data buffer at [{idx}] must be bytes") |
| 689 |
|
| 690 |
data_idx = idx |
| 691 |
data = buffer.data |
| 692 |
signature_input.append(data) |
| 693 |
|
| 694 |
elif buffer.type in [BufferType.sign_only, BufferType.data_readonly]: |
| 695 |
if not isinstance(buffer.data, bytes): |
| 696 |
raise InvalidTokenError( |
| 697 |
context_msg=f"wrap_iov IOV {buffer.type.name} buffer at [{idx}] must be bytes" |
| 698 |
) |
| 699 |
|
| 700 |
data = buffer.data |
| 701 |
signature_input.append(data) |
| 702 |
|
| 703 |
else: |
| 704 |
raise InvalidTokenError(context_msg=f"wrap_iov unsupported IOV buffer type {buffer.type.name}") |
| 705 |
|
| 706 |
res.append(IOVResBuffer(buffer.type, data)) |
| 707 |
|
| 708 |
if header_idx == -1: |
| 709 |
raise InvalidTokenError(context_msg="wrap_iov no IOV header buffer present") |
| 710 |
|
| 711 |
if data_idx == -1: |
| 712 |
raise InvalidTokenError(context_msg="wrap_iov no IOV data buffer present") |
| 713 |
|
| 714 |
enc_msg, signature = seal( |
| 715 |
self._context_attr, |
| 716 |
self._handle_out, |
| 717 |
self._sign_key_out, |
| 718 |
self._seq_num_out, |
| 719 |
res[data_idx][1] or b"", |
| 720 |
to_sign=b"".join(signature_input), |
| 721 |
) |
| 722 |
|
| 723 |
res[header_idx] = IOVResBuffer(BufferType.header, signature) |
| 724 |
res[data_idx] = IOVResBuffer(BufferType.data, enc_msg) |
| 725 |
|
| 726 |
return IOVWrapResult(tuple(res), encrypted=True) |
| 727 |
|
| 728 |
def wrap_winrm(self, data: bytes) -> WinRMWrapResult: |
| 729 |
enc_data = self.wrap(data).data |
| 730 |
return WinRMWrapResult(header=enc_data[:16], data=enc_data[16:], padding_length=0) |
| 731 |
|
| 732 |
def unwrap(self, data: bytes) -> UnwrapResult: |
| 733 |
if not self._handle_in: |
| 734 |
raise NoContextError(context_msg="Cannot unwrap until context has been established") |
| 735 |
|
| 736 |
signature = data[:16] |
| 737 |
msg = self._handle_in.update(data[16:]) |
| 738 |
self.verify(msg, signature) |
| 739 |
|
| 740 |
return UnwrapResult(data=msg, encrypted=True, qop=0) |
| 741 |
|
| 742 |
def unwrap_iov( |
| 743 |
self, |
| 744 |
iov: typing.Iterable[IOV], |
| 745 |
) -> IOVUnwrapResult: |
| 746 |
if self.context_attr & ContextReq.integrity == 0 and self.context_attr & ContextReq.confidentiality == 0: |
| 747 |
raise OperationNotAvailableError(context_msg="NTLM unwrap without integrity or confidentiality") |
| 748 |
|
| 749 |
if not self._handle_in or self._sign_key_in is None: |
| 750 |
raise NoContextError(context_msg="Cannot unwrap until context has been established") |
| 751 |
|
| 752 |
buffers = self._build_iov_list(iov, lambda b: b) |
| 753 |
|
| 754 |
# Check if it's a stream + data set of buffers and just call unwrap. |
| 755 |
if ( |
| 756 |
len(buffers) == 2 |
| 757 |
and buffers[0].type == BufferType.stream |
| 758 |
and buffers[1].type == BufferType.data |
| 759 |
and isinstance(buffers[0].data, bytes) |
| 760 |
): |
| 761 |
unwrap_res = self.unwrap(buffers[0].data) |
| 762 |
|
| 763 |
return IOVUnwrapResult( |
| 764 |
( |
| 765 |
IOVResBuffer(BufferType.stream, buffers[0].data), |
| 766 |
IOVResBuffer(BufferType.data, unwrap_res.data), |
| 767 |
), |
| 768 |
encrypted=unwrap_res.encrypted, |
| 769 |
qop=unwrap_res.qop, |
| 770 |
) |
| 771 |
|
| 772 |
header_idx = -1 |
| 773 |
data_idx = -1 |
| 774 |
data_sig_idx = -1 |
| 775 |
signature_input: typing.List[bytes] = [] |
| 776 |
|
| 777 |
res: typing.List[IOVResBuffer] = [] |
| 778 |
|
| 779 |
for idx, buffer in enumerate(buffers): |
| 780 |
data: bytes |
| 781 |
|
| 782 |
if not isinstance(buffer.data, bytes): |
| 783 |
raise InvalidTokenError( |
| 784 |
context_msg=f"unwrap_iov IOV {buffer.type.name} buffer at [{idx}] must be bytes" |
| 785 |
) |
| 786 |
|
| 787 |
data = buffer.data |
| 788 |
|
| 789 |
if buffer.type == BufferType.header: |
| 790 |
if header_idx != -1: |
| 791 |
raise InvalidTokenError(context_msg="unwrap_iov must only be used with 1 header IOV buffer.") |
| 792 |
|
| 793 |
header_idx = idx |
| 794 |
|
| 795 |
elif buffer.type == BufferType.data: |
| 796 |
if data_idx != -1: |
| 797 |
raise InvalidTokenError(context_msg="unwrap_iov must only be used with 1 data IOV buffer.") |
| 798 |
|
| 799 |
data_idx = idx |
| 800 |
data_sig_idx = len(signature_input) |
| 801 |
signature_input.append(b"") # Replaced after decryption |
| 802 |
|
| 803 |
elif buffer.type in [BufferType.sign_only, BufferType.data_readonly]: |
| 804 |
signature_input.append(data) |
| 805 |
|
| 806 |
else: |
| 807 |
raise InvalidTokenError(context_msg=f"unwrap_iov unsupported IOV buffer type {buffer.type.name}") |
| 808 |
|
| 809 |
res.append(IOVResBuffer(buffer.type, data)) |
| 810 |
|
| 811 |
if header_idx == -1: |
| 812 |
raise InvalidTokenError(context_msg="unwrap_iov no IOV header buffer present") |
| 813 |
|
| 814 |
if data_idx == -1: |
| 815 |
raise InvalidTokenError(context_msg="unwrap_iov no IOV data buffer present") |
| 816 |
|
| 817 |
dec = self._handle_in.update(res[data_idx].data or b"") |
| 818 |
res[data_idx] = IOVResBuffer(BufferType.data, dec) |
| 819 |
signature_input[data_sig_idx] = dec |
| 820 |
|
| 821 |
self.verify(b"".join(signature_input), res[header_idx].data or b"") |
| 822 |
|
| 823 |
return IOVUnwrapResult(tuple(res), encrypted=True, qop=0) |
| 824 |
|
| 825 |
def unwrap_winrm(self, header: bytes, data: bytes) -> bytes: |
| 826 |
if not self._handle_in: |
| 827 |
raise NoContextError(context_msg="Cannot unwrap until context has been established") |
| 828 |
|
| 829 |
msg = self._handle_in.update(data) |
| 830 |
self.verify(msg, header) |
| 831 |
|
| 832 |
return msg |
| 833 |
|
| 834 |
def sign(self, data: bytes, qop: typing.Optional[int] = None) -> bytes: |
| 835 |
if qop: |
| 836 |
raise UnsupportedQop(context_msg="Unsupported QoP value %s specified for NTLM" % qop) |
| 837 |
|
| 838 |
if not self._handle_out or self._sign_key_out is None: |
| 839 |
raise NoContextError(context_msg="Cannot sign until context has been established") |
| 840 |
|
| 841 |
return sign( |
| 842 |
self._context_attr, |
| 843 |
self._handle_out, |
| 844 |
self._sign_key_out, |
| 845 |
self._seq_num_out, |
| 846 |
data, |
| 847 |
) |
| 848 |
|
| 849 |
def verify(self, data: bytes, mic: bytes) -> int: |
| 850 |
if not self._handle_in or self._sign_key_in is None: |
| 851 |
raise NoContextError(context_msg="Cannot verify until context has been established") |
| 852 |
|
| 853 |
expected_sig = sign( |
| 854 |
self._context_attr, |
| 855 |
self._handle_in, |
| 856 |
self._sign_key_in, |
| 857 |
self._seq_num_in, |
| 858 |
data, |
| 859 |
) |
| 860 |
|
| 861 |
if expected_sig != mic: |
| 862 |
raise BadMICError(context_msg="Invalid Message integrity Check (MIC) detected") |
| 863 |
|
| 864 |
return 0 |
| 865 |
|
| 866 |
@property |
| 867 |
def _context_attr_map(self) -> typing.List[typing.Tuple[ContextReq, int]]: |
| 868 |
# https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/a4a41f0d-ca27-44bf-ad1d-6f8c3a3796f2 |
| 869 |
return [ |
| 870 |
(ContextReq.replay_detect, NegotiateFlags.sign), |
| 871 |
(ContextReq.sequence_detect, NegotiateFlags.sign), |
| 872 |
(ContextReq.confidentiality, NegotiateFlags.seal), |
| 873 |
(ContextReq.integrity, NegotiateFlags.sign), |
| 874 |
] |
| 875 |
|
| 876 |
@property |
| 877 |
def _requires_mech_list_mic(self) -> bool: |
| 878 |
# If called before the Authenticate message has been created it force the MIC to be present on the message. |
| 879 |
# When called after the Auth message it will return whether the MIC was generated or not. |
| 880 |
if not self._complete: |
| 881 |
self._mic_required = True |
| 882 |
return False |
| 883 |
|
| 884 |
return self._mic_required |
| 885 |
|
| 886 |
@property |
| 887 |
def _seq_num_in(self) -> int: |
| 888 |
if self._context_attr & NegotiateFlags.extended_session_security: |
| 889 |
num = self.__seq_num_in |
| 890 |
self.__seq_num_in += 1 |
| 891 |
|
| 892 |
else: |
| 893 |
num = self.__seq_num_out |
| 894 |
self.__seq_num_out += 1 |
| 895 |
|
| 896 |
return num |
| 897 |
|
| 898 |
@property |
| 899 |
def _seq_num_out(self) -> int: |
| 900 |
num = self.__seq_num_out |
| 901 |
self.__seq_num_out += 1 |
| 902 |
return num |
| 903 |
|
| 904 |
def _calculate_mic( |
| 905 |
self, |
| 906 |
session_key: bytes, |
| 907 |
negotiate: bytes, |
| 908 |
challenge: bytes, |
| 909 |
authenticate: bytes, |
| 910 |
) -> bytes: |
| 911 |
"""Calculates the MIC value for the negotiated context.""" |
| 912 |
return hmac_md5(session_key, negotiate + challenge + authenticate) |
| 913 |
|
| 914 |
def _compute_response( |
| 915 |
self, |
| 916 |
challenge: Challenge, |
| 917 |
credential: _NTLMCredential, |
| 918 |
channel_bindings: typing.Optional[GssChannelBindings], |
| 919 |
) -> typing.Tuple[bytes, bytes, bytes]: |
| 920 |
"""Compute the NT and LM responses and the key exchange key.""" |
| 921 |
client_challenge = os.urandom(8) |
| 922 |
|
| 923 |
if self._nt_v2: |
| 924 |
target_info = challenge.target_info.copy() if challenge.target_info else TargetInfo() |
| 925 |
|
| 926 |
if AvId.timestamp in target_info: |
| 927 |
time = target_info[AvId.timestamp] |
| 928 |
self._mic_required = True |
| 929 |
|
| 930 |
else: |
| 931 |
time = FileTime.now() |
| 932 |
|
| 933 |
# The docs seem to indicate that a 0'd bindings hash means to ignore it but that does not seem to be the |
| 934 |
# case. Instead only add the bindings if they have been specified by the caller. |
| 935 |
if channel_bindings: |
| 936 |
target_info[AvId.channel_bindings] = md5(channel_bindings.pack()) |
| 937 |
target_info[AvId.target_name] = self.spn or "" |
| 938 |
|
| 939 |
if self._mic_required: |
| 940 |
target_info[AvId.flags] = target_info.get(AvId.flags, AvFlags(0)) | AvFlags.mic |
| 941 |
|
| 942 |
ntv2_hash = ntowfv2(credential.username or "", credential.nt_hash, credential.domain) |
| 943 |
nt_challenge, lm_challenge, key_exchange_key = compute_response_v2( |
| 944 |
ntv2_hash, challenge.server_challenge, client_challenge, time, target_info |
| 945 |
) |
| 946 |
|
| 947 |
if self._mic_required: |
| 948 |
lm_challenge = b"\x00" * 24 |
| 949 |
|
| 950 |
return nt_challenge, lm_challenge, key_exchange_key |
| 951 |
|
| 952 |
else: |
| 953 |
return compute_response_v1( |
| 954 |
challenge.flags, |
| 955 |
credential.nt_hash, |
| 956 |
credential.lm_hash, |
| 957 |
challenge.server_challenge, |
| 958 |
client_challenge, |
| 959 |
no_lm_response=not self._lm, |
| 960 |
) |
| 961 |
|
| 962 |
def _reset_ntlm_crypto_state(self, outgoing: bool = True) -> None: |
| 963 |
direction = "out" if outgoing else "in" |
| 964 |
getattr(self, f"_handle_{direction}").reset() |