| 1 |
#!/usr/bin/env python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# PYTHON_ARGCOMPLETE_OK |
| 4 |
|
| 5 |
# Copyright: (c) 2020 Jordan Borean (@jborean93) <[email protected]> |
| 6 |
# MIT License (see LICENSE or https://opensource.org/licenses/MIT) |
| 7 |
|
| 8 |
""" |
| 9 |
Script that can be used to parse a Negotiate token and output a human readable structure. You can pass in an actual |
| 10 |
SPNEGO token or just a raw Kerberos or NTLM token, the script should be smart enough to detect the structure of the |
| 11 |
input. |
| 12 |
""" |
| 13 |
|
| 14 |
import argparse |
| 15 |
import base64 |
| 16 |
import json |
| 17 |
import os.path |
| 18 |
import re |
| 19 |
import struct |
| 20 |
import sys |
| 21 |
import typing |
| 22 |
|
| 23 |
from spnego._asn1 import ( |
| 24 |
unpack_asn1, |
| 25 |
unpack_asn1_object_identifier, |
| 26 |
unpack_asn1_sequence, |
| 27 |
) |
| 28 |
from spnego._context import GSSMech |
| 29 |
from spnego._kerberos import ( |
| 30 |
KerberosV5Msg, |
| 31 |
parse_enum, |
| 32 |
parse_flags, |
| 33 |
parse_kerberos_token, |
| 34 |
) |
| 35 |
from spnego._ntlm_raw.crypto import hmac_md5, ntowfv1, ntowfv2, rc4k |
| 36 |
from spnego._ntlm_raw.messages import ( |
| 37 |
Authenticate, |
| 38 |
AvId, |
| 39 |
Challenge, |
| 40 |
Negotiate, |
| 41 |
NegotiateFlags, |
| 42 |
NTClientChallengeV2, |
| 43 |
TargetInfo, |
| 44 |
Version, |
| 45 |
) |
| 46 |
from spnego._spnego import InitialContextToken, NegTokenInit, NegTokenResp, unpack_token |
| 47 |
from spnego._text import to_bytes |
| 48 |
from spnego._tls_struct import ( |
| 49 |
DistinguishedNameType, |
| 50 |
TlsCipherSuite, |
| 51 |
TlsClientCertificateType, |
| 52 |
TlsCompressionMethod, |
| 53 |
TlsContentType, |
| 54 |
TlsECCurveType, |
| 55 |
TlsECPointFormat, |
| 56 |
TlsExtensionType, |
| 57 |
TlsHandshakeMessageType, |
| 58 |
TlsProtocolVersion, |
| 59 |
TlsPskKeyExchangeMode, |
| 60 |
TlsServerNameType, |
| 61 |
TlsSignatureScheme, |
| 62 |
TlsSupportedGroup, |
| 63 |
) |
| 64 |
|
| 65 |
HAS_ARGCOMPLETE = False |
| 66 |
try: |
| 67 |
import argcomplete |
| 68 |
|
| 69 |
HAS_ARGCOMPLETE = True |
| 70 |
except ImportError: # pragma: nocover |
| 71 |
pass |
| 72 |
|
| 73 |
|
| 74 |
HAS_YAML = False |
| 75 |
try: |
| 76 |
from ruamel import yaml |
| 77 |
|
| 78 |
HAS_YAML = True |
| 79 |
except ImportError: # pragma: nocover |
| 80 |
pass |
| 81 |
|
| 82 |
|
| 83 |
def _parse_ntlm_version( |
| 84 |
version: typing.Optional[Version], |
| 85 |
) -> typing.Optional[typing.Dict[str, typing.Union[int, str]]]: |
| 86 |
if not version: |
| 87 |
return None |
| 88 |
|
| 89 |
return { |
| 90 |
"Major": version.major, |
| 91 |
"Minor": version.minor, |
| 92 |
"Build": version.build, |
| 93 |
"Reserved": base64.b16encode(version.reserved).decode(), |
| 94 |
"NTLMRevision": version.revision, |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
def _parse_ntlm_target_info( |
| 99 |
target_info: typing.Optional[TargetInfo], |
| 100 |
) -> typing.Optional[typing.List[typing.Dict[str, typing.Any]]]: |
| 101 |
if target_info is None: |
| 102 |
return None |
| 103 |
|
| 104 |
text_values = [ |
| 105 |
AvId.nb_computer_name, |
| 106 |
AvId.nb_domain_name, |
| 107 |
AvId.dns_computer_name, |
| 108 |
AvId.dns_domain_name, |
| 109 |
AvId.dns_tree_name, |
| 110 |
AvId.target_name, |
| 111 |
] |
| 112 |
|
| 113 |
info = [] |
| 114 |
for av_id, raw_value in target_info.items(): |
| 115 |
|
| 116 |
if av_id == AvId.eol: |
| 117 |
value = None |
| 118 |
elif av_id in text_values: |
| 119 |
value = raw_value |
| 120 |
elif av_id == AvId.flags: |
| 121 |
value = parse_flags(raw_value) |
| 122 |
elif av_id == AvId.timestamp: |
| 123 |
value = str(raw_value) |
| 124 |
elif av_id == AvId.single_host: |
| 125 |
value = { |
| 126 |
"Size": raw_value.size, |
| 127 |
"Z4": raw_value.z4, |
| 128 |
"CustomData": base64.b16encode(raw_value.custom_data).decode(), |
| 129 |
"MachineId": base64.b16encode(raw_value.machine_id).decode(), |
| 130 |
} |
| 131 |
else: |
| 132 |
value = base64.b16encode(raw_value).decode() |
| 133 |
|
| 134 |
info.append({"AvId": parse_enum(av_id), "Value": value}) |
| 135 |
|
| 136 |
return info |
| 137 |
|
| 138 |
|
| 139 |
def _parse_ntlm_negotiate(data: Negotiate) -> typing.Dict[str, typing.Any]: |
| 140 |
b_data = data.pack() |
| 141 |
|
| 142 |
msg = { |
| 143 |
"NegotiateFlags": parse_flags(data.flags, enum_type=NegotiateFlags), |
| 144 |
"DomainNameFields": { |
| 145 |
"Len": struct.unpack("<H", b_data[16:18])[0], |
| 146 |
"MaxLen": struct.unpack("<H", b_data[18:20])[0], |
| 147 |
"BufferOffset": struct.unpack("<I", b_data[20:24])[0], |
| 148 |
}, |
| 149 |
"WorkstationFields": { |
| 150 |
"Len": struct.unpack("<H", b_data[24:26])[0], |
| 151 |
"MaxLen": struct.unpack("<H", b_data[26:28])[0], |
| 152 |
"BufferOffset": struct.unpack("<I", b_data[28:32])[0], |
| 153 |
}, |
| 154 |
"Version": _parse_ntlm_version(data.version), |
| 155 |
"Payload": { |
| 156 |
"DomainName": data.domain_name, |
| 157 |
"Workstation": data.workstation, |
| 158 |
}, |
| 159 |
} |
| 160 |
|
| 161 |
return msg |
| 162 |
|
| 163 |
|
| 164 |
def _parse_ntlm_challenge(data: Challenge) -> typing.Dict[str, typing.Any]: |
| 165 |
b_data = data.pack() |
| 166 |
|
| 167 |
msg = { |
| 168 |
"TargetNameFields": { |
| 169 |
"Len": struct.unpack("<H", b_data[12:14])[0], |
| 170 |
"MaxLen": struct.unpack("<H", b_data[14:16])[0], |
| 171 |
"BufferOffset": struct.unpack("<I", b_data[16:20])[0], |
| 172 |
}, |
| 173 |
"NegotiateFlags": parse_flags(data.flags, enum_type=NegotiateFlags), |
| 174 |
"ServerChallenge": base64.b16encode(b_data[24:32]).decode(), |
| 175 |
"Reserved": base64.b16encode(b_data[32:40]).decode(), |
| 176 |
"TargetInfoFields": { |
| 177 |
"Len": struct.unpack("<H", b_data[40:42])[0], |
| 178 |
"MaxLen": struct.unpack("<H", b_data[42:44])[0], |
| 179 |
"BufferOffset": struct.unpack("<I", b_data[44:48])[0], |
| 180 |
}, |
| 181 |
"Version": _parse_ntlm_version(data.version), |
| 182 |
"Payload": { |
| 183 |
"TargetName": data.target_name, |
| 184 |
"TargetInfo": _parse_ntlm_target_info(data.target_info), |
| 185 |
}, |
| 186 |
} |
| 187 |
|
| 188 |
return msg |
| 189 |
|
| 190 |
|
| 191 |
def _parse_ntlm_authenticate(data: Authenticate, password: typing.Optional[str]) -> typing.Dict[str, typing.Any]: |
| 192 |
b_data = data.pack() |
| 193 |
|
| 194 |
msg: typing.Dict[str, typing.Any] = { |
| 195 |
"LmChallengeResponseFields": { |
| 196 |
"Len": struct.unpack("<H", b_data[12:14])[0], |
| 197 |
"MaxLen": struct.unpack("<H", b_data[14:16])[0], |
| 198 |
"BufferOffset": struct.unpack("<I", b_data[16:20])[0], |
| 199 |
}, |
| 200 |
"NtChallengeResponseFields": { |
| 201 |
"Len": struct.unpack("<H", b_data[20:22])[0], |
| 202 |
"MaxLen": struct.unpack("<H", b_data[22:24])[0], |
| 203 |
"BufferOffset": struct.unpack("<I", b_data[24:28])[0], |
| 204 |
}, |
| 205 |
"DomainNameFields": { |
| 206 |
"Len": struct.unpack("<H", b_data[28:30])[0], |
| 207 |
"MaxLen": struct.unpack("<H", b_data[30:32])[0], |
| 208 |
"BufferOffset": struct.unpack("<I", b_data[32:36])[0], |
| 209 |
}, |
| 210 |
"UserNameFields": { |
| 211 |
"Len": struct.unpack("<H", b_data[36:38])[0], |
| 212 |
"MaxLen": struct.unpack("<H", b_data[38:40])[0], |
| 213 |
"BufferOffset": struct.unpack("<I", b_data[40:44])[0], |
| 214 |
}, |
| 215 |
"WorkstationFields": { |
| 216 |
"Len": struct.unpack("<H", b_data[44:46])[0], |
| 217 |
"MaxLen": struct.unpack("<H", b_data[46:48])[0], |
| 218 |
"BufferOffset": struct.unpack("<I", b_data[48:52])[0], |
| 219 |
}, |
| 220 |
"EncryptedRandomSessionKeyFields": { |
| 221 |
"Len": struct.unpack("<H", b_data[52:54])[0], |
| 222 |
"MaxLen": struct.unpack("<H", b_data[54:56])[0], |
| 223 |
"BufferOffset": struct.unpack("<I", b_data[56:60])[0], |
| 224 |
}, |
| 225 |
"NegotiateFlags": parse_flags(data.flags, enum_type=NegotiateFlags), |
| 226 |
"Version": _parse_ntlm_version(data.version), |
| 227 |
"MIC": base64.b16encode(data.mic).decode() if data.mic else None, |
| 228 |
"Payload": { |
| 229 |
"LmChallengeResponse": None, |
| 230 |
"NtChallengeResponse": None, |
| 231 |
"DomainName": data.domain_name, |
| 232 |
"UserName": data.user_name, |
| 233 |
"Workstation": data.workstation, |
| 234 |
"EncryptedRandomSessionKey": None, |
| 235 |
}, |
| 236 |
} |
| 237 |
|
| 238 |
key_exchange_key = None |
| 239 |
lm_response_data = data.lm_challenge_response |
| 240 |
nt_response_data = data.nt_challenge_response |
| 241 |
|
| 242 |
if lm_response_data: |
| 243 |
lm_response: typing.Dict[str, typing.Any] = { |
| 244 |
"ResponseType": None, |
| 245 |
"LMProofStr": None, |
| 246 |
} |
| 247 |
|
| 248 |
if not nt_response_data or len(nt_response_data) == 24: |
| 249 |
lm_response["ResponseType"] = "LMv1" |
| 250 |
lm_response["LMProofStr"] = base64.b16encode(lm_response_data).decode() |
| 251 |
|
| 252 |
else: |
| 253 |
lm_response["ResponseType"] = "LMv2" |
| 254 |
lm_response["LMProofStr"] = base64.b16encode(lm_response_data[:16]).decode() |
| 255 |
lm_response["ChallengeFromClient"] = base64.b16encode(lm_response_data[16:]).decode() |
| 256 |
|
| 257 |
msg["Payload"]["LmChallengeResponse"] = lm_response |
| 258 |
|
| 259 |
if nt_response_data: |
| 260 |
nt_response: typing.Dict[str, typing.Any] = { |
| 261 |
"ResponseType": None, |
| 262 |
"NTProofStr": None, |
| 263 |
} |
| 264 |
|
| 265 |
if len(nt_response_data) == 24: |
| 266 |
nt_response["ResponseType"] = "NTLMv1" |
| 267 |
nt_response["NTProofStr"] = base64.b16encode(nt_response_data).decode() |
| 268 |
|
| 269 |
# TODO: need to get a sane way to include the server challenge for ESS KXKEY. |
| 270 |
# if password and lm_response_data: |
| 271 |
# session_base_key = hashlib.new('md4', ntowfv1(password)).digest() |
| 272 |
# lmowf = lmowfv1(password) |
| 273 |
# if data.flags & NegotiateFlags.extended_session_security == 0: |
| 274 |
# key_exchange_key = kxkey(data.flags, session_base_key, lmowf, lm_response_data, b"") |
| 275 |
|
| 276 |
else: |
| 277 |
nt_proof_str = nt_response_data[:16] |
| 278 |
nt_response["ResponseType"] = "NTLMv2" |
| 279 |
nt_response["NTProofStr"] = base64.b16encode(nt_proof_str).decode() |
| 280 |
|
| 281 |
challenge = NTClientChallengeV2.unpack(nt_response_data[16:]) |
| 282 |
b_challenge = nt_response_data[16:] |
| 283 |
|
| 284 |
nt_response["ClientChallenge"] = { |
| 285 |
"RespType": challenge.resp_type, |
| 286 |
"HiRespType": challenge.hi_resp_type, |
| 287 |
"Reserved1": struct.unpack("<H", b_challenge[2:4])[0], |
| 288 |
"Reserved2": struct.unpack("<I", b_challenge[4:8])[0], |
| 289 |
"TimeStamp": str(challenge.time_stamp), |
| 290 |
"ChallengeFromClient": base64.b16encode(challenge.challenge_from_client).decode(), |
| 291 |
"Reserved3": struct.unpack("<I", b_challenge[24:28])[0], |
| 292 |
"AvPairs": _parse_ntlm_target_info(challenge.av_pairs), |
| 293 |
"Reserved4": struct.unpack("<I", b_challenge[-4:])[0], |
| 294 |
} |
| 295 |
|
| 296 |
if password: |
| 297 |
response_key_nt = ntowfv2(msg["Payload"]["UserName"], ntowfv1(password), msg["Payload"]["DomainName"]) |
| 298 |
key_exchange_key = hmac_md5(response_key_nt, nt_proof_str) |
| 299 |
|
| 300 |
msg["Payload"]["NtChallengeResponse"] = nt_response |
| 301 |
|
| 302 |
if data.encrypted_random_session_key: |
| 303 |
msg["Payload"]["EncryptedRandomSessionKey"] = base64.b16encode(data.encrypted_random_session_key).decode() |
| 304 |
|
| 305 |
if data.flags & NegotiateFlags.key_exch and (data.flags & NegotiateFlags.sign or data.flags & NegotiateFlags.seal): |
| 306 |
session_key = None |
| 307 |
if key_exchange_key: |
| 308 |
session_key = rc4k(key_exchange_key, typing.cast(bytes, data.encrypted_random_session_key)) |
| 309 |
|
| 310 |
else: |
| 311 |
session_key = key_exchange_key |
| 312 |
|
| 313 |
msg["SessionKey"] = base64.b16encode(session_key).decode() if session_key else "Failed to derive" |
| 314 |
|
| 315 |
return msg |
| 316 |
|
| 317 |
|
| 318 |
def _parse_spnego_init( |
| 319 |
data: NegTokenInit, |
| 320 |
secret: typing.Optional[str] = None, |
| 321 |
encoding: typing.Optional[str] = None, |
| 322 |
) -> typing.Dict[str, typing.Any]: |
| 323 |
mech_types = [parse_enum(m, enum_type=GSSMech) for m in data.mech_types] if data.mech_types else None |
| 324 |
|
| 325 |
mech_token = None |
| 326 |
if data.mech_token: |
| 327 |
mech_token = parse_token(data.mech_token, secret=secret, encoding=encoding) |
| 328 |
|
| 329 |
encoding = encoding or "utf-8" |
| 330 |
|
| 331 |
msg = { |
| 332 |
"mechTypes": mech_types, |
| 333 |
"reqFlags": parse_flags(data.req_flags) if data.req_flags is not None else None, |
| 334 |
"mechToken": mech_token, |
| 335 |
"mechListMIC": base64.b16encode(data.mech_list_mic).decode() if data.mech_list_mic is not None else None, |
| 336 |
} |
| 337 |
|
| 338 |
if data.hint_name or data.hint_address: |
| 339 |
# This is a NegTokenInit2 structure. |
| 340 |
msg["negHints"] = { |
| 341 |
"hintName": data.hint_name.decode(encoding) if data.hint_name else None, |
| 342 |
"hintAddress": data.hint_address.decode(encoding) if data.hint_address else None, |
| 343 |
} |
| 344 |
|
| 345 |
return msg |
| 346 |
|
| 347 |
|
| 348 |
def _parse_spnego_resp( |
| 349 |
data: NegTokenResp, |
| 350 |
secret: typing.Optional[str] = None, |
| 351 |
encoding: typing.Optional[str] = None, |
| 352 |
) -> typing.Dict[str, typing.Any]: |
| 353 |
supported_mech = parse_enum(data.supported_mech, enum_type=GSSMech) if data.supported_mech else None |
| 354 |
|
| 355 |
response_token = None |
| 356 |
if data.response_token: |
| 357 |
response_token = parse_token(data.response_token, secret=secret, encoding=encoding) |
| 358 |
|
| 359 |
msg = { |
| 360 |
"negState": parse_enum(data.neg_state) if data.neg_state is not None else None, |
| 361 |
"supportedMech": supported_mech, |
| 362 |
"responseToken": response_token, |
| 363 |
"mechListMIC": base64.b16encode(data.mech_list_mic).decode() if data.mech_list_mic is not None else None, |
| 364 |
} |
| 365 |
return msg |
| 366 |
|
| 367 |
|
| 368 |
def _parse_tls_handshake_client_hello( |
| 369 |
view: memoryview, |
| 370 |
) -> typing.Dict[str, typing.Any]: |
| 371 |
protocol_version = TlsProtocolVersion(struct.unpack(">H", view[:2])[0]) |
| 372 |
view = view[2:] |
| 373 |
|
| 374 |
random = view[:32] |
| 375 |
view = view[32:] |
| 376 |
|
| 377 |
session_id_len = struct.unpack("B", view[:1])[0] |
| 378 |
view = view[1:] |
| 379 |
|
| 380 |
session_id = view[:session_id_len] |
| 381 |
view = view[session_id_len:] |
| 382 |
|
| 383 |
cipher_suites_len = struct.unpack(">H", view[:2])[0] |
| 384 |
view = view[2:] |
| 385 |
cipher_suites_view = view[:cipher_suites_len] |
| 386 |
view = view[cipher_suites_len:] |
| 387 |
|
| 388 |
cipher_suites = [] |
| 389 |
while cipher_suites_view: |
| 390 |
cs = TlsCipherSuite(struct.unpack(">H", cipher_suites_view[:2])[0]) |
| 391 |
cipher_suites.append(f"{cs.name} - 0x{cs.value:04X}") |
| 392 |
cipher_suites_view = cipher_suites_view[2:] |
| 393 |
|
| 394 |
compression_methods_len = struct.unpack("B", view[:1])[0] |
| 395 |
view = view[1:] |
| 396 |
compression_methods_view = view[:compression_methods_len] |
| 397 |
view = view[compression_methods_len:] |
| 398 |
|
| 399 |
compression_methods = [] |
| 400 |
while compression_methods_view: |
| 401 |
cm = TlsCompressionMethod(struct.unpack("B", compression_methods_view[:1])[0]) |
| 402 |
compression_methods.append(parse_enum(cm)) |
| 403 |
compression_methods_view = compression_methods_view[1:] |
| 404 |
|
| 405 |
extensions_len = struct.unpack(">H", view[:2])[0] |
| 406 |
view = view[2:] |
| 407 |
extensions_view = view[:extensions_len] |
| 408 |
view = view[extensions_len:] |
| 409 |
extensions = _parse_tls_extensions(extensions_view, True) |
| 410 |
|
| 411 |
return { |
| 412 |
"ProtocolVersion": parse_enum(protocol_version), |
| 413 |
"Random": base64.b16encode(random).decode(), |
| 414 |
"SessionID": base64.b16encode(session_id).decode(), |
| 415 |
"CipherSuites": cipher_suites, |
| 416 |
"CompressionMethods": compression_methods, |
| 417 |
"Extensions": extensions, |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
def _parse_tls_handshake_server_hello( |
| 422 |
view: memoryview, |
| 423 |
) -> typing.Dict[str, typing.Any]: |
| 424 |
protocol_version = TlsProtocolVersion(struct.unpack(">H", view[:2])[0]) |
| 425 |
view = view[2:] |
| 426 |
|
| 427 |
random = view[:32] |
| 428 |
view = view[32:] |
| 429 |
|
| 430 |
session_id_len = struct.unpack("B", view[:1])[0] |
| 431 |
view = view[1:] |
| 432 |
|
| 433 |
session_id = view[:session_id_len] |
| 434 |
view = view[session_id_len:] |
| 435 |
|
| 436 |
cipher_suite = TlsCipherSuite(struct.unpack(">H", view[:2])[0]) |
| 437 |
view = view[2:] |
| 438 |
|
| 439 |
compression_method = TlsCompressionMethod(struct.unpack("B", view[:1])[0]) |
| 440 |
view = view[1:] |
| 441 |
|
| 442 |
extensions_len = struct.unpack(">H", view[:2])[0] |
| 443 |
view = view[2:] |
| 444 |
extensions_view = view[:extensions_len] |
| 445 |
view = view[extensions_len:] |
| 446 |
extensions = _parse_tls_extensions(extensions_view, False) |
| 447 |
|
| 448 |
return { |
| 449 |
"ProtocolVersion": parse_enum(protocol_version), |
| 450 |
"Random": base64.b16encode(random).decode(), |
| 451 |
"SessionID": base64.b16encode(session_id).decode(), |
| 452 |
"CipherSuite": f"{cipher_suite.name} - 0x{cipher_suite.value:04X}", |
| 453 |
"CompressionMethod": parse_enum(compression_method), |
| 454 |
"Extensions": extensions, |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
def _parse_tls_handshake_certificate_request( |
| 459 |
view: memoryview, |
| 460 |
) -> typing.Dict[str, typing.Any]: |
| 461 |
cert_types_len = struct.unpack("B", view[:1])[0] |
| 462 |
view = view[1:] |
| 463 |
cert_types_view = view[:cert_types_len] |
| 464 |
view = view[cert_types_len:] |
| 465 |
|
| 466 |
cert_types = [] |
| 467 |
while cert_types_view: |
| 468 |
ct = TlsClientCertificateType(struct.unpack("B", cert_types_view[:1])[0]) |
| 469 |
cert_types.append(parse_enum(ct)) |
| 470 |
cert_types_view = cert_types_view[1:] |
| 471 |
|
| 472 |
sig_algos_len = struct.unpack(">H", view[:2])[0] |
| 473 |
view = view[2:] |
| 474 |
sig_algos_view = view[:sig_algos_len] |
| 475 |
view = view[sig_algos_len:] |
| 476 |
|
| 477 |
sig_algos = [] |
| 478 |
while sig_algos_view: |
| 479 |
algo = TlsSignatureScheme(struct.unpack(">H", sig_algos_view[:2])[0]) |
| 480 |
sig_algos.append(parse_enum(algo)) |
| 481 |
sig_algos_view = sig_algos_view[2:] |
| 482 |
|
| 483 |
dn_len = struct.unpack(">H", view[:2])[0] |
| 484 |
view = view[2:] |
| 485 |
dn_view = view[:dn_len] |
| 486 |
view = view[dn_len:] |
| 487 |
|
| 488 |
dns = [] |
| 489 |
while dn_view: |
| 490 |
entry_len = struct.unpack(">H", dn_view[:2])[0] |
| 491 |
dn_view = dn_view[2:] |
| 492 |
entry_view = dn_view[:entry_len] |
| 493 |
dn_view = dn_view[entry_len:] |
| 494 |
|
| 495 |
for dn_entry in unpack_asn1_sequence(entry_view): |
| 496 |
for dn_set in unpack_asn1_sequence(dn_entry): |
| 497 |
dn_data = unpack_asn1(dn_set.b_data)[0] |
| 498 |
dn_oid, dn_str = unpack_asn1_sequence(dn_data) |
| 499 |
oid = DistinguishedNameType(unpack_asn1_object_identifier(dn_oid)) |
| 500 |
|
| 501 |
dns.append( |
| 502 |
{ |
| 503 |
"OID": parse_enum(oid), |
| 504 |
"Value": dn_str.b_data.tobytes().decode("utf-8"), |
| 505 |
} |
| 506 |
) |
| 507 |
|
| 508 |
return { |
| 509 |
"CertificateTypes": cert_types, |
| 510 |
"SignatureAlgorithms": sig_algos, |
| 511 |
"CertificateAuthorities": dns, |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
def _parse_tls_handshake_server_key_exchange( |
| 516 |
view: memoryview, |
| 517 |
protocol_version: TlsProtocolVersion, |
| 518 |
) -> typing.Dict[str, typing.Any]: |
| 519 |
curve_type = TlsECCurveType(struct.unpack("B", view[:1])[0]) |
| 520 |
view = view[1:] |
| 521 |
|
| 522 |
curve = TlsSupportedGroup(struct.unpack(">H", view[:2])[0]) |
| 523 |
view = view[2:] |
| 524 |
|
| 525 |
pubkey_len = struct.unpack("B", view[:1])[0] |
| 526 |
view = view[1:] |
| 527 |
|
| 528 |
pubkey = view[:pubkey_len].tobytes() |
| 529 |
view = view[pubkey_len:] |
| 530 |
|
| 531 |
signature_algo = None |
| 532 |
if protocol_version >= TlsProtocolVersion.tls1_2: |
| 533 |
signature_algo = TlsSignatureScheme(struct.unpack(">H", view[:2])[0]) |
| 534 |
view = view[2:] |
| 535 |
|
| 536 |
signature_len = struct.unpack(">H", view[:2])[0] |
| 537 |
view = view[2:] |
| 538 |
|
| 539 |
signature = view[:signature_len].tobytes() |
| 540 |
|
| 541 |
return { |
| 542 |
"CurveType": parse_enum(curve_type), |
| 543 |
"Curve": parse_enum(curve), |
| 544 |
"PublicKey": base64.b16encode(pubkey).decode(), |
| 545 |
"SignatureAlgorithm": parse_enum(signature_algo) if signature_algo else None, |
| 546 |
"Signature": base64.b16encode(signature).decode(), |
| 547 |
} |
| 548 |
|
| 549 |
|
| 550 |
def _parse_tls_extensions( |
| 551 |
view: memoryview, |
| 552 |
is_client_hello: bool, |
| 553 |
) -> typing.List[typing.Dict[str, typing.Any]]: |
| 554 |
extensions = [] |
| 555 |
|
| 556 |
while view: |
| 557 |
ext_type = TlsExtensionType(struct.unpack(">H", view[:2])[0]) |
| 558 |
view = view[2:] |
| 559 |
|
| 560 |
ext_len = struct.unpack(">H", view[:2])[0] |
| 561 |
view = view[2:] |
| 562 |
|
| 563 |
ext_data = view[:ext_len] |
| 564 |
view = view[ext_len:] |
| 565 |
|
| 566 |
data: typing.Any = None |
| 567 |
if ext_len == 0: |
| 568 |
data = None |
| 569 |
|
| 570 |
elif ext_type == TlsExtensionType.server_name: |
| 571 |
data_len = struct.unpack(">H", ext_data[:2])[0] |
| 572 |
data_view = ext_data[2 : 2 + data_len] |
| 573 |
data = [] |
| 574 |
while data_view: |
| 575 |
name_type = TlsServerNameType(struct.unpack("B", data_view[:1])[0]) |
| 576 |
name_len = struct.unpack(">H", data_view[1:3])[0] |
| 577 |
name = data_view[3 : 3 + name_len].tobytes().decode("utf-8") |
| 578 |
data.append( |
| 579 |
{ |
| 580 |
"Type": parse_enum(name_type), |
| 581 |
"Name": name, |
| 582 |
} |
| 583 |
) |
| 584 |
data_view = data_view[3 + name_len :] |
| 585 |
|
| 586 |
elif ext_type == TlsExtensionType.ec_point_formats: |
| 587 |
data_len = struct.unpack("B", ext_data[:1])[0] |
| 588 |
data_view = ext_data[1 : 1 + data_len] |
| 589 |
data = [parse_enum(TlsECPointFormat(b)) for b in list(data_view)] |
| 590 |
|
| 591 |
elif ext_type == TlsExtensionType.supported_groups: |
| 592 |
data_len = struct.unpack(">H", ext_data[:2])[0] |
| 593 |
data_view = ext_data[2 : 2 + data_len] |
| 594 |
data = [] |
| 595 |
while data_view: |
| 596 |
data.append(parse_enum(TlsSupportedGroup(struct.unpack(">H", data_view[:2])[0]))) |
| 597 |
data_view = data_view[2:] |
| 598 |
|
| 599 |
elif ext_type == TlsExtensionType.application_layer_protocol_negotiation: |
| 600 |
data_len = struct.unpack(">H", ext_data[:2])[0] |
| 601 |
data_view = ext_data[2 : 2 + data_len] |
| 602 |
data = [] |
| 603 |
while data_view: |
| 604 |
alpn_len = struct.unpack("B", data_view[:1])[0] |
| 605 |
data_view = data_view[1:] |
| 606 |
data.append(data_view[:alpn_len].tobytes().decode()) |
| 607 |
data_view = data_view[alpn_len:] |
| 608 |
|
| 609 |
elif ext_type == TlsExtensionType.session_ticket: |
| 610 |
data = base64.b16encode(ext_data.tobytes()).decode() |
| 611 |
|
| 612 |
elif ext_type == TlsExtensionType.signature_algorithms: |
| 613 |
data_len = struct.unpack(">H", ext_data[:2])[0] |
| 614 |
data_view = ext_data[2 : 2 + data_len] |
| 615 |
data = [] |
| 616 |
while data_view: |
| 617 |
data.append(parse_enum(TlsSignatureScheme(struct.unpack(">H", data_view[:2])[0]))) |
| 618 |
data_view = data_view[2:] |
| 619 |
|
| 620 |
elif ext_type == TlsExtensionType.supported_versions: |
| 621 |
if is_client_hello: |
| 622 |
data_len = struct.unpack("B", ext_data[:1])[0] |
| 623 |
data_view = ext_data[1 : 1 + data_len] |
| 624 |
data = [] |
| 625 |
while data_view: |
| 626 |
data.append(parse_enum(TlsProtocolVersion(struct.unpack(">H", data_view[:2])[0]))) |
| 627 |
data_view = data_view[2:] |
| 628 |
|
| 629 |
else: |
| 630 |
data = parse_enum(TlsProtocolVersion(struct.unpack(">H", ext_data[:2])[0])) |
| 631 |
|
| 632 |
elif ext_type == TlsExtensionType.psk_key_exchange_modes: |
| 633 |
data_len = struct.unpack("B", ext_data[:1])[0] |
| 634 |
data_view = ext_data[1 : 1 + data_len] |
| 635 |
data = [] |
| 636 |
while data_view: |
| 637 |
data.append(parse_enum(TlsPskKeyExchangeMode(struct.unpack("B", data_view[:1])[0]))) |
| 638 |
data_view = data_view[1:] |
| 639 |
|
| 640 |
elif ext_type == TlsExtensionType.key_share: |
| 641 |
if is_client_hello: |
| 642 |
data_len = struct.unpack(">H", ext_data[:2])[0] |
| 643 |
data_view = ext_data[2 : 2 + data_len] |
| 644 |
data = [] |
| 645 |
while data_view: |
| 646 |
key_share_group = TlsSupportedGroup(struct.unpack(">H", data_view[:2])[0]) |
| 647 |
key_exchange_len = struct.unpack(">H", data_view[2:4])[0] |
| 648 |
key_exchange = data_view[4 : 4 + key_exchange_len].tobytes() |
| 649 |
|
| 650 |
data.append( |
| 651 |
{ |
| 652 |
"Group": parse_enum(key_share_group), |
| 653 |
"Key": base64.b16encode(key_exchange).decode(), |
| 654 |
} |
| 655 |
) |
| 656 |
data_view = data_view[4 + key_exchange_len :] |
| 657 |
else: |
| 658 |
key_share_group = TlsSupportedGroup(struct.unpack(">H", ext_data[:2])[0]) |
| 659 |
key_exchange_len = struct.unpack(">H", ext_data[2:4])[0] |
| 660 |
key_exchange = ext_data[4 : 4 + key_exchange_len].tobytes() |
| 661 |
data = { |
| 662 |
"Group": parse_enum(key_share_group), |
| 663 |
"Key": base64.b16encode(key_exchange).decode(), |
| 664 |
} |
| 665 |
|
| 666 |
formated_data = { |
| 667 |
"ExtensionType": parse_enum(ext_type), |
| 668 |
} |
| 669 |
if data is not None: |
| 670 |
formated_data["Data"] = data |
| 671 |
if data is not None or ext_data: |
| 672 |
formated_data["RawData"] = base64.b16encode(ext_data).decode() |
| 673 |
extensions.append(formated_data) |
| 674 |
|
| 675 |
return extensions |
| 676 |
|
| 677 |
|
| 678 |
def main(args: typing.List[str]) -> None: |
| 679 |
"""Main program entry point.""" |
| 680 |
parsed_args = parse_args(args) |
| 681 |
|
| 682 |
if parsed_args.token: |
| 683 |
b_data = to_bytes(parsed_args.token) |
| 684 |
else: |
| 685 |
if parsed_args.file: |
| 686 |
file_path = os.path.abspath(os.path.expanduser(os.path.expandvars(parsed_args.file))) |
| 687 |
b_file_path = to_bytes(file_path) |
| 688 |
if not os.path.exists(b_file_path): |
| 689 |
raise ValueError("Cannot find file at path '%s'" % file_path) |
| 690 |
|
| 691 |
with open(b_file_path, mode="rb") as fd: |
| 692 |
b_data = fd.read() |
| 693 |
else: |
| 694 |
b_data = sys.stdin.buffer.read() |
| 695 |
|
| 696 |
if re.match(b"^[a-fA-F0-9\\s]+$", b_data): |
| 697 |
# Input data was a hex string. |
| 698 |
b_data = base64.b16decode(re.sub(b"[\\s]", b"", b_data.strip().upper())) |
| 699 |
if re.match(b"^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$", b_data): |
| 700 |
# Input data was a base64 string. |
| 701 |
b_data = base64.b64decode(b_data.strip()) |
| 702 |
|
| 703 |
token_info: typing.Union[typing.List, typing.Dict] |
| 704 |
if re.match(b"[\x00|\x14|\x15|\x16|\x17]\x03[\x01|\x02|\x03]", b_data): |
| 705 |
token_info = parse_tls_token(b_data) |
| 706 |
else: |
| 707 |
token_info = parse_token(b_data, secret=parsed_args.secret, encoding=parsed_args.encoding) |
| 708 |
|
| 709 |
if parsed_args.output_format == "yaml" and HAS_YAML: |
| 710 |
y = yaml.YAML() |
| 711 |
y.default_flow_style = False |
| 712 |
y.dump(token_info, sys.stdout) |
| 713 |
else: |
| 714 |
print(json.dumps(token_info, indent=4)) |
| 715 |
|
| 716 |
|
| 717 |
def parse_args(args: typing.List[str]) -> argparse.Namespace: |
| 718 |
"""Parse and return args.""" |
| 719 |
parser = argparse.ArgumentParser(description="Parse Microsoft authentication tokens into a human readable format.") |
| 720 |
|
| 721 |
data = parser.add_mutually_exclusive_group() |
| 722 |
|
| 723 |
data.add_argument( |
| 724 |
"-t", "--token", dest="token", help="Raw base64 encoded or hex string token as a command line argument." |
| 725 |
) |
| 726 |
|
| 727 |
data.add_argument( |
| 728 |
"-f", |
| 729 |
"--file", |
| 730 |
default="", |
| 731 |
dest="file", |
| 732 |
help="Path to file that contains raw bytes, base64, or hex string of token to parse, Defaults to reading from " |
| 733 |
"stdin if neither -t or -f is specified.", |
| 734 |
) |
| 735 |
|
| 736 |
parser.add_argument( |
| 737 |
"--encoding", |
| 738 |
dest="encoding", |
| 739 |
help="The encoding to use when trying to decode text fields from bytes in tokens that don't have a negotiated " |
| 740 |
"encoding. This defaults to 'windows-1252' for NTLM tokens and 'utf-8' for Kerberos/SPNEGO tokens.", |
| 741 |
) |
| 742 |
|
| 743 |
parser.add_argument( |
| 744 |
"--format", |
| 745 |
"--output-format", |
| 746 |
choices=["json", "yaml"], |
| 747 |
default="json", |
| 748 |
dest="output_format", |
| 749 |
type=lambda s: s.lower(), |
| 750 |
help="Set the output format of the token, default is (json). Using yaml requires the ruamel.yaml Python " |
| 751 |
"library to be installed pip install pyspnego[yaml].", |
| 752 |
) |
| 753 |
|
| 754 |
parser.add_argument( |
| 755 |
"--secret", |
| 756 |
"--password", |
| 757 |
dest="secret", |
| 758 |
default=None, |
| 759 |
help="Optional info that is the secret information for a protocol that can be used to decrypt encrypted " |
| 760 |
"fields and/or derive the unique session key in the exchange. This is currently only supported by NTLM " |
| 761 |
"tokens to generate the session key.", |
| 762 |
) |
| 763 |
|
| 764 |
if HAS_ARGCOMPLETE: |
| 765 |
argcomplete.autocomplete(parser) |
| 766 |
|
| 767 |
parsed_args = parser.parse_args(args) |
| 768 |
|
| 769 |
if parsed_args.output_format == "yaml" and not HAS_YAML: |
| 770 |
raise ValueError("Cannot output as yaml as ruamel.yaml is not installed.") |
| 771 |
|
| 772 |
return parsed_args |
| 773 |
|
| 774 |
|
| 775 |
def parse_token( |
| 776 |
b_data: bytes, |
| 777 |
secret: typing.Optional[str] = None, |
| 778 |
encoding: typing.Optional[str] = None, |
| 779 |
mech: typing.Optional[typing.Union[str, GSSMech]] = None, |
| 780 |
) -> typing.Dict[str, typing.Any]: |
| 781 |
""" |
| 782 |
:param b_data: A byte string of the token to parse. This can be a NTLM or GSSAPI (SPNEGO/Kerberos) token. |
| 783 |
:param secret: The secret data used to decrypt fields and/or derive session keys. |
| 784 |
:param encoding: The encoding to use for token fields that represent text. This is only used for fields where there |
| 785 |
is no negotiation for the encoding of that particular field. Defaults to 'windows-1252' for NTLM and 'utf-8' |
| 786 |
for Kerberos. |
| 787 |
:return: A dict containing the parsed token data. |
| 788 |
""" |
| 789 |
gss_mech: typing.Optional[GSSMech] = None |
| 790 |
if mech and not isinstance(mech, GSSMech): |
| 791 |
gss_mech = GSSMech.from_oid(mech) |
| 792 |
|
| 793 |
try: |
| 794 |
token = unpack_token(b_data, mech=gss_mech, unwrap=True, encoding=encoding) |
| 795 |
except Exception as e: |
| 796 |
return { |
| 797 |
"MessageType": "Unknown - Failed to parse see Data for more details.", |
| 798 |
"Data": "Failed to parse token: %s" % str(e), |
| 799 |
"RawData": base64.b16encode(b_data).decode(), |
| 800 |
} |
| 801 |
|
| 802 |
msg_type = "Unknown" |
| 803 |
data: typing.Union[str, typing.Dict[str, typing.Any]] = "Failed to parse SPNEGO token due to unknown mech type" |
| 804 |
|
| 805 |
# SPNEGO messages. |
| 806 |
if isinstance(token, InitialContextToken): |
| 807 |
msg_type = "SPNEGO InitialContextToken" |
| 808 |
data = { |
| 809 |
"thisMech": parse_enum(token.this_mech, enum_type=GSSMech), |
| 810 |
"innerContextToken": parse_token( |
| 811 |
token.inner_context_token, mech=token.this_mech, secret=secret, encoding=encoding |
| 812 |
), |
| 813 |
} |
| 814 |
|
| 815 |
elif isinstance(token, NegTokenInit): |
| 816 |
data = _parse_spnego_init(token, secret, encoding) |
| 817 |
if "negHints" in data: |
| 818 |
msg_type = "SPNEGO NegTokenInit2" |
| 819 |
|
| 820 |
else: |
| 821 |
msg_type = "SPNEGO NegTokenInit" |
| 822 |
|
| 823 |
elif isinstance(token, NegTokenResp): |
| 824 |
msg_type = "SPNEGO NegTokenResp" |
| 825 |
data = _parse_spnego_resp(token, secret, encoding) |
| 826 |
|
| 827 |
# NTLM messages. |
| 828 |
elif isinstance(token, (Negotiate, Challenge, Authenticate)): |
| 829 |
msg_type = parse_enum(token.MESSAGE_TYPE) |
| 830 |
|
| 831 |
if isinstance(token, Negotiate): |
| 832 |
data = _parse_ntlm_negotiate(token) |
| 833 |
|
| 834 |
elif isinstance(token, Challenge): |
| 835 |
data = _parse_ntlm_challenge(token) |
| 836 |
|
| 837 |
else: |
| 838 |
data = _parse_ntlm_authenticate(token, secret) |
| 839 |
|
| 840 |
# Kerberos messages. |
| 841 |
elif isinstance(token, KerberosV5Msg): |
| 842 |
msg_type = parse_enum(token.MESSAGE_TYPE) |
| 843 |
data = parse_kerberos_token(token, secret, encoding) |
| 844 |
|
| 845 |
return { |
| 846 |
"MessageType": msg_type, |
| 847 |
"Data": data, |
| 848 |
"RawData": base64.b16encode(b_data).decode(), |
| 849 |
} |
| 850 |
|
| 851 |
|
| 852 |
def parse_tls_token( |
| 853 |
b_data: bytes, |
| 854 |
) -> typing.List[typing.Dict[str, typing.Any]]: |
| 855 |
""" |
| 856 |
:param b_data: A byte string of the TLS token to parse. |
| 857 |
:return: A dict containing the parsed TLS token data. |
| 858 |
""" |
| 859 |
view = memoryview(b_data) |
| 860 |
|
| 861 |
res = [] |
| 862 |
while view: |
| 863 |
content_type = TlsContentType(struct.unpack("B", view[:1])[0]) |
| 864 |
protocol_version = TlsProtocolVersion(struct.unpack(">H", view[1:3])[0]) |
| 865 |
token_length = struct.unpack(">H", view[3:5])[0] |
| 866 |
token_view = view[5 : 5 + token_length] |
| 867 |
|
| 868 |
data: typing.Optional[typing.List[typing.Dict[str, typing.Any]]] = None |
| 869 |
if content_type == TlsContentType.handshake: |
| 870 |
data = [] |
| 871 |
|
| 872 |
while token_view: |
| 873 |
handshake_type = TlsHandshakeMessageType(struct.unpack("B", token_view[:1])[0]) |
| 874 |
message_len = struct.unpack(">L", b"\x00" + token_view[1:4])[0] |
| 875 |
handshake_view = token_view[4 : 4 + message_len] |
| 876 |
|
| 877 |
handshake_data: typing.Optional[typing.Dict[str, typing.Any]] = None |
| 878 |
if handshake_type == TlsHandshakeMessageType.client_hello: |
| 879 |
handshake_data = _parse_tls_handshake_client_hello(handshake_view) |
| 880 |
|
| 881 |
elif handshake_type == TlsHandshakeMessageType.server_hello: |
| 882 |
handshake_data = _parse_tls_handshake_server_hello(handshake_view) |
| 883 |
|
| 884 |
elif handshake_type == TlsHandshakeMessageType.certificate: |
| 885 |
cert_len = struct.unpack(">I", b"\x00" + handshake_view[:3])[0] |
| 886 |
handshake_data = { |
| 887 |
"Certificate": base64.b16encode(handshake_view[3 : 3 + cert_len].tobytes()).decode(), |
| 888 |
} |
| 889 |
|
| 890 |
elif handshake_type == TlsHandshakeMessageType.certificate_request: |
| 891 |
handshake_data = _parse_tls_handshake_certificate_request(handshake_view) |
| 892 |
|
| 893 |
elif handshake_type == TlsHandshakeMessageType.server_key_exchange: |
| 894 |
handshake_data = _parse_tls_handshake_server_key_exchange(handshake_view, protocol_version) |
| 895 |
|
| 896 |
elif handshake_type == TlsHandshakeMessageType.client_key_exchange: |
| 897 |
key_len = struct.unpack("B", handshake_view[:1])[0] |
| 898 |
handshake_data = { |
| 899 |
"PublicKey": base64.b16encode(handshake_view[1 : 1 + key_len].tobytes()).decode(), |
| 900 |
} |
| 901 |
|
| 902 |
formatted_handshake_data: typing.Dict[str, typing.Any] = { |
| 903 |
"HandshakeType": parse_enum(handshake_type), |
| 904 |
} |
| 905 |
if handshake_data is not None: |
| 906 |
formatted_handshake_data["Data"] = handshake_data |
| 907 |
formatted_handshake_data["RawData"] = base64.b16encode(token_view[: 4 + message_len].tobytes()).decode() |
| 908 |
data.append(formatted_handshake_data) |
| 909 |
token_view = token_view[4 + message_len :] |
| 910 |
|
| 911 |
formatted_data: typing.Dict[str, typing.Any] = { |
| 912 |
"ContentType": parse_enum(content_type), |
| 913 |
"ProtocolVersion": parse_enum(protocol_version), |
| 914 |
} |
| 915 |
if data is not None: |
| 916 |
formatted_data["Data"] = data |
| 917 |
|
| 918 |
formatted_data["RawData"] = base64.b16encode(view[: 5 + token_length].tobytes()).decode() |
| 919 |
|
| 920 |
res.append(formatted_data) |
| 921 |
view = view[5 + token_length :] |
| 922 |
|
| 923 |
return res |
| 924 |
|
| 925 |
|
| 926 |
if __name__ == "__main__": # pragma: nocover |
| 927 |
main(sys.argv[1:]) |