From 7a903b4f95dbf89aed8316f066382d75357ceca8 Mon Sep 17 00:00:00 2001 From: Justin Bollinger Date: Tue, 27 Oct 2026 17:06:15 -0400 Subject: [PATCH 1/2] lookupsid: add -lookup-domain and -lookup-name -lookup-domain DOMAIN resolves DOMAIN's SID with LsarLookupNames on the target and brute forces RIDs under it instead of the target's own domain. The target forwards the lookups, so a trusted domain can be enumerated through a host in another domain. This helps when the target is fixed, e.g. a relayed session through ntlmrelayx's SOCKS proxy. -lookup-name NAME (repeatable) resolves specific names to their SID and account type instead of brute forcing, e.g. -lookup-name 'DOMAIN\HOST$'. Names without a domain are qualified with -lookup-domain when given. Co-Authored-By: Claude Opus 5.5 --- examples/lookupsid.py | 56 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 3 deletions(-) diff --git a/examples/lookupsid.py b/examples/lookupsid.py index 07d5e67b89..a6538ceb34 100755 --- a/examples/lookupsid.py +++ b/examples/lookupsid.py @@ -40,7 +40,7 @@ class LSALookupSid: } def __init__(self, username='', password='', domain='', port = None, - hashes = None, domain_sids = False, use_kerberos = False, maxRid=4000): + hashes = None, domain_sids = False, use_kerberos = False, maxRid=4000, lookup_domain=None, lookup_names=None): self.__username = username self.__password = password @@ -50,6 +50,11 @@ def __init__(self, username='', password='', domain='', port = None, self.__lmhash = '' self.__nthash = '' self.__domain_sids = domain_sids + self.__lookup_domain = lookup_domain + self.__lookup_names = lookup_names + if lookup_names and lookup_domain: + # Unqualified names default to the -lookup-domain domain + self.__lookup_names = [n if '\\' in n or '@' in n else '%s\\%s' % (lookup_domain, n) for n in lookup_names] self.__doKerberos = use_kerberos if hashes is not None: self.__lmhash, self.__nthash = hashes.split(':') @@ -97,7 +102,43 @@ def __bruteForce(self, rpctransport, maxRid): resp = lsad.hLsarOpenPolicy2(dce, MAXIMUM_ALLOWED | lsat.POLICY_LOOKUP_NAMES) policyHandle = resp['PolicyHandle'] - if self.__domain_sids: # get the Domain SID + if self.__lookup_names: # resolve specific names to SIDs and skip the brute force + try: + resp = lsat.hLsarLookupNames(dce, policyHandle, self.__lookup_names) + except DCERPCException as e: + if str(e).find('STATUS_NONE_MAPPED') >= 0: + for name in self.__lookup_names: + print("%s: not found" % name) + dce.disconnect() + return entries + elif str(e).find('STATUS_SOME_NOT_MAPPED') >= 0: + resp = e.get_packet() + else: + raise + for name, item in zip(self.__lookup_names, resp['TranslatedSids']['Sids']): + if item['Use'] == SID_NAME_USE.SidTypeUnknown: + print("%s: not found" % name) + continue + domain = resp['ReferencedDomains']['Domains'][item['DomainIndex']] + sid = domain['Sid'].formatCanonical() + if item['Use'] != SID_NAME_USE.SidTypeDomain: + sid += '-%d' % item['RelativeId'] + print("%s: %s (%s)" % (name, sid, SID_NAME_USE.enumItems(item['Use']).name)) + dce.disconnect() + return entries + + if self.__lookup_domain: # resolve another domain's SID by name (e.g. a trusted domain) + try: + resp = lsat.hLsarLookupNames(dce, policyHandle, (self.__lookup_domain,)) + except DCERPCException as e: + if str(e).find('STATUS_NONE_MAPPED') >= 0: + raise Exception('Could not resolve domain %s from the target' % self.__lookup_domain) + raise + item = resp['TranslatedSids']['Sids'][0] + if item['Use'] != SID_NAME_USE.SidTypeDomain: + raise Exception('%s resolved to a %s, not a domain' % (self.__lookup_domain, SID_NAME_USE.enumItems(item['Use']).name)) + domainSid = resp['ReferencedDomains']['Domains'][item['DomainIndex']]['Sid'].formatCanonical() + elif self.__domain_sids: # get the Domain SID resp = lsad.hLsarQueryInformationPolicy2(dce, policyHandle, lsad.POLICY_INFORMATION_CLASS.PolicyPrimaryDomainInformation) domainSid = resp['PolicyInformation']['PolicyPrimaryDomainInfo']['Sid'].formatCanonical() else: # Get the target host SID @@ -166,6 +207,10 @@ def __bruteForce(self, rpctransport, maxRid): group.add_argument('-port', choices=['139', '445'], nargs='?', default='445', metavar="destination port", help='Destination port to connect to SMB Server') group.add_argument('-domain-sids', action='store_true', help='Enumerate Domain SIDs (will likely forward requests to the DC)') + group.add_argument('-lookup-domain', action='store', metavar='DOMAIN', help='Resolve DOMAIN\'s SID through the target and ' + 'enumerate it instead (e.g. a trusted domain; requests are forwarded across the trust)') + group.add_argument('-lookup-name', action='append', metavar='NAME', help='Resolve NAME (e.g. DOMAIN\\HOST$) to its SID ' + 'instead of brute forcing. Can be repeated. Names without a domain use -lookup-domain if given') group = parser.add_argument_group('authentication') @@ -182,6 +227,11 @@ def __bruteForce(self, rpctransport, maxRid): options = parser.parse_args() + if options.lookup_domain and options.domain_sids: + parser.error('-lookup-domain and -domain-sids are mutually exclusive') + if options.lookup_name and options.domain_sids: + parser.error('-lookup-name cannot be combined with -domain-sids') + # Init the example's logger theme logger.init(options.ts, options.debug) @@ -197,7 +247,7 @@ def __bruteForce(self, rpctransport, maxRid): if options.target_ip is None: options.target_ip = remoteName - lookup = LSALookupSid(username, password, domain, int(options.port), options.hashes, options.domain_sids, options.k, options.maxRid) + lookup = LSALookupSid(username, password, domain, int(options.port), options.hashes, options.domain_sids, options.k, options.maxRid, options.lookup_domain, options.lookup_name) try: lookup.dump(remoteName, options.target_ip) except: From 35d98a4efe681760f8096ca60f335ef6b600e3d3 Mon Sep 17 00:00:00 2001 From: Justin Bollinger Date: Mon, 23 Nov 2026 19:09:47 -0500 Subject: [PATCH 2/2] lookupsid: apply review feedback for -lookup-domain/-lookup-name Reject an empty -lookup-domain so a wrapper passing an unset variable does not silently fall back to the account domain. Clarify the help text for both flags and log "Resolving names" instead of "Brute forcing SIDs" when -lookup-name is used. Co-Authored-By: Claude Opus 5.5 --- examples/lookupsid.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/examples/lookupsid.py b/examples/lookupsid.py index a6538ceb34..0ddcab3a20 100755 --- a/examples/lookupsid.py +++ b/examples/lookupsid.py @@ -61,7 +61,10 @@ def __init__(self, username='', password='', domain='', port = None, def dump(self, remoteName, remoteHost): - logging.info('Brute forcing SIDs at %s' % remoteName) + if self.__lookup_names: + logging.info('Resolving names at %s', remoteName) + else: + logging.info('Brute forcing SIDs at %s', remoteName) stringbinding = self.KNOWN_PROTOCOLS[self.__port]['bindstr'] % remoteName logging.info('StringBinding %s'%stringbinding) @@ -207,10 +210,11 @@ def __bruteForce(self, rpctransport, maxRid): group.add_argument('-port', choices=['139', '445'], nargs='?', default='445', metavar="destination port", help='Destination port to connect to SMB Server') group.add_argument('-domain-sids', action='store_true', help='Enumerate Domain SIDs (will likely forward requests to the DC)') - group.add_argument('-lookup-domain', action='store', metavar='DOMAIN', help='Resolve DOMAIN\'s SID through the target and ' - 'enumerate it instead (e.g. a trusted domain; requests are forwarded across the trust)') - group.add_argument('-lookup-name', action='append', metavar='NAME', help='Resolve NAME (e.g. DOMAIN\\HOST$) to its SID ' - 'instead of brute forcing. Can be repeated. Names without a domain use -lookup-domain if given') + group.add_argument('-lookup-domain', metavar='DOMAIN', + help='Domain to enumerate. With -lookup-name, use DOMAIN as the default qualifier for unqualified names.') + group.add_argument('-lookup-name', action='append', metavar='NAME', + help='Resolve NAME to its SID and show the SID type reported by the server. ' + 'Repeat for multiple names. Skips RID enumeration.') group = parser.add_argument_group('authentication') @@ -227,6 +231,8 @@ def __bruteForce(self, rpctransport, maxRid): options = parser.parse_args() + if options.lookup_domain == '': + parser.error('-lookup-domain requires a non-empty domain name') if options.lookup_domain and options.domain_sids: parser.error('-lookup-domain and -domain-sids are mutually exclusive') if options.lookup_name and options.domain_sids: