Skip to content

Commit c2fe271

Browse files
committed
Fix ldap result parsing minor code improvements
1 parent fa8d5d5 commit c2fe271

2 files changed

Lines changed: 11 additions & 19 deletions

File tree

nxc/parsers/ldap_results.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from impacket.ldap import ldapasn1 as ldapasn1_impacket
22

3+
34
def parse_result_attributes(ldap_response):
45
parsed_response = []
56
for entry in ldap_response:
@@ -12,15 +13,11 @@ def parse_result_attributes(ldap_response):
1213
for val in attribute["vals"].components:
1314
try:
1415
encoding = val.encoding
15-
16-
print(f"Val: {str(val)}, Type: {type(val)}, Encoding: {encoding}")
17-
print(str(val).encode(encoding).decode("utf-8"))
18-
# Attempt to decode as UTF-8
19-
decoded_val = val.decode("utf-8")
20-
except (UnicodeDecodeError, AttributeError):
21-
# If it fails, fall back to hexadecimal representation
22-
decoded_val = val.hex() if isinstance(val, bytes) else str(val)
23-
val_list.append(decoded_val)
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__()
20+
val_list.append(val_decoded)
2421
attribute_map[str(attribute["type"])] = val_list if len(val_list) > 1 else val_list[0]
2522
parsed_response.append(attribute_map)
2623
return parsed_response

nxc/protocols/ldap.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,12 +1092,7 @@ def find_delegation(self):
10921092
UF_TRUSTED_FOR_DELEGATION = 0x80000
10931093
UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION = 0x1000000
10941094
UF_ACCOUNTDISABLE = 0x2
1095-
1096-
def processAttributeValue(attribute):
1097-
# Extract the payload value from the AttributeValue object
1098-
if hasattr(attribute, "payload"):
1099-
return str(attribute.payload)
1100-
return str(attribute)
1095+
SERVER_TRUST_ACCOUNT = 0x2000
11011096

11021097
def printTable(items, header):
11031098
colLen = []
@@ -1126,11 +1121,11 @@ def printTable(items, header):
11261121
self.logger.highlight(outputFormat.format(*row))
11271122

11281123
# Building the search filter
1129-
search_filter = ("(&(|(UserAccountControl:1.2.840.113556.1.4.803:=16777216)"
1130-
"(UserAccountControl:1.2.840.113556.1.4.803:=524288)"
1124+
search_filter = (f"(&(|(UserAccountControl:1.2.840.113556.1.4.803:={UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION})"
1125+
f"(UserAccountControl:1.2.840.113556.1.4.803:={UF_TRUSTED_FOR_DELEGATION})"
11311126
"(msDS-AllowedToDelegateTo=*)(msDS-AllowedToActOnBehalfOfOtherIdentity=*))"
1132-
"(!(UserAccountControl:1.2.840.113556.1.4.803:=2))"
1133-
"(!(UserAccountControl:1.2.840.113556.1.4.803:=8192)))")
1127+
f"(!(UserAccountControl:1.2.840.113556.1.4.803:={UF_ACCOUNTDISABLE}))"
1128+
f"(!(UserAccountControl:1.2.840.113556.1.4.803:={SERVER_TRUST_ACCOUNT})))")
11341129

11351130
attributes = ["sAMAccountName", "pwdLastSet", "userAccountControl", "objectCategory",
11361131
"msDS-AllowedToActOnBehalfOfOtherIdentity", "msDS-AllowedToDelegateTo"]

0 commit comments

Comments
 (0)