o
    j6                     @   s   d dl T d dlmZ d dlT d dlT d dlT d dlT 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rre
jd d e Ze Zed dS dS )   )*)enable_debug_privilege    )PROCESSOR_ARCHITECTURE)loggerNc                   @   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/local/live_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   s4   t  }| |_|j|_|j|_|j|j |_||_|S r   )r   r	   lpBaseOfDllr
   SizeOfImager   r   r   )r	   module_infor   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__2   s   (zModule.__str__N)__name__
__module____qualname__r   r   staticmethodr   r!   r   r   r   r   r      s    
r   c                   @   s@   e Zd Zdd Ze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 S r   )BaseAddressAllocationBaseAllocationProtect
RegionSize
EndAddressdatar   r   r   r   r   6   s   
zPage.__init__c                 C   s>   t  }| j|_| j|_| j|_t| jd|_| j| j |_|S )Ni  @)r&   r'   r(   r)   minr*   r+   )	page_infopr   r   r   r   ?   s   z
Page.parsec                 C   s   t || j| j| _d S r   )ReadProcessMemoryr'   r*   r,   )r   process_handler   r   r   	read_dataI   s   zPage.read_datac                 C   r   r   )r'   r+   r   r   r   r   r   L   r   zPage.inrangec                 C   s   t || jkr	g S t|| j| j}g }d}t |t |krH||}|dkr(|S ||| | j  ||d d  }||d 7 }t |t |ks|S )Nr   r   )lenr*   r0   r'   findappend)r   patternr1   r,   floffsetmarkerr   r   r   searchO   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!   _   s   zPage.__str__N)
r"   r#   r$   r   r%   r   r2   r   r;   r!   r   r   r   r   r&   5   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 )/BufferedLiveReaderc                 C   s   || _ g | _d | _d | _d S r   )readerr   current_segmentcurrent_position)r   r=   r   r   r   r   d   s   
zBufferedLiveReader.__init__c                 C   s   | j D ]}||r|| _|| _ dS q| jj D ]"}||r:|| jj t|}| j 	| || _|| _ dS qt
d| )z
		
		Nz4Memory address 0x%08x is not in process memory space)r   r   r>   r?   r=   r2   r1   copydeepcopyr6   	Exception)r   requested_positionpage
newsegmentr   r   r   _select_segmentk   s    



z"BufferedLiveReader._select_segmentc                 C      | j S r   )r=   r   r   r   r   
get_reader      zBufferedLiveReader.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   r      z.Seek function whence value must be between 0-2z5Seek would cross memory segment boundaries (use move)N)r>   r'   r?   r+   rB   r   )r   r9   whencetr   r   r   seek   s   zBufferedLiveReader.seekc                 C   s   |  | dS )z@
		Moves the buffer to a virtual address specified by address
		N)rF   )r   addressr   r   r   move   s   
zBufferedLiveReader.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   r   )r=   processor_architecturer   AMD64r?   rM   )r   	alignmentr9   offset_to_alignedr   r   r   align   s   
zBufferedLiveReader.alignc                 C   rG   )z)
		Returns the current virtual address
		)r?   r   r   r   r   tell   s   zBufferedLiveReader.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   rB   r,   r'   )r   lengthrL   r   r   r   peek   s   
"zBufferedLiveReader.peekr3   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
		r3   zYou shouldnt be doing thisNrX   )rB   r>   remaining_lenr?   r+   r,   r'   r   )r   r   rL   old_new_posr   r   r   read   s   

 zBufferedLiveReader.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
		rP   littleT	byteordersignedrQ   r=   rR   r   rS   int
from_bytesr]   r   r   r   r   read_int      zBufferedLiveReader.read_intc                 C   r^   )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
		rP   r_   Fr`   rQ   rc   r   r   r   r   	read_uint   rg   zBufferedLiveReader.read_uintc                 C   s$   | j j|}|dkrdS || j S )z:
		Searches for a pattern in the current memory segment
		r3   )r>   r,   r5   r?   )r   r7   posr   r   r   r5      s   
zBufferedLiveReader.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
		r3   Tr   )r>   r,   r5   r6   start_address)r   r7   ri   
last_foundr   r   r   find_all   s   zBufferedLiveReader.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   r3   )r=   r;   r4   )r   r7   pos_sr   r   r   find_global  s   zBufferedLiveReader.find_globalrQ   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   ro   r   r   r   find_all_global  s   z"BufferedLiveReader.find_all_globalc                 C   s   |  | |  S r   )rO   rh   )r   ri   r   r   r   get_ptr  s   
zBufferedLiveReader.get_ptrc                 C   sL   | j jtjkr| | tj| dddd}|d | S | | |  S )NrQ   r_   Tr`   )	r=   rR   r   rS   rO   rd   re   r]   rh   )r   ri   ptrr   r   r   get_ptr_with_offset  s   

z&BufferedLiveReader.get_ptr_with_offsetFc                 C   s   | j j||||d}|S )N)
find_firstreverse_order)r=   search_module)r   module_namer7   rt   ru   rL   r   r   r   find_in_module'  s   z!BufferedLiveReader.find_in_module)r   r   )r3   rQ   FF)r"   r#   r$   r   rF   rH   rM   rO   rV   rW   rZ   r]   rf   rh   r5   rl   rn   rp   rq   rs   rx   r   r   r   r   r<   c   s&    


		
	r<   c                   @   s\   e Zd Zd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ddZdS )
LiveReaderN	lsass.exec                 C   sJ   d | _ || _|| _|| _d | _d | _g | _g | _d | _| 	  | 
  d S r   )rR   process_namer1   process_pidr?   BuildNumbermodulesr   msv_dll_timestampsanity_checksetup)r   r1   r}   r~   r   r   r   r   -  s   zLiveReader.__init__c                 C   sH   t jdk}t dk}t d}|dkrtd||kr"tddS )z
		Check if user is insane
		Windows API functions don't like when a 32 bit process is accessing a 64 bit process's memory space,
		Therefore you must use a 64 bit python on a 64bit Windows and a 32bit python on a 32bit Windows
		l        Windows64FzThis will only run on WindowszPPython interpreter must be the same architecure of the OS you are running it on.N)sysmaxsizeplatformsystemmachineendswithrB   )r   is_python_64
is_windowsis_windows_64r   r   r   r   <  s   
zLiveReader.sanity_checkc                 C   sD  t dd t  t dd t }t|jjj| _t dd t	
t	jd}t	|d\}}t|| _| jd u r| jd u re| jd u rGtdt dd t| j| _t dd	| j| jf  t dd
 t dd| j  ttd| j| _| jd u rtdt  nt d t dd t| j}|D ]5}t| j|}t d| d}t| dkrtt|j }|| _!t"| j|}	| j#$t%&||	| qt ddt'| j#  |j(}
|
|j)k rt*| j|
}| j+$t,&| |
|j-7 }
|
|j)k st ddt'| j+  | j+D ]}| j#D ]}|.|j/dkr|j+$| qqd S )Nr   zEnabling debug privilegezGetting generic system infozGetting build numberz-SOFTWARE\Microsoft\Windows NT\CurrentVersion\CurrentBuildNumberz5Process name or PID or opened handle must be providedzSearching for lsass.exez%s found at PID %dz$Checking Lsass.exe protection statusz
Opening %sFz#Failed to open lsass.exe Reason: %szUsing pre-defined handlezEnumerating modulesr   z
msv1_0.dllzFound %d moduleszFound %d pagesT)0r   logr   GetSystemInfor   idwwProcessorArchitecturerR   winregOpenKeyHKEY_LOCAL_MACHINEQueryValueExrd   r   r1   r~   r}   rB   pid_for_nameOpenProcessPROCESS_ALL_ACCESSctypesWinErrordebugEnumProcessModulesGetModuleFileNameExWntpathbasenamelowerosstatst_ctimer   GetModuleInformationr   r6   r   r   r4   lpMinimumApplicationAddresslpMaximumApplicationAddressVirtualQueryExr   r&   r*   r   r'   )r   sysinfokeybuildnumberrL   module_handlesmodule_handlemodule_file_pathr   modinfocurrent_addressr.   rD   modr   r   r   r   K  sb   



	







zLiveReader.setupc                 C   rG   r   )r1   r   r   r   r   get_handler  rI   zLiveReader.get_handlerrQ   c                 C   s(   g }| j D ]}|j|@ r|| q|S r   )r   r)   r6   )r   ro   rL   rD   r   r   r   
get_memory  s   


zLiveReader.get_memoryc                 C   s   t | S r   )r<   r   r   r   r   get_buffered_reader  s   zLiveReader.get_buffered_readerc                 C   s0   | j D ]}|j | dkr|  S qd S )Nr3   )r   r	   r   r5   )r   rw   r   r   r   r   get_module_by_name  s
   
zLiveReader.get_module_by_nameFc                 C   s`   |  |}|d u rtd| g }|jD ]}|||| j7 }t|dkr-|du r-|  S q|S )NzCould not find module! %sr   T)r   rB   r   r;   r1   r4   )r   rw   r7   rt   ru   r   needlesrD   r   r   r   rv     s   

zLiveReader.search_modulec                 C   s0   g }| j D ]}|j|@ r|||| j7 }q|S r   )r   r)   r;   r1   )r   r7   ro   rL   rD   r   r   r   r;     s   

zLiveReader.search)Nr|   Nry   rz   )r"   r#   r$   r   r   r   r   r   r   r   rv   r;   r   r   r   r   r{   ,  s    
B

r{   __main__)leveli   )common.live_reader_ctypescommon.privilegesr   common.psapicommon.versioncommon.kernel32common.fileinfo!minidump.streams.SystemInfoStreamr   pypykatzr   r   r@   r   r   r   r   r   r&   r<   r{   r"   basicConfiglrr   blrrO   r   r   r   r   <module>   s4   . J 