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	zt| 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&    
