Skip to content

Commit af743bf

Browse files
authored
Merge pull request Pennyw0rth#642 from Pennyw0rth/neff-parse-uuids
Add sid parsing directly to the ldap attribute parser
2 parents 6c30f3b + d5bd57d commit af743bf

2 files changed

Lines changed: 35 additions & 27 deletions

File tree

nxc/parsers/ldap_results.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from impacket.ldap import ldapasn1 as ldapasn1_impacket
2+
from uuid import UUID
23

34

45
def parse_result_attributes(ldap_response):
@@ -11,16 +12,43 @@ def parse_result_attributes(ldap_response):
1112
for attribute in entry["attributes"]:
1213
val_list = []
1314
for val in attribute["vals"].components:
14-
try:
15-
encoding = val.encoding
16-
val_decoded = str(val).encode(encoding).decode("utf-8")
17-
except UnicodeDecodeError:
18-
# If we can't decode the value, we'll just return the bytes
19-
val_decoded = val.__bytes__()
15+
# Typical Byte objects we know how to decode
16+
if str(attribute["type"]) == "objectGUID":
17+
val_decoded = UUID(bytes=val.__bytes__())
18+
elif str(attribute["type"]) == "objectSid":
19+
val_decoded = sid_to_str(val.__bytes__())
20+
else:
21+
# For the rest we try to decode the value with its encoding
22+
try:
23+
encoding = val.encoding
24+
val_decoded = str(val).encode(encoding).decode("utf-8")
25+
except UnicodeDecodeError:
26+
# If we can't decode the value, we'll just return the bytes
27+
val_decoded = val.__bytes__()
2028
val_list.append(val_decoded)
2129
if len(val_list) == 1:
2230
attribute_map[str(attribute["type"])] = val_list[0]
2331
else:
2432
attribute_map[str(attribute["type"])] = val_list
2533
parsed_response.append(attribute_map)
2634
return parsed_response
35+
36+
37+
def sid_to_str(sid):
38+
try:
39+
# revision
40+
revision = int(sid[0])
41+
# count of sub authorities
42+
sub_authorities = int(sid[1])
43+
# big endian
44+
identifier_authority = int.from_bytes(sid[2:8], byteorder="big")
45+
# If true then it is represented in hex
46+
if identifier_authority >= 2**32:
47+
identifier_authority = hex(identifier_authority)
48+
49+
# loop over the count of small endians
50+
sub_authority = "-" + "-".join([str(int.from_bytes(sid[8 + (i * 4): 12 + (i * 4)], byteorder="little")) for i in range(sub_authorities)])
51+
return "S-" + str(revision) + "-" + str(identifier_authority) + sub_authority
52+
except Exception:
53+
pass
54+
return sid

nxc/protocols/ldap.py

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -578,25 +578,6 @@ def hash_login(self, domain, username, ntlm_hash):
578578
def get_sid(self):
579579
self.logger.highlight(f"Domain SID {self.sid_domain}")
580580

581-
def sid_to_str(self, sid):
582-
try:
583-
# revision
584-
revision = int(sid[0])
585-
# count of sub authorities
586-
sub_authorities = int(sid[1])
587-
# big endian
588-
identifier_authority = int.from_bytes(sid[2:8], byteorder="big")
589-
# If true then it is represented in hex
590-
if identifier_authority >= 2**32:
591-
identifier_authority = hex(identifier_authority)
592-
593-
# loop over the count of small endians
594-
sub_authority = "-" + "-".join([str(int.from_bytes(sid[8 + (i * 4): 12 + (i * 4)], byteorder="little")) for i in range(sub_authorities)])
595-
return "S-" + str(revision) + "-" + str(identifier_authority) + sub_authority
596-
except Exception:
597-
pass
598-
return sid
599-
600581
def check_if_admin(self):
601582
# 1. get SID of the domaine
602583
search_filter = "(userAccountControl:1.2.840.113556.1.4.803:=8192)"
@@ -606,8 +587,7 @@ def check_if_admin(self):
606587
answers = []
607588
if resp and (self.password != "" or self.lmhash != "" or self.nthash != "" or self.aesKey != "") and self.username != "":
608589
for item in resp_parsed:
609-
sid = self.sid_to_str(item["objectSid"])
610-
self.sid_domain = "-".join(sid.split("-")[:-1])
590+
self.sid_domain = "-".join(item["objectSid"].split("-")[:-1])
611591

612592
# 2. get all group cn name
613593
search_filter = f"(|(objectSid={self.sid_domain}-512)(objectSid={self.sid_domain}-544)(objectSid={self.sid_domain}-519)(objectSid=S-1-5-32-549)(objectSid=S-1-5-32-551))"

0 commit comments

Comments
 (0)