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 |rz|dkrj|}	nt|d}	t|	|_
W n ty } ztdt|  |d }~ww t	d |rz|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 }~qd }~ww t d|  |||< qd }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