Skip to content

Commit e4095ce

Browse files
committed
Replace manual parsing with ldap parser and remove duplicate code
1 parent bdfd869 commit e4095ce

File tree

1 file changed

+33
-45
lines changed

1 file changed

+33
-45
lines changed

nxc/modules/daclread.py

Lines changed: 33 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -279,35 +279,44 @@ def on_login(self, context, connection):
279279

280280
# Searching for the principal SID
281281
if self.principal_sAMAccountName is not None:
282-
_lookedup_principal = self.principal_sAMAccountName
283282
try:
284-
self.principal_sid = format_sid(
285-
self.ldap_session.search(
286-
searchBase=self.baseDN,
287-
searchFilter=f"(sAMAccountName={escape_filter_chars(_lookedup_principal)})",
288-
attributes=["objectSid"],
289-
)[0][1][0][1][0]
283+
resp = connection.search(
284+
searchFilter=f"(sAMAccountName={escape_filter_chars(self.principal_sAMAccountName)})",
285+
attributes=["objectSid"],
290286
)
287+
resp_parsed = parse_result_attributes(resp)[0]
288+
self.principal_sid = resp_parsed["objectSid"]
291289
context.log.highlight(f"Found principal SID to filter on: {self.principal_sid}")
292290
except Exception as e:
293-
context.log.fail(f"Principal SID not found in LDAP ({_lookedup_principal})")
291+
context.log.fail(f"Principal SID not found in LDAP ({self.principal_sAMAccountName})")
294292
context.log.debug(f"Exception: {e}, {traceback.format_exc()}")
295293
return
296294

297295
# Searching for the targets SID and their Security Descriptors
298296
# If there is only one target
299297
if (self.target_sAMAccountName or self.target_DN) and self.target_file is None:
300-
# Searching for target account with its security descriptor
301298
try:
302-
self.search_target_principal_security_descriptor(context, connection)
299+
# Searching for target account with its security descriptor
300+
if self.target_sAMAccountName: # noqa: SIM108
301+
search_filter = f"(sAMAccountName={escape_filter_chars(self.target_sAMAccountName)})"
302+
else:
303+
search_filter = f"(distinguishedName={escape_filter_chars(self.target_DN)})"
304+
305+
resp = connection.search(
306+
searchFilter=search_filter,
307+
attributes=["distinguishedName", "nTSecurityDescriptor"],
308+
searchControls=security_descriptor_control(sdflags=0x04),
309+
)
310+
resp_parsed = parse_result_attributes(resp)[0]
311+
303312
# Extract security descriptor data
304-
self.target_principal_dn = self.target_principal[0]
305-
self.principal_raw_security_descriptor = str(self.target_principal[1][0][1][0]).encode("latin-1")
313+
self.target_principal_dn = resp_parsed["distinguishedName"]
314+
self.principal_raw_security_descriptor = resp_parsed["nTSecurityDescriptor"]
306315
self.principal_security_descriptor = ldaptypes.SR_SECURITY_DESCRIPTOR(data=self.principal_raw_security_descriptor)
307-
context.log.highlight(f"Target principal found in LDAP ({self.target_principal[0]})")
316+
context.log.highlight(f"Target principal found in LDAP ({self.target_principal_dn})")
308317
except Exception as e:
309318
context.log.fail(f"Target SID not found in LDAP ({self.target_sAMAccountName})")
310-
context.log.exception(e)
319+
context.log.debug(f"Exception: {e}, {traceback.format_exc()}")
311320
return
312321

313322
if self.action == "read":
@@ -322,10 +331,16 @@ def on_login(self, context, connection):
322331
try:
323332
self.target_sAMAccountName = target.strip()
324333
# Searching for target account with its security descriptor
325-
self.search_target_principal_security_descriptor(context, connection)
334+
resp = connection.search(
335+
searchFilter=f"(sAMAccountName={escape_filter_chars(self.target_sAMAccountName)})",
336+
attributes=["distinguishedName", "nTSecurityDescriptor"],
337+
searchControls=security_descriptor_control(sdflags=0x04),
338+
)
339+
resp_parsed = parse_result_attributes(resp)[0]
340+
326341
# Extract security descriptor data
327-
self.target_principal_dn = self.target_principal[0]
328-
self.principal_raw_security_descriptor = str(self.target_principal[1][0][1][0]).encode("latin-1")
342+
self.target_principal_dn = resp_parsed["distinguishedName"]
343+
self.principal_raw_security_descriptor = resp_parsed["nTSecurityDescriptor"]
329344
self.principal_security_descriptor = ldaptypes.SR_SECURITY_DESCRIPTOR(data=self.principal_raw_security_descriptor)
330345
context.log.highlight(f"Target principal found in LDAP ({self.target_sAMAccountName})")
331346
except Exception:
@@ -359,33 +374,6 @@ def backup(self, context):
359374
context.log.highlight("DACL backed up to %s", self.filename)
360375
self.filename = None
361376

362-
# Attempts to retrieve the DACL in the Security Descriptor of the specified target
363-
def search_target_principal_security_descriptor(self, context, connection):
364-
_lookedup_principal = ""
365-
# Set SD flags to only query for DACL
366-
controls = security_descriptor_control(sdflags=0x04)
367-
if self.target_sAMAccountName is not None:
368-
_lookedup_principal = self.target_sAMAccountName
369-
target = self.ldap_session.search(
370-
searchBase=self.baseDN,
371-
searchFilter=f"(sAMAccountName={escape_filter_chars(_lookedup_principal)})",
372-
attributes=["nTSecurityDescriptor"],
373-
searchControls=controls,
374-
)
375-
if self.target_DN is not None:
376-
_lookedup_principal = self.target_DN
377-
target = self.ldap_session.search(
378-
searchBase=_lookedup_principal,
379-
searchFilter=f"(distinguishedName={_lookedup_principal})",
380-
attributes=["nTSecurityDescriptor"],
381-
searchControls=controls,
382-
)
383-
try:
384-
self.target_principal = target[0]
385-
except Exception:
386-
context.log.fail(f"Principal not found in LDAP ({_lookedup_principal}), probably an LDAP session issue.")
387-
sys.exit(0)
388-
389377
# Attempts to retrieve the SID and Distinguisehd Name from the sAMAccountName
390378
# Not used for the moment
391379
# - samname : a sAMAccountName
@@ -505,7 +493,7 @@ def parse_ace(self, context, ace):
505493

506494
def print_parsed_dacl(self, context, parsed_dacl):
507495
"""Prints a full DACL by printing each parsed ACE
508-
496+
509497
parsed_dacl : a parsed DACL from parse_dacl()
510498
"""
511499
context.log.debug("Printing parsed DACL")

0 commit comments

Comments
 (0)