Skip to content

Commit 644298e

Browse files
committed
Add ldap parsing to sid resolv function
1 parent e4095ce commit 644298e

1 file changed

Lines changed: 10 additions & 14 deletions

File tree

nxc/modules/daclread.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ def on_login(self, context, connection):
276276
context.log.highlight("Be careful, this module cannot read the DACLS recursively.")
277277
self.baseDN = connection.ldap_connection._baseDN
278278
self.ldap_session = connection.ldap_connection
279+
self.connection = connection
280+
self.context = context
279281

280282
# Searching for the principal SID
281283
if self.principal_sAMAccountName is not None:
@@ -391,27 +393,21 @@ def get_user_info(self, context, samname):
391393
context.log.fail(f"User not found in LDAP: {samname}")
392394
return False
393395

394-
# Attempts to resolve a SID and return the corresponding samaccountname
395-
# - sid : the SID to resolve
396-
def resolveSID(self, context, sid):
396+
def resolveSID(self, sid):
397+
"""Resolves a SID to its corresponding sAMAccountName."""
397398
# Tries to resolve the SID from the well known SIDs
398399
if sid in WELL_KNOWN_SIDS:
399400
return WELL_KNOWN_SIDS[sid]
400401
# Tries to resolve the SID from the LDAP domain dump
401402
else:
402403
try:
403-
self.ldap_session.search(
404-
searchBase=self.baseDN,
405-
searchFilter=f"(objectSid={sid})",
406-
attributes=["sAMAccountName"],
407-
)[0][0]
408-
return self.ldap_session.search(
409-
searchBase=self.baseDN,
404+
resp = self.connection.search(
410405
searchFilter=f"(objectSid={sid})",
411406
attributes=["sAMAccountName"],
412-
)[0][1][0][1][0]
407+
)
408+
return parse_result_attributes(resp)[0]["sAMAccountName"]
413409
except Exception:
414-
context.log.debug(f"SID not found in LDAP: {sid}")
410+
self.context.log.debug(f"SID not found in LDAP: {sid}")
415411
return ""
416412

417413
# Parses a full DACL
@@ -450,7 +446,7 @@ def parse_ace(self, context, ace):
450446
# Extracts the access mask (by parsing the simple permissions) and the principal's SID
451447
if ace["TypeName"] in ["ACCESS_ALLOWED_ACE", "ACCESS_DENIED_ACE"]:
452448
access_mask = f"{', '.join(self.parse_perms(ace['Ace']['Mask']['Mask']))} (0x{ace['Ace']['Mask']['Mask']:x})"
453-
trustee_sid = f"{self.resolveSID(context, ace['Ace']['Sid'].formatCanonical()) or 'UNKNOWN'} ({ace['Ace']['Sid'].formatCanonical()})"
449+
trustee_sid = f"{self.resolveSID(ace['Ace']['Sid'].formatCanonical()) or 'UNKNOWN'} ({ace['Ace']['Sid'].formatCanonical()})"
454450
parsed_ace = {
455451
"Access mask": access_mask,
456452
"Trustee (SID)": trustee_sid
@@ -478,7 +474,7 @@ def parse_ace(self, context, ace):
478474
parsed_ace["Inherited type (GUID)"] = f"UNKNOWN ({inh_obj_type})"
479475
# Extract the Trustee SID (the object that has the right over the DACL bearer)
480476
parsed_ace["Trustee (SID)"] = "{} ({})".format(
481-
self.resolveSID(context, ace["Ace"]["Sid"].formatCanonical()) or "UNKNOWN",
477+
self.resolveSID(ace["Ace"]["Sid"].formatCanonical()) or "UNKNOWN",
482478
ace["Ace"]["Sid"].formatCanonical(),
483479
)
484480
else: # if the ACE is not an access allowed

0 commit comments

Comments
 (0)