From d3c95bef492c2dcfeeaabfa5cca8eb8b13332e2b Mon Sep 17 00:00:00 2001 From: Tom Spencer Date: Fri, 7 Jul 2023 18:00:11 -0700 Subject: [PATCH 1/2] Remove unnecessary calls to DRSCrackNames in LDAP and full DRSUAPI DC Syncs --- impacket/examples/secretsdump.py | 86 ++++++++++++++------------------ 1 file changed, 37 insertions(+), 49 deletions(-) diff --git a/impacket/examples/secretsdump.py b/impacket/examples/secretsdump.py index 3905407e71..b35d9c0e60 100644 --- a/impacket/examples/secretsdump.py +++ b/impacket/examples/secretsdump.py @@ -69,7 +69,7 @@ from impacket.ldap.ldap import SimplePagedResultsControl, LDAPSearchError from impacket.ldap.ldapasn1 import SearchResultEntry from impacket.dcerpc.v5 import transport, rrp, scmr, wkst, samr, epm, drsuapi -from impacket.dcerpc.v5.dtypes import NULL +from impacket.dcerpc.v5.dtypes import NULL, SID from impacket.dcerpc.v5.rpcrt import RPC_C_AUTHN_LEVEL_PKT_PRIVACY, DCERPCException, RPC_C_AUTHN_GSS_NEGOTIATE from impacket.dcerpc.v5.dcom import wmi from impacket.dcerpc.v5.dcom.oaut import IID_IDispatch, IDispatch, DISPPARAMS, DISPATCH_PROPERTYGET, \ @@ -548,10 +548,18 @@ def DRSGetNCChanges(self, userEntry): request['pmsgIn']['V8']['uuidDsaObjDest'] = self.__NtdsDsaObjectGuid request['pmsgIn']['V8']['uuidInvocIdSrc'] = self.__NtdsDsaObjectGuid + # Convert string SID to 2.4.2.2 packet SID + tsid = SID() + tsid.fromCanonical(userEntry) + packetSid = pack(" Date: Wed, 12 Jul 2023 23:13:15 -0700 Subject: [PATCH 2/2] Support graceful fallback for DCs that don't support SID lookups --- impacket/examples/secretsdump.py | 103 +++++++++++++++++++++++++------ 1 file changed, 83 insertions(+), 20 deletions(-) diff --git a/impacket/examples/secretsdump.py b/impacket/examples/secretsdump.py index b35d9c0e60..586a00adb5 100644 --- a/impacket/examples/secretsdump.py +++ b/impacket/examples/secretsdump.py @@ -535,22 +535,24 @@ def DRSCrackNames(self, formatOffered=drsuapi.DS_NAME_FORMAT.DS_DISPLAY_NAME, resp = drsuapi.hDRSCrackNames(self.__drsr, self.__hDrs, 0, formatOffered, formatDesired, (name,)) return resp - def DRSGetNCChanges(self, userEntry): - if self.__drsr is None: - self.__connectDrds() + # Wrapper for calling _DRSGetNCChanges with a GUID + def DRSGetNCChangesGuid(self, userGuid): + dsName = drsuapi.DSNAME() + dsName['SidLen'] = 0 + dsName['Guid'] = string_to_bin(userGuid[1:-1]) + dsName['Sid'] = '' + dsName['NameLen'] = 0 + dsName['StringName'] = ('\x00') + dsName['structLen'] = len(dsName.getData()) - LOG.debug('Calling DRSGetNCChanges for %s ' % userEntry) - request = drsuapi.DRSGetNCChanges() - request['hDrs'] = self.__hDrs - request['dwInVersion'] = 8 + return self._DRSGetNCChanges(userGuid, dsName) - request['pmsgIn']['tag'] = 8 - request['pmsgIn']['V8']['uuidDsaObjDest'] = self.__NtdsDsaObjectGuid - request['pmsgIn']['V8']['uuidInvocIdSrc'] = self.__NtdsDsaObjectGuid + # Wrapper for calling _DRSGetNCChanges with a SID + def DRSGetNCChangesSid(self, userSid): # Convert string SID to 2.4.2.2 packet SID tsid = SID() - tsid.fromCanonical(userEntry) + tsid.fromCanonical(userSid) packetSid = pack("