o
    Žõ±ji  ã                   @   sÊ   d dl Z d dlmZ d dlT ddlT G dd„ dƒZG dd„ dƒZed	krce je j	d
� dZ
ee
ƒZeejj d¡ƒ e e¡Zeejƒ e ee¡Ze ¡ ZejD ]Zeeeje ƒƒ qQedƒ dS dS )é    N)Úpypykatz)Ú*é   c                   @   s    e Zd Zdd„ Zedd„ ƒZdS )ÚRekallModulec                 C   s   d | _ d | _d | _d | _d S ©N)ÚnameÚ
start_addrÚend_addrÚsize©Úself© r   úŸ/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/pypykatz/commons/readers/rekall/rekallreader.pyÚ__init__   s   
zRekallModule.__init__c                 C   s@   t ƒ }t| jƒ ¡ |_t| jƒ|_t| jƒ|_	|j|j	 |_
|S r   )r   ÚstrÚBaseDllNameÚlowerr   ÚintÚDllBaser   ÚSizeOfImager
   r	   )ÚmoduleÚrmr   r   r   Úfrom_module   s   zRekallModule.from_moduleN)Ú__name__Ú
__module__Ú__qualname__r   Ústaticmethodr   r   r   r   r   r      s    r   c                   @   s¶   e Zd Zd)dd„Zdd„ Zed)dd„ƒZe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„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd+d'd(„ZdS ),ÚRekallReaderNc                 C   s@   d| _ d| _d| _d| _d| _i | _|| _d| _|| _d| _	dS )zà
		Timestamp override will change the msv_dll_timestamp value.
		If None > no change
		If 0 > it disables the ANIT_MIMIKATZ structs on certain builds
		If 1 > it enforces to use the ANTI_MIMIKATZ structs on certain builds
		N)
ÚsessionÚ
lsass_taskÚccÚtask_asÚcur_posÚmodulesÚtimestamp_overrideÚprocessor_architectureÚBuildNumberÚmsv_dll_timestamp)r   r$   Úbuildnumberr   r   r   r   "   s   
zRekallReader.__init__c                 C   sp   | j j ¡ | _|  ¡  | j ¡ | _|  ¡ | _| j	s|  
¡ | _	| jr2| jdkr*dt_n| jdkr2dt_|  ¡  d S )Nr   iÿÿGSr   i HS)r   Úpluginsr    Úsearch_lsassr   Úget_process_address_spacer!   Úget_archr%   r&   Úget_buildnumberr$   Úsysinfor'   Úload_modulesr   r   r   r   Úsetup8   s   



zRekallReader.setupc                 C   s@   t  d|  ¡ tj| g d¢t  d¡ddddgd�}t |||¡S )	NzInvoking recall on file %s)ÚrsdsÚpeÚwindows_kernel_filer   Úfulll       
 z4https://github.com/google/rekall-profiles/raw/masterz#http://profiles.rekall-forensic.com)ÚfilenameÚ
autodetectÚloggerÚautodetect_build_localÚautodetect_scan_lengthÚprofile_path)ÚloggingÚinfor   ÚSessionÚ	getLoggerr   Úfrom_session)Úmemory_filer$   r(   Úrsessionr   r   r   Úfrom_memory_fileH   s   ú	zRekallReader.from_memory_filec                 C   s   t ||ƒ}| |_| ¡  |S r   )r   r   r0   )r   r$   r(   Úrrr   r   r   r?   W   s   
zRekallReader.from_sessionc                 C   s   t | jjjƒS r   )r   r   ÚPebÚOSBuildNumberr   r   r   r   r-   ^   s   zRekallReader.get_buildnumberc                 C   s&   | j j d¡dd … dkrtjS tjS )NÚarchéþÿÿÿÚ64)r   ÚprofileÚmetadataÚKatzSystemArchitectureÚX64ÚX86r   r   r   r   r,   a   s   zRekallReader.get_archc                 C   sN   |du r| j tjkrd}nd}| j| }|dkrdS || | }|  |¡ dS )zD
		Repositions the current reader to match architecture alignment
		Né   é   r   )r%   rK   rL   r"   Úread)r   Ú	alignmentÚoffsetÚoffset_to_alignedr   r   r   Úalignf   s   

zRekallReader.alignc                 C   s   | j S )z)
		Returns the current virtual address
		©r"   r   r   r   r   Útellv   s   zRekallReader.tellc                 C   ó8   | 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
		rN   ÚlittleT©Ú	byteorderÚsignedrO   ©r%   rK   rL   r   Ú
from_bytesrP   r   r   r   r   Úread_int|   ó   zRekallReader.read_intc                 C   rW   )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
		rN   rX   FrY   rO   r\   r   r   r   r   Ú	read_uint‡   r_   zRekallReader.read_uintc                 C   s   |   |¡ |  ¡ S r   )Úmover`   ©r   Úposr   r   r   Úget_ptr’   s   
zRekallReader.get_ptrc                 C   sJ   | j tjkr|  |¡ tj|  d¡ddd�}|d | S |  |¡ |  ¡ S )NrO   rX   TrY   )r%   rK   rL   ra   r   r]   rP   r`   )r   rc   Úptrr   r   r   Úget_ptr_with_offset–   s   

z RekallReader.get_ptr_with_offsetc                 C   s
   || _ d S r   rU   rb   r   r   r   ra       s   
zRekallReader.movec                 C   s"   | j  | j|¡}|  j|7  _|S r   ©r!   rP   r"   )r   r
   Údatar   r   r   rP   £   s   zRekallReader.readc                 C   s   | j  | j|¡S r   rg   )r   r
   r   r   r   Úpeek¨   s   zRekallReader.peekc                 C   sv   | j  ¡ | _| j  ¡ D ]-}t |¡| jt|jƒ 	¡ < t|jƒ 	¡ dkr8t
|jƒ| _| jdkr8| jj d¡ d| _qd S )Nz
msv1_0.dllr   z™WARNING! msv.dll timestamp not found! This could cause errors with older windows builds. You might need to specify it manually for anti_mimikatz structs!)r   r+   r!   Úget_load_modulesr   r   r#   r   r   r   r   ÚTimeDateStampr'   r   r;   r<   )r   r   r   r   r   r/   «   s   
€úzRekallReader.load_modulesc                 C   s€   | j j d¡ g }| j jjdd� ¡ D ]}| |¡ qt|ƒdkr(| j j d¡ t|ƒdkr2tdƒ‚|d | _	| j
 | j	¡ d S )NzSearching LSASS processz	lsass.exe)Ú
proc_regexr   zAWARNING! Multiple processes matched the filter!! Using first one!r   zLSASS.exe not found!)r   r;   r<   r)   ÚpslistÚfilter_processesÚappendÚlenÚ	Exceptionr   r    ÚSwitchProcessContext)r   ÚtasksÚtaskr   r   r   r*   µ   s   
zRekallReader.search_lsassc                 C   s\   | j  ¡ D ]&}| j | j j¡}|std| ƒ‚t|jƒ ¡ |kr!q| j	j
 d¡ |  S d S )NzCant get module! %szFound module!)r   rj   r!   ÚvtopÚ
obj_offsetrq   r   r   r   r   r;   r<   )r   Úmodule_namer   Úprocess_offsetr   r   r   Úload_moduleÄ   s   ÷zRekallReader.load_moduleFc           
      C   sŽ   |  ¡ | jvrtd| ƒ‚| j|  ¡  }g }tjj|g| j ¡ d�}| jj	 
d|j ¡ |jt|jƒt|jƒd�D ]\}}	| t|ƒ¡ q9|S )Nz'Module is not in lsass emmory space! %s)ÚneedlesÚaddress_spacezmodule.start_addr %s)rR   Úmaxlen)r   r#   rq   ÚrekallÚscanÚMultiStringScannerr   r+   r   r;   r<   r   r   r
   ro   )
r   rw   ÚpatternÚ
find_firstÚreverse_orderr   ÚresÚscannerÚhitÚ_r   r   r   Úfind_in_moduleÑ   s   þ"zRekallReader.find_in_module)NNr   )FF)r   r   r   r   r0   r   rB   r?   r-   r,   rT   rV   r^   r`   rd   rf   ra   rP   ri   r/   r*   ry   r‡   r   r   r   r   r   !   s,    



r   Ú__main__)Úlevelz
memory.dmprF   zDone!)r;   Úpypykatz.pypykatzr   Úpypykatz.commons.commonÚ r   r   r   ÚbasicConfigÚDEBUGr@   ÚreaderÚprintr   rI   rJ   ÚKatzSystemInfoÚfrom_rekallreaderr.   ÚarchitectureÚpÚstartÚmimiÚlogon_sessionsÚluidr   r   r   r   r   Ú<module>   s(   	 ?


ô