from impacket.ldap import ldap
from impacket.ldap.ldaptypes import (
SR_SECURITY_DESCRIPTOR, ACE, ACCESS_ALLOWED_OBJECT_ACE
)
# Connect and authenticate
conn = ldap.LDAPConnection('ldap://dc.example.com')
conn.login('admin', 'password', 'EXAMPLE')
# Query security descriptor
results = conn.search(
searchFilter='(sAMAccountName=testuser)',
attributes=['nTSecurityDescriptor']
)
for entry in results:
if not entry.get('objectName'):
continue
for attr in entry['attributes']:
if str(attr['type']) == 'nTSecurityDescriptor':
sd_data = bytes(attr['vals'][0])
sd = SR_SECURITY_DESCRIPTOR(data=sd_data)
print(f"Owner: {sd['OwnerSid'].formatCanonical()}")
# Parse DACL
dacl = sd['Dacl']
print(f"DACL has {dacl['AceCount']} ACEs")
for ace in dacl.aces:
ace_info = ace['Ace']
print(f" Type: {ace['TypeName']}")
print(f" SID: {ace_info['Sid'].formatCanonical()}")
print(f" Mask: 0x{ace_info['Mask']['Mask']:08x}")
# Check for object ACEs
if ace['AceType'] == ACCESS_ALLOWED_OBJECT_ACE.ACE_TYPE:
if ace_info.hasFlag(ACCESS_ALLOWED_OBJECT_ACE.ACE_OBJECT_TYPE_PRESENT):
print(f" Object GUID: {ace_info['ObjectType'].hex()}")
conn.close()