o
    Žõ±j,  ã                   @   s¼   d dl Z 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
G dd„ dƒZG dd„ dƒZG dd	„ d	ƒZG d
d„ dƒZedkr\ejdd� eƒ Ze ¡ Ze d¡ dS dS )é    N)ÚPROCESSOR_ARCHITECTURE)Úloggerc                   @   s0   e Zd Zdd„ Zdd„ Zedd„ ƒZdd„ Zd	S )
ÚModulec                 C   s4   d | _ d | _d | _d | _g | _d | _d | _d | _d S ©N)ÚnameÚbaseaddressÚsizeÚ
endaddressÚpagesÚversioninfoÚchecksumÚ	timestamp©Úself© r   úš/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/pypykatz/commons/readers/zipdump/reader.pyÚ__init__   s   
zModule.__init__c                 C   ó   | j |  ko| jk S   S r   )r   r	   ©r   Úaddrr   r   r   Úinrange   ó   zModule.inrangec                 C   s0   t ƒ }| |_||_||_|j|j |_||_|S r   )r   r   r   r   r	   r   )r   ÚbaseAddrÚbaseSizer   Úmr   r   r   Úparse!   s   zModule.parsec                 C   s(   d| j t| jƒt| jƒt| jƒ| jf S )Nz%s %s %s %s %s)r   Úhexr   r   r	   r   r   r   r   r   Ú__str__-   s   (zModule.__str__N)Ú__name__Ú
__module__Ú__qualname__r   r   Ústaticmethodr   r   r   r   r   r   r      s    
r   c                   @   sB   e Zd Zdd„ Zeddd„ƒZdd„ Zd	d
„ Zdd„ Zdd„ Z	dS )ÚPagec                 C   s.   d | _ d | _d | _d | _d | _d | _d | _d S r   )ÚfileobjÚBaseAddressÚAllocationBaseÚAllocationProtectÚ
RegionSizeÚ
EndAddressÚdatar   r   r   r   r   1   s   
zPage.__init__Nr   c                 C   s8   t ƒ }| |_||_||_||_t|dƒ|_|| |_|S )Ni  @)r"   r#   r$   r%   r&   Úminr'   r(   )r#   r$   r%   r'   r&   Úpr   r   r   r   ;   s   
z
Page.parsec                 C   s0   | j d urd S | j d¡ | j ¡ | _ d | _d S ©Nr   )r)   r#   ÚseekÚreadr   r   r   r   Ú	read_dataF   s
   

zPage.read_datac                 C   r   r   )r$   r(   r   r   r   r   r   M   r   zPage.inrangec                 C   s˜   t |ƒ| jkr	g S |  ¡  t | j¡}g }d}t |ƒt |ƒkrJ| |¡}|dkr*|S | || | j ¡ ||d d … }||d 7 }t |ƒt |ƒks|S )Nr   éÿÿÿÿé   )	Úlenr'   r/   ÚcopyÚdeepcopyr)   ÚfindÚappendr$   )r   Úpatternr)   ÚflÚoffsetÚmarkerr   r   r   ÚsearchP   s   
úzPage.searchc                 C   s   d| j | j| j| jf S )Nz0x%08x 0x%08x %s 0x%08x)r$   r%   r&   r'   r   r   r   r   r   a   s   zPage.__str__r,   )
r   r   r    r   r!   r   r/   r   r;   r   r   r   r   r   r"   0   s    

r"   c                   @   s¦   e Zd Zdd„ Zdd„ Zdd„ Zd*dd	„Zd
d„ Zd+dd„Zdd„ Z	dd„ Z
d,dd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd-d!d"„Zd#d$„ Zd%d&„ Zd.d(d)„ZdS )/ÚBufferedZipDumpReaderc                 C   s   || _ g | _d | _d | _d S r   )Úreaderr
   Úcurrent_segmentÚcurrent_position)r   r=   r   r   r   r   h   s   
zBufferedZipDumpReader.__init__c                 C   s|   | j D ]}| |¡r|| _|| _ dS q| jj D ]}| |¡r7| ¡  t |¡}| j  |¡ || _|| _ dS qt	d| ƒ‚)z
		
		Nz4Memory address 0x%08x is not in process memory space)
r
   r   r>   r?   r=   r/   r3   r4   r6   Ú	Exception)r   Úrequested_positionÚpageÚ
newsegmentr   r   r   Ú_select_segmento   s    

ý

úz%BufferedZipDumpReader._select_segmentc                 C   ó   | j S r   )r=   r   r   r   r   Ú
get_reader†   ó   z BufferedZipDumpReader.get_readerr   c                 C   sf   |dkr| j j| }n|dkr| j| }n|dkr | j j| }ntdƒ‚| j  |¡s.tdƒ‚|| _dS )aD  
		Changes the current address to an offset of offset. The whence parameter controls from which position should we count the offsets.
		0: beginning of the current memory segment
		1: from current position
		2: from the end of the current memory segment
		If you wish to move out from the segment, use the 'move' function
		r   r1   é   z.Seek function whence value must be between 0-2z5Seek would cross memory segment boundaries (use move)N)r>   r$   r?   r(   r@   r   )r   r9   ÚwhenceÚtr   r   r   r-   ‰   s   zBufferedZipDumpReader.seekc                 C   s   |   |¡ dS )z@
		Moves the buffer to a virtual address specified by address
		N)rD   )r   Úaddressr   r   r   Úmove    s   
zBufferedZipDumpReader.moveNc                 C   sR   |du r| j jtjkrd}nd}| j| }|dkrdS || | }|  |d¡ dS )zD
		Repositions the current reader to match architecture alignment
		Né   é   r   r1   )r=   Úprocessor_architecturer   ÚAMD64r?   r-   )r   Ú	alignmentr9   Úoffset_to_alignedr   r   r   Úalign§   s   
zBufferedZipDumpReader.alignc                 C   rE   )z)
		Returns the current virtual address
		)r?   r   r   r   r   Útell·   s   zBufferedZipDumpReader.tellc                 C   s@   | j | }| j |¡stdƒ‚| jj| j | jj || jj … S )z@
		Returns up to length bytes from the current memory segment
		ú#Would read over segment boundaries!)r?   r>   r   r@   r)   r$   )r   ÚlengthrJ   r   r   r   Úpeek½   s   
"zBufferedZipDumpReader.peekr0   c                 C   s    |dk rt dƒ‚|dkr+| j | j¡}|sdS | j}| jj| _| jj|| jj d… S | j| }| j |¡s:t dƒ‚| j}|| _| jj|| jj || jj … S )z‰
		Returns data bytes of size size from the current segment. If size is -1 it returns all the remaining data bytes from memory segment
		r0   zYou shouldnt be doing thisNrU   )r@   r>   Úremaining_lenr?   r(   r)   r$   r   )r   r   rJ   Úold_new_posr   r   r   r.   Æ   s   

 zBufferedZipDumpReader.readc                 C   ó:   | j jtjkrtj|  d¡ddd�S tj|  d¡ddd�S )z¯
		Reads an integer. The size depends on the architecture. 
		Reads a 4 byte small-endian singed int on 32 bit arch
		Reads an 8 byte small-endian singed int on 64 bit arch
		rM   ÚlittleT©Ú	byteorderÚsignedrN   ©r=   rO   r   rP   ÚintÚ
from_bytesr.   r   r   r   r   Úread_intÝ   ó   zBufferedZipDumpReader.read_intc                 C   rZ   )z³
		Reads an integer. The size depends on the architecture. 
		Reads a 4 byte small-endian unsinged int on 32 bit arch
		Reads an 8 byte small-endian unsinged int on 64 bit arch
		rM   r[   Fr\   rN   r_   r   r   r   r   Ú	read_uintè   rc   zBufferedZipDumpReader.read_uintc                 C   s$   | j j |¡}|dkrdS || j S )z:
		Searches for a pattern in the current memory segment
		r0   )r>   r)   r5   r?   )r   r7   Úposr   r   r   r5   ó   s   
zBufferedZipDumpReader.findc                 C   s@   g }d}	 | j j ||d ¡}|dkr	 |S | || j j ¡ q)zp
		Searches for all occurrences of a pattern in the current memory segment, returns all occurrences as a list
		r0   Tr1   )r>   r)   r5   r6   Ústart_address)r   r7   re   Ú
last_foundr   r   r   Úfind_allü   s   þüzBufferedZipDumpReader.find_allc                 C   s$   | j  |¡}t|ƒdkrdS |d S )zx
		Searches for the pattern in the whole process memory space and returns the first occurrence.
		This is exhaustive!
		r   r0   )r=   r;   r2   )r   r7   Úpos_sr   r   r   Úfind_global
  s   z!BufferedZipDumpReader.find_globalrN   c                 C   s   | j j||d�S )z�
		Searches for the pattern in the whole process memory space and returns a list of addresses where the pattern begins.
		This is exhaustive!
		)Úallocationprotect)r=   r;   )r   r7   rk   r   r   r   Úfind_all_global  s   z%BufferedZipDumpReader.find_all_globalc                 C   s   |   |¡ |  ¡ S r   )rL   rd   )r   re   r   r   r   Úget_ptr  s   
zBufferedZipDumpReader.get_ptrc                 C   sL   | j jtjkr|  |¡ tj|  d¡ddd�}|d | S |  |¡ |  ¡ S )NrN   r[   Tr\   )	r=   rO   r   rP   rL   r`   ra   r.   rd   )r   re   Úptrr   r   r   Úget_ptr_with_offset"  s   

z)BufferedZipDumpReader.get_ptr_with_offsetFc                 C   s   | j j||||d�}|S )N)Ú
find_firstÚreverse_order)r=   Úsearch_module)r   Úmodule_namer7   rp   rq   rJ   r   r   r   Úfind_in_module+  s   z$BufferedZipDumpReader.find_in_module)r   r   )r0   ©rN   ©FF)r   r   r    r   rD   rF   r-   rL   rS   rT   rW   r.   rb   rd   r5   rh   rj   rl   rm   ro   rt   r   r   r   r   r<   g   s&    


		
	r<   c                   @   s^   e Zd Zdd„ Zedd„ ƒZdd„ Zdd„ Zdd
d„Zdd„ Z	dd„ Z
ddd„Zddd„ZdS )ÚZipDumpReaderc                 C   s4   d | _ d | _d | _d | _d | _g | _g | _d| _d S r,   )ÚzipobjÚsysinforO   r?   ÚBuildNumberÚmodulesr
   Úmsv_dll_timestampr   r   r   r   r   1  s   
zZipDumpReader.__init__c                 C   s    t ƒ }t | d¡|_| ¡  |S )NÚr)rw   ÚzipfileÚZipFilerx   Úsetup)Úfilenamer=   r   r   r   Ú	from_file<  s   zZipDumpReader.from_filec           	   	   C   s\  | j  d¡�}t |¡| _W d   ƒ n1 sw   Y  t| jƒ t| jd d ƒ| _t 	dd¡ t
| jd d ƒ| _d}| jd D ]}| j t |d	 |d
 |d |¡¡ qBt 	ddt| jƒ ¡ | j  ¡ D ]*}| d¡du rqqgt
|d d… dƒ}| j  |¡j}| j t | j  |¡|||¡¡ qg| jD ]}| jD ]}| |j¡dkrª|j |¡ qšq•d S )Nzsysinfo.jsonÚsysInfoÚArchitecturer1   zGetting build numberrz   r   r{   ÚexePathr   r   zFound %d modulesz.binFéüÿÿÿé   T)rx   ÚopenÚjsonÚloadry   Úprintr   rO   r   Úlogr`   rz   r{   r6   r   r   r2   ÚnamelistÚendswithÚgetinfoÚ	file_sizer
   r"   r   r$   )	r   Úfr   Úmoduler�   r$   r'   rB   Úmodr   r   r   r€   C  s>   ÿ
üÿ	"

€þÿzZipDumpReader.setupc                 C   rE   r   )Úprocess_handler   r   r   r   Úget_handleri  rG   zZipDumpReader.get_handlerrN   c                 C   s(   g }| j D ]}|j|@ r| |¡ q|S r   )r
   r&   r6   )r   rk   rJ   rB   r   r   r   Ú
get_memoryl  s   


€zZipDumpReader.get_memoryc                 C   s   t | ƒS r   )r<   r   r   r   r   Úget_buffered_readers  s   z!ZipDumpReader.get_buffered_readerc                 C   s0   | j D ]}|j ¡  | ¡ ¡dkr|  S qd S )Nr0   )r{   r   Úlowerr5   )r   rs   r“   r   r   r   Úget_module_by_namev  s
   
ÿz ZipDumpReader.get_module_by_nameFc                 C   s\   |   |¡}|d u rtd| ƒ‚g }|jD ]}|| |¡7 }t|ƒdkr+|du r+|  S q|S )NzCould not find module! %sr   T)r™   r@   r
   r;   r2   )r   rs   r7   rp   rq   r“   ÚneedlesrB   r   r   r   rr   |  s   

€zZipDumpReader.search_modulec                 C   s,   g }| j D ]}|j|@ r|| |¡7 }q|S r   )r
   r&   r;   )r   r7   rk   rJ   rB   r   r   r   r;   ˆ  s   

€zZipDumpReader.searchNru   rv   )r   r   r    r   r!   r‚   r€   r•   r–   r—   r™   rr   r;   r   r   r   r   rw   0  s    
&

rw   Ú__main__r1   )Úleveli   )r~   Ú!minidump.streams.SystemInfoStreamr   Úpypykatzr   Úsysr3   ÚplatformÚosÚntpathr‰   r   r"   r<   rw   r   ÚbasicConfigÚlrr—   ÚblrrL   r   r   r   r   Ú<module>   s(   7 J_û