Skip to content

Commit 2191dee

Browse files
Merge pull request Pennyw0rth#309 from Pennyw0rth/neff-add-ldapquery
Add ldap query option
2 parents db41c08 + a2d43ca commit 2191dee

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

nxc/protocols/ldap.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from impacket.krb5.types import Principal, KerberosException
2929
from impacket.ldap import ldap as ldap_impacket
3030
from impacket.ldap import ldapasn1 as ldapasn1_impacket
31+
from impacket.ldap.ldap import LDAPFilterSyntaxError
3132
from impacket.smb import SMB_DIALECT
3233
from impacket.smbconnection import SMBConnection, SessionError
3334

@@ -1054,6 +1055,36 @@ def kerberoasting(self):
10541055
self.logger.highlight("No entries found!")
10551056
self.logger.fail("Error with the LDAP account used")
10561057

1058+
def query(self):
1059+
"""
1060+
Query the LDAP server with the specified filter and attributes.
1061+
Example usage:
1062+
--query "(sAMAccountName=Administrator)" "sAMAccountName pwdLastSet memberOf"
1063+
"""
1064+
search_filter = self.args.query[0]
1065+
attributes = [attr.strip() for attr in self.args.query[1].split(" ")]
1066+
if len(attributes) == 1 and attributes[0] == "":
1067+
attributes = None
1068+
if not search_filter:
1069+
self.logger.fail("No filter specified")
1070+
return
1071+
self.logger.debug(f"Querying LDAP server with filter: {search_filter} and attributes: {attributes}")
1072+
try:
1073+
resp = self.search(search_filter, attributes, 0)
1074+
except LDAPFilterSyntaxError as e:
1075+
self.logger.fail(f"LDAP Filter Syntax Error: {e}")
1076+
return
1077+
for item in resp:
1078+
if isinstance(item, ldapasn1_impacket.SearchResultEntry) is not True:
1079+
continue
1080+
self.logger.success(f"Response for object: {item['objectName']}")
1081+
for attribute in item["attributes"]:
1082+
attr = f"{attribute['type']}:"
1083+
vals = str(attribute["vals"]).replace("\n", "")
1084+
if "SetOf: " in vals:
1085+
vals = vals.replace("SetOf: ", "")
1086+
self.logger.highlight(f"{attr:<20} {vals}")
1087+
10571088
def trusted_for_delegation(self):
10581089
# Building the search filter
10591090
searchFilter = "(userAccountControl:1.2.840.113556.1.4.803:=524288)"

nxc/protocols/ldap/proto_args.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def proto_args(parser, std_parser, module_parser):
1313
egroup.add_argument("--kerberoasting", help="Output TGS ticket to crack with hashcat to file")
1414

1515
vgroup = ldap_parser.add_argument_group("Retrieve useful information on the domain", "Options to to play with Kerberos")
16+
vgroup.add_argument("--query", nargs=2, help="Query LDAP with a custom filter and attributes")
1617
vgroup.add_argument("--trusted-for-delegation", action="store_true", help="Get the list of users and computers with flag TRUSTED_FOR_DELEGATION")
1718
vgroup.add_argument("--password-not-required", action="store_true", help="Get the list of users with flag PASSWD_NOTREQD")
1819
vgroup.add_argument("--admin-count", action="store_true", help="Get objets that had the value adminCount=1")

0 commit comments

Comments
 (0)