diff --git a/nxc/protocols/ldap.py b/nxc/protocols/ldap.py index 333b4aaf01..2130e5759b 100644 --- a/nxc/protocols/ldap.py +++ b/nxc/protocols/ldap.py @@ -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"] @@ -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: - 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 = [] @@ -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) diff --git a/nxc/protocols/ldap/proto_args.py b/nxc/protocols/ldap/proto_args.py index e9813c6341..89a9ae5d20 100644 --- a/nxc/protocols/ldap/proto_args.py +++ b/nxc/protocols/ldap/proto_args.py @@ -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")