Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 27 additions & 49 deletions nxc/protocols/ldap.py
Original file line number Diff line number Diff line change
Expand Up @@ -1270,38 +1270,11 @@ def query(self):
self.logger.highlight(f"{attribute:<20} {value}")

def find_delegation(self):
def printTable(items, header):
colLen = []

# Calculating maximum length before parsing CN.
for i, col in enumerate(header):
rowMaxLen = max(len(row[1].split(",")[0].split("CN=")[-1]) for row in items) if i == 1 else max(len(str(row[i])) for row in items)
colLen.append(max(rowMaxLen, len(col)))

# Create the format string for each row
outputFormat = " ".join([f"{{{num}:{width}s}}" for num, width in enumerate(colLen)])

# Print header
self.logger.highlight(outputFormat.format(*header))
self.logger.highlight(" ".join(["-" * itemLen for itemLen in colLen]))

# Print rows
for row in items:
# Get first CN value.
if "CN=" in row[1]:
row[1] = row[1].split(",")[0].split("CN=")[-1]

# Added join for DelegationRightsTo
row[3] = ", ".join(str(x) for x in row[3]) if isinstance(row[3], list) else row[3]

self.logger.highlight(outputFormat.format(*row))

# Building the search filter
search_filter = (f"(&(|(UserAccountControl:1.2.840.113556.1.4.803:={UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION})"
f"(UserAccountControl:1.2.840.113556.1.4.803:={UF_TRUSTED_FOR_DELEGATION})"
"(msDS-AllowedToDelegateTo=*)(msDS-AllowedToActOnBehalfOfOtherIdentity=*))"
f"(!(UserAccountControl:1.2.840.113556.1.4.803:={UF_ACCOUNTDISABLE})))")
# f"(!(UserAccountControl:1.2.840.113556.1.4.803:={UF_SERVER_TRUST_ACCOUNT})))") This would filter out RBCD to DCs

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

Expand All @@ -1311,33 +1284,26 @@ def printTable(items, header):
self.logger.debug(f"Total of records returned {len(resp_parsed)}")

for item in resp_parsed:
sAMAccountName = ""
userAccountControl = 0
delegation = ""
objectType = ""
rightsTo = []
protocolTransition = 0

try:
sAMAccountName = item["sAMAccountName"]

userAccountControl = int(item["userAccountControl"])
objectType = item.get("objectCategory")
objectType = item["objectCategory"]

# Unconstrained delegation
# Filter out DCs, unconstrained delegation to DCs is not a useful information
if userAccountControl & UF_TRUSTED_FOR_DELEGATION and not userAccountControl & UF_SERVER_TRUST_ACCOUNT:
delegation = "Unconstrained"
rightsTo.append("N/A")
elif userAccountControl & UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION:
delegation = "Constrained w/ Protocol Transition"
protocolTransition = 1
# Append immediately: an account can also have constrained delegation configured
answers.append([sAMAccountName, objectType, "Unconstrained", "N/A"])

# Constrained delegation
if item.get("msDS-AllowedToDelegateTo") is not None:
Comment thread
mpgn marked this conversation as resolved.
Resolved
if protocolTransition == 0:
if userAccountControl & UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION:
delegation = "Constrained w/ Protocol Transition"
else:
delegation = "Constrained"
rightsTo = item.get("msDS-AllowedToDelegateTo")
answers.append([sAMAccountName, objectType, delegation, item["msDS-AllowedToDelegateTo"]])

# Not an elif as an object could both have RBCD and another type of delegation
# Resource-Based Constrained Delegation
if item.get("msDS-AllowedToActOnBehalfOfOtherIdentity") is not None:
databyte = item.get("msDS-AllowedToActOnBehalfOfOtherIdentity")
rbcdRights = []
Expand All @@ -1357,19 +1323,31 @@ def printTable(items, header):

for rights, objType in zip(rbcdRights, rbcdObjType, strict=True):
answers.append([rights, objType, "Resource-Based Constrained", sAMAccountName])

if delegation in ["Unconstrained", "Constrained", "Constrained w/ Protocol Transition"]:
answers.append([sAMAccountName, objectType, delegation, rightsTo])

except Exception as e:
self.logger.error(f"Skipping item, cannot process due to error {e}")

if answers:
printTable(answers, header=["AccountName", "AccountType", "DelegationType", "DelegationRightsTo"])
# Print header
self.logger.highlight(f"{'AccountName':<15} {'AccountType':<15} {'DelegationType':<35} {'DelegationRightsTo':<40}")
self.logger.highlight(f"{'-' * 15} {'-' * 15} {'-' * 35} {'-' * 40}")

# Print rows
for row in answers:
# Get first CN value.
if "CN=" in row[1]:
row[1] = row[1].split(",")[0].split("CN=")[-1]

# Added join for DelegationRightsTo
row[3] = ", ".join(str(x) for x in row[3]) if isinstance(row[3], list) else row[3]

self.logger.highlight(f"{row[0]:<15} {row[1]:<15} {row[2]:<35} {row[3]:<40}")
else:
self.logger.fail("No entries found!")

def trusted_for_delegation(self):
self.logger.fail("[REMOVED] Use --find-delegation instead.")
return

# Building the search filter
searchFilter = f"(userAccountControl:1.2.840.113556.1.4.803:={UF_TRUSTED_FOR_DELEGATION})"
resp = self.search(searchFilter, attributes=["sAMAccountName"], sizeLimit=0)
Expand Down
2 changes: 1 addition & 1 deletion nxc/protocols/ldap/proto_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def proto_args(parser, parents):
vgroup.add_argument("--base-dn", metavar="BASE_DN", dest="base_dn", type=str, default=None, help="base DN for search queries")
vgroup.add_argument("--query", nargs=2, help="Query LDAP with a custom filter and attributes")
vgroup.add_argument("--find-delegation", action="store_true", help="Finds delegation relationships within an Active Directory domain. (Enabled Accounts only)")
vgroup.add_argument("--trusted-for-delegation", action="store_true", help="Get the list of users and computers with flag TRUSTED_FOR_DELEGATION")
vgroup.add_argument("--trusted-for-delegation", action="store_true", help="[REMOVED] Use --find-delegation instead.")
vgroup.add_argument("--password-not-required", action="store_true", help="Get the list of users with flag PASSWD_NOTREQD")
vgroup.add_argument("--admin-count", action="store_true", help="Get user that had the value adminCount=1")
vgroup.add_argument("--users", nargs="*", help="Enumerate domain users")
Expand Down
Loading