Debian Codegraph @

tests/integration/templates/test_integration.py.tmpl

raw
1 # Copyright: (c) 2020, Jordan Borean (@jborean93) <[email protected]>
2 # MIT License (see LICENSE or https://opensource.org/licenses/MIT)
3
4 from __future__ import annotations
5
6 import base64
7 import collections
8 import dataclasses
9 import getpass
10 import io
11 import os
12 import pytest
13 import re
14 import requests
15 import sansldap
16 import socket
17 import spnego
18 import spnego.channel_bindings
19 import spnego.iov
20 import spnego.tls
21 import ssl
22 import struct
23 import subprocess
24 import sys
25 import tempfile
26 import typing as t
27 import uuid
28 import warnings
29
30 from cryptography import x509
31 from cryptography.hazmat.backends import default_backend
32 from cryptography.hazmat.primitives import hashes
33 from requests.packages.urllib3.exceptions import InsecureRequestWarning
34 from xml.etree import ElementTree as ET
35
36
37 USERNAME = '{{ domain_upn }}'
38 PASSWORD = '{{ domain_password }}'
39 HOSTNAME = socket.gethostname()
40 HOST_FQDN = '%s.{{ domain_name }}' % HOSTNAME
41 WIN_DC = '{{ groups['win_controller'][0] | lower }}.{{ domain_name | lower }}'
42 WIN_DC_IP = socket.gethostbyname(WIN_DC)
43 WIN_SERVER_UNTRUSTED = '{{ groups["win_children"][0] }}.{{ domain_name }}' # Not trusted for delegation in AD
44 WIN_SERVER_TRUSTED = '{{ groups["win_children"][1] }}.{{ domain_name }}' # Trusted for delegation in AD
45 WIN_SERVER_TRUSTED_IP = socket.gethostbyname(WIN_SERVER_TRUSTED)
46 KERBEROS_PROVIDER = '{{ krb_provider }}'
47
48 IS_SYSTEM = False
49 if os.name == 'nt':
50 current_user = getpass.getuser()
51 IS_SYSTEM = current_user == '%s$' % HOSTNAME
52
53 BIND_TIME_FEATURE_NEGOTIATION = (uuid.UUID("6cb71c2c-9812-4540-0300-000000000000"), 1, 0)
54 EMP = (uuid.UUID("e1af8308-5d1f-11c9-91a4-08002b14a0fa"), 3, 0)
55 ISD_KEY = (uuid.UUID("b9785960-524f-11df-8b6d-83dcded72085"), 1, 0)
56 NDR = (uuid.UUID("8a885d04-1ceb-11c9-9fe8-08002b104860"), 2, 0)
57 NDR64 = (uuid.UUID("71710533-beba-4937-8319-b5dbef9ccc36"), 1, 0)
58
59 SMBHeader = collections.namedtuple('SMBHeader', ['status', 'command', 'session_id', 'data'])
60 SMBNegotiateResponse = collections.namedtuple('SMBNegotiateResponse', ['dialect', 'buffer'])
61 SMBSessionResponse = collections.namedtuple('SMBSessionResponse', ['flags', 'buffer'])
62
63 WSMAN_NS = {
64 "s": "http://www.w3.org/2003/05/soap-envelope",
65 "xs": "http://www.w3.org/2001/XMLSchema",
66 "xsi": "http://www.w3.org/2001/XMLSchema-instance",
67 "wsa": "http://schemas.xmlsoap.org/ws/2004/08/addressing",
68 "wsman": "http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd",
69 "wsmid": "http://schemas.dmtf.org/wbem/wsman/identify/1/wsmanidentity.xsd",
70 "wsmanfault": "http://schemas.microsoft.com/wbem/wsman/1/wsmanfault",
71 "cim": "http://schemas.dmtf.org/wbem/wscim/1/common",
72 "wsmv": "http://schemas.microsoft.com/wbem/wsman/1/wsman.xsd",
73 "cfg": "http://schemas.microsoft.com/wbem/wsman/1/config",
74 "sub": "http://schemas.microsoft.com/wbem/wsman/1/subscription",
75 "rsp": "http://schemas.microsoft.com/wbem/wsman/1/windows/shell",
76 "m": "http://schemas.microsoft.com/wbem/wsman/1/machineid",
77 "cert": "http://schemas.microsoft.com/wbem/wsman/1/config/service/certmapping",
78 "plugin": "http://schemas.microsoft.com/wbem/wsman/1/config/PluginConfiguration",
79 "wsen": "http://schemas.xmlsoap.org/ws/2004/09/enumeration",
80 "wsdl": "http://schemas.xmlsoap.org/wsdl",
81 "wst": "http://schemas.xmlsoap.org/ws/2004/09/transfer",
82 "wsp": "http://schemas.xmlsoap.org/ws/2004/09/policy",
83 "wse": "http://schemas.xmlsoap.org/ws/2004/08/eventing",
84 "i": "http://schemas.microsoft.com/wbem/wsman/1/cim/interactive.xsd",
85 "xml": "http://www.w3.org/XML/1998/namespace",
86 "pwsh": "http://schemas.microsoft.com/powershell",
87 }
88
89
90 class HTTPWinRMAuth(requests.auth.AuthBase):
91
92 def __init__(self, context):
93 self.context = context
94 self.header = None
95
96 if context.protocol == 'credssp':
97 self.valid_protocols = ['CredSSP']
98 else:
99 self.valid_protocols = ['Negotiate', 'Kerberos', 'NTLM']
100 self._regex = re.compile(r'(%s)\s*([^,]*),?' % '|'.join(self.valid_protocols), re.I)
101
102 def __call__(self, request):
103 request.headers['Connection'] = 'Keep-Alive'
104 request.register_hook('response', self.response_hook)
105
106 return request
107
108 def response_hook(self, response, **kwargs):
109 if response.status_code == 401:
110 response = self.handle_401(response, **kwargs)
111
112 return response
113
114 def handle_401(self, response, **kwargs):
115 auth_supported = response.headers.get('www-authenticate', '')
116 matched_protocols = [p for p in self.valid_protocols if p.upper() in auth_supported.upper()]
117 if not matched_protocols:
118 return response
119 self.header = matched_protocols = matched_protocols[0]
120
121 out_token = self.context.step()
122 while not self.context.complete or out_token is not None:
123 response.content
124 response.raw.release_conn()
125 request = response.request.copy()
126
127 auth_header = b'%s %s' % (self.header.encode(), base64.b64encode(out_token))
128 request.headers['Authorization'] = auth_header
129 response = response.connection.send(request, **kwargs)
130
131 in_token = None
132 auth_header = response.headers.get('www-authenticate', '')
133 token_match = self._regex.search(auth_header)
134 if token_match:
135 in_token = token_match.group(2)
136
137 if not in_token:
138 break
139
140 out_token = self.context.step(base64.b64decode(in_token))
141
142 return response
143
144
145 @dataclasses.dataclass(frozen=True)
146 class SecTrailer:
147 type: int
148 level: int
149 pad_length: int
150 context_id: int
151 data: bytes
152
153
154 @dataclasses.dataclass(frozen=True)
155 class Tower:
156 service: t.Tuple[uuid.UUID, int, int]
157 data_rep: t.Tuple[uuid.UUID, int, int]
158 protocol: int
159 port: int
160 addr: int
161
162
163 @dataclasses.dataclass
164 class GetKeyRequest:
165 opnum: int = dataclasses.field(init=False, repr=False, default=0)
166
167 target_sd: bytes
168 root_key_id: t.Optional[uuid.UUID]
169 l0_key_id: int
170 l1_key_id: int
171 l2_key_id: int
172
173 # MS-GKDI 3.1.4.1 GetKey (Opnum 0)
174 # https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-gkdi/4cac87a3-521e-4918-a272-240f8fabed39
175 # HRESULT GetKey(
176 # [in] handle_t hBinding,
177 # [in] ULONG cbTargetSD,
178 # [in] [size_is(cbTargetSD)] [ref] char* pbTargetSD,
179 # [in] [unique] GUID* pRootKeyID,
180 # [in] LONG L0KeyID,
181 # [in] LONG L1KeyID,
182 # [in] LONG L2KeyID,
183 # [out] unsigned long* pcbOut,
184 # [out] [size_is(, *pcbOut)] byte** ppbOut);
185
186 def pack(self) -> bytes:
187 # Strictly speaking it is only 4 bytes but NDR64 needs 8 byte alignment
188 # on the field after.
189 target_sd_len = len(self.target_sd).to_bytes(8, byteorder="little")
190 root_key_id = b"\x00" * 8
191 if self.root_key_id:
192 root_key_id = b"\x00\x00\x02\x00\x00\x00\x00\x00" + self.root_key_id.bytes_le
193
194 return b"".join(
195 [
196 # cbTargetSD
197 target_sd_len,
198 # pbTargetSD - pointer header includes the length + padding
199 target_sd_len,
200 self.target_sd,
201 b"\x00" * (-len(self.target_sd) % 8),
202 # pRootKeyID
203 root_key_id,
204 # L0KeyID
205 self.l0_key_id.to_bytes(4, byteorder="little", signed=True),
206 # L1KeyID
207 self.l1_key_id.to_bytes(4, byteorder="little", signed=True),
208 # L2KeyID
209 self.l2_key_id.to_bytes(4, byteorder="little", signed=True),
210 ]
211 )
212
213 @classmethod
214 def unpack_response(
215 cls,
216 data: t.Union[bytes, bytearray, memoryview],
217 ) -> GroupKeyEnvelope:
218 view = memoryview(data)
219
220 hresult = struct.unpack("<I", view[-4:].tobytes())[0]
221 view = view[:-4]
222 if hresult != 0:
223 raise Exception(f"GetKey failed 0x{hresult:08X}")
224
225 key_length = struct.unpack("<I", view[:4])[0]
226 view = view[8:] # Skip padding as well
227 # Skip the referent id and double up on pointer size
228 key = view[16 : 16 + key_length].tobytes()
229 assert len(key) == key_length
230 # print(f"GetKey Response: {base64.b16encode(data).decode()}")
231
232 return GroupKeyEnvelope.unpack(key)
233
234
235 @dataclasses.dataclass(frozen=True)
236 class GroupKeyEnvelope:
237 # https://winprotocoldoc.blob.core.windows.net/productionwindowsarchives/MS-GKDI/%5bMS-GKDI%5d.pdf
238 # 2.2.4 Group Key Envelope
239 version: int
240 flags: int
241 l0: int
242 l1: int
243 l2: int
244 root_key_identifier: uuid.UUID
245 kdf_algorithm: str
246 kdf_parameters: bytes
247 secret_algorithm: str
248 secret_parameters: bytes
249 private_key_length: int
250 public_key_length: int
251 domain_name: str
252 forest_name: str
253 l1_key: bytes
254 l2_key: bytes
255
256 @property
257 def is_public_key(self) -> bool:
258 return bool(self.flags & 1)
259
260 @classmethod
261 def unpack(
262 cls,
263 data: t.Union[bytes, bytearray, memoryview],
264 ) -> GroupKeyEnvelope:
265 view = memoryview(data)
266
267 version = struct.unpack("<I", view[:4])[0]
268
269 assert view[4:8].tobytes() == b"\x4B\x44\x53\x4B"
270
271 flags = struct.unpack("<I", view[8:12])[0]
272 l0_index = struct.unpack("<I", view[12:16])[0]
273 l1_index = struct.unpack("<I", view[16:20])[0]
274 l2_index = struct.unpack("<I", view[20:24])[0]
275 root_key_identifier = uuid.UUID(bytes_le=view[24:40].tobytes())
276 kdf_algo_len = struct.unpack("<I", view[40:44])[0]
277 kdf_para_len = struct.unpack("<I", view[44:48])[0]
278 sec_algo_len = struct.unpack("<I", view[48:52])[0]
279 sec_para_len = struct.unpack("<I", view[52:56])[0]
280 priv_key_len = struct.unpack("<I", view[56:60])[0]
281 publ_key_len = struct.unpack("<I", view[60:64])[0]
282 l1_key_len = struct.unpack("<I", view[64:68])[0]
283 l2_key_len = struct.unpack("<I", view[68:72])[0]
284 domain_len = struct.unpack("<I", view[72:76])[0]
285 forest_len = struct.unpack("<I", view[76:80])[0]
286 view = view[80:]
287
288 kdf_algo = view[: kdf_algo_len - 2].tobytes().decode("utf-16-le")
289 view = view[kdf_algo_len:]
290
291 kdf_param = view[:kdf_para_len].tobytes()
292 view = view[kdf_para_len:]
293
294 secret_algo = view[: sec_algo_len - 2].tobytes().decode("utf-16-le")
295 view = view[sec_algo_len:]
296
297 secret_param = view[:sec_para_len].tobytes()
298 view = view[sec_para_len:]
299
300 domain = view[: domain_len - 2].tobytes().decode("utf-16-le")
301 view = view[domain_len:]
302
303 forest = view[: forest_len - 2].tobytes().decode("utf-16-le")
304 view = view[forest_len:]
305
306 l1_key = view[:l1_key_len].tobytes()
307 view = view[l1_key_len:]
308
309 l2_key = view[:l2_key_len].tobytes()
310 view = view[l2_key_len:]
311
312 return GroupKeyEnvelope(
313 version=version,
314 flags=flags,
315 l0=l0_index,
316 l1=l1_index,
317 l2=l2_index,
318 root_key_identifier=root_key_identifier,
319 kdf_algorithm=kdf_algo,
320 kdf_parameters=kdf_param,
321 secret_algorithm=secret_algo,
322 secret_parameters=secret_param,
323 private_key_length=priv_key_len,
324 public_key_length=publ_key_len,
325 domain_name=domain,
326 forest_name=forest,
327 l1_key=l1_key,
328 l2_key=l2_key,
329 )
330
331
332 def get_cbt_data(server):
333 cert_pem = ssl.get_server_certificate((server, 5986))
334 b_cert = ssl.PEM_cert_to_DER_cert(cert_pem)
335 cert = x509.load_der_x509_certificate(b_cert, default_backend())
336
337 hash_algorithm = cert.signature_hash_algorithm
338
339 if hash_algorithm.name in ['md5', 'sha1']:
340 digest = hashes.Hash(hashes.SHA256(), default_backend())
341 else:
342 digest = hashes.Hash(hash_algorithm, default_backend())
343
344 digest.update(b_cert)
345 b_cert_hash = digest.finalize()
346
347 b_app_data = b"tls-server-end-point:" + b_cert_hash
348
349 return spnego.channel_bindings.GssChannelBindings(application_data=b_app_data)
350
351
352 def smb_send(smb_socket, message_id, command, data, session_id=0, tree_id=0):
353 header = b"\xFESMB" # ProtocolId
354 header += struct.pack("<H", 64) # StructureSize
355 header += struct.pack("<H", 1) # CreditCharge
356 header += b"\x00\x00\x00\x00" # Status
357 header += struct.pack("<H", command) # Command
358 header += struct.pack("<H", 1) # CreditRequest
359 header += struct.pack("<I", 0) # Flags
360 header += struct.pack("<I", 0) # NextCommand
361 header += struct.pack("<Q", message_id) # MessageId
362 header += b"\x00\x00\x00\x00" # Reserved
363 header += struct.pack("<I", tree_id) # TreeId
364 header += struct.pack("<Q", session_id) # SessionId
365 header += b"\x00" * 16 # Signature
366 header += data
367
368 payload = struct.pack(">I", len(header)) + header
369 smb_socket.send(payload)
370
371 payload_len = struct.unpack(">I", smb_socket.recv(4))[0]
372 payload = smb_socket.recv(payload_len)
373
374 status = struct.unpack("<I", payload[8:12])[0]
375 command = struct.unpack("<H", payload[12:14])[0]
376 session_id = struct.unpack("<Q", payload[40:48])[0]
377 data = payload[64:]
378
379 if status != 0:
380 raise Exception("SMB Exception: 0x{:02X}".format(status))
381
382 if command == 0:
383 data = smb_unpack_negotiate(data)
384
385 elif command == 1:
386 data = smb_unpack_session(data)
387
388 return SMBHeader(status=status, command=command, session_id=session_id, data=data)
389
390
391 def smb_negotiate_request(smb_socket, message_id):
392 negotiate = struct.pack("<H", 36) # StructureSize
393 negotiate += struct.pack("<H", 1) # DialectCount
394 negotiate += struct.pack("<H", 1) # SecurityMode - SMB2_NEGOTIATE_SIGNING_ENABLED
395 negotiate += b"\x00\x00" # Reserved
396 negotiate += struct.pack("<I", 0x00000040) # Capabilities - SMB2_GLOBAL_CAP_ENCRYPTION
397 negotiate += uuid.uuid4().bytes # ClientGuid
398 negotiate += b"\x00\x00\x00\x00\x00\x00\x00\x00"
399 negotiate += struct.pack("<H", 0x0302) # Dialects - [SMB 3.0.2]
400
401 return smb_send(smb_socket, message_id, 0x0000, negotiate)
402
403
404 def smb_unpack_negotiate(data):
405 dialect_revision = struct.unpack("<H", data[4:6])[0]
406 buffer_offset = struct.unpack("<H", data[56:58])[0] - 64
407 buffer_length = struct.unpack("<H", data[58:60])[0]
408 if buffer_length:
409 sec_buffer = data[buffer_offset:buffer_offset + buffer_length]
410
411 else:
412 sec_buffer = None
413
414 return SMBNegotiateResponse(dialect=dialect_revision, buffer=sec_buffer)
415
416
417 def smb_session_setup(smb_socket, message_id, token):
418 setup = struct.pack("<H", 25) # StructureSize
419 setup += b"\x00" # Flags
420 setup += struct.pack("B", 0x01) # SecurityMode - SMB2_NEGOTIATE_SIGNING_ENABLED
421 setup += b"\x00\x00\x00\x00" # Capabilities
422 setup += b"\x00\x00\x00\x00" # Channel
423 setup += struct.pack("<H", 64 + 24) # SecurityBufferOffset
424 setup += struct.pack("<H", len(token)) # SecurityBufferLength
425 setup += b"\x00\x00\x00\x00\x00\x00\x00\x00" # PreviousSessionId
426 setup += token # Buffer
427
428 return smb_send(smb_socket, message_id, 0x0001, setup)
429
430
431 def smb_unpack_session(data):
432 flags = struct.unpack("<H", data[2:4])[0]
433 buffer_offset = struct.unpack("<H", data[4:6])[0] - 64
434 buffer_length = struct.unpack("<H", data[6:8])[0]
435 if buffer_length:
436 sec_buffer = data[buffer_offset:buffer_offset + buffer_length]
437
438 else:
439 sec_buffer = None
440
441 return SMBSessionResponse(flags=flags, buffer=sec_buffer)
442
443
444 def winrm_run(context, expected_header, server, command, arguments=None, ssl=False):
445 http = requests.Session()
446 http.auth = HTTPWinRMAuth(context)
447 warnings.simplefilter('ignore', category=InsecureRequestWarning)
448 http.headers = {
449 'User-Agent': 'pyspnego_client',
450 }
451
452 if ssl:
453 endpoint = 'https://%s:5986/wsman' % server
454 http.verify = False
455
456 else:
457 endpoint = 'http://%s:5985/wsman' % server
458
459 # We need to ensure we have set up the context already so we can start encrypting the data.
460 request = requests.Request('POST', endpoint, data=None)
461 prep_request = http.prepare_request(request)
462 response = http.send(prep_request)
463 response.raise_for_status()
464
465 setattr(http, 'endpoint', endpoint)
466 setattr(http, 'session_id', str(uuid.uuid4()).upper())
467
468 shell_id = wsman_create(http)
469 try:
470 cmd_id = wsman_command(http, shell_id, command, arguments)
471 rc, stdout, stderr = wsman_receive(http, shell_id, cmd_id)
472 wsman_signal(http, shell_id, cmd_id,
473 'http://schemas.microsoft.com/wbem/wsman/1/windows/shell/signal/Terminate')
474
475 if stderr.startswith('#< CLIXML'):
476 # Strip off the '#< CLIXML\r\n' by finding the 2nd index of '<'
477 output = stderr[stderr.index('<', 2):]
478 element = ET.fromstring(output)
479 namespace = element.tag.replace("Objs", "")[1:-1]
480
481 errors = []
482 for error in element.findall("{%s}S[@S='Error']" % namespace):
483 errors.append(error.text)
484
485 stderr = "".join(errors).replace('_x000D_', '\r').replace('_x000A_', '\n')
486
487 # Make sure the protocol authentication protocol was actually tested.
488 assert http.auth.header == expected_header
489
490 return rc, stdout, stderr
491
492 finally:
493 wsman_delete(http, shell_id)
494
495
496 def wsman_command(http, shell_id, command, arguments=None):
497 rsp = WSMAN_NS['rsp']
498
499 command_line = ET.Element('{%s}CommandLine' % rsp)
500 ET.SubElement(command_line, '{%s}Command' % rsp).text = command
501 for argument in arguments or []:
502 ET.SubElement(command_line, '{%s}Arguments' % rsp).text = argument
503
504 command_response = wsman_envelope('http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Command', http,
505 body=command_line, selector_set={'ShellId': shell_id},
506 option_set={'WINRS_SKIP_CMD_SHELL': False})
507
508 return command_response.find('s:Body/rsp:CommandResponse/rsp:CommandId', WSMAN_NS).text
509
510
511 def wsman_create(http):
512 rsp = WSMAN_NS['rsp']
513
514 shell = ET.Element('{%s}Shell' % rsp)
515 ET.SubElement(shell, '{%s}InputStreams' % rsp).text = 'stdin'
516 ET.SubElement(shell, '{%s}OutputStreams' % rsp).text = 'stdout stderr'
517
518 create_response = wsman_envelope('http://schemas.xmlsoap.org/ws/2004/09/transfer/Create', http, body=shell,
519 option_set={'WINRS_CODEPAGE': 65001})
520 return create_response.find('s:Body/rsp:Shell/rsp:ShellId', WSMAN_NS).text
521
522
523 def wsman_delete(http, shell_id):
524 wsman_envelope('http://schemas.xmlsoap.org/ws/2004/09/transfer/Delete', http, selector_set={'ShellId': shell_id})
525
526
527 def wsman_receive(http, shell_id, command_id):
528 rsp = WSMAN_NS['rsp']
529
530 out = {
531 'stdout': io.BytesIO(),
532 'stderr': io.BytesIO(),
533 }
534 while True:
535 receive = ET.Element('{%s}Receive' % rsp)
536 ET.SubElement(receive, '{%s}DesiredStream' % rsp, attrib={'CommandId': command_id}).text = 'stdout stderr'
537
538 receive_response = wsman_envelope('http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Receive', http,
539 body=receive, selector_set={'ShellId': shell_id},
540 option_set={'WSMAN_CMDSHELL_OPTION_KEEPALIVE': True})
541
542 streams = receive_response.findall('s:Body/rsp:ReceiveResponse/rsp:Stream', WSMAN_NS)
543 for stream in streams:
544 if stream.text:
545 b_data = base64.b64decode(stream.text)
546 out[stream.attrib['Name']].write(b_data)
547
548 state = receive_response.find('s:Body/rsp:ReceiveResponse/rsp:CommandState', WSMAN_NS)
549 if state.attrib['State'].endswith('Done'):
550 rc = int(state.find('rsp:ExitCode', WSMAN_NS).text)
551 break
552
553 return rc, out['stdout'].getvalue().decode('utf-8'), out['stderr'].getvalue().decode('utf-8')
554
555
556 def wsman_signal(http, shell_id, command_id, code):
557 rsp = WSMAN_NS['rsp']
558
559 signal = ET.Element('{%s}Signal' % rsp, attrib={'CommandId': command_id})
560 ET.SubElement(signal, '{%s}Code' % rsp).text = code
561 wsman_envelope('http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Signal', http, body=signal,
562 selector_set={'ShellId': shell_id})
563
564
565 def wsman_envelope(action, http, selector_set=None, option_set=None, body=None):
566 s = WSMAN_NS['s']
567 wsa = WSMAN_NS['wsa']
568 wsman = WSMAN_NS['wsman']
569 wsmv = WSMAN_NS['wsmv']
570 xml = WSMAN_NS['xml']
571 understand = '{%s}mustUnderstand' % s
572
573 envelope = ET.Element('{%s}Envelope' % s)
574 header = ET.SubElement(envelope, '{%s}Header' % WSMAN_NS['s'])
575
576 ET.SubElement(header, '{%s}Action' % wsa, attrib={understand: 'true'}).text = action
577 ET.SubElement(header, '{%s}SessionId' % wsmv, attrib={understand: 'false'}).text = 'uuid:%s' % http.session_id.upper()
578 ET.SubElement(header, '{%s}To' % wsa).text = http.endpoint
579 ET.SubElement(header, '{%s}MaxEnvelopeSize' % wsman, attrib={understand: 'true'}).text = '153600'
580 ET.SubElement(header, '{%s}MessageID' % wsa).text = 'uuid:%s' % str(uuid.uuid4()).upper()
581 ET.SubElement(header, '{%s}OperationTimeout' % wsman).text = 'PT30S'
582
583 reply_to = ET.SubElement(header, '{%s}ReplyTo' % wsa)
584 ET.SubElement(
585 reply_to, '{%s}Address' % wsa, attrib={understand: "true"}
586 ).text = 'http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous'
587
588 ET.SubElement(
589 header, '{%s}ResourceURI' % wsman, attrib={understand: 'true'}
590 ).text = 'http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd'
591
592 for e in ['DataLocale', 'Locale']:
593 ET.SubElement(
594 header, '{%s}%s' % (wsmv, e), attrib={understand: 'false', '{%s}lang' % xml: 'en-US'}
595 )
596
597 for set_value, name, option_name in [(selector_set, 'SelectorSet', 'Selector'),
598 (option_set, 'OptionSet', 'Option')]:
599 if not set_value:
600 continue
601
602 set_element = ET.SubElement(header, '{%s}%s' % (wsman, name))
603 if name == 'OptionSet':
604 set_element.attrib = {understand: 'true'}
605
606 for key, value in set_value.items():
607 ET.SubElement(set_element, '{%s}%s' % (wsman, option_name), Name=key).text = str(value)
608
609 envelope_body = ET.SubElement(envelope, '{%s}Body' % s)
610 if body is not None:
611 envelope_body.append(body)
612
613 content = ET.tostring(envelope, encoding='utf-8', method='xml')
614 boundary = 'Encrypted Boundary'
615
616 if http.endpoint.startswith('http://'):
617 auth_protocol = 'CredSSP' if http.auth.context.protocol == 'credssp' else 'SPNEGO'
618 protocol = 'application/HTTP-%s-session-encrypted' % auth_protocol
619
620 max_size = 16384 if auth_protocol == 'CredSSP' else len(content)
621 chunks = [content[i:i + max_size] for i in range(0, len(content), max_size)]
622 encrypted_chunks = []
623 for chunk in chunks:
624 header, wrapped_data, padding_length = http.auth.context.wrap_winrm(chunk)
625 wrapped_data = struct.pack("<i", len(header)) + header + wrapped_data
626 msg_length = str(len(content) + padding_length)
627
628 content = "\r\n".join([
629 '--%s' % boundary,
630 '\tContent-Type: %s' % protocol,
631 '\tOriginalContent: type=application/soap+xml;charset=UTF-8;Length=%s' % msg_length,
632 '--%s' % boundary,
633 '\tContent-Type: application/octet-stream',
634 '',
635 ])
636 encrypted_chunks.append(content.encode() + wrapped_data)
637
638 content_sub_type = 'multipart/encrypted' if len(encrypted_chunks) == 1 \
639 else 'multipart/x-multi-encrypted'
640 content_type = '%s;protocol="%s";boundary="%s"' % (content_sub_type, protocol, boundary)
641 content = b"".join(encrypted_chunks) + ("--%s--\r\n" % boundary).encode()
642
643 else:
644 content_type = 'application/soap+xml;charset=UTF-8'
645
646 headers = {
647 'Content-Length': str(len(content)),
648 'Content-Type': content_type,
649 }
650
651 request = http.prepare_request(requests.Request('POST', http.endpoint, data=content, headers=headers))
652 response = http.send(request)
653 response.raise_for_status()
654 content = response.content
655
656 content_type = response.headers.get('content-type', '')
657 if content_type.startswith('multipart/encrypted;') or content_type.startswith('multipart/x-multi-encrypted;'):
658 boundary = re.search('boundary=[''|\\"](.*)[''|\\"]', content_type).group(1)
659 parts = re.compile((r"--\s*%s\r\n" % re.escape(boundary)).encode()).split(content)
660 parts = list(filter(None, parts))
661
662 content = b""
663 for i in range(0, len(parts), 2):
664 header = parts[i].strip()
665 payload = parts[i + 1]
666
667 expected_length = int(header.split(b"Length=")[1])
668
669 # remove the end MIME block if it exists
670 payload = re.sub((r'--\s*%s--\r\n$' % boundary).encode(), b'', payload)
671
672 wrapped_data = payload.replace(b"\tContent-Type: application/octet-stream\r\n", b"")
673 header_length = struct.unpack("<i", wrapped_data[:4])[0]
674 header = wrapped_data[4:4 + header_length]
675 wrapped_data = wrapped_data[4 + header_length:]
676
677 unwrapped_data = http.auth.context.unwrap_winrm(header, wrapped_data)
678 assert len(unwrapped_data) == expected_length
679 content += unwrapped_data
680
681 return ET.fromstring(content)
682
683
684 def create_pdu(
685 packet_type: int,
686 packet_flags: int,
687 call_id: int,
688 header_data: bytes,
689 *,
690 stub_data: t.Optional[bytes] = None,
691 sec_trailer: t.Optional[SecTrailer] = None,
692 ) -> bytes:
693 # https://pubs.opengroup.org/onlinepubs/9629399/toc.pdf
694 # 12.6.3 Connection-oriented PDU Data Types - PDU Header
695 data = bytearray()
696 data += struct.pack("B", 5) # Version
697 data += struct.pack("B", 0) # Version minor
698 data += struct.pack("B", packet_type)
699 data += struct.pack("B", packet_flags)
700 data += b"\x10\x00\x00\x00" # Data Representation
701 data += b"\x00\x00" # Fragment length - set at the end below
702 data += struct.pack("<H", len(sec_trailer.data) if sec_trailer else 0)
703 data += struct.pack("<I", call_id)
704 data += header_data
705 data += stub_data or b""
706
707 if sec_trailer:
708 data += struct.pack("B", sec_trailer.type)
709 data += struct.pack("B", sec_trailer.level)
710 data += struct.pack("B", sec_trailer.pad_length)
711 data += struct.pack("B", 0) # Auth Rsrvd
712 data += struct.pack("<I", sec_trailer.context_id)
713 data += sec_trailer.data
714
715 memoryview(data)[8:10] = struct.pack("<H", len(data))
716
717 return bytes(data)
718
719
720 def create_bind(
721 service: t.Tuple[uuid.UUID, int, int],
722 syntaxes: t.List[bytes],
723 auth_data: t.Optional[bytes] = None,
724 sign_header: bool = False,
725 auth_level: int = 6,
726 auth_type: int = 9,
727 ) -> bytes:
728 context_header = b"\x00\x00\x01\x00"
729 context_header += service[0].bytes_le
730 context_header += struct.pack("<H", service[1])
731 context_header += struct.pack("<H", service[2])
732 context_data = bytearray()
733 for idx, s in enumerate(syntaxes):
734 offset = len(context_data)
735 context_data += context_header
736 memoryview(context_data)[offset : offset + 2] = struct.pack("<H", idx)
737 context_data += s
738
739 bind_data = bytearray()
740 bind_data += b"\xd0\x16" # Max Xmit Frag
741 bind_data += b"\xd0\x16" # Max Recv Frag
742 bind_data += b"\x00\x00\x00\x00" # Assoc Group
743 bind_data += b"\x03\x00\x00\x00" # Num context items
744 bind_data += context_data
745
746 sec_trailer: t.Optional[SecTrailer] = None
747 if auth_data:
748 sec_trailer = SecTrailer(
749 type=auth_type,
750 level=auth_level,
751 pad_length=0,
752 context_id=0,
753 data=auth_data,
754 )
755
756 return create_pdu(
757 packet_type=11,
758 packet_flags=0x03 | (0x4 if sign_header else 0x0),
759 call_id=1,
760 header_data=bytes(bind_data),
761 sec_trailer=sec_trailer,
762 )
763
764
765 def create_alter_context(
766 service: t.Tuple[uuid.UUID, int, int],
767 token: bytes,
768 sign_header: bool = False,
769 auth_level: int = 6,
770 auth_type: int = 9,
771 ) -> bytes:
772 ctx1 = b"\x01\x00\x01\x00"
773 ctx1 += service[0].bytes_le
774 ctx1 += struct.pack("<H", service[1])
775 ctx1 += struct.pack("<H", service[1])
776 ctx1 += NDR64[0].bytes_le + struct.pack("<H", NDR64[1]) + struct.pack("<H", NDR[2])
777
778 alter_context_data = bytearray()
779 alter_context_data += b"\xd0\x16" # Max Xmit Frag
780 alter_context_data += b"\xd0\x16" # Max Recv Frag
781 alter_context_data += b"\x00\x00\x00\x00" # Assoc Group
782 alter_context_data += b"\x01\x00\x00\x00" # Num context items
783 alter_context_data += ctx1
784
785 auth_data = SecTrailer(
786 type=auth_type,
787 level=auth_level,
788 pad_length=0,
789 context_id=0,
790 data=token,
791 )
792
793 return create_pdu(
794 packet_type=14,
795 packet_flags=0x03 | (0x4 if sign_header else 0x0),
796 call_id=1,
797 header_data=bytes(alter_context_data),
798 sec_trailer=auth_data,
799 )
800
801
802 def create_request(
803 opnum: int,
804 data: bytes,
805 ctx: t.Optional[spnego.ContextProxy] = None,
806 sign_header: bool = False,
807 auth_level: int = 6,
808 auth_type: int = 9,
809 ) -> bytes:
810 # Add Verification trailer to data
811 # MS-RPCE 2.2.2.13 Veritifcation Trailer
812 # https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rpce/0e9fea61-1bff-4478-9bfe-a3b6d8b64ac3
813 if ctx:
814 pcontext = bytearray()
815 pcontext += ISD_KEY[0].bytes_le
816 pcontext += struct.pack("<H", ISD_KEY[1]) + struct.pack("<H", ISD_KEY[2])
817 pcontext += NDR64[0].bytes_le
818 pcontext += struct.pack("<H", NDR64[1]) + struct.pack("<H", NDR64[2])
819
820 verification_trailer = bytearray()
821 verification_trailer += b"\x8a\xe3\x13\x71\x02\xf4\x36\x71" # Signature
822
823 verification_trailer += b"\x02\x40" # Trailer Command - PCONTEXT + End
824 verification_trailer += struct.pack("<H", len(pcontext))
825 verification_trailer += pcontext
826
827 # Verification trailer to added to a 4 byte boundary on the stub data
828 data_padding = -len(data) % 4
829 data += b"\x00" * data_padding
830
831 data += verification_trailer
832 alloc_hint = len(data)
833 auth_padding = -len(data) % 16
834 data += b"\x00" * auth_padding
835
836 else:
837 alloc_hint = len(data)
838
839 request_data = bytearray()
840 request_data += struct.pack("<I", alloc_hint)
841 request_data += struct.pack("<H", 1) # Context id
842 request_data += struct.pack("<H", opnum)
843
844 if ctx:
845 header_length = ctx.query_message_sizes().header
846
847 sec_trailer = SecTrailer(
848 type=auth_type,
849 level=auth_level,
850 pad_length=auth_padding,
851 context_id=0,
852 data=b"\x00" * header_length,
853 )
854 pdu_req = bytearray(
855 create_pdu(
856 packet_type=0,
857 packet_flags=0x03,
858 call_id=1,
859 header_data=bytes(request_data),
860 stub_data=data,
861 sec_trailer=sec_trailer,
862 )
863 )
864
865 sign_type = spnego.iov.BufferType.sign_only if sign_header else spnego.iov.BufferType.data_readonly
866 sec_trailer_data = pdu_req[-(header_length + 8) : -header_length]
867 res = ctx.wrap_iov(
868 [
869 (sign_type, bytes(pdu_req[:24])),
870 data,
871 (sign_type, bytes(sec_trailer_data)),
872 spnego.iov.BufferType.header,
873 ],
874 encrypt=True,
875 qop=None,
876 )
877
878 enc_data = res.buffers[1].data
879 sig = res.buffers[3].data
880
881 data_view = memoryview(pdu_req)
882 data_view[24 : 24 + len(data)] = enc_data
883 data_view[-header_length:] = sig
884
885 return bytes(pdu_req)
886
887 else:
888 return create_pdu(
889 packet_type=0,
890 packet_flags=0x03,
891 call_id=1,
892 header_data=bytes(request_data),
893 stub_data=data,
894 )
895
896
897 def get_fault_pdu_error(data: memoryview) -> int:
898 status = struct.unpack("<I", data[24:28])[0]
899
900 return status
901
902
903 def parse_bind_ack(data: bytes) -> t.Optional[bytes]:
904 view = memoryview(data)
905
906 pkt_type = struct.unpack("B", view[2:3])[0]
907 if pkt_type == 3:
908 err = get_fault_pdu_error(view)
909 raise Exception(f"Receive Fault PDU: 0x{err:08X}")
910
911 assert pkt_type == 12
912
913 auth_length = struct.unpack("<H", view[10:12])[0]
914 if auth_length:
915 auth_blob = view[-auth_length:].tobytes()
916
917 return auth_blob
918
919 else:
920 return None
921
922
923 def parse_alter_context(data: bytes) -> bytes:
924 view = memoryview(data)
925
926 pkt_type = struct.unpack("B", view[2:3])[0]
927 if pkt_type == 3:
928 err = get_fault_pdu_error(view)
929 raise Exception(f"Receive Fault PDU: 0x{err:08X}")
930
931 assert pkt_type == 15
932
933 auth_length = struct.unpack("<H", view[10:12])[0]
934 if auth_length:
935 auth_blob = view[-auth_length:].tobytes()
936
937 return auth_blob
938 else:
939 return b""
940
941
942 def parse_response(
943 data: bytes,
944 ctx: t.Optional[spnego.ContextProxy] = None,
945 sign_header: bool = False,
946 ) -> bytes:
947 view = memoryview(data)
948
949 pkt_type = struct.unpack("B", view[2:3])[0]
950 if pkt_type == 3: # False
951 err = get_fault_pdu_error(view)
952 raise Exception(f"Receive Fault PDU: 0x{err:08X}")
953
954 assert pkt_type == 2
955 frag_length = struct.unpack("<H", view[8:10])[0]
956 auth_length = struct.unpack("<H", view[10:12])[0]
957
958 assert len(view) == frag_length
959 if auth_length:
960 auth_data = view[-(auth_length + 8) :]
961 stub_data = view[24 : len(view) - (auth_length + 8)]
962 padding = struct.unpack("B", auth_data[2:3])[0]
963
964 else:
965 auth_data = memoryview(b"")
966 stub_data = view[24:]
967 padding = 0
968
969 if ctx:
970 sign_type = spnego.iov.BufferType.sign_only if sign_header else spnego.iov.BufferType.data_readonly
971 res = ctx.unwrap_iov(
972 [
973 (sign_type, data[:24]),
974 stub_data.tobytes(),
975 (sign_type, auth_data[:8].tobytes()),
976 (spnego.iov.BufferType.header, auth_data[8:].tobytes()),
977 ],
978 )
979 decrypted_stub = res.buffers[1].data
980
981 return decrypted_stub[: len(decrypted_stub) - padding]
982
983 else:
984 return stub_data.tobytes()
985
986
987 def create_ept_map_request(
988 service: t.Tuple[uuid.UUID, int, int],
989 data_rep: t.Tuple[uuid.UUID, int, int],
990 protocol: int = 0x0B, # TCP/IP
991 port: int = 135,
992 address: int = 0,
993 ) -> t.Tuple[int, bytes]:
994 # MS-RPCE 2.2.1.2.5 ept_map Method
995 # https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rpce/ab744583-430e-4055-8901-3c6bc007e791
996 # void ept_map(
997 # [in] handle_t hEpMapper,
998 # [in, ptr] UUID* obj,
999 # [in, ptr] twr_p_t map_tower,
1000 # [in, out] ept_lookup_handle_t* entry_handle,
1001 # [in, range(0,500)] unsigned long max_towers,
1002 # [out] unsigned long* num_towers,
1003 # [out, ptr, size_is(max_towers), length_is(*num_towers)]
1004 # twr_p_t* ITowers,
1005 # [out] error_status* status
1006 # );
1007 def build_floor(protocol: int, lhs: bytes, rhs: bytes) -> bytes:
1008 data = bytearray()
1009 data += struct.pack("<H", len(lhs) + 1)
1010 data += struct.pack("B", protocol)
1011 data += lhs
1012 data += struct.pack("<H", len(rhs))
1013 data += rhs
1014
1015 return bytes(data)
1016
1017 floors: t.List[bytes] = [
1018 build_floor(
1019 protocol=0x0D,
1020 lhs=service[0].bytes_le + struct.pack("<H", service[1]),
1021 rhs=struct.pack("<H", service[2]),
1022 ),
1023 build_floor(
1024 protocol=0x0D,
1025 lhs=data_rep[0].bytes_le + struct.pack("<H", data_rep[1]),
1026 rhs=struct.pack("<H", data_rep[2]),
1027 ),
1028 build_floor(protocol=protocol, lhs=b"", rhs=b"\x00\x00"),
1029 build_floor(protocol=0x07, lhs=b"", rhs=struct.pack(">H", port)),
1030 build_floor(protocol=0x09, lhs=b"", rhs=struct.pack(">I", address)),
1031 ]
1032
1033 tower = bytearray()
1034 tower += struct.pack("<H", len(floors))
1035 for f in floors:
1036 tower += f
1037 tower_padding = -(len(tower) + 4) % 8
1038
1039 data = bytearray()
1040 data += b"\x01" + (b"\x00" * 23) # Blank UUID pointer with referent id 1
1041 data += b"\x02\x00\x00\x00\x00\x00\x00\x00" # Tower referent id 2
1042 data += struct.pack("<Q", len(tower))
1043 data += struct.pack("<I", len(tower))
1044 data += tower
1045 data += b"\x00" * tower_padding
1046 data += b"\x00" * 20 # Context handle
1047 data += struct.pack("<I", 4) # Max towers
1048
1049 return 3, bytes(data)
1050
1051
1052 def parse_ept_map_response(data: bytes) -> t.List[Tower]:
1053 def unpack_floor(view: memoryview) -> t.Tuple[int, int, memoryview, memoryview]:
1054 lhs_len = struct.unpack("<H", view[:2])[0]
1055 proto = view[2]
1056 lhs = view[3 : lhs_len + 2]
1057 offset = lhs_len + 2
1058
1059 rhs_len = struct.unpack("<H", view[offset : offset + 2])[0]
1060 rhs = view[offset + 2 : offset + rhs_len + 2]
1061
1062 return offset + rhs_len + 2, proto, lhs, rhs
1063
1064 view = memoryview(data)
1065
1066 return_code = struct.unpack("<I", view[-4:])[0]
1067 assert return_code == 0
1068 num_towers = struct.unpack("<I", view[20:24])[0]
1069 # tower_max_count = struct.unpack("<Q", view[24:32])[0]
1070 # tower_offset = struct.unpack("<Q", view[32:40])[0]
1071 tower_count = struct.unpack("<Q", view[40:48])[0]
1072
1073 tower_data_offset = 8 * tower_count # Ignore referent ids
1074 view = view[48 + tower_data_offset :]
1075 towers: t.List[Tower] = []
1076 for _ in range(tower_count):
1077 tower_length = struct.unpack("<Q", view[:8])[0]
1078 padding = -(tower_length + 4) % 8
1079 floor_len = struct.unpack("<H", view[12:14])[0]
1080 assert floor_len == 5
1081 view = view[14:]
1082
1083 offset, proto, lhs, rhs = unpack_floor(view)
1084 view = view[offset:]
1085 assert proto == 0x0D
1086 service = (
1087 uuid.UUID(bytes_le=lhs[:16].tobytes()),
1088 struct.unpack("<H", lhs[16:])[0],
1089 struct.unpack("<H", rhs)[0],
1090 )
1091
1092 offset, proto, lhs, rhs = unpack_floor(view)
1093 view = view[offset:]
1094 assert proto == 0x0D
1095 data_rep = (
1096 uuid.UUID(bytes_le=lhs[:16].tobytes()),
1097 struct.unpack("<H", lhs[16:])[0],
1098 struct.unpack("<H", rhs)[0],
1099 )
1100
1101 offset, protocol, _, _ = unpack_floor(view)
1102 view = view[offset:]
1103 assert protocol == 0x0B
1104
1105 offset, proto, lhs, rhs = unpack_floor(view)
1106 view = view[offset:]
1107 assert proto == 0x07
1108 port = struct.unpack(">H", rhs)[0]
1109
1110 offset, proto, lhs, rhs = unpack_floor(view)
1111 view = view[offset:]
1112 assert proto == 0x09
1113 addr = struct.unpack(">I", rhs)[0]
1114
1115 towers.append(
1116 Tower(
1117 service=service,
1118 data_rep=data_rep,
1119 protocol=protocol,
1120 port=port,
1121 addr=addr,
1122 )
1123 )
1124 view = view[padding:]
1125
1126 assert len(towers) == num_towers
1127
1128 return towers
1129
1130
1131 def rpc_get_key(
1132 dc: str,
1133 ctx: spnego.ContextProxy,
1134 target_sd: bytes,
1135 root_key_id: t.Optional[uuid.UUID],
1136 l0: int,
1137 l1: int,
1138 l2: int,
1139 auth_level: str = "privacy_integrity",
1140 auth_protocol: str = "negotiate",
1141 ) -> GroupKeyEnvelope:
1142 sign_header = False
1143 if auth_level == "privacy_integrity":
1144 sign_header = True
1145 auth_level = "privacy"
1146
1147 auth_level = {
1148 "none": 0, # RPC_C_AUTHN_LEVEL_DEFAULT
1149 "integrity": 5, # RPC_C_AUTHN_LEVEL_PKT_INTEGRITY
1150 "privacy": 6, # RPC_C_AUTHN_LEVEL_PKT_PRIVACY
1151 }[auth_level]
1152 auth_type = {
1153 "negotiate": 0x09, # RPC_C_AUTHN_GSS_NEGOTIATE
1154 "ntlm": 0x0A, # RPC_C_AUTHN_WINNT
1155 "kerberos": 0x10, # RPC_C_AUTHN_GSS_KERBEROS
1156 }[auth_protocol]
1157 bind_syntaxes = [
1158 NDR[0].bytes_le + struct.pack("<H", NDR[1]) + struct.pack("<H", NDR[2]),
1159 NDR64[0].bytes_le + struct.pack("<H", NDR64[1]) + struct.pack("<H", NDR64[2]),
1160 BIND_TIME_FEATURE_NEGOTIATION[0].bytes_le
1161 + struct.pack("<H", BIND_TIME_FEATURE_NEGOTIATION[1])
1162 + struct.pack("<H", BIND_TIME_FEATURE_NEGOTIATION[2]),
1163 ]
1164
1165 # Find the dynamic endpoint port for the ISD service.
1166 with socket.create_connection((dc, 135)) as s:
1167 bind_data = create_bind(
1168 EMP,
1169 bind_syntaxes,
1170 sign_header=False,
1171 )
1172 s.sendall(bind_data)
1173 resp = s.recv(4096)
1174 parse_bind_ack(resp)
1175
1176 opnum, map_request = create_ept_map_request(ISD_KEY, NDR)
1177 request = create_request(
1178 opnum,
1179 map_request,
1180 )
1181 s.sendall(request)
1182 resp = s.recv(4096)
1183
1184 ept_response = parse_response(resp)
1185 isd_towers = parse_ept_map_response(ept_response)
1186 assert len(isd_towers) > 0
1187 isd_port = isd_towers[0].port
1188
1189 out_token = ctx.step()
1190 assert out_token
1191
1192 with socket.create_connection((dc, isd_port)) as s:
1193 bind_data = create_bind(
1194 ISD_KEY,
1195 bind_syntaxes,
1196 auth_data=out_token,
1197 sign_header=sign_header,
1198 auth_level=auth_level,
1199 auth_type=auth_type,
1200 )
1201
1202 s.sendall(bind_data)
1203 resp = s.recv(4096)
1204 in_token = parse_bind_ack(resp)
1205
1206 out_token = ctx.step(in_token)
1207
1208 while out_token:
1209 # For odd numbered legs we should be sending the MS-RPCE rpc_auth3
1210 # PDU. This doesn't seem to be necessary in testing though and
1211 # doing so just adds more complication, use the alter context PDU
1212 # like normal.
1213 out_data = create_alter_context(
1214 ISD_KEY,
1215 out_token,
1216 sign_header=sign_header,
1217 auth_level=auth_level,
1218 auth_type=auth_type,
1219 )
1220 s.sendall(out_data)
1221 resp = s.recv(4096)
1222 in_token = parse_alter_context(resp)
1223
1224 if in_token:
1225 out_token = ctx.step(in_token)
1226 else:
1227 out_token = None
1228
1229 assert ctx.complete
1230 assert not out_token
1231
1232 get_key_req = GetKeyRequest(target_sd, root_key_id, l0, l1, l2)
1233 request = create_request(
1234 get_key_req.opnum,
1235 get_key_req.pack(),
1236 ctx=ctx,
1237 sign_header=sign_header,
1238 auth_level=auth_level,
1239 auth_type=auth_type,
1240 )
1241 s.sendall(request)
1242 resp = s.recv(4096)
1243
1244 create_key_resp = parse_response(resp, ctx=ctx, sign_header=sign_header)
1245 return GetKeyRequest.unpack_response(create_key_resp)
1246
1247
1248 @pytest.mark.parametrize('service', ['HTTP', 'cifs'])
1249 def test_kerberos_authentication(service, monkeypatch):
1250 if os.name != 'nt':
1251 monkeypatch.setenv('KRB5_KTNAME', '/etc/%s.keytab' % service)
1252
1253 else:
1254 if not IS_SYSTEM:
1255 pytest.skip("Cannot run Kerberos server tests when not running as SYSTEM")
1256
1257 c = spnego.client(USERNAME, PASSWORD, hostname=HOST_FQDN, service=service)
1258 s = spnego.server()
1259
1260 in_token = None
1261 while not c.complete:
1262 out_token = c.step(in_token)
1263 if not out_token:
1264 break
1265
1266 in_token = s.step(out_token)
1267
1268 if os.name == 'nt':
1269 # SSPI will report the user in the Netlogon form, use PowerShell to convert the UPN to the Netlogon user.
1270 pwsh_command = base64.b64encode('''
1271 $username = '{0}'
1272 ([Security.Principal.NTAccount]$username).Translate(
1273 [Security.Principal.SecurityIdentifier]
1274 ).Translate([Security.Principal.NTAccount]).Value
1275 '''.format(USERNAME).encode('utf-16-le')).decode()
1276 expected_username = subprocess.Popen(['powershell', '-EncodedCommand', pwsh_command],
1277 stdout=subprocess.PIPE).communicate()[0].strip().decode()
1278
1279 else:
1280 expected_username = USERNAME
1281
1282 assert s.client_principal == expected_username
1283 assert s.session_key == c.session_key
1284 assert c.negotiated_protocol == 'kerberos'
1285 assert s.negotiated_protocol == 'kerberos'
1286
1287 plaintext = os.urandom(16)
1288 c_iov_res = c.wrap_iov([spnego.iov.BufferType.header, plaintext, spnego.iov.BufferType.padding])
1289 assert c_iov_res.encrypted
1290 assert len(c_iov_res.buffers) == 3
1291 assert c_iov_res.buffers[1].data != plaintext
1292
1293 s_iov_res = s.unwrap_iov(c_iov_res.buffers)
1294 assert s_iov_res.encrypted
1295 assert s_iov_res.qop == 0
1296 assert len(s_iov_res.buffers) == 3
1297 assert s_iov_res.buffers[1].data == plaintext
1298
1299 plaintext = os.urandom(16)
1300 s_iov_res = s.wrap_iov([spnego.iov.BufferType.header, plaintext, spnego.iov.BufferType.padding])
1301 assert s_iov_res.encrypted
1302 assert len(s_iov_res.buffers) == 3
1303 assert s_iov_res.buffers[1].data != plaintext
1304
1305 c_iov_res = c.unwrap_iov(s_iov_res.buffers)
1306 assert c_iov_res.encrypted
1307 assert c_iov_res.qop == 0
1308 assert len(c_iov_res.buffers) == 3
1309 assert c_iov_res.buffers[1].data == plaintext
1310
1311
1312 @pytest.mark.skipif(os.name == 'nt', reason='Windows doesnt support keytabs')
1313 @pytest.mark.parametrize('protocol, set_principal', [
1314 ('kerberos', False),
1315 ('kerberos', True),
1316 ('negotiate', False),
1317 ('negotiate', True),
1318 ])
1319 def test_kerberos_authentication_with_keytab(protocol, set_principal, monkeypatch):
1320 monkeypatch.setenv('KRB5_KTNAME', '/etc/HTTP.keytab')
1321
1322 auth_kwargs = {}
1323 if protocol != 'negotiate':
1324 auth_kwargs['protocol'] = protocol
1325
1326 if set_principal:
1327 keytab = spnego.KerberosKeytab(keytab=os.path.expanduser('~/user.keytab'), principal=USERNAME)
1328 else:
1329 keytab = spnego.KerberosKeytab(keytab=os.path.expanduser('~/user.keytab'))
1330
1331 c = spnego.client(keytab, hostname=HOST_FQDN, service='HTTP', **auth_kwargs)
1332 s = spnego.server(**auth_kwargs)
1333
1334 in_token = None
1335 while not c.complete:
1336 out_token = c.step(in_token)
1337 if not out_token:
1338 break
1339
1340 in_token = s.step(out_token)
1341
1342 assert s.client_principal == USERNAME
1343 assert s.session_key == c.session_key
1344 assert c.negotiated_protocol == 'kerberos'
1345 assert s.negotiated_protocol == 'kerberos'
1346
1347
1348 @pytest.mark.parametrize('protocol, use_cache_cred', [
1349 ('kerberos', False),
1350 ('kerberos', True),
1351 ('negotiate', False),
1352 ('negotiate', True),
1353 ])
1354 @pytest.mark.skipif(os.name == 'nt', reason='Due to how its run this doesnt really work for Windows')
1355 def test_kerberos_authentication_with_cache(protocol, use_cache_cred, monkeypatch, tmp_path):
1356 cred = None
1357
1358 monkeypatch.setenv('KRB5_KTNAME', '/etc/HTTP.keytab')
1359
1360 kinit_args = ['kinit']
1361 config = {}
1362 if KERBEROS_PROVIDER == 'Heimdal':
1363 kinit_args.append('--password-file=STDIN')
1364
1365 kinit_args.append(USERNAME)
1366
1367 tmp_ccache = tmp_path / 'test_ccache'
1368 open(tmp_ccache, mode='wb').close()
1369 kinit_env = os.environ.copy()
1370 kinit_env['KRB5CCNAME'] = tmp_ccache
1371
1372 if use_cache_cred:
1373 cred = spnego.KerberosCCache(ccache=tmp_ccache)
1374
1375 else:
1376 monkeypatch.setenv('KRB5CCNAME', str(tmp_ccache))
1377 kinit_env['KRB5CCNAME'] = tmp_ccache
1378
1379 process = subprocess.Popen(kinit_args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
1380 env=kinit_env)
1381 stdout, stderr = process.communicate(PASSWORD.encode() + b'\n')
1382 if process.returncode != 0:
1383 raise Exception("Failed to get Kerberos credential %d: %s %s" % (process.returncode, stdout, stderr))
1384
1385 auth_kwargs = {}
1386 if protocol != 'negotiate':
1387 auth_kwargs['protocol'] = protocol
1388
1389 c = spnego.client(cred, hostname=HOST_FQDN, service='HTTP', **auth_kwargs)
1390 s = spnego.server(**auth_kwargs)
1391
1392 in_token = None
1393 while not c.complete:
1394 out_token = c.step(in_token)
1395 if not out_token:
1396 break
1397
1398 in_token = s.step(out_token)
1399
1400 assert s.client_principal == USERNAME
1401 assert s.session_key == c.session_key
1402 assert c.negotiated_protocol == 'kerberos'
1403 assert s.negotiated_protocol == 'kerberos'
1404
1405
1406 @pytest.mark.skipif(KERBEROS_PROVIDER != 'MIT', reason="Cannot test on Windows and Heimdal does not work")
1407 def test_winrm_rc4_wrapping(monkeypatch):
1408 context_kwargs = {
1409 'hostname': WIN_SERVER_TRUSTED,
1410 'service': 'HTTP',
1411 }
1412 if os.name != 'nt' or IS_SYSTEM:
1413 context_kwargs['username'] = USERNAME
1414 context_kwargs['password'] = PASSWORD
1415
1416 with tempfile.NamedTemporaryFile() as temp_cfg:
1417 if KERBEROS_PROVIDER == 'Heimdal':
1418 temp_cfg.write(b'''[libdefaults]
1419 default_etypes = arcfour-hmac-md5''')
1420
1421 else:
1422 temp_cfg.write(b'''[libdefaults]
1423 default_tkt_enctypes = arcfour-hmac-md5
1424 default_tgs_enctypes = arcfour-hmac-md5''')
1425
1426 temp_cfg.flush()
1427
1428 monkeypatch.setenv('KRB5_CONFIG', '%s:/etc/krb5.conf' % temp_cfg.name)
1429 c = spnego.client(**context_kwargs)
1430 winrm_run(c, 'Negotiate', WIN_SERVER_TRUSTED, 'hostname.exe')
1431
1432
1433 def test_winrm_no_delegate():
1434 context_kwargs = {
1435 'hostname': WIN_SERVER_TRUSTED,
1436 'service': 'HTTP',
1437 }
1438 if os.name != 'nt' or IS_SYSTEM:
1439 context_kwargs['username'] = USERNAME
1440 context_kwargs['password'] = PASSWORD
1441
1442 c = spnego.client(**context_kwargs)
1443
1444 pwsh_script = base64.b64encode('''$ErrorActionPreference = 'Stop'
1445 (Get-Item -LiteralPath \\\\{0}\\c$\\temp\\test_integration.py).FullName
1446 '''.format(WIN_SERVER_UNTRUSTED).encode('utf-16-le')).decode()
1447 rc, stdout, stderr = winrm_run(c, 'Negotiate', WIN_SERVER_TRUSTED, 'powershell.exe',
1448 ['-EncodedCommand', pwsh_script])
1449
1450 # Without the delegate flag being set in the context we will fail to access any network paths
1451 assert rc == 1
1452 assert stdout == ''
1453 assert 'Access is denied' in stderr
1454
1455
1456 def test_winrm_delegate():
1457 context_kwargs = {
1458 'hostname': WIN_SERVER_TRUSTED,
1459 'service': 'HTTP',
1460 'context_req': spnego.ContextReq.default | spnego.ContextReq.delegate,
1461 }
1462 if os.name != 'nt' or IS_SYSTEM:
1463 context_kwargs['username'] = USERNAME
1464 context_kwargs['password'] = PASSWORD
1465
1466 c = spnego.client(**context_kwargs)
1467
1468 pwsh_script = base64.b64encode('''$ErrorActionPreference = 'Stop'
1469 (Get-Item -LiteralPath \\\\{0}\\c$\\temp\\test_integration.py).FullName
1470 '''.format(WIN_SERVER_UNTRUSTED).encode('utf-16-le')).decode()
1471 rc, stdout, stderr = winrm_run(c, 'Negotiate', WIN_SERVER_TRUSTED, 'powershell.exe',
1472 ['-EncodedCommand', pwsh_script])
1473
1474 assert rc == 0
1475 assert stdout.lower().strip() == '\\\\{0}\\c$\\temp\\test_integration.py'.format(WIN_SERVER_UNTRUSTED).lower()
1476 assert stderr == ''
1477
1478
1479 @pytest.mark.skipif(os.name == 'nt', reason='Windows always sets ok as delegate so nothing to test there')
1480 def test_winrm_ok_as_delegate():
1481 # In the environment setup, this host is not trusted for delegation in AD. This test shows that this flag isn't
1482 # used for GSSAPI unless set by the krb5.conf file.
1483 context_kwargs = {
1484 'hostname': WIN_SERVER_UNTRUSTED,
1485 'service': 'HTTP',
1486 'context_req': spnego.ContextReq.default | spnego.ContextReq.delegate,
1487 'username': USERNAME,
1488 'password': PASSWORD,
1489 }
1490
1491 c = spnego.client(**context_kwargs)
1492
1493 pwsh_script = base64.b64encode('''$ErrorActionPreference = 'Stop'
1494 (Get-Item -LiteralPath \\\\{0}\\c$\\temp).FullName
1495 '''.format(WIN_SERVER_TRUSTED).encode('utf-16-le')).decode()
1496 rc, stdout, stderr = winrm_run(c, 'Negotiate', WIN_SERVER_UNTRUSTED, 'powershell.exe',
1497 ['-EncodedCommand', pwsh_script])
1498
1499 assert rc == 0
1500 assert stdout.lower().strip() == '\\\\{0}\\c$\\temp'.format(WIN_SERVER_TRUSTED).lower()
1501 assert stderr == ''
1502
1503
1504 @pytest.mark.parametrize('option', [
1505 spnego.NegotiateOptions.none,
1506 spnego.NegotiateOptions.use_negotiate,
1507 ])
1508 def test_winrm_cbt(option):
1509 cbt = get_cbt_data(WIN_SERVER_TRUSTED)
1510
1511 protocol = 'negotiate'
1512 if os.name == 'nt':
1513 if option == spnego.NegotiateOptions.use_negotiate:
1514 pytest.skip("The Python negotiate provider is not for use in Windows")
1515
1516 elif KERBEROS_PROVIDER == 'MIT':
1517 # There's a bug in MIT Negotiate where it doesn't pass along the channel bindings. This has been fixed but
1518 # is not present in any released version yet so just use Kerberos for this test.
1519 protocol = 'kerberos'
1520
1521 c = spnego.client(USERNAME, PASSWORD, hostname=WIN_SERVER_TRUSTED, service='HTTP', channel_bindings=cbt,
1522 protocol=protocol, options=option)
1523 winrm_run(c, 'Negotiate', WIN_SERVER_TRUSTED, 'hostname.exe', ssl=True)
1524
1525
1526 # FUTURE: Enable TLSv1.3 tests once Windows supports it
1527 @pytest.mark.parametrize('target, protocol, tls_protocol', [
1528 (WIN_SERVER_TRUSTED, 'kerberos', None),
1529 (WIN_SERVER_TRUSTED, 'kerberos', "TLSv1.2"),
1530 # (WIN_SERVER_TRUSTED, 'kerberos', "TLSv1.3"),
1531 # Connecting with an IP breaks Kerberos authentication.
1532 (WIN_SERVER_TRUSTED_IP, 'ntlm', None),
1533 (WIN_SERVER_TRUSTED_IP, 'ntlm', "TLSv1.2"),
1534 # (WIN_SERVER_TRUSTED_IP, 'ntlm', "TLSv1.3"),
1535 ])
1536 def test_winrm_credssp(target, protocol, tls_protocol):
1537 client_kwargs = {}
1538 if tls_protocol:
1539 tls_context = spnego.tls.default_tls_context()
1540
1541 default_opt = ssl.Options.OP_NO_SSLv2 | \
1542 ssl.Options.OP_NO_SSLv3 | \
1543 ssl.Options.OP_NO_TLSv1 | \
1544 ssl.Options.OP_NO_TLSv1_1 | \
1545 ssl.Options.OP_NO_TLSv1_2 | \
1546 ssl.Options.OP_NO_TLSv1_3
1547
1548 try:
1549 ver = ssl.TLSVersion.TLSv1_2 if tls_protocol == "TLSv1.2" else ssl.TLSVersion.TLSv1_3
1550 tls_context.context.minimum_version = ver
1551 tls_context.context.maximum_version = ver
1552 except (ValueError, AttributeError):
1553 remove_opt = ssl.Options.OP_NO_TLSv1_2 if tls_protocol == "TLSv1.2" else ssl.Options.OP_NO_TLSv1_3
1554 tls_context.context.options |= (default_opt & ~remove_opt)
1555
1556 client_kwargs["credssp_tls_context"] = tls_context
1557
1558 c = spnego.client(USERNAME, PASSWORD, hostname=target, service='HTTP', protocol='credssp', **client_kwargs)
1559
1560 pwsh_script = base64.b64encode(u'''$ErrorActionPreference = 'Stop'
1561 (Get-Item -LiteralPath \\\\{0}\\c$\\temp).FullName
1562 '''.format(WIN_SERVER_UNTRUSTED).encode('utf-16-le')).decode()
1563 rc, stdout, stderr = winrm_run(c, 'CredSSP', target, 'powershell.exe', ['-EncodedCommand', pwsh_script])
1564
1565 assert rc == 0
1566 assert stdout.lower().strip() == '\\\\{0}\\c$\\temp'.format(WIN_SERVER_UNTRUSTED).lower()
1567 assert stderr == ''
1568 assert c.negotiated_protocol == protocol
1569
1570 # Python 3.6 on Windows seems to always report it as TLSv1.0 but on packet inspection it really is correct.
1571 # Just ignoring that particular version with this check.
1572 if tls_protocol and (os.name != 'nt' or sys.version_info > (3,7)):
1573 assert c._tls_object.cipher()[1] == tls_protocol
1574
1575
1576 def test_smb_auth():
1577 s = socket.create_connection((WIN_SERVER_TRUSTED, 445), timeout=10)
1578 mid = 0
1579 try:
1580 negotiate_response = smb_negotiate_request(s, mid)
1581 mid += 1
1582
1583 if os.name != 'nt' or IS_SYSTEM:
1584 c = spnego.client(USERNAME, PASSWORD, hostname=WIN_SERVER_TRUSTED, service='cifs')
1585
1586 else:
1587 c = spnego.client(hostname=WIN_SERVER_TRUSTED, service='cifs')
1588
1589 out_token = c.step(negotiate_response.data.buffer)
1590
1591 session_response = smb_session_setup(s, mid, out_token)
1592 mid += 1
1593 out_token = c.step(session_response.data.buffer)
1594
1595 assert out_token is None
1596 assert c.complete
1597 assert c.negotiated_protocol == 'kerberos'
1598 assert isinstance(c.session_key, bytes)
1599
1600 # SMB2 LOGOFF
1601 smb_send(s, mid, 0x0002, b"\x04\x00\x00\x00", session_id=session_response.session_id)
1602 mid += 1
1603
1604 finally:
1605 s.close()
1606
1607
1608 @pytest.mark.parametrize('protocol', ["kerberos", "negotiate", "negotiate-ntlm", "ntlm"])
1609 def test_ldap_no_integrity(protocol: str) -> None:
1610 context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
1611 context.load_default_certs()
1612
1613 # Connecting over LDAPS will allow us to test out no_integrity works as
1614 # MS LDAP doesn't allow integrity/confidentiality over an existing LDAPS
1615 # connection.
1616
1617 with socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) as sock:
1618 sock.settimeout(10)
1619
1620 with context.wrap_socket(sock, server_hostname=WIN_DC) as ssock:
1621 ssock.connect((WIN_DC, 636))
1622
1623 auth_kwargs = {
1624 "service": "ldap",
1625 "context_req": spnego.ContextReq.mutual_auth | spnego.ContextReq.no_integrity,
1626 }
1627 if protocol == "negotiate-ntlm":
1628 auth_kwargs["hostname"] = WIN_DC_IP
1629 auth_kwargs["protocol"] = "negotiate"
1630 else:
1631 auth_kwargs["hostname"] = WIN_DC
1632 auth_kwargs["protocol"] = protocol
1633
1634 if os.name != 'nt' or IS_SYSTEM:
1635 c = spnego.client(
1636 USERNAME,
1637 PASSWORD,
1638 **auth_kwargs,
1639 )
1640
1641 else:
1642 c = spnego.client(**auth_kwargs)
1643
1644 ldap = sansldap.LDAPClient()
1645
1646 in_token = None
1647 while not c.complete:
1648 token = c.step(in_token=in_token)
1649 if not token:
1650 break
1651
1652 ldap.bind_sasl("GSS-SPNEGO", cred=token)
1653 ssock.write(ldap.data_to_send())
1654
1655 msg = ldap.receive(ssock.recv(4096))[0]
1656 assert isinstance(msg, sansldap.BindResponse)
1657 if msg.result.result_code not in [
1658 sansldap.LDAPResultCode.SUCCESS,
1659 sansldap.LDAPResultCode.SASL_BIND_IN_PROGRESS
1660 ]:
1661 raise Exception(f"Failed to bind: {msg.result.diagnostics_message}")
1662
1663 in_token = msg.server_sasl_creds
1664
1665 if msg.result.result_code != sansldap.LDAPResultCode.SUCCESS:
1666 raise Exception(f"Failed to bind {msg.result.result_code.name}: {msg.result.diagnostics_message}")
1667
1668 # LDAP Whoami
1669 ldap.extended_request("1.3.6.1.4.1.4203.1.11.3")
1670 ssock.write(ldap.data_to_send())
1671
1672 msg = ldap.receive(ssock.recv(4096))[0]
1673 assert isinstance(msg, sansldap.ExtendedResponse)
1674 assert msg.value.startswith(b"u:SPNEGO\\")
1675
1676
1677 @pytest.mark.parametrize(
1678 'protocol, auth_level',
1679 [
1680 ("kerberos", "privacy_integrity"),
1681 ("kerberos", "privacy"),
1682 ("negotiate", "privacy_integrity"),
1683 ("negotiate", "privacy"),
1684 ("negotiate-ntlm", "privacy_integrity"),
1685 ("negotiate-ntlm", "privacy"),
1686 ("ntlm", "privacy_integrity"),
1687 ("ntlm", "privacy"),
1688 ],
1689 )
1690 def test_dce_rpc_auth(protocol: str, auth_level: str) -> None:
1691 auth_kwargs = {
1692 "service": "host",
1693 "context_req": spnego.ContextReq.default | spnego.ContextReq.dce_style
1694 }
1695 if protocol == "negotiate-ntlm":
1696 auth_kwargs["hostname"] = WIN_DC_IP
1697 auth_kwargs["protocol"] = "negotiate"
1698 else:
1699 auth_kwargs["hostname"] = WIN_DC
1700 auth_kwargs["protocol"] = protocol
1701
1702 if protocol == 'ntlm':
1703 auth_kwargs['username'] = USERNAME
1704 auth_kwargs['password'] = PASSWORD
1705 auth_kwargs['options'] = spnego.NegotiateOptions.use_ntlm
1706 elif os.name != 'nt' or IS_SYSTEM:
1707 auth_kwargs['username'] = USERNAME
1708 auth_kwargs['password'] = PASSWORD
1709
1710 c = spnego.client(**auth_kwargs)
1711
1712 # O:SYG:SYD:(A;;CCDC;;;WD)(A;;DC;;;WD)
1713 target_sd = (
1714 b"\x01\x00\x04\x80\x44\x00\x00\x00"
1715 b"\x50\x00\x00\x00\x00\x00\x00\x00"
1716 b"\x14\x00\x00\x00\x02\x00\x30\x00"
1717 b"\x02\x00\x00\x00\x00\x00\x14\x00"
1718 b"\x03\x00\x00\x00\x01\x01\x00\x00"
1719 b"\x00\x00\x00\x01\x00\x00\x00\x00"
1720 b"\x00\x00\x14\x00\x02\x00\x00\x00"
1721 b"\x01\x01\x00\x00\x00\x00\x00\x01"
1722 b"\x00\x00\x00\x00\x01\x01\x00\x00"
1723 b"\x00\x00\x00\x05\x12\x00\x00\x00"
1724 b"\x01\x01\x00\x00\x00\x00\x00\x05"
1725 b"\x12\x00\x00\x00"
1726 )
1727
1728 res = rpc_get_key(
1729 WIN_DC,
1730 c,
1731 target_sd,
1732 root_key_id=None,
1733 l0=-1,
1734 l1=-1,
1735 l2=-1,
1736 auth_level=auth_level,
1737 auth_protocol=auth_kwargs["protocol"],
1738 )
1739 assert isinstance(res, GroupKeyEnvelope)