o
    Žõ±jÜ  ã                   @   s~   d dl Z d dl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 G dd„ dƒZedkr=e ¡ Zeeeƒƒ dS dS )	é    N)ÚAIOWinRegHive)Úlogger)ÚUniversalEncoder)Ú*c                   @   s€   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	d
„Zdd„ Zdd„ Z	dd„ Z
eddd„ƒZeddd„ƒZeddd„ƒZedd„ ƒZdS )ÚOffineRegistryzÝ
	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   s4   d | _ d | _d | _d | _d | _d | _d | _d | _d S ©N)Úsam_hiveÚsecurity_hiveÚsystem_hiveÚsoftware_hiveÚsystemÚsamÚsecurityÚsoftware©Úself© r   ú“/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/pypykatz/registry/offline_parser.pyÚ__init__   s   
zOffineRegistry.__init__c                 C   sŠ   t | jƒ| _| j ¡ }| j ¡  | jrt| j|ƒ| _| j ¡  | jr0t	| j|| jƒ| _
| j
 ¡  | jr?t| j|ƒ| _| j ¡  |  ¡  d S r   )ÚSYSTEMr
   r   Úget_bootkeyÚget_secretsr   ÚSAMr   r	   ÚSECURITYr   r   ÚSOFTWAREr   Úget_default_logonÚcleanup)r   Úbootkeyr   r   r   r   #   s   




zOffineRegistry.get_secretsc                 C   s0   | j | j| jfD ]}z| ¡  W q   Y qd S r   )r
   r	   r   Úclose)r   Úhiver   r   r   r   6   s   üzOffineRegistry.cleanupFc                 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OffineRegistry.to_filec                 C   s   t j|  ¡ tddd�S )Né   T)ÚclsÚindentÚ	sort_keys)ÚjsonÚdumpsÚto_dictr   r   r   r   r   r&   D   s   zOffineRegistry.to_jsonc                 C   sR   i }| j  ¡ |d< | jr| j ¡ |d< | jr| j ¡ |d< | jr'| j ¡ |d< |S )Nr   r   r   r   )r   r1   r   r   r   ©r   Útr   r   r   r1   G   s   zOffineRegistry.to_dictc                 C   sJ   t | jƒ}| jr|t | jƒ7 }| jr|t | jƒ7 }| jr#|t | jƒ7 }|S r   )r%   r   r   r   r   r2   r   r   r   Ú__str__S   s   
zOffineRegistry.__str__Nc                 C   sR   t  | ¡}d }d }d }|rt  |¡}|rt  |¡}|r t  |¡}tj||||d�S )N)Úsam_buffÚsecurity_buffÚsoftware_buff)ÚioÚBytesIOr   Úfrom_buffer)Úsystem_dataÚsam_dataÚsecurity_dataÚsoftware_dataÚsystem_buffr5   r6   r7   r   r   r   Ú
from_bytes]   s   



zOffineRegistry.from_bytesc                 C   s   t j| |||dd�S )NT)Úsam_pathÚsecurity_pathÚsoftware_pathÚnotfile)r   Ú
from_files)r?   r5   r6   r7   r   r   r   r:   m   s   zOffineRegistry.from_bufferc              
   C   sî  t ƒ }z|dkr| }nt| dƒ}t|ƒ|_W n ty- } zt dt|ƒ ¡ |‚d }~ww |r[z|dkr8|}nt|dƒ}t|ƒ|_W n tyZ } zt dt|ƒ ¡ |‚d }~ww t 	d¡ |r�z|dkrj|}	nt|dƒ}	t|	ƒ|_
W n tyŒ } zt dt|ƒ ¡ |‚d }~ww t 	d¡ |r¿z|dkrœ|}
nt|dƒ}
t|
ƒ|_W n ty¾ } zt dt|ƒ ¡ |‚d }~ww t 	d¡ | ¡  z|	 ¡  W n   Y z| ¡  W n   Y z| ¡  W n   Y z|
 ¡  W |S    Y |S )	NTÚrbz&Failed to open SYSTEM hive! Reason: %sz#Failed to open SAM hive! Reason: %sz5SAM hive path not supplied! Parsing SAM will not workz(Failed to open SECURITY hive! Reason: %sz?SECURITY hive path not supplied! Parsing SECURITY will not workz?SOFTWARE hive path not supplied! Parsing SOFTWARE will not work)r   r#   r   r
   Ú	Exceptionr   Úerrorr%   r   Úwarningr	   r   r   r   )Úsystem_pathrA   rB   rC   rD   ÚpoÚsys_hiveÚer   Úsec_hiveÚsof_hiver   r   r   rE   q   s„   
€þ
€þ

€þ

€þ

ýzOffineRegistry.from_filesc                  C   sÂ  t  d¡ zddlm}  ddlm} dd l}dd l}dd l}dd l	}W n t
y9 } z
t  dt|ƒ ¡ ‚ d }~ww | | ¡ | d¡ ¡ ¡}| | ¡ | d¡ ¡ ¡}| | ¡ | d¡ ¡ ¡}	d|fd|fd	|	fg}
t  d
¡ z
| ƒ }| |¡ W n t
y’ } zt  dt|ƒ ¡ |‚d }~ww t  d¡ i }|
D ]I\}}t  d| ¡ z|j|j|dd�}| ||¡ | ¡  W n t
yÙ } zt  d|t|ƒf ¡ W Y d }~qœd }~ww t  d| ¡ |||< qœd }d|v �rzt |d|v r÷|nd d	|v rþ|	nd ¡}W n  t
�y } zdd l}| ¡  W Y d }~n
d }~ww t  d¡ t  d¡ |
D ]4\}}z| |¡ W n  t
�yU } zt  d||t|ƒf ¡ W Y d }~�q*d }~ww t  d| ¡ �q*|S )Nz$Obtaining registry from local systemr   )ÚProcessManipulator)Ú	SE_BACKUPzBCould not import necessary packages! Are you on Windows? Error: %sr+   r   r   r   z Obtaining SE_BACKUP privilege...zMFailed to obtain SE_BACKUP privilege! Registry dump will not work! Reason: %szObtaining SE_BACKUP OK!zDumping %s...i   )ÚaccesszDumping %s FAILED!! Reason: %szDumping %s OK!z&Failed to dump SYSTEM hive, exiting...zCleaning up temp fileszpFailed to clean up temp file for %s! Sensitive files might have been left on the filesystem! Path: %s Reason: %szCleanup for %s OK!)r   ÚdebugÚ*pypykatz.commons.winapi.processmanipulatorrP   Ú!pypykatz.commons.winapi.constantsrQ   ÚwinregÚtempfileÚosÚntpathrG   rH   r%   ÚjoinÚ
gettempdirÚurandomÚhexÚset_privilegeÚOpenKeyÚHKEY_LOCAL_MACHINEÚSaveKeyÚCloser   rE   Ú	tracebackÚ	print_excÚremove)rP   rQ   rV   rW   rX   rY   rM   Úsam_nameÚsystem_nameÚsecurity_nameÚ	locationsrK   Údumped_namesÚreg_nameÚlocationÚkeyrc   r   r   r   Úfrom_live_systemÀ   s~   
€þý
€þ
"€ÿ

*€þ

&€ÿzOffineRegistry.from_live_system)F)NNN)NNNF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r*   r&   r1   r4   Ústaticmethodr@   r:   rE   rn   r   r   r   r   r      s"    

Nr   Ú__main__)r/   Úaiowinreg.hiver   Úpypykatz.registryr   Úpypykatz.commons.commonr   Úpypykatz.registry.sam.samÚ#pypykatz.registry.security.securityÚpypykatz.registry.system.systemÚ#pypykatz.registry.software.softwarer   ro   rn   rK   Úprintr%   r   r   r   r   Ú<module>   s    tþ