o
    Žõ±jf  ã                   @   s¬   d dl Z d dl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 d dlT d dlT d dlT d dlT dd	„ ZG d
d„ dƒZdd„ Zdd„ ZedkrTeƒ  dS dS )é    N)ÚSMBConnectionFactory)Ú
SMBMachine)ÚRRPRPCÚSMBWinRegHive)Úlogger)ÚUniversalEncoder)Ú*c                 Ã   s   �t | ƒ d S ©N)Úprint)Údata© r   ú’/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/pypykatz/registry/remote_parser.pyÚprint_cb   s   €r   c                   @   sd   e Zd ZdZefdd„Zedefdd„ƒZedd„ ƒZ	d	d
„ Z
ddd„Zdd„ Zdd„ Zdd„ ZdS )ÚRemoteRegistryzÝ
	This class represents an offline registry
	You will need to set at least the SYSTEM hive (to get bootkey)
	In case you have the SAM and/or SECURITY hives, it will parse them for the stored credentials/secrets as well.
	c                 C   sF   || _ || _d | _d | _d | _d | _d | _d | _d | _d | _	d | _
d S r	   )r   Ú
connectionÚrrprpcÚsam_hiveÚsecurity_hiveÚsystem_hiveÚsoftware_hiveÚsystemÚsamÚsecurityÚsoftware)Úselfr   r   r   r   r   Ú__init__   s   
zRemoteRegistry.__init__Úurlc                 Ã   sB   �t  | ¡}| ¡ }| ¡ I d H \}}|d ur|‚t |¡I d H S r	   )r   Úfrom_urlÚget_connectionÚloginr   Úfrom_smb_connection)r   Úconnection_factoryr   Ú_Úerrr   r   r   r   +   s   €
zRemoteRegistry.from_urlc                 Ã   s   �t | ƒ}|S r	   )r   )r   Úpor   r   r   r    5   s   €z"RemoteRegistry.from_smb_connectionc           	      Ã   s4  �t | jƒ}|  d¡I d H  | d¡I d H \}}|d ur|‚d }tdƒD ]0}|  d¡I d H  t d¡I d H  |  d|d  ¡I d H  t | j¡I d H \| _	}|d urSq$ nt
dƒ‚|  d	¡I d H  �z	zÁt| j	d
| jd�| _| j ¡ I d H  t| j	d| jd�| _| j ¡ I d H  t| j	d| jd�| _| j ¡ I d H  t| j	d| jd�| _| j ¡ I d H  |  d¡I d H  t| jƒ| _| j ¡ I d H }| j ¡ I d H  |  d¡I d H  t| j|ƒ| _| j ¡ I d H  |  d¡I d H  t| j|| jƒ| _| j ¡ I d H  |  d¡I d H  t| j|ƒ| _| j ¡ I d H  | jjd u�r#| j | jj| jj¡ W n t
�y; } z
dd l}|  ¡  |‚d }~ww W |  d¡I d H  | j| j| j| jfD ]}z
| !¡ I d H  W �qO   Y �qO|  d¡I d H  d S |  d¡I d H  | j| j| j| jfD ]}z
| !¡ I d H  W �q~   Y �q~|  d¡I d H  w )Nz&[+] Enabling RemoteRegistry service...r   é   z#[+] Waiting for service to start...é   z6[+] Connecting to RemoteRegistry service... Attempt %sé   z,Could not connect to RemoteRegistry service!z'[+] Connected to RemoteRegistry servicezHKLM\SYSTEM)r   zHKLM\SAMzHKLM\SECURITYzHKLM\SOFTWAREz[+] Parsing SYSTEM hive...z[+] Parsing SAM hive...z[+] Parsing SECURITY hive...z[+] Parsing SOFTWARE hive...r   z[+] Closing remote hives...z	[+] Done!)"r   r   r   Úenable_serviceÚrangeÚasyncioÚsleepr   Úfrom_smbconnectionr   Ú	Exceptionr   r   Úsetupr   r   r   ÚSYSTEMr   Úget_bootkeyÚget_secretsÚSAMr   ÚSECURITYr   ÚSOFTWAREr   Úget_default_logonÚdefault_logon_userÚset_default_userÚdefault_logon_domainÚ	tracebackÚ	print_excÚclose)	r   Úmachiner"   r#   ÚiÚbootkeyÚer9   Úhiver   r   r   r1   :   s~   €
€€ý€úzRemoteRegistry.get_secretsFc                 C   sj   t |ddd��$}|dkr| t| ƒ¡ n| |  ¡ ¡ W d   ƒ d S W d   ƒ d S 1 s.w   Y  d S )NÚaÚ )ÚnewlineF)ÚopenÚwriteÚstrÚto_json)r   Ú	file_pathÚjson_formatÚfr   r   r   Úto_file}   s   ü"þzRemoteRegistry.to_filec                 C   s   t j|  ¡ tddd�S )Nr&   T)ÚclsÚindentÚ	sort_keys)ÚjsonÚdumpsÚto_dictr   )r   r   r   r   rG   „   s   zRemoteRegistry.to_jsonc                 C   sR   i }| j  ¡ |d< | jr| j ¡ |d< | jr| j ¡ |d< | jr'| j ¡ |d< |S )Nr/   r2   r3   r4   )r   rQ   r   r   r   ©r   Útr   r   r   rQ   ‡   s   zRemoteRegistry.to_dictc                 C   sJ   t | jƒ}| jr|t | jƒ7 }| jr|t | jƒ7 }| jr#|t | jƒ7 }|S r	   )rF   r   r   r   r   rR   r   r   r   Ú__str__“   s   
zRemoteRegistry.__str__N)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   ÚstaticmethodrF   r   r    r1   rK   rG   rQ   rT   r   r   r   r   r      s    	

Cr   c                  Ã   sH   �dd l } t | j¡ d}t |¡I d H }| ¡ I d H  tt|ƒƒ d S )Nr   z2smb2+ntlm-password://vagrant:vagrant@192.168.56.11)	Úloggingr   ÚsetLevelÚDEBUGr   r   r1   r
   rF   )rZ   r   r$   r   r   r   Úamain�   s   €r]   c                  C   s   dd l } |  tƒ ¡ d S )Nr   )r*   Úrunr]   )r*   r   r   r   Úmain¥   s   r_   Ú__main__)rO   r*   Ú!aiosmb.commons.connection.factoryr   Ú!aiosmb.commons.interfaces.machiner   Ú*aiosmb.dcerpc.v5.interfaces.remoteregistryr   r   Úpypykatz.registryr   Úpypykatz.commons.commonr   Úpypykatz.registry.sam.asamÚ$pypykatz.registry.security.asecurityÚ pypykatz.registry.system.asystemÚ$pypykatz.registry.software.asoftwarer   r   r]   r_   rU   r   r   r   r   Ú<module>   s&    
ÿ