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| qqd 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_