Repository navigation
lookupsid: add -lookup-domain and -lookup-name #2349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bandrel
wants to merge
2
commits into
fortra:master
Choose a base branch
from
bandrel:lookupsid-lookup-domain-name
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+60
−4
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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: | ||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If a wrapper passes
Suggested change
|
||||||||||
| 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: | ||||||||||
|
|
||||||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A nit one... Rephrased help text for the newly added parameters. Think it clarifies better the double-use of
-lookup-domain