
    it                    b   S r SrSrSrSSKrSSKrSSKrSSKrSSKrSSK	r	SSK
r
SSKrSSKrSSKJr  SSKJr  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKrSSKrSSKr\R2                  " S\R4                  " S5      5        \rSS jr\" SS9S 5       r\" SS9S 5       rS r Sr!Sr"Sr#Sr$Sr%Sr&Sr'Sr(Sr)Sr*Sr+Sr,Sr-Sr.Sr/Sr0S r1S!r2S"r3S#r4S$r5S%r6S& r7/ S'Qr8\7" \85      r9/ S(Qr:\7" \:5      r;/ S)Qr<\7" \<5      r=/ S*Qr>\7" \>5      r?/ S+Qr@\7" \@5      rA/ S,QrB\7" \B5      rC/ S-QrD\7" \D5      rE/ S.QrF\7" \F5      rGSrH/ S/QrI\7" \I5      rJ/ S0QrK\7" \K5      rLSrMS1rNS2rOS3rPS4rQS5rRS6rSS7rTS8rUS9rV/ S:QrW\7" \W5      rX/ S;QrY\7" \Y5      rZ/ S<Qr[\7" \[5      r\\]" \[5      r\\[ H(  u  r^r_\_\\;   a  \\\_   R                  \^5        M"  \^/\\\_'   M*     S= raS> rbS? rcS@ rdSA reSB rf " SC SD\g5      rh " SE SF5      ri " SG SH\j5      rk " SI SJ5      rlS1S1S1S1S2S2S4S4S4S4S4S7S7S7S1SK.rm\" SS9SL 5       rn\" SSMSN9SO 5       ro " SP SQ5      rp " SR SS\p5      rq\" SSSN9ST 5       rr " SU SV\p5      rs " SW SX5      rt " SY SZ\t5      ru " S[ S\\t5      rv " S] S^\t5      rw " S_ S`\t5      rx " Sa Sb\t5      ry " Sc Sd\t5      rz " Se Sf\t5      r{ " Sg Sh\t5      r| " Si Sj\t5      r} " Sk Sl\t5      r~ " Sm Sn\t5      r " So Sp\t5      r " Sq Sr\t5      r " Ss St\t5      r " Su Sv\t5      r " Sw Sx\t5      r " Sy Sz\s5      r " S{ S|5      r " S} S~\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S5      r\f" \
GR$                  \
GR&                  -   \
GR(                  -   S-   5      rS r\f" \
GR$                  \
GR&                  -   \
GR(                  -   5      r\" SS9 SS\\\\4   S\S\4S jj5       r " S S5      rS r\S:X  a  \" 5         gg)ab  pefile, Portable Executable reader module

All the PE file basic structures are available with their default names as
attributes of the instance returned.

Processed elements such as the import table are made available with lowercase
names, to differentiate them from the upper case basic structure names.

pefile has been tested against many edge cases such as corrupted and malformed
PEs as well as malware, which often attempts to abuse the format way beyond its
standard use. To the best of my knowledge most of the abuse is handled
gracefully.

Copyright (c) 2005-2023 Ero Carrera <ero.carrera@gmail.com>
zEro Carreraz2023.2.7zero.carrera@gmail.com    N)Counter)Union)sha1)sha256)sha512md5backslashreplace_backslashreplaceFc                 T   ^ ^ U(       d  [         R                  " T T5      $ U U4S jnU$ )Nc                    >^ [         R                  " TT5      " U 5      m[         R                  " U 5      U4S j5       nU$ )Nc                  <   > [         R                  " T" U 0 UD65      $ N)copymodcopy)argskwargscached_funcs     3C:\des-py\RoboSAPF\venv\Lib\site-packages\pefile.pywrapper-lru_cache.<locals>.decorator.<locals>.wrapper;   s     <<T <V <==    )	functools	lru_cachewraps)fr   r   maxsizetypeds     @r   	decoratorlru_cache.<locals>.decorator8   s<    ))'59!<			> 
	> r   )r   r   )r   r   r   r   s   ``  r   r   r   4   s&    ""7E22 r      )r   c                 <    U[         :  a  U $ [        U S-  5      S-  $ )N   )FILE_ALIGNMENT_HARDCODED_VALUEint)valfile_alignments     r   cache_adjust_FileAlignmentr(   E   s$    66
e%%r   c                 V    US:  a  UnU(       a  X-  (       a  U[        X-  5      -  $ U $ )N   )r%   )r&   section_alignmentr'   s      r   cache_adjust_SectionAlignmentr,   L   s2    6!* S4 C(?$@AAJr   c                 $    U R                  S5      $ Nr   )count)datas    r   count_zeroesr1   \   s    ::a=r          r#          iMZ  iZM  iNE  iLE  iLX  iVZ  iPE             l            i  i  c                 X    [        U  Vs/ s H  oS   US   4PM     snU -   5      $ s  snf )N   r   )dict)pairses     r   two_way_dictr=      s.    u-u!A$!u-566-s   '))IMAGE_DIRECTORY_ENTRY_EXPORTr   )IMAGE_DIRECTORY_ENTRY_IMPORTr9   )IMAGE_DIRECTORY_ENTRY_RESOURCE   )IMAGE_DIRECTORY_ENTRY_EXCEPTION   )IMAGE_DIRECTORY_ENTRY_SECURITY   )IMAGE_DIRECTORY_ENTRY_BASERELOC   )IMAGE_DIRECTORY_ENTRY_DEBUG   )IMAGE_DIRECTORY_ENTRY_COPYRIGHT   )IMAGE_DIRECTORY_ENTRY_GLOBALPTR   )IMAGE_DIRECTORY_ENTRY_TLS	   )!IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG
   )"IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT   )IMAGE_DIRECTORY_ENTRY_IAT   )"IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT   )$IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR   )IMAGE_DIRECTORY_ENTRY_RESERVED   ))IMAGE_FILE_RELOCS_STRIPPEDr9   )IMAGE_FILE_EXECUTABLE_IMAGErA   )IMAGE_FILE_LINE_NUMS_STRIPPEDrE   )IMAGE_FILE_LOCAL_SYMS_STRIPPEDrM   )IMAGE_FILE_AGGRESIVE_WS_TRIMr6   )IMAGE_FILE_LARGE_ADDRESS_AWAREr5   )IMAGE_FILE_16BIT_MACHINE@   )IMAGE_FILE_BYTES_REVERSED_LO   )IMAGE_FILE_32BIT_MACHINE   )IMAGE_FILE_DEBUG_STRIPPEDr#   )"IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP   )IMAGE_FILE_NET_RUN_FROM_SWAPr!   )IMAGE_FILE_SYSTEMr*   )IMAGE_FILE_DLLr3   )IMAGE_FILE_UP_SYSTEM_ONLY @  )IMAGE_FILE_BYTES_REVERSED_HIr4   ).)IMAGE_SCN_TYPE_REGr   )IMAGE_SCN_TYPE_DSECTr9   )IMAGE_SCN_TYPE_NOLOADrA   )IMAGE_SCN_TYPE_GROUPrE   )IMAGE_SCN_TYPE_NO_PADrM   )IMAGE_SCN_TYPE_COPYr6   )IMAGE_SCN_CNT_CODEr5   )IMAGE_SCN_CNT_INITIALIZED_DATArc   ) IMAGE_SCN_CNT_UNINITIALIZED_DATAre   )IMAGE_SCN_LNK_OTHERrg   )IMAGE_SCN_LNK_INFOr#   )IMAGE_SCN_LNK_OVERrj   )IMAGE_SCN_LNK_REMOVEr!   )IMAGE_SCN_LNK_COMDATr*   )IMAGE_SCN_MEM_PROTECTEDro   )IMAGE_SCN_NO_DEFER_SPEC_EXCro   )IMAGE_SCN_GPRELr4   )IMAGE_SCN_MEM_FARDATAr4   )IMAGE_SCN_MEM_SYSHEAP   )IMAGE_SCN_MEM_PURGEABLE   )IMAGE_SCN_MEM_16BITr   )IMAGE_SCN_MEM_LOCKEDi   )IMAGE_SCN_MEM_PRELOADi   )IMAGE_SCN_ALIGN_1BYTESr2   )IMAGE_SCN_ALIGN_2BYTESi    )IMAGE_SCN_ALIGN_4BYTESi  0 )IMAGE_SCN_ALIGN_8BYTESi  @ )IMAGE_SCN_ALIGN_16BYTESi  P )IMAGE_SCN_ALIGN_32BYTESi  ` )IMAGE_SCN_ALIGN_64BYTESi  p )IMAGE_SCN_ALIGN_128BYTESi   )IMAGE_SCN_ALIGN_256BYTESi   )IMAGE_SCN_ALIGN_512BYTESi   )IMAGE_SCN_ALIGN_1024BYTESi   )IMAGE_SCN_ALIGN_2048BYTESi   )IMAGE_SCN_ALIGN_4096BYTESi   )IMAGE_SCN_ALIGN_8192BYTESi   )IMAGE_SCN_ALIGN_MASKi   )IMAGE_SCN_LNK_NRELOC_OVFLi   )IMAGE_SCN_MEM_DISCARDABLEi   )IMAGE_SCN_MEM_NOT_CACHEDi   )IMAGE_SCN_MEM_NOT_PAGED   )IMAGE_SCN_MEM_SHARED   )IMAGE_SCN_MEM_EXECUTEi    )IMAGE_SCN_MEM_READi   @)IMAGE_SCN_MEM_WRITEr7   ))IMAGE_DEBUG_TYPE_UNKNOWNr   )IMAGE_DEBUG_TYPE_COFFr9   )IMAGE_DEBUG_TYPE_CODEVIEWrA   )IMAGE_DEBUG_TYPE_FPOrC   )IMAGE_DEBUG_TYPE_MISCrE   )IMAGE_DEBUG_TYPE_EXCEPTIONrG   )IMAGE_DEBUG_TYPE_FIXUPrI   )IMAGE_DEBUG_TYPE_OMAP_TO_SRCrK   )IMAGE_DEBUG_TYPE_OMAP_FROM_SRCrM   )IMAGE_DEBUG_TYPE_BORLANDrO   )IMAGE_DEBUG_TYPE_RESERVED10rQ   )IMAGE_DEBUG_TYPE_CLSIDrS   )IMAGE_DEBUG_TYPE_VC_FEATURErU   )IMAGE_DEBUG_TYPE_POGOrW   )IMAGE_DEBUG_TYPE_ILTCGrY   )IMAGE_DEBUG_TYPE_MPXr[   )IMAGE_DEBUG_TYPE_REPROr6   )&IMAGE_DEBUG_TYPE_EX_DLLCHARACTERISTICS   ))IMAGE_SUBSYSTEM_UNKNOWNr   )IMAGE_SUBSYSTEM_NATIVEr9   )IMAGE_SUBSYSTEM_WINDOWS_GUIrA   )IMAGE_SUBSYSTEM_WINDOWS_CUIrC   )IMAGE_SUBSYSTEM_OS2_CUIrG   )IMAGE_SUBSYSTEM_POSIX_CUIrK   )IMAGE_SUBSYSTEM_NATIVE_WINDOWSrM   )IMAGE_SUBSYSTEM_WINDOWS_CE_GUIrO   )IMAGE_SUBSYSTEM_EFI_APPLICATIONrQ   )'IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVERrS   )"IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVERrU   )IMAGE_SUBSYSTEM_EFI_ROMrW   )IMAGE_SUBSYSTEM_XBOXrY   )(IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATIONr6   )$)IMAGE_FILE_MACHINE_UNKNOWNr   )IMAGE_FILE_MACHINE_I386iL  )IMAGE_FILE_MACHINE_R3000ib  )IMAGE_FILE_MACHINE_R4000if  )IMAGE_FILE_MACHINE_R10000ih  )IMAGE_FILE_MACHINE_WCEMIPSV2ii  )IMAGE_FILE_MACHINE_ALPHAi  )IMAGE_FILE_MACHINE_SH3i  )IMAGE_FILE_MACHINE_SH3DSPi  )IMAGE_FILE_MACHINE_SH3Ei  )IMAGE_FILE_MACHINE_SH4i  )IMAGE_FILE_MACHINE_SH5i  )IMAGE_FILE_MACHINE_ARMi  )IMAGE_FILE_MACHINE_THUMBi  )IMAGE_FILE_MACHINE_ARMNTi  )IMAGE_FILE_MACHINE_AM33i  )IMAGE_FILE_MACHINE_POWERPCi  )IMAGE_FILE_MACHINE_POWERPCFPi  )IMAGE_FILE_MACHINE_IA64r#   )IMAGE_FILE_MACHINE_MIPS16if  )IMAGE_FILE_MACHINE_ALPHA64  )IMAGE_FILE_MACHINE_AXP64r   )IMAGE_FILE_MACHINE_MIPSFPUif  )IMAGE_FILE_MACHINE_MIPSFPU16if  )IMAGE_FILE_MACHINE_TRICOREi   )IMAGE_FILE_MACHINE_CEFi  )IMAGE_FILE_MACHINE_EBCi  )IMAGE_FILE_MACHINE_RISCV32i2P  )IMAGE_FILE_MACHINE_RISCV64idP  )IMAGE_FILE_MACHINE_RISCV128i(Q  )IMAGE_FILE_MACHINE_LOONGARCH32i2b  )IMAGE_FILE_MACHINE_LOONGARCH64idb  )IMAGE_FILE_MACHINE_AMD64id  )IMAGE_FILE_MACHINE_M32RiA  )IMAGE_FILE_MACHINE_ARM64id  )IMAGE_FILE_MACHINE_CEEi  ))IMAGE_REL_BASED_ABSOLUTEr   )IMAGE_REL_BASED_HIGHr9   )IMAGE_REL_BASED_LOWrA   )IMAGE_REL_BASED_HIGHLOWrC   )IMAGE_REL_BASED_HIGHADJrE   )IMAGE_REL_BASED_MIPS_JMPADDRrG   )IMAGE_REL_BASED_SECTIONrI   )IMAGE_REL_BASED_RELrK   )IMAGE_REL_BASED_MIPS_JMPADDR16rO   )IMAGE_REL_BASED_IA64_IMM64rO   )IMAGE_REL_BASED_DIR64rQ   )IMAGE_REL_BASED_HIGH3ADJrS   ))IMAGE_LIBRARY_PROCESS_INITr9   )IMAGE_LIBRARY_PROCESS_TERMrA   )IMAGE_LIBRARY_THREAD_INITrE   )IMAGE_LIBRARY_THREAD_TERMrM   )(IMAGE_DLLCHARACTERISTICS_HIGH_ENTROPY_VAr5   )%IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASErc   )(IMAGE_DLLCHARACTERISTICS_FORCE_INTEGRITYre   )"IMAGE_DLLCHARACTERISTICS_NX_COMPATrg   )%IMAGE_DLLCHARACTERISTICS_NO_ISOLATIONr#   )IMAGE_DLLCHARACTERISTICS_NO_SEHrj   ) IMAGE_DLLCHARACTERISTICS_NO_BINDr!   )%IMAGE_DLLCHARACTERISTICS_APPCONTAINERr*   )#IMAGE_DLLCHARACTERISTICS_WDM_DRIVERr3   )!IMAGE_DLLCHARACTERISTICS_GUARD_CFro   ).IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWAREr4   ))UNW_FLAG_EHANDLERr9   )UNW_FLAG_UHANDLERrA   )UNW_FLAG_CHAININFOrE   ))RAXr   )RCXr9   )RDXrA   )RBXrC   )RSPrE   )RBPrG   )RSIrI   )RDIrK   )R8rM   )R9rO   )R10rQ   )R11rS   )R12rU   )R13rW   )R14rY   )R15r[   r9   rA   rC   rE   rG   rI   rM   rO   rQ   ))	RT_CURSORr9   )	RT_BITMAPrA   )RT_ICONrC   )RT_MENUrE   )	RT_DIALOGrG   )	RT_STRINGrI   )
RT_FONTDIRrK   )RT_FONTrM   )RT_ACCELERATORrO   )	RT_RCDATArQ   )RT_MESSAGETABLErS   )RT_GROUP_CURSORrU   )RT_GROUP_ICONrY   )
RT_VERSIONr6   )RT_DLGINCLUDE   )RT_PLUGPLAY   )RT_VXDr   )RT_ANICURSOR   )
RT_ANIICON   )RT_HTML   )RT_MANIFEST   )^)LANG_NEUTRALr   )LANG_INVARIANT   )LANG_AFRIKAANS6   )LANG_ALBANIAN   )LANG_ARABICr9   )LANG_ARMENIAN+   )LANG_ASSAMESEM   )
LANG_AZERI,   )LANG_BASQUE-   )LANG_BELARUSIAN#   )LANG_BENGALIE   )LANG_BULGARIANrA   )LANG_CATALANrC   )LANG_CHINESErE   )LANG_CROATIAN   )
LANG_CZECHrG   )LANG_DANISHrI   )LANG_DIVEHIe   )
LANG_DUTCHr(  )LANG_ENGLISHrO   )LANG_ESTONIAN%   )LANG_FAEROESE8   )
LANG_FARSI)   )LANG_FINNISHrS   )LANG_FRENCHrU   )LANG_GALICIANV   )LANG_GEORGIAN7   )LANG_GERMANrK   )
LANG_GREEKrM   )LANG_GUJARATIG   )LANG_HEBREWrW   )
LANG_HINDI9   )LANG_HUNGARIANrY   )LANG_ICELANDICr[   )LANG_INDONESIAN!   )LANG_ITALIANr6   )LANG_JAPANESEr&  )LANG_KANNADAK   )LANG_KASHMIRI`   )
LANG_KAZAK?   )LANG_KONKANIW   )LANG_KOREAN   )LANG_KYRGYZrc   )LANG_LATVIAN&   )LANG_LITHUANIAN'   )LANG_MACEDONIAN/   )
LANG_MALAY>   )LANG_MALAYALAML   )LANG_MANIPURIX   )LANG_MARATHIN   )LANG_MONGOLIANP   )LANG_NEPALIa   )LANG_NORWEGIANr   )
LANG_ORIYAH   )LANG_POLISHr+  )LANG_PORTUGUESEr-  )LANG_PUNJABIF   )LANG_ROMANIANr1  )LANG_RUSSIAN   )LANG_SANSKRITO   )LANG_SERBIANrJ  )LANG_SINDHIY   )LANG_SLOVAK   )LANG_SLOVENIAN$   )LANG_SPANISHrQ   )LANG_SWAHILIA   )LANG_SWEDISH   )LANG_SYRIACZ   )
LANG_TAMILI   )
LANG_TATARD   )LANG_TELUGUJ   )	LANG_THAI   )LANG_TURKISH   )LANG_UKRAINIAN"   )	LANG_URDUr5   )
LANG_UZBEKC   )LANG_VIETNAMESE*   )LANG_GAELIC<   )LANG_MALTESE:   )
LANG_MAORI(   )LANG_RHAETO_ROMANCEr/  )
LANG_SAAMI;   )LANG_SORBIAN.   )	LANG_SUTU0   )LANG_TSONGA1   )LANG_TSWANA2   )
LANG_VENDA3   )
LANG_XHOSA4   )	LANG_ZULU5   )LANG_ESPERANTO   )
LANG_WALON   )LANG_CORNISH   )
LANG_WELSH   )LANG_BRETON   )g)SUBLANG_NEUTRALr   )SUBLANG_DEFAULTr9   )SUBLANG_SYS_DEFAULTrA   )SUBLANG_ARABIC_SAUDI_ARABIAr9   )SUBLANG_ARABIC_IRAQrA   )SUBLANG_ARABIC_EGYPTrC   )SUBLANG_ARABIC_LIBYArE   )SUBLANG_ARABIC_ALGERIArG   )SUBLANG_ARABIC_MOROCCOrI   )SUBLANG_ARABIC_TUNISIArK   )SUBLANG_ARABIC_OMANrM   )SUBLANG_ARABIC_YEMENrO   )SUBLANG_ARABIC_SYRIArQ   )SUBLANG_ARABIC_JORDANrS   )SUBLANG_ARABIC_LEBANONrU   )SUBLANG_ARABIC_KUWAITrW   )SUBLANG_ARABIC_UAErY   )SUBLANG_ARABIC_BAHRAINr[   )SUBLANG_ARABIC_QATARr6   )SUBLANG_AZERI_LATINr9   )SUBLANG_AZERI_CYRILLICrA   )SUBLANG_CHINESE_TRADITIONALr9   )SUBLANG_CHINESE_SIMPLIFIEDrA   )SUBLANG_CHINESE_HONGKONGrC   )SUBLANG_CHINESE_SINGAPORErE   )SUBLANG_CHINESE_MACAUrG   )SUBLANG_DUTCHr9   )SUBLANG_DUTCH_BELGIANrA   )SUBLANG_ENGLISH_USr9   )SUBLANG_ENGLISH_UKrA   )SUBLANG_ENGLISH_AUSrC   )SUBLANG_ENGLISH_CANrE   )SUBLANG_ENGLISH_NZrG   )SUBLANG_ENGLISH_EIRErI   )SUBLANG_ENGLISH_SOUTH_AFRICArK   )SUBLANG_ENGLISH_JAMAICArM   )SUBLANG_ENGLISH_CARIBBEANrO   )SUBLANG_ENGLISH_BELIZErQ   )SUBLANG_ENGLISH_TRINIDADrS   )SUBLANG_ENGLISH_ZIMBABWErU   )SUBLANG_ENGLISH_PHILIPPINESrW   )SUBLANG_FRENCHr9   )SUBLANG_FRENCH_BELGIANrA   )SUBLANG_FRENCH_CANADIANrC   )SUBLANG_FRENCH_SWISSrE   )SUBLANG_FRENCH_LUXEMBOURGrG   )SUBLANG_FRENCH_MONACOrI   )SUBLANG_GERMANr9   )SUBLANG_GERMAN_SWISSrA   )SUBLANG_GERMAN_AUSTRIANrC   )SUBLANG_GERMAN_LUXEMBOURGrE   )SUBLANG_GERMAN_LIECHTENSTEINrG   )SUBLANG_ITALIANr9   )SUBLANG_ITALIAN_SWISSrA   )SUBLANG_KASHMIRI_SASIArA   )SUBLANG_KASHMIRI_INDIArA   )SUBLANG_KOREANr9   )SUBLANG_LITHUANIANr9   )SUBLANG_MALAY_MALAYSIAr9   )SUBLANG_MALAY_BRUNEI_DARUSSALAMrA   )SUBLANG_NEPALI_INDIArA   )SUBLANG_NORWEGIAN_BOKMALr9   )SUBLANG_NORWEGIAN_NYNORSKrA   )SUBLANG_PORTUGUESErA   )SUBLANG_PORTUGUESE_BRAZILIANr9   )SUBLANG_SERBIAN_LATINrA   )SUBLANG_SERBIAN_CYRILLICrC   )SUBLANG_SPANISHr9   )SUBLANG_SPANISH_MEXICANrA   )SUBLANG_SPANISH_MODERNrC   )SUBLANG_SPANISH_GUATEMALArE   )SUBLANG_SPANISH_COSTA_RICArG   )SUBLANG_SPANISH_PANAMArI   )"SUBLANG_SPANISH_DOMINICAN_REPUBLICrK   )SUBLANG_SPANISH_VENEZUELArM   )SUBLANG_SPANISH_COLOMBIArO   )SUBLANG_SPANISH_PERUrQ   )SUBLANG_SPANISH_ARGENTINArS   )SUBLANG_SPANISH_ECUADORrU   )SUBLANG_SPANISH_CHILErW   )SUBLANG_SPANISH_URUGUAYrY   )SUBLANG_SPANISH_PARAGUAYr[   )SUBLANG_SPANISH_BOLIVIAr6   )SUBLANG_SPANISH_EL_SALVADORr&  )SUBLANG_SPANISH_HONDURASrs  )SUBLANG_SPANISH_NICARAGUAr(  )SUBLANG_SPANISH_PUERTO_RICOr   )SUBLANG_SWEDISHr9   )SUBLANG_SWEDISH_FINLANDrA   )SUBLANG_URDU_PAKISTANr9   )SUBLANG_URDU_INDIArA   )SUBLANG_UZBEK_LATINr9   )SUBLANG_UZBEK_CYRILLICrA   )SUBLANG_DUTCH_SURINAMrC   )SUBLANG_ROMANIANr9   )SUBLANG_ROMANIAN_MOLDAVIArA   )SUBLANG_RUSSIANr9   )SUBLANG_RUSSIAN_MOLDAVIArA   )SUBLANG_CROATIANr9   )SUBLANG_LITHUANIAN_CLASSICrA   )SUBLANG_GAELICr9   )SUBLANG_GAELIC_SCOTTISHrA   )SUBLANG_GAELIC_MANXrC   c                     [         R                  U S5      n[        R                  U/ 5       H  nX#;   d  M
  Us  $    [        R                  US/5      S   $ )N	*unknown*r   )LANGgetSUBLANG)
lang_valuesublang_value	lang_namesublang_names       r   get_sublang_name_for_langrC    sO    [1IM26 $	 7 ;;}{m4Q77r   c                    SnSnU[        U 5      :  a  XUS-    n[        U5      S:  a  g [        R                  " SU5      S   nUS-  nUS:w  aN  SUS-  s=::  a  [        U 5      ::  a5  O  O2 [        XX6S-  -    5      R	                  S5      X!'   US:  a  g X6S-  -  nUS-  nU[        U 5      :  a  M  g g ! [
         a    US-  n N7f = f)Nr   rA   z<hutf-16ler9   rC   )lenstructunpackbdecodeUnicodeDecodeError)r0   counterlierror_count
data_slicelen_s          r   parse_stringsrR    s    	AK
c$i-a!e_
z?Q}}T:.q1	Q19dQh3#d)3t1H56==jI
 aMA1# c$i- & q s   -#B6 6CCc                     U R                  5        Vs/ s H>  n[        U[        [        45      (       d  M   UR	                  U5      (       d  M8  X U   4PM@     sn$ s  snf )zRead the flags from a dictionary and return them in a usable form.

Will return a list of (flag, value) for all flags in "flag_dict"
matching the filter "flag_filter".
)keys
isinstancestrbytes
startswith)	flag_dictflag_filterflags      r   retrieve_flagsr\    sX     NN$$DdS%L) 	 .2ook.J 	 $  s   AA
Ac                 l    U H.  u  p4XA-  (       a  SU R                   U'   M  SU R                   U'   M0     g)zWill process the flags and set attributes in the object accordingly.

The object "obj" will gain attributes named after the flags provided in
"flags" and valued True/False, matching the results of applying each
flag value from "flags" to flag_field.
TFN)__dict__)obj
flag_fieldflagsr[  values        r   	set_flagsrc    s2     !%CLL!&CLL	 r   c                 .    U S:g  =(       a
    X S-
  -  S:H  $ )Nr   r9    )r&   s    r   power_of_tworf    s    !8.aQ..r   c                     [        U [        5      (       a  U $ [        U [        5      (       a  [        U 5      $ [        R                  " U S5      $ )Ncp1252)rU  rW  	bytearraycodecsencode)xs    r   rI  rI    s;    !U	Ay	!	!Qx}}Q))r   c                   :   ^  \ rS rSrU 4S jrU 4S jrS rSrU =r$ )
AddressSeti  c                 >   > [         TU ]  5         S U l        S U l        g r   )super__init__minmax)self	__class__s    r   rq  AddressSet.__init__  s    r   c                    > [         TU ]  U5        U R                  c  UO[        U R                  U5      U l        U R                  c  Xl        g [        U R                  U5      U l        g r   )rp  addrr  rs  )rt  rb  ru  s     r   rx  AddressSet.add  sK    E HH,5#dhh2F HH,5#dhh2Fr   c                 l    U R                   b  U R                  c  S$ U R                  U R                   -
  $ r.   )rr  rs  rt  s    r   diffAddressSet.diff  s,    HH$(8qQdhh>QQr   )rs  rr  )	__name__
__module____qualname____firstlineno__rq  rx  r|  __static_attributes____classcell__ru  s   @r   rn  rn    s    
G
R Rr   rn  c                   T    \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rS rS rSrg)!UnicodeStringWrapperPostProcessori  zThis class attempts to help the process of identifying strings
that might be plain Unicode or Pascal. A list of strings will be
wrapped on it with the hope the overlappings will help make the
decision about their type.c                 *    Xl         X l        S U l        g r   )perva_ptrstring)rt  r  r  s      r   rq  *UnicodeStringWrapperPostProcessor.__init__  s    r   c                     U R                   $ )zGet the RVA of the string.)r  r{  s    r   get_rva)UnicodeStringWrapperPostProcessor.get_rva  s    ||r   c                 &    U R                  SS5      $ )z6Return the escaped UTF-8 representation of the string.utf-8r
   )rJ  r{  s    r   __str__)UnicodeStringWrapperPostProcessor.__str__  s    {{7$788r   c                 X    U R                   (       d  gU R                   R                  " U6 $ )N )r  rJ  )rt  r   s     r   rJ  (UnicodeStringWrapperPostProcessor.decode  s!    {{{{!!4((r   c                     Sn g)z>Make this instance None, to express it's no known string type.Nre  r{  s    r   
invalidate,UnicodeStringWrapperPostProcessor.invalidate  s    r   c                 $    U R                   R                  U R                  S-   U R                  5       S9U l        g ! [
         aH    U R                   R                  5       R                  SR                  U R                  S-   5      5         g f = f)NrA   
max_lengthzCFailed rendering pascal string, attempting to read from RVA 0x{0:x})	r  get_string_u_at_rvar  get_pascal_16_lengthr  PEFormatErrorget_warningsappendformatr{  s    r   render_pascal_162UnicodeStringWrapperPostProcessor.render_pascal_16  s}    	''55q T-F-F-H 6 DK  	GG  "))66<fT\\A=M6N	s   := ABBc                 8    U R                  U R                  5      $ r   )9_UnicodeStringWrapperPostProcessor__get_word_value_at_rvar  r{  s    r   r  6UnicodeStringWrapperPostProcessor.get_pascal_16_length  s    ++DLL99r   c                      U R                   R                  US5      n[        U5      S:  a  g[        R
                  " SU5      S   $ ! [         a     gf = f)NrA   F<Hr   )r  get_datar  rF  rG  rH  )rt  rvar0   s      r   __get_word_value_at_rva9UnicodeStringWrapperPostProcessor.__get_word_value_at_rva  sW    	77##C+D t9q=}}T4(++  		s   A 
AAc                 \    U R                  US-
  5      S:X  a  XR                  -
  U l        gg)zThe next RVA is taken to be the one immediately following this one.

Such RVA could indicate the natural end of the string and will be checked
to see if there's a Unicode NULL character there.
rA   r   TF)r  r  length)rt  next_rva_ptrs     r   ask_unicode_160UnicodeStringWrapperPostProcessor.ask_unicode_16$  s0     ''q(89Q>&5DKr   c                      U R                   R                  U R                  5      U l        g ! [         aE    U R                   R                  5       R                  SR                  U R                  5      5         g f = f)NzDFailed rendering unicode string, attempting to read from RVA 0x{0:x})r  r  r  r  r  r  r  r  r{  s    r   render_unicode_163UnicodeStringWrapperPostProcessor.render_unicode_160  s^    	''55dllCDK 	GG  "))66<fT\\6J	s   *- AA<;A<)r  r  r  r  N)r~  r  r  r  __doc__rq  r  r  rJ  r  r  r  r  r  r  r  re  r   r   r  r    s9    "

9)
	:	,
r   r  c                   $    \ rS rSrSrS rS rSrg)r  i:  z"Generic PE format error exception.c                     Xl         g r   rb  )rt  rb  s     r   rq  PEFormatError.__init__=  s    
r   c                 ,    [        U R                  5      $ r   )reprrb  r{  s    r   r  PEFormatError.__str__@  s    DJJr   r  N)r~  r  r  r  r  rq  r  r  re  r   r   r  r  :  s    , r   r  c                   N    \ rS rSrSrS rSS jrSS jrSS jrS r	S r
S	 rS
rg)DumpiD  z1Convenience class for dumping the PE information.c                     / U l         g r   textr{  s    r   rq  Dump.__init__G  s	    	r   c                 8    U H  nU R                  X25        M     g)zUAdds a list of lines.

The list can be indented with the optional argument 'indent'.
N)add_line)rt  txtindentlines       r   	add_linesDump.add_linesJ  s    
 DMM$' r   c                 .    U R                  US-   U5        g)zLAdds a line.

The line can be indented with the optional argument 'indent'.

N)rx  rt  r  r  s      r   r  Dump.add_lineR  s    
 	tV$r   c                 `    U R                   R                  SR                  SU-  U5      5        g)zlAdds some text, no newline will be appended.

The text can be indented with the optional argument 'indent'.
z{0}{1} N)r  r  r  r  s      r   rx  Dump.addY  s%    
 			vs;<r   c                 F    U R                  SR                  SU5      5        g)zAdds a header element.z
{0}{1}{0}
z
----------N)r  r  )rt  r  s     r   
add_headerDump.add_header`  s    m**8S9:r   c                 :    U R                   R                  S5        g)zAdds a newline.r  N)r  r  r{  s    r   add_newlineDump.add_newlined  s    		r   c                 F    SR                  S U R                   5       5      $ )z"Get the text in its current state.r  c              3   D   #    U  H  nS R                  U5      v   M     g7f){0}N)r  ).0rI  s     r   	<genexpr> Dump.get_text.<locals>.<genexpr>j  s     :	1u||A	s    )joinr  r{  s    r   get_textDump.get_texth  s    ww:		:::r   r  Nr   )r~  r  r  r  r  rq  r  r  rx  r  r  r  r  re  r   r   r  r  D  s(    ;(%=;;r   r  )rl  crI  BhHrN  IrM  Lr   qQdsc           	      R   SnU nU S   [         R                  ;   aw  [        SR                  U  Vs/ s H  o3[         R                  ;   d  M  UPM     sn5      5      nSR                  U  Vs/ s H  o3[         R                  ;  d  M  UPM     sn5      n[        U   U-  $ s  snf s  snf )Nr9   r   r  )r  digitsr%   r  STRUCT_SIZEOF_TYPES)tr/   _tr  s       r   sizeof_typer    s    E	
Btv}}BGG@1&---?Q@ABWW=Av}}&<a=>r"U** A=s   BB+B$B$T)r   r   c           
         Sn/ n0 n/ nSnSnU  H  nSU;   d  M  UR                  SS5      u  pX-  nUR                  S 5        U	R                  S5      n
/ nU
 H\  n	X;   a?  U Vs/ s H  oS [        U	5       PM     nnUR                  U	5      nSR	                  X5      n	UR                  U	5        XcU	'   M^     U[        U5      -  nUR                  U5        M     [        R                  " U5      nUUUUU4$ s  snf )N<r   ,r9   z	{0}_{1:d})splitr  rF  r/   r  r  rG  calcsize)r  __format_str____unpacked_data_elms____field_offsets____keys____format_length__offsetelmelm_typeelm_name	elm_namesnamesrl  search_list	occ_counts                  r   
set_formatr    s$    NHF#:!$3!2H&N"))$/ s+IE%'?G"Hx!_s8}#5xK"H + 1 1( ;I*11(FHX&.4(+ & k(++F
 OOE"+ . 7 	  #Is   $C8c                   |    \ rS rSrSrSS jrS\4S jrS rS r	S	 r
S
 rS rS rS rS rS rS rSS jrS rSrg)	Structurei  zPrepare structure object to extract members from data.

Format is a list containing definitions for the elements
of the structure.
Nc                 2   SU l         / U l        SU l        0 U l        / U l        US   n[        U[        5      (       d  [        U5      n[        U5      u  U l         U l        U l        U l        U l        SU l        X0l	        U(       a  X l
        g US   U l
        g )Nr  r   r9   F)r  r  r  r  r  rU  tupler  __all_zeroes____file_offset__name)rt  r  r
  file_offsetr  s        r   rq  Structure.__init__  s    !!"!#&(#1I!U##aA qM	
'"M" $*Iq	DIr   returnc                     U R                   $ r   )r  r{  s    r   __get_format__Structure.__get_format__      """r   c                 :    U R                   U R                  U   -   $ )zLReturn the offset within the field for the requested field in the structure.)r	  r  rt  
field_names     r   get_field_absolute_offset#Structure.get_field_absolute_offset  s    ##d&<&<Z&HHHr   c                      U R                   U   $ )z?Return the offset within the structure for the requested field.)r  r  s     r   get_field_relative_offset#Structure.get_field_relative_offset  s    %%j11r   c                     U R                   $ r   r	  r{  s    r   get_file_offsetStructure.get_file_offset  s    ###r   c                     Xl         g r   r  rt  r  s     r   set_file_offsetStructure.set_file_offset  s    %r   c                     U R                   $ )z/Returns true is the unpacked data is all zeros.)r  r{  s    r   
all_zeroesStructure.all_zeroes  s     """r   c                     U R                   $ )zReturn size of the structure.)r  r{  s    r   sizeofStructure.sizeof  s     %%%r   c                    [        U5      n[        U5      U R                  :  a  US U R                   nO$[        U5      U R                  :  a  [        S5      e[	        U5      [        U5      :X  a  SU l        [        R                  " U R                  U5      U l	        [        U R                  5       H'  u  p#U R                  U    H  n[        XU5        M     M)     g )Nz-Data length less than expected header length.T)rI  rF  r  r  r1   r  rG  rH  r  r  	enumerater  setattr)rt  r0   idxr&   keys        r   
__unpack__Structure.__unpack__  s    wt9t---0$001D Y/// OPPT*"&D&,mmD4G4G&N#!$"="=>HC}}S)3' * ?r   c                     / n[        U R                  5       H@  u  p#S nU R                  U    H  n[        X5      nXC:w  d  M    O   UR	                  U5        MB     [
        R                  " U R                  /UQ76 $ r   )r)  r  r  getattrr  rG  packr  )rt  
new_valuesr+  r&   new_valr,  s         r   __pack__Structure.__pack__  sv    
!$"="=>HCG}}S)!$, > * g& ? {{4..<<<r   c                 @    SR                  U R                  5       5      $ )Nr  )r  dumpr{  s    r   r  Structure.__str__  s    yy%%r   c           
          SSR                  U R                  5        Vs/ s H"  nSR                  UR                  5       5      PM$     sn5      -  $ s  snf )Nz<Structure: %s>r  )r  r7  r  )rt  r  s     r   __repr__Structure.__repr__  sB     HH499;?;achhqwwy);?@
 	
?s   )A
c           
         / nUR                  SR                  U R                  5      5        [        R                   Vs/ s H#  o3[        R
                  ;  d  M  [        U5      PM%     nnU R                   GH  nU GH  n[        X5      n[        U[        [        45      (       ax  UR                  S5      (       a  SR                  U5      nOSR                  U5      nUS:X  d  US:X  a2   US[        R                  " [        R                  " U5      5      -  -  nOO[#        U5      nUR                  S	5      (       aA  S
R%                  UR'                  S5       Vs/ s H  nSR                  U5      PM     sn5      nOPS
R%                  UR'                  S5       Vs/ s H$  nX4;   a  [)        U5      OSR                  U5      PM&     sn5      nUR                  SU R*                  U   U R,                  -   U R*                  U   US-   U4-  5        GM     GM     U$ s  snf ! [          a    US-  n Ndf = fs  snf s  snf )z1Returns a string representation of the structure.z[{0}]
Signature_z{:<8X}z0x{:<8X}TimeDateStampdwTimeStampz	 [%s UTC]z [INVALID TIME]	Signaturer      z{:02X}z	\x{0:02x}z0x%-8X 0x%-3X %-30s %s:)r  r  r
  r  	printable
whitespaceordr  r0  rU  r%   longrX  timeasctimegmtime
ValueErrorri  r  rstripchrr  r	  )	rt  indentationr7  rN  printable_bytesrT  r,  r&   val_strs	            r   r7  Structure.dump$  s    GNN499-. #,,
,q9J9J0JFCF, 	 
 MMDd(cC;//~~l33"*//#"6","3"3C"8o-1E9#{T\\$++cBR5S'SSG 2F (nG~~k22"$''9@9PQ9PAX__Q/9PQ# #%''
 *1)@	 *AA %&$8 !$A%1%8%8%;!< *A	# ,..s3d6J6JJ..s3c		;  "P ]
$  * 9#'88G9 Rs)   HH-0H$H9+H>$H65H6c                 L   0 nU R                   US'   U R                   H  nU H  n[        X5      n[        U[        [
        45      (       a>  US:X  d  US:X  a1   SU[        R                  " [        R                  " U5      5      4-  nOJOISR                  S U Vs/ s H%  n[        U[        5      (       d  [        U5      OUPM'     sn 5       5      nU R                  U   U R                  -   U R                  U   US.X'   M     M     U$ ! [         a    SU-  n NJf = fs  snf )	z5Returns a dictionary representation of the structure.r  r>  r?  z0x%-8X [%s UTC]z0x%-8X [INVALID TIME]r  c              3   |   #    U  H2  n[        U5      [        R                  ;   a  [        U5      OS U-  v   M4     g7f)z\x%02xN)rL  r  rC  )r  r  s     r   r  &Structure.dump_dict.<locals>.<genexpr>r  s5      "!WA #&a&F,<,<"<A)a-O!Ws   :<)
FileOffsetOffsetValue)r
  r  r0  rU  r%   rF  rG  rH  rI  rJ  r  rE  r  r	  )rt  	dump_dictrT  r,  r&   r  s         r   rW  Structure.dump_dict[  s0    	!%	+ MMDd(cC;//o-1E@"3 # $T[[-= >7 #C 2F '' "SV!WSVa
1c0B0B#a&"ISV!W" C #'"8"8"=@T@T"T"44S9 "	%  "2   * @"9C"?C@
 "Xs   /D,D!DD)r  r  r	  r  r  r  r  r
  NNr  )r~  r  r  r  r  rq  rV  r  r  r  r  r   r#  r&  r-  r4  r  r:  r7  rW  r  re  r   r   r  r    sV    "8# #I2$&#
&
(.= &

5n"r   r  c                   |    \ rS rSrSrS rS rS rSS jrS r	S	 r
S
 rS rS rS rS rS rS rS rS rS rSrg)SectionStructurei  z#Convenience section handling class.c                     SU;   a  US   U l         US	 S U l        S U l        S U l        S U l        [
        R                  " U /UQ70 UD6  S U l        S U l        S U l	        S U l
        g )Nr  )r  PointerToRawDataVirtualAddressSizeOfRawDataMisc_VirtualSizer  rq  PointerToRawData_adjVirtualAddress_adjsection_min_addrsection_max_addr)rt  arglargds      r   rq  SectionStructure.__init__  ss    4<4jDGT
 $"! $4/$/$/$(!"& $ $r   c                     U R                   cV  U R                  bI  U R                  R                  U R                  U R                  R                  R
                  5      U l         U R                   $ r   )ra  r]  r  adjust_FileAlignmentOPTIONAL_HEADERFileAlignmentr{  s    r   get_PointerToRawData_adj)SectionStructure.get_PointerToRawData_adj  sZ    $$,$$0,0GG,H,H))477+B+B+P+P-) (((r   c                    U R                   cu  U R                  bh  U R                  R                  U R                  U R                  R                  R
                  U R                  R                  R                  5      U l         U R                   $ r   )rb  r^  r  adjust_SectionAlignmentrj  SectionAlignmentrk  r{  s    r   get_VirtualAddress_adj'SectionStructure.get_VirtualAddress_adj  sn    ""*"".*.''*I*I''GG++<<GG++99+'
 &&&r   Nc                    Uc  U R                  5       nO#XR                  5       -
  U R                  5       -   nUb  XB-   nOU R                  b  X@R                  -   nOUnU(       a  Ub  Ub  [        XTU R                  -   5      nU R
                  bB  U R                  b5  XPR
                  U R                  -   :  a  U R
                  U R                  -   nU R                  R                  XE $ )a  Get data chunk from a section.

Allows to query data from the section by passing the
addresses where the PE file would be loaded by default.
It is then possible to retrieve code and data by their real
addresses as they would be if loaded.

Note that sections on disk can include padding that would
not be loaded to memory. That is the case if `section.SizeOfRawData`
is greater than `section.Misc_VirtualSize`, and that means
that data past `section.Misc_VirtualSize` is padding.
In case you are not interested in this padding, passing
`ignore_padding=True` will truncate the result in order
not to return the padding (if any).

Returns bytes() under Python 3.x and set() under Python 2.7
)rl  rq  r_  rr  r`  r]  r  __data__)rt  startr  ignore_paddingr  ends         r   r  SectionStructure.get_data  s    & =224F 3355--/0F /C+---CCco&2DcD$9$99:C
   ,1C1C1O**T-?-???++d.@.@@ww++r   c                    US:X  a  [        [        S5      n[        XU5        OZSU;   aT  [        X5      (       aD  U(       a  U R                  S==   [        U   -  ss'   OU R                  S==   [        U   -  ss'   X R                  U'   g )NCharacteristics
IMAGE_SCN_)r\  SECTION_CHARACTERISTICSrc  hasattrr^  )rt  r
  r&   section_flagss       r   __setattr__SectionStructure.__setattr__  s{    $$*+BLQM d/T!gd&9&9/04KD4QQ0/04KD4QQ0!dr   c                 H    XR                  5       -
  U R                  5       -   $ r   )rl  rq  r  s     r   get_rva_from_offset$SectionStructure.get_rva_from_offset  s"    5577$:U:U:WWWr   c                 H    XR                  5       -
  U R                  5       -   $ r   )rq  rl  rt  r  s     r   get_offset_from_rva$SectionStructure.get_offset_from_rva  s"    0022T5R5R5TTTr   c                 ~    U R                   c  gU R                  5       nX!s=:*  =(       a    X R                  -   :  $ s  $ )z<Check whether the section contains the file offset provided.F)r]  rl  r_  )rt  r  ra  s      r   contains_offset SectionStructure.contains_offset  sI       ( #<<> VV-ADVDV-VV	
V	
r   c                 X   U R                   b6  U R                  b)  U R                   Us=:*  =(       a    U R                  :  $ s  $ U R                  5       n[        U R                  R
                  5      U R                  5       -
  U R                  :  a  U R                  nO [        U R                  U R                  5      nU R                  b;  U R                  U R                  :  a!  X#-   U R                  :  a  U R                  U-
  nX l         X#-   U l        X!s=:*  =(       a    X#-   :  $ s  $ )z8Check whether the section contains the address provided.)rc  rd  rq  rF  r  rt  rl  r_  r`  rs  next_section_virtual_addressr^  )rt  r  rb  sizes       r   contains_rvaSectionStructure.contains_rva  s      ,1F1F1R((CGG$2G2GGGGG!88: tww 4#@#@#BBTEWEWW ((Dt))4+@+@AD --911D4G4GG")D,M,MM447IID 2 2 9!DD+=+DDDDDr   c                 $    U R                  U5      $ r   )r  r  s     r   containsSectionStructure.contains  s      %%r   c                 @    U R                  U R                  5       5      $ )z1Calculate and return the entropy for the section.)	entropy_Hr  r{  s    r   get_entropySectionStructure.get_entropy  s     ~~dmmo..r   c                 `    [         b'  [        U R                  5       5      R                  5       $ g)z/Get the SHA-1 hex-digest of the section's data.N)r   r  	hexdigestr{  s    r   get_hash_sha1SectionStructure.get_hash_sha1   s)     (2244 r   c                 `    [         b'  [        U R                  5       5      R                  5       $ g)z1Get the SHA-256 hex-digest of the section's data.N)r   r  r  r{  s    r   get_hash_sha256 SectionStructure.get_hash_sha256&  )     $--/*4466 r   c                 `    [         b'  [        U R                  5       5      R                  5       $ g)z1Get the SHA-512 hex-digest of the section's data.N)r   r  r  r{  s    r   get_hash_sha512 SectionStructure.get_hash_sha512,  r  r   c                 `    [         b'  [        U R                  5       5      R                  5       $ g)z-Get the MD5 hex-digest of the section's data.N)r	   r  r  r{  s    r   get_hash_md5SectionStructure.get_hash_md52  s(     ?t}}'1133 r   c                     U(       d  g[        [        U5      5      nSnUR                  5        H6  n[        U5      [	        U5      -  nX5[
        R                  " US5      -  -  nM8     U$ )z)Calculate the entropy of a chunk of data.g        r   rA   )r   ri  valuesfloatrF  mathlog)rt  r0   
occurencesentropyrl  p_xs         r   r  SectionStructure.entropy_H8  sa     Yt_-
""$A(SY&CTXXc1---G % r   )	r`  r]  ra  r_  r^  rb  r  rd  rc  )NNF)r~  r  r  r  r  rq  rl  rq  r  r  r  r  r  r  r  r  r  r  r  r  r  r  re  r   r   r[  r[    sZ    -%)'*,X" XU

$EL&/
5774r   r[  c                 8    " S S5      n/ n0 nU" X#5      nU S    H  nSU;  a#  UR                  5         UR                  U5        M,  UR                  SS5      u  pgSU;   a  [        S5      eUR                  SS5      u  ph[	        U5      nXdR                  5       :w  d  XR                  5       :  a!  UR                  5         UR                  U5        UR                  Xx5        M     UR                  5         [        [        U5      5      u  ppn/ n[        U5       Hq  u  nnX;  a  UR                  U5        M  X?   u  n
nU Vs/ s H  nU[        R                     /PM     nnUR                  U5        U H  nUUS      UUS   '   M     Ms     XXX4$ s  snf )Nc                   >    \ rS rSrS rS rS rS rS rS r	S r
S	rg
))set_bitfields_format.<locals>.AccumulatoriJ  c                 T    / U l         SU l        S U l        SU l        X l        Xl        g )N~r   )
_subfields_name_type
_bits_left_comp_fields_format)rt  fmtcomp_fieldss      r   rq  2set_bitfields_format.<locals>.Accumulator.__init__K  s+     DO DJDJDO +Lr   c                 *   U R                   c  g U R                  R                  U R                   S-   U R                  -   5        U R                   U R                  4U R
                  [        U R                  5      S-
  '   SU l        S U l         / U l        g )Nr  r9   r  )r  r  r  r  r  r  rF  r{  s    r   wrap_up1set_bitfields_format.<locals>.Accumulator.wrap_upU  ss    zz!LL

S 04:: =>8<

DOO7TDc$,,/!34DJDJ DOr   c                 2    [         U   S-  U l        Xl        g NrM   )r  r  r  )rt  tps     r   new_type2set_bitfields_format.<locals>.Accumulator.new_type^  s    1"59DOJr   c                     U =R                   U-  sl         U =R                  U-  sl        U R                  R                  X45        g r   )r  r  r  r  )rt  r
  bitcnts      r   add_subfield6set_bitfields_format.<locals>.Accumulator.add_subfieldb  s2    JJ$JOOv%OOO""D>2r   c                     U R                   $ r   )r  r{  s    r   get_type2set_bitfields_format.<locals>.Accumulator.get_typeg      ::r   c                     U R                   $ r   )r  r{  s    r   get_name2set_bitfields_format.<locals>.Accumulator.get_namej  r  r   c                     U R                   $ r   )r  r{  s    r   get_bits_left7set_bitfields_format.<locals>.Accumulator.get_bits_leftm  s    ??"r   )r  r  r  r  r  r  N)r~  r  r  r  rq  r  r  r  r  r  r  r  re  r   r   Accumulatorr  J  s%    		!		3
			#r   r  r9   rB  r  z3Structures with bitfields do not support unions yetr   )r  r  r  NotImplementedErrorr%   r  r  r  r  r  r  r)  StructureWithBitfieldsBTF_NAME_IDXextend)r  r  old_fmtr  acr  r  r  elm_bits
format_str_field_offsetsrT  format_lengthextended_keysr+  r&   sbfr   bf_namesns                        r   set_bitfields_formatr  H  s   $# $#L GK	W	*BayczJJLNN3 YYsA.(?%E  &^^C3x={{}$3C3C3E(EJJLKK!
+' ( JJL8B5>8R5J=MdOS!  %!3FIJcQ-::;<cJX&A"/A"7M!A$  $ }MWW Ks   Fc                   z   ^  \ rS rSrSrSrSrSrSrSS jr	U 4S jr
U 4S jrSU 4S jjrU 4S	 jrS
 rS rSrU =r$ )r  i  a  
Extends Structure's functionality with support for bitfields such as:
    ('B:4,LowerHalf', 'B:4,UpperHalf')
To this end, two lists are maintained:
    * self.__keys__ that contains compound fields, for example
      ('B,~LowerHalfUpperHalf'), and is used during packing/unpaking
    * self.__keys_ext__ containing a separate key for each field (ex., LowerHalf,
      UpperHalf) to simplify implementation of dump()
This way the implementation of unpacking/packing and dump() from Structure can be
reused.

In addition, we create a dictionary:
    <comound_field_index_in_keys> -->
        (data type, [ (subfield name, length in bits)+ ] )
that facilitates bitfield paking and unpacking.

With lru_cache() creating only once instance per format string, the memory
overhead is negligible.
r   r9   c                    [        U5      u  U l        U l        U l        U l        U l        U l        [        U R                  5       Vs/ s H  nS PM     snU l        SU l	        X0l
        US :w  a  X l        g US   U l        g s  snf )NFr   )r  r  r  r  r  __keys_ext____compound_fields__ranger  r  r	  r
  )rt  r  r
  r  rN  s        r   rq  StructureWithBitfields.__init__  s     !(	
""M$ 6;4;Q;Q5R&S5Rt5R&S##* DLD	fQi	 'Ts   Bc                 L   > [         [        U ]  U5        U R                  5         g r   )rp  r  r-  _unpack_bitfield_attributesrt  r0   ru  s     r   r-  !StructureWithBitfields.__unpack__  s!     	$d6t<((*r   c                    > U R                  5          [        [        U ]  5       nU R	                  5         U$ ! U R	                  5         f = fr   )_pack_bitfield_attributesrp  r  r4  r  r  s     r   r4  StructureWithBitfields.__pack__  sE    &&(	//?AD,,. ,,.s	   7 A	c                    > U R                   nU R                  U l          [        [        U ]  U5      nX l         U$ ! X l         f = fr   )r  r  rp  r  r7  )rt  rM  tkretru  s       r   r7  StructureWithBitfields.dump  sC    ]]))	.:;GCM
 Ms	   ; Ac                    > U R                   nU R                  U l          [        [        U ]  5       nXl         U$ ! Xl         f = fr   )r  r  rp  r  rW  )rt  r  r  ru  s      r   rW   StructureWithBitfields.dump_dict  sA    ]]))	.?ACM
 Ms	   : Ac                    U R                   R                  5        H  nU R                  U   S   n[        X5      n[	        X5        SnU R                   U   [
        R                      HX  nSU[
        R                     -  S-
  nXd-  n[        U U[
        R                     X6-  U-	  5        XE[
        R                     -  nMZ     M     g)zQReplace compound attributes corresponding to bitfields with separate
sub-fields.
r   r9   N)
r  rT  r  r0  delattrr  CF_SUBFLD_IDXBTF_BITCNT_IDXr*  r  )rt  rN  cf_namecvaloffstsfmasks          r   r  2StructureWithBitfields._unpack_bitfield_attributes  s     ))..0AmmA&q)G4)DD"E..q12H2V2VWR 6 E EFF!K-::;[U*
 2AABB X 1r   c                    U R                   R                  5        H  nU R                  U   S   nSu  p4U R                   U   [        R                      HW  nSU[        R
                     -  S-
  n[        X[        R                     5      U-  nXGU-  -  nX5[        R
                     -  nMY     [        XU5        M     g)z(Pack attributes into a compound bitfieldr   r   r   r9   N)	r  rT  r  r  r  r  r0  r  r*  )rt  rN  r  r  acc_valr  r  	field_vals           r   r  0StructureWithBitfields._pack_bitfield_attributes  s    ))..0AmmA&q)G!NE..q12H2V2VWR 6 E EFF!KD%;%H%H"IJTQ  --2AABB X D7+ 1r   )
r  r  r  r	  r  r  r  r  r  r
  rY  r  )r~  r  r  r  r  r  r  CF_TYPE_IDXr  rq  r-  r4  r7  rW  r  r  r  r  r  s   @r   r  r    sG    ( LNKM8 +C&, ,r   r  c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )DataContaineri  zGeneric data container.c                 d   > [         [        U ]
  nUR                  5        H  u  p4U" X45        M     g r   )rp  r  r  items)rt  r   bare_setattrr,  rb  ru  s        r   rq  DataContainer.__init__	  s)    ]D=**,JC$ 'r   re  )r~  r  r  r  r  rq  r  r  r  s   @r   r  r    s    !% %r   r  c                       \ rS rSrSrSrg)ImportDescDatai  zHolds import descriptor information.

dll:        name of the imported DLL
imports:    list of imported symbols (ImportData instances)
struct:     IMAGE_IMPORT_DESCRIPTOR structure
re  Nr~  r  r  r  r  r  re  r   r   r  r        r   r  c                       \ rS rSrSrS rSrg)
ImportDatai  zHolds imported symbol's information.

ordinal:    Ordinal of the symbol
name:       Name of the symbol
bound:      If the symbol is bound, this contains
            the address.
c                    [        U S5      (       Ga  [        U S5      (       Ga  [        U S5      (       Ga  US:X  a  U R                  R                  [        :X  a  [        nO$U R                  R                  [
        :X  a  [        nWUS-  -  U R                  l        U R                  R                  U R                  l	        U R                  R                  U R                  l
        U R                  R                  U R                  l        GOUS:X  a  U R                  b  X R                  l	        U R                  R                  U R                  l	        U R                  R                  U R                  l
        U R                  R                  U R                  l        GO>US:X  a  X R                  l	        U R                  R                  U R                  l        U R                  R                  U R                  l
        U R                  R                  U R                  l        OUS:X  a  U R                  (       a  U R                  R                  U R                  5      nU R                  R                  U R                   SU-  5        [#        U5      [#        U R$                  5      :  a  ['        S5      eU R                  R)                  U R                  U5        X R*                  U'   g )Nordinalboundr
    addressr   9The export name provided is longer than the existing one.)r}  r  PE_TYPEOPTIONAL_HEADER_MAGIC_PEIMAGE_ORDINAL_FLAGOPTIONAL_HEADER_MAGIC_PE_PLUSIMAGE_ORDINAL_FLAG64struct_tableOrdinalAddressOfDataFunctionForwarderString
struct_iatname_offsetr  set_dword_at_offsetordinal_offsetrF  r
  r  set_bytes_at_offsetr^  )rt  r
  r&   ordinal_flagname_rvas        r   r  ImportData.__setattr__!  s$    D)$$g&&f%% y 77??&>>#5LWW__(EE#7L -9C&L,I!!)262C2C2K2K!!/-1->->-F-F!!*484E4E4M4M!!1??.47OO148OO4Q4QDOO1/3/L/LDOO,6:oo6S6SDOO3"25!!/,0,=,=,K,K!!)-1->->-L-L!!*484E4E4S4S!!1 ###ww::4;K;KLHGG//++g-A 3x#dii.0+W  GG//0@0@#F!dr   re  Nr~  r  r  r  r  r  r  re  r   r   r  r    s    6"r   r  c                       \ rS rSrSrSrg)ExportDirDataiZ  zHolds export directory information.

struct:     IMAGE_EXPORT_DIRECTORY structure
symbols:    list of exported symbols (ExportData instances)re  Nr  re  r   r   r,  r,  Z  s    Cr   r,  c                       \ rS rSrSrS rSrg)
ExportDataia  aD  Holds exported symbols' information.

ordinal:    ordinal of the symbol
address:    address of the symbol
name:       name of the symbol (None if the symbol is
            exported by ordinal only)
forwarder:  if the symbol is forwarded it will
            contain the name of the target symbol,
            None otherwise.
c                    [        U S5      (       GaC  [        U S5      (       Ga1  [        U S5      (       Ga  [        U S5      (       Ga  US:X  a'  U R                  R                  U R                  U5        OUS:X  a'  U R                  R	                  U R
                  U5        OUS:X  aT  [        U5      [        U R                  5      :  a  [        S5      eU R                  R                  U R                  U5        OYUS:X  aS  [        U5      [        U R                  5      :  a  [        S5      eU R                  R                  U R                  U5        X R                  U'   g )Nr  r  	forwarderr
  r  z<The forwarder name provided is longer than the existing one.)r}  r  set_word_at_offsetr%  r$  address_offsetrF  r
  r  r&  r#  r0  forwarder_offsetr^  rt  r
  r&   s      r   r  ExportData.__setattr__m  s    D)$$i((k**f%% y **4+>+>D"++D,?,?E s8c$))n,'S  ++D,<,<cB$ s8c$..11'V  ++D,A,A3G!dr   re  Nr*  re  r   r   r.  r.  a  s    	""r   r.  c                       \ rS rSrSrSrg)ResourceDirDatai  zHolds resource directory information.

struct:     IMAGE_RESOURCE_DIRECTORY structure
entries:    list of entries (ResourceDirEntryData instances)
re  Nr  re  r   r   r7  r7        r   r7  c                       \ rS rSrSrSrg)ResourceDirEntryDatai  a  Holds resource directory entry data.

struct:     IMAGE_RESOURCE_DIRECTORY_ENTRY structure
name:       If the resource is identified by name this
            attribute will contain the name string. None
            otherwise. If identified by id, the id is
            available at 'struct.Id'
id:         the id, also in struct.Id
directory:  If this entry has a lower level directory
            this attribute will point to the
            ResourceDirData instance representing it.
data:       If this entry has no further lower directories
            and points to the actual resource data, this
            attribute will reference the corresponding
            ResourceDataEntryData instance.
(Either of the 'directory' or 'data' attribute will exist,
but not both.)
re  Nr  re  r   r   r:  r:    s    r   r:  c                       \ rS rSrSrSrg)ResourceDataEntryDatai  zHolds resource data entry information.

struct:     IMAGE_RESOURCE_DATA_ENTRY structure
lang:       Primary language ID
sublang:    Sublanguage ID
re  Nr  re  r   r   r<  r<    r  r   r<  c                       \ rS rSrSrSrg)	DebugDatai  zHolds debug information.

struct:     IMAGE_DEBUG_DIRECTORY structure
entries:    list of entries (IMAGE_DEBUG_TYPE instances)
re  Nr  re  r   r   r>  r>    r8  r   r>  c                       \ rS rSrSrSrg)DynamicRelocationDatai  zHolds dynamic relocation information.

struct:        IMAGE_DYNAMIC_RELOCATION structure
symbol:        Symbol to which dynamic relocations must be applied
relocations:   List of dynamic relocations for this symbol (BaseRelocationData instances)
re  Nr  re  r   r   r@  r@    r  r   r@  c                       \ rS rSrSrSrg)BaseRelocationDatai  zHolds base relocation information.

struct:     IMAGE_BASE_RELOCATION structure
entries:    list of relocation data (RelocationData instances)
re  Nr  re  r   r   rB  rB    r8  r   rB  c                       \ rS rSrSrS rSrg)RelocationDatai  zHolds relocation information.

type:       Type of relocation
            The type string can be obtained by
            RELOCATION_TYPE[type]
rva:        RVA of the relocation
c                    [        U S5      (       aa  U R                  R                  nUS:X  a  US-  US-  -  nO)US:X  a#  [        X R                  -
  S5      nUS-  US-  -  nX0R                  l        X R
                  U'   g )NrG  typerU     r  r   i   )r}  rG  Datars  base_rvar^  )rt  r
  r&   wordr  s        r   r  RelocationData.__setattr__  s     4"" ;;##Dv~r	dUl3S==0!4v&5.9  $KK!dr   re  Nr*  re  r   r   rD  rD    s    "r   rD  c                       \ rS rSrSrSrg)TlsDatai  zBHolds TLS information.

struct:     IMAGE_TLS_DIRECTORY structure
re  Nr  re  r   r   rM  rM    s    r   rM  c                       \ rS rSrSrSrg)BoundImportDescDatai  a  Holds bound import descriptor data.

This directory entry will provide information on the
DLLs this PE file has been bound to (if bound at all).
The structure will contain the name and timestamp of the
DLL at the time of binding so that the loader can know
whether it differs from the one currently present in the
system and must, therefore, re-bind the PE's imports.

struct:     IMAGE_BOUND_IMPORT_DESCRIPTOR structure
name:       DLL name
entries:    list of entries (BoundImportRefData instances)
            the entries will exist if this DLL has forwarded
            symbols. If so, the destination DLL will have an
            entry in this list.
re  Nr  re  r   r   rO  rO    s    r   rO  c                       \ rS rSrSrSrg)LoadConfigDatai  zHolds Load Config data.

struct:     IMAGE_LOAD_CONFIG_DIRECTORY structure
name:       dll name
dynamic_relocations: dynamic relocation information, if present
re  Nr  re  r   r   rQ  rQ    r  r   rQ  c                       \ rS rSrSrSrg)BoundImportRefDatai  zHolds bound import forwarder reference data.

Contains the same information as the bound descriptor but
for forwarded DLLs, if any.

struct:     IMAGE_BOUND_FORWARDER_REF structure
name:       dll name
re  Nr  re  r   r   rS  rS    s    r   rS  c                       \ rS rSrSrSrg)ExceptionsDirEntryDatai   zHolds the data related to SEH (and stack unwinding, in particular)

struct      an instance of RUNTIME_FUNTION
unwindinfo  an instance of UNWIND_INFO
re  Nr  re  r   r   rU  rU     r8  r   rU  c                   |   ^  \ rS rSrSrSU 4S jjrU 4S jrSU 4S jjrU 4S jrS r	S r
U 4S	 jrS
 rS rSrU =r$ )
UnwindInfoi(  zHandles the complexities of UNWIND_INFO structure:
* variable number of UWIND_CODEs
* optional ExceptionHandler and FunctionEntry fields
c                    > [         [        U ]  SUS9  [         [        U ]  5       U l        S U l        [        SSS9U l        S U l        SU l	        g )N)UNWIND_INFO)zB:3,Versionz	B:5,FlagszB,SizeOfPrologzB,CountOfCodeszB:4,FrameRegisterzB:4,FrameOffsetr  UNWIND_CODE)B,CodeOffsetB:4,UnwindOp
B:4,OpInfor   F)
rp  rW  rq  r&  
_full_size_opt_field_namer  
_code_info_chained_entry_finished_unpacking)rt  r  ru  s     r   rq  UnwindInfo.__init__.  s`    j$(
 $ 	) 	
  
D8:#0K
 ##( r   c                 J  > U R                   (       a  g[        [        U ]  U5        U R                  S-   S-  n[        [        U ]  5       X R                  R                  5       -  -   nUU R                  S:X  a  SO[        S   -   U l	        [        U5      U R                  :  a  gU R                  S:w  a(  U R                  S:w  a  S[        U R                  5      -   $ / U l        [        [        U ]  5       nU R                  nUS:  Ga  U R                  R                  XX@R                  R                  5       -    5        [        R!                  U R                  5      nUc  S[        U R                  U-   5      -   $ UR#                  U R                  U 5      nU R                  R                  5       U-  nUR%                  U R                  XXH-    U U R                  U-   5        XH-  nXW-  nU R                  R'                  U5        US:  a  GM  U R(                  (       d  U R*                  (       a  S	U l        U R.                  (       a  S
U l        U R,                  S:w  a;  [1        U U R,                  [2        R4                  " SXU[        S   -    5      S   5        SU l         g)zUnpacks the UNWIND_INFO "in two calls", with the first call establishing
a full size of the structure and the second, performing the actual unpacking.
Nr9   r   r  rA   z&Unsupported version of UNWIND_INFO at zUnknown UNWIND_CODE at ExceptionHandlerFunctionEntry<IT)rd  rp  rW  r-  CountOfCodesr&  rb  Flagsr  r`  rF  Versionhexr	  UnwindCodesPrologEpilogOpsFactorycreatelength_in_code_structures
initializer  r  r  ra  r  r*  rG  rH  )
rt  r0   codes_cnt_maxhdlr_offsetro
codes_leftucodelen_in_codesopc_sizeru  s
            r   unpack_in_stagesUnwindInfo.unpack_in_stagesF  sT    ##j$*40**Q."4*d*,}?U?U?W/WW 	 &qA&9#&>
 t9t&<<1!2;c$BVBV>WWW:t+-&&
1nOO&&too6L6L6N1N'OP*11$//BE}03t7K7Kb7P3QQQ ::4??DQL--/,>H"-($$r)	 NB&J##E*! 1n$ !!T%;%;#5D ""#2D 4'$$$[;Ns;S-ST $( r   c                   > U R                   S :w  aS  U R                  [        S   -
  U R                  U R                   '   U R                  R                  U R                   /5         [        [        U ]#  U5      nU R                   S :w  a  U R                  R                  5         UR                  SSR                  [         Vs/ s H  n[        XS   5      (       d  M  US   PM     sn5      -   5        UR                  SSR                  U R                   Vs/ s H$  oDR                  5       (       d  M  [        U5      PM&     sn5      -   5        U$ ! U R                   S :w  a  U R                  R                  5         f f = fs  snf s  snf )Nr  Flags: , r   zUnwind codes: z; )ra  r`  r  r  r  r  rp  rW  r7  popr  unwind_info_flagsr0  ro  is_validrV  )rt  rM  r7  r  r  ru  s        r   r7  UnwindInfo.dump  sU    4'"5c":: ""4#7#78 $$d&:&:%;<	(T/<D##t+!!%%'ii'8P'8!GDA$<O1'8PQR	
 	ii)9)9J)9AZZ\Q)9JKL	
  ##t+!!%%' ,
 Q Ks$   &E F	FF4F-E?c                   > U R                   S :w  aS  U R                  [        S   -
  U R                  U R                   '   U R                  R                  U R                   /5         [        [        U ]#  5       nU R                   S :w  a  U R                  R                  5         U$ ! U R                   S :w  a  U R                  R                  5         f f = f)Nr  )
ra  r`  r  r  r  r  rp  rW  rW  r  )rt  r  ru  s     r   rW  UnwindInfo.dump_dict  s    4'"5c":: ""4#7#78 $$d&:&:%;<	(
D35C##t+!!%%'
 ##t+!!%%' ,s   &B$ $-Cc                    US:X  a  [        X[        5        OZSU;   aT  [        X5      (       aD  U(       a  U R                  S==   [        U   -  ss'   OU R                  S==   [        U   -  ss'   X R                  U'   g )Nrl  	UNW_FLAG_)rc  r  r}  r^  UNWIND_INFO_FLAGSr4  s      r   r  UnwindInfo.__setattr__  si    7?d!23D WT%8%8g&*;D*AA&g&*;D*AA&!dr   c                     U R                   $ r   )r`  r{  s    r   r&  UnwindInfo.sizeof  s    r   c                 x  > [        U R                  5      n[        [        U ]  5       US[        [        U ]  5       & [        [        U ]  5       nU R                   H  nX#R                  R                  5       -   U R                  :  a    OVUR                  R	                  5       XX#R                  R                  5       -   & X#R                  R                  5       -  nM     U R                  S :w  aK  [        R                  " S[        X R                  5      5      UU R                  [        S   -
  U R                  & U$ )Nr   rj  r  )ri  r`  rp  rW  r4  r&  ro  rG  ra  r1  r0  r  )rt  r0   
cur_offsetucru  s       r   r4  UnwindInfo.__pack__  s    )5::t5U5WQz4/12:t35
""BII,,..@ACASASAUDj99+;+;+==>))**,,J	 # 4' D'$0D0D"EF "5c"::T__ r   c                     U R                   $ r   )rc  r{  s    r   get_chained_function_entry%UnwindInfo.get_chained_function_entry  r  r   c                 F    U R                   S :w  a  [        S5      eXl         g )Nz(Chained function entry cannot be changed)rc  r  )rt  entrys     r   set_chained_function_entry%UnwindInfo.set_chained_function_entry  s"    $& JKK#r   )ro  rc  rb  rd  r`  ra  r  )r~  r  r  r  r  rq  r{  r7  rW  r  r&  r4  r  r  r  r  r  s   @r   rW  rW  (  s;    
)0<|6"$#$ $r   rW  c                   0    \ rS rSrSrS rS rS rS rSr	g)	PrologEpilogOpi  zMeant as an abstract class representing a generic unwind code.
There is a subclass of PrologEpilogOp for each member of UNWIND_OP_CODES enum.
c                 v    [        U R                  U5      US9U l        U R                  R                  U5        g )NrZ  )r  _get_formatrG  r-  )rt  unw_coder0   unw_infor  s        r   rs  PrologEpilogOp.initialize  s2    ,X&K
 	t$r   c                     g)zComputes how many UNWIND_CODE structures UNWIND_CODE occupies.
May be called before initialize() and, for that reason, should not rely on
the values of intance attributes.
r9   re  rt  r  r  s      r   rr  (PrologEpilogOp.length_in_code_structures  s    
 r   c                     g)NTre  r{  s    r   r  PrologEpilogOp.is_valid  s    r   c                     g)Nr[  re  rt  r  s     r   r  PrologEpilogOp._get_format  s    Nr   rG  N)
r~  r  r  r  r  rs  rr  r  r  r  re  r   r   r  r    s    %Or   r  c                   $    \ rS rSrSrS rS rSrg)PrologEpilogOpPushRegi  UWOP_PUSH_NONVOLc                     g)N)UNWIND_CODE_PUSH_NONVOL)r]  r^  B:4,Regre  r  s     r   r  !PrologEpilogOpPushReg._get_format  s    Wr   c                 B    S[         U R                  R                     -   $ )Nz	.PUSHREG )	REGISTERSrG  Regr{  s    r   r  PrologEpilogOpPushReg.__str__  s    Yt{{777r   re  N)r~  r  r  r  r  r  r  r  re  r   r   r  r    s    X8r   r  c                   0    \ rS rSrSrS rS rS rS rSr	g)	PrologEpilogOpAllocLargei  UWOP_ALLOC_LARGEc                 :    SSSSUR                   S:X  a  S44$ S44$ )NUNWIND_CODE_ALLOC_LARGEr]  r^  r_  r   zH,AllocSizeInQwordszI,AllocSizeOpInfor  s     r   r  $PrologEpilogOpAllocLarge._get_format  sB    %)1A)=%	
 	
 DQ	
 	
r   c                 *    UR                   S:X  a  S$ S$ )Nr   rA   rC   r  r  s      r   rr  2PrologEpilogOpAllocLarge.length_in_code_structures  s    OOq(q/a/r   c                     U R                   R                  S:X  a  U R                   R                  S-  $ U R                   R                  $ )Nr   rM   )rG  r  AllocSizeInQwords	AllocSizer{  s    r   get_alloc_size'PrologEpilogOpAllocLarge.get_alloc_size  sC     {{!!Q& KK))A-	
 &&	
r   c                 :    S[        U R                  5       5      -   $ Nz.ALLOCSTACK rn  r  r{  s    r   r   PrologEpilogOpAllocLarge.__str__      D$7$7$9 :::r   re  N)
r~  r  r  r  r  r  rr  r  r  r  re  r   r   r  r    s    	
0
;r   r  c                   *    \ rS rSrSrS rS rS rSrg)PrologEpilogOpAllocSmalli  UWOP_ALLOC_SMALLc                     g)N)UNWIND_CODE_ALLOC_SMALL)r]  r^  zB:4,AllocSizeInQwordsMinus8re  r  s     r   r  $PrologEpilogOpAllocSmall._get_format      
r   c                 :    U R                   R                  S-  S-   $ r  )rG  AllocSizeInQwordsMinus8r{  s    r   r  'PrologEpilogOpAllocSmall.get_alloc_size  s    {{22Q6::r   c                 :    S[        U R                  5       5      -   $ r  r  r{  s    r   r   PrologEpilogOpAllocSmall.__str__  r  r   re  N)	r~  r  r  r  r  r  r  r  r  re  r   r   r  r    s    
;;r   r  c                   2   ^  \ rS rSrSrU 4S jrS rSrU =r$ )PrologEpilogOpSetFPi#  UWOP_SET_FPREGc                 x   > [         [        U ]  XX45        UR                  U l        UR
                  S-  U l        g Nr6   )rp  r  rs  FrameRegister_frame_registerFrameOffset_frame_offsetrt  r  r0   r  r  ru  s        r   rs  PrologEpilogOpSetFP.initialize&  s;    !43H	
  (55%11B6r   c                 `    S[         U R                     -   S-   [        U R                  5      -   $ )Nz
.SETFRAME r  )r  r  rn  r  r{  s    r   r  PrologEpilogOpSetFP.__str__-  s:    ,,-. $$$%&	
r   )r  r  )	r~  r  r  r  r  rs  r  r  r  r  s   @r   r  r  #  s    7
 
r   r  c                   0    \ rS rSrSrS rS rS rS rSr	g)	PrologEpilogOpSaveRegi6  UWOP_SAVE_NONVOLc                     gNrA   re  )rt  unwcoder  s      r   rr  /PrologEpilogOpSaveReg.length_in_code_structures9      r   c                 4    U R                   R                  S-  $ r  )rG  OffsetInQwordsr{  s    r   
get_offset PrologEpilogOpSaveReg.get_offset<  s    {{))A--r   c                     g)N)UNWIND_CODE_SAVE_NONVOL)r]  r^  r  zH,OffsetInQwordsre  r  s     r   r  !PrologEpilogOpSaveReg._get_format?  r  r   c                 |    S[         U R                  R                     -   S-   [        U R	                  5       5      -   $ Nz	.SAVEREG r  )r  rG  r  rn  r  r{  s    r   r  PrologEpilogOpSaveReg.__str__E  s0    Yt{{77$>T__EVAWWWr   re  N
r~  r  r  r  r  rr  r  r  r  r  re  r   r   r  r  6  s    .
Xr   r  c                   0    \ rS rSrSrS rS rS rS rSr	g)	PrologEpilogOpSaveRegFariI  UWOP_SAVE_NONVOL_FARc                     gNrC   re  r  s      r   rr  2PrologEpilogOpSaveRegFar.length_in_code_structuresL  r  r   c                 .    U R                   R                  $ r   rG  rU  r{  s    r   r  #PrologEpilogOpSaveRegFar.get_offsetO      {{!!!r   c                     g)N)UNWIND_CODE_SAVE_NONVOL_FARr]  r^  r  zI,Offsetre  r  s     r   r  $PrologEpilogOpSaveRegFar._get_formatR  r  r   c                     S[         U R                  R                     -   S-   [        U R                  R                  5      -   $ r  )r  rG  r  rn  rU  r{  s    r   r   PrologEpilogOpSaveRegFar.__str__X  s3    Yt{{77$>T[[EWEWAXXXr   re  Nr  re  r   r   r  r  I  s    "
Yr   r  c                   0    \ rS rSrSrS rS rS rS rSr	g)	PrologEpilogOpSaveXMMi\  UWOP_SAVE_XMM128c                     g)N)UNWIND_CODE_SAVE_XMM128)r]  r^  r  zH,OffsetIn2Qwordsre  r  s     r   r  !PrologEpilogOpSaveXMM._get_format_  r  r   c                     gr  re  r  s      r   rr  /PrologEpilogOpSaveXMM.length_in_code_structurese  r  r   c                 4    U R                   R                  S-  $ r  )rG  OffsetIn2Qwordsr{  s    r   r   PrologEpilogOpSaveXMM.get_offseth  s    {{**R//r   c                     S[        U R                  R                  5      -   S-   [        U R	                  5       5      -   $ Nz.SAVEXMM128 XMMr  )rV  rG  r  rn  r  r{  s    r   r  PrologEpilogOpSaveXMM.__str__k  s0     3t{{#77$>T__EVAWWWr   re  N
r~  r  r  r  r  r  rr  r  r  r  re  r   r   r  r  \  s    
0Xr   r  c                   0    \ rS rSrSrS rS rS rS rSr	g)	PrologEpilogOpSaveXMMFario  UWOP_SAVE_XMM128_FARc                     g)N)UNWIND_CODE_SAVE_XMM128_FARr  re  r  s     r   r  $PrologEpilogOpSaveXMMFar._get_formatr  r  r   c                     gr  re  r  s      r   rr  2PrologEpilogOpSaveXMMFar.length_in_code_structuresx  r  r   c                 .    U R                   R                  $ r   r  r{  s    r   r  #PrologEpilogOpSaveXMMFar.get_offset{  r  r   c                     S[        U R                  R                  5      -   S-   [        U R                  R                  5      -   $ r  )rV  rG  r  rn  rU  r{  s    r   r   PrologEpilogOpSaveXMMFar.__str__~  s3     3t{{#77$>T[[EWEWAXXXr   re  Nr  re  r   r   r  r  o  s    
"Yr   r  c                       \ rS rSrSrS rSrg)PrologEpilogOpPushFramei  UWOP_PUSH_MACHFRAMEc                 J    SU R                   R                  (       a  S-   $ S-   $ )Nz
.PUSHFRAMEz <code>r  )rG  r  r{  s    r   r  PrologEpilogOpPushFrame.__str__  s!    DKK,>,>yGGBGGr   re  N)r~  r  r  r  r  r  r  re  r   r   r  r    s    Hr   r  c                   J   ^  \ rS rSrSrU 4S jrS rS rS rS r	S r
S	rU =r$ )
PrologEpilogOpEpilogMarkeri  UWOP_EPILOGc                   > SU l         [        US5      (       + U l        [        [        U ]  XX45        U R                  (       a8  [        USU R                  R                  5        UR                  S-  S:H  U l         UR                  U l        g )NTSizeOfEpilogr9   r   )_long_offstr}  _firstrp  r#  rs  r*  rG  Sizer  r&  _epilog_sizer  s        r   rs  %PrologEpilogOpEpilogMarker.initialize  st    !(N;;($:H	
 ;;Hndkk.>.>?'2a7D$11r   c                 Z    U R                   (       a  SUR                  S-  S:X  a  S4$ S4$ g)NUNWIND_CODE_EPILOGr9   )zB,OffsetLow,Sizer^  	B:4,Flags)zB,Sizer^  r.  B,OffsetLowz
B:4,UnusedB:4,OffsetHigh)r-  )r/  r^  r0  )r(  r  r  s     r   r  &PrologEpilogOpEpilogMarker._get_format  sC     ;;$??Q&!+ B 	 r   c                 R    [        US5      (       d  UR                  S-  S:X  a  S$ S$ )Nr&  r9   r   rA   )r}  r  r  s      r   rr  4PrologEpilogOpEpilogMarker.length_in_code_structures  s6     8^44(//A:MRS9S 	
 	
r   c                     U R                   R                  U R                  (       a  U R                   R                  S-  -  $ S-  $ )NrM   r   )rG  	OffsetLowr'  
OffsetHighr{  s    r   r  %PrologEpilogOpEpilogMarker.get_offset  s@    {{$$+/+;+;DKK""a'
 	
AB
 	
r   c                 (    U R                  5       S:  $ r.   )r  r{  s    r   r  #PrologEpilogOpEpilogMarker.is_valid  s     1$$r   c                     U R                  5       S:  a5  S[        U R                  5      -   S-   [        U R                  5       5      -   $ S$ )Nr   zEPILOG: size=z, offset from the end=-r  )r  rn  r*  r{  s    r   r  "PrologEpilogOpEpilogMarker.__str__  sX      1$	 $##$%'( $//#$%	
 	
r   )r*  r(  r'  )r~  r  r  r  r  rs  r  rr  r  r  r  r  r  r  s   @r   r#  r#    s)    	2.


%	
 	
r   r#  c                   T    \ rS rSrSr\\\\\	\
\\\\\\\\\\\\\\0
r\S 5       rSrg)rp  i  zBA factory for creating unwind codes based on the value of UnwindOpc                 v    U R                   nU[        R                  ;   a  [        R                  U   " 5       $ S $ r   )UnwindOprp  _class_dict)r  codes     r   rq  PrologEpilogOpsFactory.create  s@     -999 #..t46	
 	
r   re  N)r~  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r$  r#  r?  staticmethodrq  r  re  r   r   rp  rp    s^    L 	/22+/6/64/K 
 
r   rp  z!#$%&'()-@^_`{}~+,.;=[]c                    ^ U b   [        U [        [        [        45      (       d  g[        S-   m[        U4S j[        U 5       5       5      $ )NFs   \/c              3   ,   >#    U  H	  oT;   v   M     g 7fr   re  )r  r  alloweds     r   r  (is_valid_dos_filename.<locals>.<genexpr>  s     ,VG|Vs   )rU  rV  rW  ri  allowed_filenameallset)r  rE  s    @r   is_valid_dos_filenamerJ    s=    y
1sE9&=>>'G,SV,,,r   r  relax_allowed_charactersr  c                    ^ SmU(       a  SmU S L=(       a@    [        U [        [        [        45      =(       a    [	        U4S j[        U 5       5       5      $ )Ns	   ._?@$()<>s   !"#$%&'()*+,-./:<>?[\]^_`{|}~@c              3   L   >#    U  H  o[         ;   =(       d    UT;   v   M     g 7fr   )allowed_function_name)r  r  allowed_extras     r   r  )is_valid_function_name.<locals>.<genexpr>	  s"     SFq++AqM/AAFs   !$)rU  rV  rW  ri  rH  rI  )r  rK  rO  s     @r   is_valid_function_namerQ  	  sN     !M;	 	Tq3y12	TSCPQFSSr   c                   (   \ rS rSrSrSrSrSrSrSr	Sr
S	rS
rSrSrSrSrSrSrSrSrSrSrSrSrSrSrSrSrSrSrSrSr Sr!S r"S!r#S"r$S#r%S$r&S%r'S&r(S'r)S(r*S)r+S*S*S*\,S+4S, jr-S- r.S. r/S/ r0S0 r1S1 r2S2 r3S3 r4S4 r5S5 r6S6 r7SS7 jr8S8 r9 SS9 jr:S: r;S; r<S< r=S= r>S> r?S? r@SS@ jrASA rBSB rCSC rDSSD jrESE rFSF rGSG rHSSH jrISI rJSJ rKSK rLSSL jrMSM rNSN rOSSO jrP  SSP jrQSSQ jrRSSR jrSSS rTSST jrUSU rVSV rW\X4SW jrYSX rZSY r[SSZ jr\S[ r]S\ r^S] r_S^ r`S_ raSS` jrbSSa jrcSb rdSc reSd rfSe rgSf rhSg riSh rjSi rkSj rlSk rmSl rnSm roSn rpSo rqSp rrSq rsSr rtSs ruSt rvSu rwSv rxSw rySx\zSy\{4Sz jr|S{ r}S| r~S} rS~ rS rS rS rS rS rS rS rS rSrg*)PEi	  a  A Portable Executable representation.

This class provides access to most of the information in a PE file.

It expects to be supplied the name of the file to load or PE data
to process and an optional argument 'fast_load' (False by default)
which controls whether to load all the directories information,
which can be quite time consuming.

pe = pefile.PE('module.dll')
pe = pefile.PE(name='module.dll')

would load 'module.dll' and process it. If the data is already
available in a buffer the same can be achieved with:

pe = pefile.PE(data=module_dll_data)

The "fast_load" can be set to a default by setting its value in the
module itself by means, for instance, of a "pefile.fast_load = True".
That will make all the subsequent instances not to load the
whole PE structure. The "full_load" method can be used to parse
the missing data at a later stage.

Basic headers information will be available in the attributes:

DOS_HEADER
NT_HEADERS
FILE_HEADER
OPTIONAL_HEADER

All of them will contain among their attributes the members of the
corresponding structures as defined in WINNT.H

The raw data corresponding to the header (from the beginning of the
file up to the start of the first section) will be available in the
instance's attribute 'header' as a string.

The sections will be available as a list in the 'sections' attribute.
Each entry will contain as attributes all the structure's members.

Directory entries will be available as attributes (if they exist):
(no other entries are processed at this point)

DIRECTORY_ENTRY_IMPORT (list of ImportDescData instances)
DIRECTORY_ENTRY_EXPORT (ExportDirData instance)
DIRECTORY_ENTRY_RESOURCE (ResourceDirData instance)
DIRECTORY_ENTRY_DEBUG (list of DebugData instances)
DIRECTORY_ENTRY_BASERELOC (list of BaseRelocationData instances)
DIRECTORY_ENTRY_TLS
DIRECTORY_ENTRY_BOUND_IMPORT (list of BoundImportData instances)

The following dictionary attributes provide ways of mapping different
constants. They will accept the numeric value and return the string
representation and the opposite, feed in the string and get the
numeric constant:

DIRECTORY_ENTRY
IMAGE_CHARACTERISTICS
SECTION_CHARACTERISTICS
DEBUG_TYPE
SUBSYSTEM_TYPE
MACHINE_TYPE
RELOCATION_TYPE
RESOURCE_TYPE
LANG
SUBLANG
)IMAGE_DOS_HEADER)z	H,e_magiczH,e_cblpzH,e_cpzH,e_crlczH,e_cparhdrzH,e_minalloczH,e_maxalloczH,e_sszH,e_spzH,e_csumzH,e_ipzH,e_csz
H,e_lfarlczH,e_ovnoz8s,e_resz	H,e_oemidzH,e_oeminfoz
20s,e_res2z
I,e_lfanew)IMAGE_FILE_HEADER)z	H,MachinezH,NumberOfSectionsI,TimeDateStampzI,PointerToSymbolTablezI,NumberOfSymbolszH,SizeOfOptionalHeaderzH,Characteristics)IMAGE_DATA_DIRECTORY)I,VirtualAddressI,Size)IMAGE_OPTIONAL_HEADER)H,MagicB,MajorLinkerVersionB,MinorLinkerVersionI,SizeOfCodeI,SizeOfInitializedDataI,SizeOfUninitializedDataI,AddressOfEntryPointI,BaseOfCodezI,BaseOfDatazI,ImageBaseI,SectionAlignmentI,FileAlignmentH,MajorOperatingSystemVersionH,MinorOperatingSystemVersionH,MajorImageVersionH,MinorImageVersionH,MajorSubsystemVersionH,MinorSubsystemVersionI,Reserved1I,SizeOfImageI,SizeOfHeaders
I,CheckSumH,SubsystemH,DllCharacteristicszI,SizeOfStackReservezI,SizeOfStackCommitzI,SizeOfHeapReservezI,SizeOfHeapCommitI,LoaderFlagsI,NumberOfRvaAndSizes)IMAGE_OPTIONAL_HEADER64)r[  r\  r]  r^  r_  r`  ra  rb  zQ,ImageBaserc  rd  re  rf  rg  rh  ri  rj  rk  rl  rm  rn  ro  rp  zQ,SizeOfStackReservezQ,SizeOfStackCommitzQ,SizeOfHeapReservezQ,SizeOfHeapCommitrq  rr  )IMAGE_NT_HEADERS)I,Signature)IMAGE_SECTION_HEADER)
z8s,Namez,I,Misc,Misc_PhysicalAddress,Misc_VirtualSizerX  zI,SizeOfRawDataI,PointerToRawDatazI,PointerToRelocationszI,PointerToLinenumberszH,NumberOfRelocationszH,NumberOfLinenumbersI,Characteristics)IMAGE_DELAY_IMPORT_DESCRIPTOR)z	I,grAttrszI,szNamezI,phmodzI,pIATzI,pINTzI,pBoundIATzI,pUnloadIATzI,dwTimeStamp)IMAGE_IMPORT_DESCRIPTOR)z$I,OriginalFirstThunk,CharacteristicsrV  zI,ForwarderChainI,NamezI,FirstThunk)IMAGE_EXPORT_DIRECTORY)rx  rV  H,MajorVersionH,MinorVersionr{  zI,BasezI,NumberOfFunctionszI,NumberOfNameszI,AddressOfFunctionszI,AddressOfNameszI,AddressOfNameOrdinals)IMAGE_RESOURCE_DIRECTORY)rx  rV  r}  r~  zH,NumberOfNamedEntrieszH,NumberOfIdEntries)IMAGE_RESOURCE_DIRECTORY_ENTRY)r{  I,OffsetToData)IMAGE_RESOURCE_DATA_ENTRY)r  rY  z
I,CodePagez
I,Reserved)VS_VERSIONINFOzH,LengthzH,ValueLengthzH,Type)VS_FIXEDFILEINFO)ru  zI,StrucVersionzI,FileVersionMSzI,FileVersionLSzI,ProductVersionMSzI,ProductVersionLSzI,FileFlagsMaskzI,FileFlagszI,FileOSz
I,FileTypezI,FileSubtypezI,FileDateMSzI,FileDateLS)StringFileInfor  )StringTabler  )Stringr  )Varr  )IMAGE_THUNK_DATA)z0I,ForwarderString,Function,Ordinal,AddressOfData)r  )z0Q,ForwarderString,Function,Ordinal,AddressOfData)IMAGE_DEBUG_DIRECTORY)rx  rV  r}  r~  zI,TypezI,SizeOfDatazI,AddressOfRawDatarw  )IMAGE_BASE_RELOCATION)rX  zI,SizeOfBlock)IMAGE_BASE_RELOCATION_ENTRY)zH,Data)0IMAGE_IMPORT_CONTROL_TRANSFER_DYNAMIC_RELOCATION)I:12,PageRelativeOffsetI:1,IndirectCallzI:19,IATIndex)/IMAGE_INDIR_CONTROL_TRANSFER_DYNAMIC_RELOCATION)r  r  zI:1,RexWPrefixzI:1,CfgCheckzI:1,Reserved)+IMAGE_SWITCHTABLE_BRANCH_DYNAMIC_RELOCATION)r  zI:4,RegisterNumber)IMAGE_TLS_DIRECTORY)zI,StartAddressOfRawDatazI,EndAddressOfRawDatazI,AddressOfIndexzI,AddressOfCallBacksI,SizeOfZeroFillrx  )r  )zQ,StartAddressOfRawDatazQ,EndAddressOfRawDatazQ,AddressOfIndexzQ,AddressOfCallBacksr  rx  )IMAGE_LOAD_CONFIG_DIRECTORY)+rY  rV  r}  r~  I,GlobalFlagsClearI,GlobalFlagsSetI,CriticalSectionDefaultTimeoutzI,DeCommitFreeBlockThresholdzI,DeCommitTotalFreeThresholdzI,LockPrefixTablezI,MaximumAllocationSizezI,VirtualMemoryThresholdI,ProcessHeapFlagszI,ProcessAffinityMaskH,CSDVersionH,Reserved1z
I,EditListzI,SecurityCookiezI,SEHandlerTablezI,SEHandlerCountzI,GuardCFCheckFunctionPointerz I,GuardCFDispatchFunctionPointerzI,GuardCFFunctionTablezI,GuardCFFunctionCountI,GuardFlagsH,CodeIntegrityFlagsH,CodeIntegrityCatalogI,CodeIntegrityCatalogOffsetI,CodeIntegrityReservedz I,GuardAddressTakenIatEntryTablez I,GuardAddressTakenIatEntryCountzI,GuardLongJumpTargetTablezI,GuardLongJumpTargetCountzI,DynamicValueRelocTablezI,CHPEMetadataPointerzI,GuardRFFailureRoutinez&I,GuardRFFailureRoutineFunctionPointerI,DynamicValueRelocTableOffsetH,DynamicValueRelocTableSectionH,Reserved2z?I,GuardRFVerifyStackPointerFunctionPointerI,HotPatchTableOffsetI,Reserved3zI,EnclaveConfigurationPointer)r  ),rY  rV  r}  r~  r  r  r  zQ,DeCommitFreeBlockThresholdzQ,DeCommitTotalFreeThresholdzQ,LockPrefixTablezQ,MaximumAllocationSizezQ,VirtualMemoryThresholdzQ,ProcessAffinityMaskr  r  r  z
Q,EditListzQ,SecurityCookiezQ,SEHandlerTablezQ,SEHandlerCountzQ,GuardCFCheckFunctionPointerz Q,GuardCFDispatchFunctionPointerzQ,GuardCFFunctionTablezQ,GuardCFFunctionCountr  r  r  r  r  z Q,GuardAddressTakenIatEntryTablez Q,GuardAddressTakenIatEntryCountzQ,GuardLongJumpTargetTablezQ,GuardLongJumpTargetCountzQ,DynamicValueRelocTablezQ,CHPEMetadataPointerzQ,GuardRFFailureRoutinez&Q,GuardRFFailureRoutineFunctionPointerr  r  r  z*Q,GuardRFVerifyStackPointerFunctionPointerzI,HotPatchTableOffsetr  zQ,EnclaveConfigurationPointer)IMAGE_DYNAMIC_RELOCATION_TABLE)z	I,VersionrY  )IMAGE_DYNAMIC_RELOCATION)I,SymbolI,BaseRelocSize)IMAGE_DYNAMIC_RELOCATION64)Q,Symbolr  )IMAGE_DYNAMIC_RELOCATION_V2)I,HeaderSizeI,FixupInfoSizer  I,SymbolGroupI,Flags)IMAGE_DYNAMIC_RELOCATION64_V2)r  r  r  r  r  )IMAGE_BOUND_IMPORT_DESCRIPTOR)rV  H,OffsetModuleNamezH,NumberOfModuleForwarderRefs)IMAGE_BOUND_FORWARDER_REF)rV  r  z
H,Reserved)RUNTIME_FUNCTION)zI,BeginAddresszI,EndAddresszI,UnwindDataNx   c                    X@l         XPl        S U l        / U l        / U l        S U l        Uc  Uc  [        S5      e/ U l        S U l        SU l	        SU l
        SU l        SU l        SU l        [        R                  [        R                   [        R"                  S.U l        Ub  UO['        5       S   n U R)                  XU5        g !   U R+                  5         e = f)NzMust supply either name or dataFr   )rC   rE   rG   	fast_load)max_symbol_exportsmax_repeated_symbol_get_section_by_rva_last_usedsections_PE__warningsr  rJ  __structures___PE__from_fileFileAlignment_WarningSectionAlignment_Warning!_PE__total_resource_entries_count_PE__total_resource_bytes_PE__total_import_symbolsrS  ;__IMAGE_IMPORT_CONTROL_TRANSFER_DYNAMIC_RELOCATION_format__:__IMAGE_INDIR_CONTROL_TRANSFER_DYNAMIC_RELOCATION_format__6__IMAGE_SWITCHTABLE_BRANCH_DYNAMIC_RELOCATION_format__#dynamic_relocation_format_by_symbolglobals	__parse__close)rt  r
  r0   r  r  r  s         r   rq  PE.__init__  s     #5#6 -1*<DL>??
 ! &+"(-% /0+ '(#&'# MMLLHH4
0 "+!6IGIk<R		NN4y1	JJLs   3C Cc                     U $ r   re  r{  s    r   	__enter__PE.__enter__T  s    r   c                 $    U R                  5         g r   )r  )rt  rF  rb  	tracebacks       r   __exit__PE.__exit__W  s    

r   c                 ^   U R                   SL a  [        U S5      (       a  [        [        R                  [        5      (       a)  [        U R
                  [        R                  5      (       d"  S[        [	        U R
                  5      5      ;   a  U R
                  R                  5         U ?g g g g )NTrt  z	mmap.mmap)r  r}  rU  mmaprF  rt  r  r  r{  s    r   r  PE.closeZ  s~    $j))DIIt,,DMM4991U1U$tDMM':";; MM!	 < * %r   c                     [        XS9n UR                  U5        U R                  R	                  U5        U$ ! [         a8  nU R                  R	                  SR                  US   X55      5         SnAgSnAff = fziApply structure format to raw data.

Returns an unpacked structure object if successful, None otherwise.
rZ  z7Corrupt header "{0}" at file offset {1}. Exception: {2}r   N)r  r-  r  r  r  r  r  rt  r  r0   r  	structureerrs         r   __unpack_data__PE.__unpack_data__f  s}     f>		  & 	""9-  	OO""IPP1I{
 	   9 
A;.A66A;c                     [        XS9n UR                  U5        U R                  R	                  U5        U$ ! [         a8  nU R                  R	                  SR                  US   X55      5         SnAgSnAff = fr  )r  r-  r  r  r  r  r  r  s         r   __unpack_data_with_bitfields__!PE.__unpack_data_with_bitfields__|  s}     +6K		  & 	""9-  	OO""IPP1I{
 	r  c           
         Ub  [         R                  " U5      nUR                  S:X  a  [        S5      eSn [	        US5      nUR                  5       U l        [        [        S5      (       a6  [        R                  " U R
                  S[        R                  5      U l	        O3[        R                  " U R
                  S[        R                  S9U l	        SU l         Ub  UR                  5         OUb  X l	        SU l        [!        U R                  5      U l        SU l        U(       d  ['        [)        U R                  5      5      R+                  5        H  u  pUS:X  a  U	[!        U R                  5      -  S:  d&  US:w  d  M/  U	[!        U R                  5      -  S:  d  MM  U R,                  R/                  SR                  USU	-  [!        U R                  5      -  5      5        M     U R                  SS n
[!        U
5      S:w  a  [        S5      eU R1                  U R2                  U
SS9U l        U R4                  R6                  [8        :X  a  [        S5      eU R4                  (       a  U R4                  R6                  [:        :w  a  [        S5      eU R4                  R<                  [!        U R                  5      :  a  [        S5      eU R4                  R<                  nU R1                  U R>                  U R                  XS-    US9U l         U R@                  (       a  U R@                  RB                  (       d  [        S5      eSU R@                  RB                  -  [D        :X  a  [        S5      eSU R@                  RB                  -  [F        :X  a  [        S5      eSU R@                  RB                  -  [H        :X  a  [        S5      eSU R@                  RB                  -  [J        :X  a  [        S5      eU R@                  RB                  [L        :w  a  [        S5      eU R1                  U RN                  U R                  US-   US-   S-    US-   S9U l(        [S        [T        S 5      nU RP                  (       d  [        S!5      e[W        U RP                  U RP                  RX                  U5        US-   U RP                  R[                  5       -   nXRP                  R\                  -   nU R1                  U R^                  U R                  XS"-    US9U l0        S#nU R`                  cW  [!        U R                  XS$-    5      U:  a9  S%nU R                  XS$-    S&U-  -   nU R1                  U R^                  UUS9U l0        U R`                  b  U R`                  Rb                  [d        :X  a  [d        U l3        OU R`                  Rb                  [h        :X  a  [h        U l3        U R1                  U Rj                  U R                  XS$-    US9U l0        S'nU R`                  cW  [!        U R                  XS$-    5      U:  a9  S%nU R                  XS$-    S&U-  -   nU R1                  U Rj                  UUS9U l0        U RP                  (       d  [        S!5      eU R`                  c  [        S(5      eU Rf                  c>  U R,                  R/                  S)R                  U R`                  Rb                  5      5        [S        [l        S*5      n[W        U R`                  U R`                  Rn                  U5        / U R`                  l8        XR`                  R[                  5       -   nU RP                  U R@                  l(        U R`                  U R@                  l0        U R`                  Rr                  U R`                  Rt                  :  a  U R,                  R/                  S+5        U R`                  Rv                  S,:  a2  U R,                  R/                  S-U R`                  Rv                  -  5        S"n[y        [{        S.U R`                  Rv                  -  5      5       H  n[!        U R                  5      U-
  S:X  a    O[!        U R                  5      U-
  S:  a  U R                  US S/-   nOU R                  UUU-    nU R1                  U R|                  UUS9nUc    Op [~        U   Ul@        UUR[                  5       -  nU R`                  Rp                  R/                  U5        UXR`                  R[                  5       -   S%-   :  d  M    O   U R                  U5      nU R                   Vs/ s HE  nUR                  S:  d  M  U R                  UR                  U R`                  R                  5      PMG     nn[!        U5      S:  a  [        U5      nOSnU(       a  UU:  a  U R                  SU U lI        OU R                  SU U lI        U R                  U R`                  Rr                  5       bq  U R                  U R`                  Rr                  5      nU[!        U R                  5      :  a2  U R,                  R/                  S0U R`                  Rr                  -  5        O2U R,                  R/                  S1U R`                  Rr                  -  5        U(       d  U R                  5         gg! [         a>  nSR                  U5      nU=(       a    S	U-  n[        S
R                  X5      5      eSnAff = f! Ub  UR                  5         f f = f! [        [        4 a       GM  f = fs  snf )2zParse a Portable Executable file.

Loads a PE file, parsing all its structures and making them available
through the instance's attributes.
Nr   zThe file is emptyrbMAP_PRIVATE)accessTr  z: %szUnable to access file '{0}'{1}Fg      ?g333333?zeByte 0x{0:02x} makes up {1:.4f}% of the file's contents. This may indicate truncation / malformation.g      Y@rc   z9Unable to read the DOS Header, possibly a truncated file.rZ  z)Probably a ZM Executable (not a PE file).zDOS Header magic not found.z.Invalid e_lfanew value, probably not a PE filerM   zNT Headers not found.r  z0Invalid NT Headers signature. Probably a NE filez0Invalid NT Headers signature. Probably a LE filez0Invalid NT Headers signature. Probably a LX filez0Invalid NT Headers signature. Probably a TE filezInvalid NT Headers signature.rE   r5   IMAGE_FILE_zFile Header missingrg   rE  r#   re   rA  r  z5No Optional Header found, invalid PE32 or PE32+ file.z*Invalid type 0x{0:04x} in Optional Header.IMAGE_DLLCHARACTERISTICS_zXSizeOfHeaders is smaller than AddressOfEntryPoint: this file cannot run under Windows 8.r6   zsSuspicious NumberOfRvaAndSizes in the Optional Header. Normal values are never larger than 0x10, the value is: 0x%xs           z[Possibly corrupt file. AddressOfEntryPoint lies outside the file. AddressOfEntryPoint: 0x%xzTAddressOfEntryPoint lies outside the sections' boundaries. AddressOfEntryPoint: 0x%x)Mosstatst_sizer  openfilenor}  r  r  rt  ACCESS_READr  IOErrorr  	Exceptionr  rF  $_PE__resource_size_limit_upperbounds _PE__resource_size_limit_reachedr   ri  r	  r  r  r  __IMAGE_DOS_HEADER_format__
DOS_HEADERe_magicIMAGE_DOSZM_SIGNATUREIMAGE_DOS_SIGNATUREe_lfanew__IMAGE_NT_HEADERS_format__
NT_HEADERSr@  IMAGE_NE_SIGNATUREIMAGE_LE_SIGNATUREIMAGE_LX_SIGNATUREIMAGE_TE_SIGNATUREIMAGE_NT_SIGNATURE__IMAGE_FILE_HEADER_format__FILE_HEADERr\  IMAGE_CHARACTERISTICSrc  rz  r&  SizeOfOptionalHeader __IMAGE_OPTIONAL_HEADER_format__rj  Magicr  r  r  "__IMAGE_OPTIONAL_HEADER64_format__DLL_CHARACTERISTICSDllCharacteristicsDATA_DIRECTORYAddressOfEntryPointSizeOfHeadersNumberOfRvaAndSizesr  r%   __IMAGE_DATA_DIRECTORY_format__DIRECTORY_ENTRYr
  KeyErrorAttributeErrorparse_sectionsr  r]  ri  rk  rr  headerget_section_by_rvar  	full_load)rt  fnamer0   r  r  fdexcpexception_msgbyte
byte_countdos_header_datant_headers_offsetimage_flagsoptional_header_offsetsections_offset&MINIMUM_VALID_OPTIONAL_HEADER_RAW_SIZEpadding_lengthpadded_datadll_characteristics_flagsr  )MAX_ASSUMED_VALID_NUMBER_OF_RVA_AND_SIZESrN  	dir_entryr  rawDataPointerslowest_section_offset	ep_offsets                              r   r  PE.__parse__  s
    775>D||q #$788B%& iik4//$(IIdkk1d>N>N$ODM %)IIdkk1TEUEU$VDM#'  >HHJ M$D 25T]]1C.-2*$+Idmm,D$E$K$K$M 
 AI*s4==/A"AC"GAI*s4==/A"AD"HOO**L &uz'9C<N'NO	 %N --,2%K  ..,,o1 / 
 ??""&;; KLL$//"9"9=P"P =>>
 ??##c$--&88 PQQ OO44..,,MM+!.CD) / 
 doo&?&? 788T__...3EE RSST__...3EE RSST__...3EE RSST__...3EE RSS??$$(:: ?@@//--MM+a/2Ca2G"2LM)A- 0 

 %%:MJ 566 	$""D$4$4$D$DkR!2Q!69I9I9P9P9R!R 13C3C3X3XX#3311MM0C3OP.	  4  
  24.   (4PU7UV 66 !N --&%)G')K $(#7#7552 $8 $D  +##))-EE7%%++/LL<'+';';;;MM.%1O !7 (< ($ :@6 ((02e5S
 >> &)N"&--.%1O#/#1K ,0+?+???#$: ,@ ,D(  566
 ' WXX<<OO""<CC((.. %3!<%
!
 	    33%	
 /1+'*>*>*E*E*GG&*&6&6#*.*>*>'
   44""001 OO"". 33d:OO""O&&::; 5:1s:(<(<(P(PPQRA4==!F*a/4==!F*Q.}}VW-	9}}V&OO ,,44d - I  !0!3	 i&&((F  //66yA &)=)=)D)D)FFO W SZ $$_5 ]]	
 #!!A%	D%%""D$8$8$F$F #	 	 
 !#$'$8!$(!$(=(F--0DK--(>)>?DK
 ##D$8$8$L$LM 00$$88I 3t}}--&&7**>>? OO"",.2.B.B.V.VW
 NN o
   %T 2 - J6M3I4;;EQ  >HHJ "H n- >
sB   B&k+ m'm(?4m(+
l359l..l33l6 6mm%$m%c                 @   SnSnU R                   R                  SSU R                  R                  5       5      nUS:X  a  g U R                   SUS-    nUSS[	        [        U5      S-  5      -   n[        [        R                  " S	R                  [	        [        U5      S-  5      5      U5      5      nX%;  a  g [        R                  " S
XUR                  U5      S-      5      nSU0nUSUR                  S5       nXS'   S n	[        5       n
[        U5       H3  u  pU
R                  U	" U5      U	" Xk[        U5      -     5      -  5        M5     [!        U
5      US'   US   nUS   U-  U:w  d  US   U:w  d	  US   U:w  a  gXS'   / nXS'   USS n[#        [	        [        U5      S-  5      5       HW  nUSU-     U:X  a-  USU-  S-      U:w  a  U R$                  R                  S5          U$ XSU-     U-  USU-  S-      U-  /-  nMY     U$ ! [         a     gf = f)zParses the rich header
see http://www.ntcore.com/files/richsign.htm for more information

Structure:
00 DanS ^ checksum, checksum, checksum, checksum
10 Symbol RVA ^ checksum, Symbol size ^ checksum...
...
XX Rich, checksum, 0, 0,...
iDanSiRichs   Richre   NrM   rE   z<{0}I<Lr9   r,  raw_datac                 F    [        U [        5      (       d  [        U 5      $ U $ r   )rU  r%   rE  )r  s    r   <lambda>&PE.parse_rich_header.<locals>.<lambda>0  s    z!S'9'9Q@q@r   
clear_datar   rA   rC   checksumr  zRich Header is malformed)rt  findrj  r  r%   rF  listrG  rH  r  r  r1  indexri  r)  r  rW  r  r  )rt  DANSRICH
rich_index	rich_datar0   r,  resultr,  ord_r0  r+  r&   r1  headervaluesrN  s                   r   parse_rich_headerPE.parse_rich_header  s]    ]]''T4//??A

 	 dZ!^<I ""?AC	NQ,>(?$?@IgnnSY!1C-DEyQD    kk$ZZ%5%9 :;6y~~g67%z@[
!(+HCtCy4#c(N0C+DDF ,$Z0| 77X%aH)<Q8@S%z'xABxs3t9q=)*A AE{d" A	?h.OO**+EF
  !a%[83T!a%!)_x5OPPL + W  		s   A8H 
HHc                     U R                   $ )zReturn the list of warnings.

Non-critical problems found when parsing the PE file are
appended to a list of warnings. This method returns the
full list.
)r  r{  s    r   r  PE.get_warningsS  s     r   c                 B    U R                    H  n[        SU5        M     g)zPrint the list of warnings.

Non-critical problems found when parsing the PE file are
appended to a list of warnings. This method prints the
full list to standard output.
>N)r  print)rt  warnings     r   show_warningsPE.show_warnings]  s     G#w 'r   c                    U R                  5          " S S5      nU R                  5       nU(       a  U" 5       U l        UR                  SS5      U R                  l        UR                  SS5      U R                  l        UR                  SS5      U R                  l        UR                  SS5      U R                  l        UR                  SS5      U R                  l        gSU l        g)	zProcess the data directories.

This method will load the data directories which might not have
been loaded if the "fast_load" option was used.
c                       \ rS rSrSrg) PE.full_load.<locals>.RichHeaderiq  re  N)r~  r  r  r  r  re  r   r   
RichHeaderrH  q  s    r   rI  r1  Nr  r,  r,  r0  )	parse_data_directoriesr<  RICH_HEADERr=  r1  r  r,  r,  r0  )rt  rI  rich_headers      r   r  PE.full_loadh  s     	##%	 	 ,,.)|D(3
D(ID%&1ooh&ED##.??5$#?D (3
D(ID%*5//,*MD'#Dr   c                    [        U R                  5      nU R                   H;  n[        UR                  5       5      nUR	                  5       nXBXU[        U5      -   & M=     [        U S5      (       Ga(  [        U S5      (       Ga  U R                   GH  nU H  n[        US5      (       d  M  UR                   H  n[        UR                  R                  5       5       H  u  pUR                  U	   n
UR                  U	   n[        U5      US   :  a?  UR                  S5      R                  S5      nUSUS   S-   UU
S   U
S   US   S-  -   & Mt  UR                  S5      R                  S5      nUUU
S   U
S   [        U5      -   & M     M     M     GM     UnU(       d  U$ [!        US	5      nUR#                  U5        UR%                  5         g)
aM  Write the PE file.

This function will process all headers and components
of the PE file and include all changes made (by just
assigning to attributes in the PE objects) and write
the changes back to a file whose name is provided as
an argument. The filename is optional, if not
provided the data will be returned as a 'str' object.
r  FileInfor  r9   r  rE  NrA   zwb+)ri  rt  r  r4  r  rF  r}  rO  r  r3  entriesr	  entries_offsetsentries_lengthsrJ  rk  r  writer  )rt  filename	file_datar  struct_datar  finfor  st_entryr,  offsetslengthsrM  encoded_datanew_file_datar   s                   r   rS  PE.write  s    dmm,	,,I#I$6$6$89K..0F<GfK(889 -
 4)**tZ((!]]E!&"5-88,1,=,=26x7G7G7M7M7O2PJC /7.F.Fs.KG.6.F.Fs.KG'*5zGAJ'>,1LL,A,H,H,T -..>
Q,? )2,3AJgajSTn9T)* 8=||G7L7S7S,68*
 -9 )2,3AJc,FW9W)*% 3Q -> "' +4 "  5!			r   c                 4
   / U l         Sn[        U R                  R                  5       GH  nU[        :  aF  U R
                  R                  SR                  U R                  R                  [        5      5          GOSn[        U R                  U S9nU(       d    GOXR                  5       U-  -   nUR                  U5        U R                  XfUR                  5       -    n[        U5      UR                  5       :X  a"  U R
                  R                  SU S35          GOU(       d"  U R
                  R                  SU S35          GOUR                  U5        U R                  R                  U5        UR                   UR"                  -   [%        U R                  5      :  a$  US-  nU R
                  R                  S	U S
35        U R'                  UR"                  U R(                  R*                  5      [%        U R                  5      :  a$  US-  nU R
                  R                  S	U S35        UR,                  S:  a$  US-  nU R
                  R                  SU S35        U R/                  UR0                  U R(                  R2                  U R(                  R*                  5      S:  a$  US-  nU R
                  R                  SU S35        U R(                  R*                  S:w  aK  UR"                  U R(                  R*                  -  S:w  a$  US-  nU R
                  R                  S	U S35        XB:  a  U R
                  R                  S5          O[5        [6        S5      n[9        XUR:                  U5        UR<                  R?                  SS5      (       au  UR<                  R?                  SS5      (       aT  UR@                  RC                  S5      S:X  a  U RE                  5       (       a  OU R
                  R                  SU S35        U R                   R                  U5        GM     U R                   RG                  S S9  [I        U R                   5       HK  u  pU	[%        U R                   5      S-
  :X  a	  SUl%        M*  U R                   U	S-      R0                  Ul%        MM     U R                  R                  S:  aG  U R                   (       a6  XR                   S   R                  5       U R                  R                  -  -   $ U$ )a  Fetch the PE file sections.

The sections will be readily available in the "sections" attribute.
Its attributes will contain all the section information plus "data"
a buffer containing the section's data.

The "Characteristics" member will be processed and attributes
representing the section characteristics (with the 'IMAGE_SCN_'
string trimmed from the constant's names) will be added to the
section instance.

Refer to the SectionStructure class for additional info.
rC   zToo many sections {0} (>={1})r   )r  zInvalid section z. Contents are null-bytes.z8. No data in the file (is this corkami's virtsectblXP?).r9   zError parsing section z$. SizeOfRawData is larger than file.z5. PointerToRawData points beyond the end of the file.r   z'Suspicious value found parsing section z*. VirtualSize is extremely large > 256MiB.z&. VirtualAddress is beyond 0x10000000.z. PointerToRawData should normally be a multiple of FileAlignment, this might imply the file is trying to confuse tools which parse this incorrectly.z,Too many warnings parsing section. Aborting.r{  r   Fr   rA  s   PAGEz!Suspicious flags set for section zf. Both IMAGE_SCN_MEM_WRITE and IMAGE_SCN_MEM_EXECUTE are set. This might indicate a packed executable.c                     U R                   $ r   )r^  )as    r   r.  #PE.parse_sections.<locals>.<lambda>4  s
    )9)9r   )r,  N)&r  r  r   NumberOfSectionsMAX_SECTIONSr  r  r  r[  __IMAGE_SECTION_HEADER_format__r&  r   rt  r1   r-  r  r_  r]  rF  ri  rj  rk  r`  ro  r^  rp  r\  r|  rc  rz  r^  r=  NamerK  	is_driversortr)  r  )
rt  r  MAX_SIMULTANEOUS_ERRORSrN  simultaneous_errorssectionsection_offsetsection_datar~  r+  s
             r   r  PE.parse_sections  s    "#t''889AL &&3::((99<
 "#&t'K'KPTUG#nn&6&::N##N3=='..2B!BL L)W^^-==&&)9!<V'WX&&&qc *& & |,&&w/$$w'?'??#dmmBTT#q(#&&,QC/ST (((($*>*>*L*LDMM"# $q(#&&,QC 0+ +
 ''*4#q(#&&=aS A0 0 ,,**((99((66
  $q(#&&=aS A) ) $$22a7--0D0D0R0RRWXX#q(#&&0 4S S #=&&'UV*+BLQM g66F##%u ""&&'>FF<<&&w/7:t~~?O?O OO**;A3 ?C C MM  )Q :Z 	9:%dmm4LCc$--(1,,7;47;}}!G8 . 4	 5 ,,q0T]]q)002T5E5E5V5VVV Mr   c                 $   SU R                   4SU R                  4SU R                  4SU R                  4SU R                  4SU R
                  4SU R                  4SU R                  4S	U R                  4S
U R                  44
nUb  [        U[        [        45      (       d  U/nU GH  n [        US      nU R                  R                  U   nUb  Xa;   a  SnUR"                  (       a  U(       a)  US   S:X  a   US   " UR"                  UR$                  SS9nOQU(       a)  US   S:X  a   US   " UR"                  UR$                  SS9nO! US   " UR"                  UR$                  5      nU(       a  [-        XS   SS U5        Uc  M  [        U[        5      (       d  M  US   U;   d  GM  UR/                  U5        GM     g! [          a       gf = f! [&         a.  n	U R(                  R+                  SUS    SU	 35         Sn	A	NSn	A	ff = f)a  Parse and process the PE file's data directories.

If the optional argument 'directories' is given, only
the directories at the specified indexes will be parsed.
Such functionality allows parsing of areas of interest
without the burden of having to parse all others.
The directories can then be specified as:

For export / import only:

  directories = [ 0, 1 ]

or (more verbosely):

  directories = [ DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_IMPORT'],
    DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_EXPORT'] ]

If 'directories' is a list, the ones that are processed will be removed,
leaving only the ones that are not present in the image.

If `forwarded_exports_only` is True, the IMAGE_DIRECTORY_ENTRY_EXPORT
attribute will only contain exports that are forwarded to another DLL.

If `import_dllnames_only` is True, symbols will not be parsed from
the import table and the entries in the IMAGE_DIRECTORY_ENTRY_IMPORT
attribute will not have a `symbols` attribute.
r?   r>   r@   rH   rF   rN   rP   rV   rR   rB   Nr   r9   T)forwarded_only)dllnames_onlyzFailed to process directoty "z": rI   )parse_import_directoryparse_export_directoryparse_resources_directoryparse_debug_directoryparse_relocations_directoryparse_directory_tlsparse_directory_load_configparse_delay_import_directoryparse_directory_bound_importsparse_exceptions_directoryrU  r  r3  r  rj  r  
IndexErrorr^  r)  r  r  r  r*  remove)
rt  directoriesforwarded_exports_onlyimport_dllnames_onlydirectory_parsingr  directory_indexr$  rb  r  s
             r   rJ  PE.parse_data_directoriesD  s"   @ ,T-H-HI+T-H-HI-t/M/MN*D,F,FG.0P0PQ($*B*BC0$2R2RS143T3TU143U3UV.0O0OP
 "kE4=99*m&E"1%("; 00??P	 "o&D++.!!H(FF %a%44%NN+/! -!!H(FF %a%44innTX!
$)!HY-E-Ey~~$VE
 AhqrlE: ({D111X,""?3a '  <  -  OO22"?azTF S s*   -%G G
GG
H!$H

Hc                 p   U R                   R                  [        S   :w  a"  U R                   R                  [        S   :w  a  g[        U R                  5      nUR                  5       n0 n/ n0 n[        X$-  5       GH  nU R                  U R                  U R                  X5      U R                  U5      S9nUc    GOiSn	UR                  S-  S:X  Ga"  UR                  U;   a  XsR                     n	O0[        U R                  UR                  5      S9n	XUR                  '   U	R                  U R                  UR                  U	R                  5       5      5      n
U
S:w  a  U R                  R                  U
5          OU	R                  U R                  UR                  U	R                  5       5      5      n
U
S:w  a  U R                  R                  U
5          OLU R                  R                  U	5        [!        X9S9nUR                  U5        XUR"                  '   X-  nGM     U H  nUR$                  c  M  ['        UR$                  S5      (       d  M/  UR$                  R(                  U;  a:  U R                  R                  S	UR*                  R-                  5       S
 S35        M   UR$                  R/                  XSR$                  R(                     5        M     U$ ! [0         aF  nU R                  R                  SUR*                  R-                  5       S
 SU 35         SnAGM  SnAff = f)zParses exception directory

All the code related to handling exception directories is documented in
https://auscitte.github.io/systems%20blog/Exception-Directory-pefile#implementation-details
r   r   NrZ  r9   r   )rG  
unwindinfori  z FunctionEntry of UNWIND_INFO at rl  z' points to an entry that does not existz/Failed parsing FunctionEntry of UNWIND_INFO at z: )r   MachineMACHINE_TYPEr  __RUNTIME_FUNCTION_format__r&  r  r  r  r  
UnwindDatarW  r{  r  r  r  rU  BeginAddressr  r}  ri  rG  r  r  r  )rt  r  r  rfrf_sizerva2rtrt_funcs	rva2infosr  uiwsr  r  s                r   rz  PE.parse_exceptions_directory  s    $$5O(PP  ((L9R,SSt778))+	t'A%%00c+ 44S9 & B zB#) MMY."==1B#0H0H0WXB/1bmm,((r}}biik)RS:OO**2.((r}}biik)RS:OO**2.##**2.*"DEOOE"&+2??#NCQ (V B}}$
 2==/::==..&8&&6ryy7P7P7RST6U=> 	88==667! 4  ! &&Eyy00215Rv? s   .1K%%
L5/:L00L5c           	          [        U R                  5      nUR                  5       nUn/ n U R                  U R                  U R                  XU-    US9nUc  U R
                  R                  S5        gUR                  5       (       a   U$ XR                  5       -  nU R                  U5      nU R                  U5      nUc  [        U R                  5      U-
  n	U R                   V
s/ s H!  n
U
R                  U:  d  M  U
R                  PM#     nn
U(       a.  [        U5      nU R                  U5      nUb  UR                  U-
  n	O)UR                  [        UR                  5       5      -   U-
  n	U(       d+  U R
                  R                  SR                  U5      5        g/ n[!        [        UR"                  [%        U	S-  5      5      5       H  nU R                  U R&                  U R                  XU-    US9nU(       d  [)        S5      eXR                  5       -  nX_R*                  -   nU R-                  SU R                  UU[.        -    5      nU(       aR  [1        U5       Vs/ s H$  n[3        U5      [4        R6                  ;  d  M"  UPM&     nn[        U5      S:  d  U(       a    OUR                  [9        UUS	95        M     XSR*                  -   nU R-                  SU R                  UU[.        -    5      nU(       aS  [1        U5       Vs/ s H$  n[3        U5      [4        R6                  ;  d  M"  UPM&     nn[        U5      S:  d  U(       a   U$ U(       d   U$ UR                  [;        UUUS
95        GMV  s  sn
f s  snf s  snf )r  rZ  Nz7The Bound Imports directory exists but can't be parsed.zHRVA of IMAGE_BOUND_IMPORT_DESCRIPTOR points to an invalid address: {0:x}rM   z(IMAGE_BOUND_FORWARDER_REF cannot be readr   rg   )rG  r
  )rG  r
  rP  )r  (__IMAGE_BOUND_IMPORT_DESCRIPTOR_format__r&  r  rt  r  r  r#  get_section_by_offsetr  rF  r  r]  rr  r  r  r  NumberOfModuleForwarderRefsr%   $__IMAGE_BOUND_FORWARDER_REF_format__r  OffsetModuleNameget_string_from_dataMAX_STRING_LENGTHri  rL  r  rC  rS  rO  )rt  r  r  	bnd_descrbnd_descr_sizeru  bound_importsrj  r  safety_boundaryr  sections_after_offsetfirst_section_after_offsetforwarder_refsr  bnd_frwd_refr  name_strr  invalid_charss                       r   ry   PE.parse_directory_bound_imports   s    dKKL	"))+,,==c.$89 - I
  
 &&M ##%%v s ##%%C005G2237K"%dmm"4{"B "]])*))K7 'A&&* & )
 ) 255J1K."889STG**1*B*B[*P ,,s73C3C3E/FFT   &&7fSk	 NI993QR?R;ST
  $33==MM#n(<= #  4   $'(RSS**,,!>!>>44t}}Vf7H.HI #,X#6%#6a#a&HXHX:X#6 " % 8}s*m%%&lJAH 777F004==&3D*DEH (2!2!c!fFDTDT6TA2  ! x=3&-      #$8^O 4)j% !s$   )NN*!NN!N9Nc           	      l   U R                   nU R                  [        :X  a  U R                  n U R	                  UU R                  U[        U5      R                  5       5      U R                  U5      S9nU(       d  g[        US9$ ! [         a#    U R                  R                  SU-  5        Sn N=f = f)r  rZ  z5Invalid TLS information. Can't read data at RVA: 0x%xNr  )__IMAGE_TLS_DIRECTORY_format__r  r   __IMAGE_TLS_DIRECTORY64_format__r  r  r  r&  r  r  r  r  rM  )rt  r  r  r  
tls_structs        r   rv  PE.parse_directory_tlsw  s     44<<88::F
	--c9V#4#;#;#=> 44S9 . J j))  	OO""JSP J		s   AB *B32B3c           	         U R                   [        :X  a  U R                  U5      nU R                  nONU R                   [        :X  a  U R                  U5      nU R
                  nOU R                  R                  S5        gSnSnUS    H-  nUS-  nU[        UR                  S5      S      -  nXc:X  d  M-    O   US   US   SU 4nSn U R                  UU R                  U[        U5      R                  5       5      U R                  U5      S9nU(       d  gSn	US:  a&  U R!                  UR"                  UR$                  5      n	['        XS	9$ ! [         a!    U R                  R                  SU-  5         Nif = f)
r  zGDon't know how to parse LOAD_CONFIG information for non-PE32/PE32+ fileNr   r9   r  rZ  z=Invalid LOAD_CONFIG information. Can't read data at RVA: 0x%xrC  )rG  dynamic_relocations)r  r  get_dword_at_rva&__IMAGE_LOAD_CONFIG_DIRECTORY_format__r  (__IMAGE_LOAD_CONFIG_DIRECTORY64_format__r  r  r  r  r  r  r  r&  r  r  parse_dynamic_relocationsDynamicValueRelocTableOffsetDynamicValueRelocTableSectionrQ  )
rt  r  r  load_config_dir_szr  fields_countercumulative_szfieldload_config_structr  s
             r   rw  PE.parse_directory_load_config  s    <<33!%!6!6s!;@@F\\::!%!6!6s!;BBFOO""  AYEaN0S1A!1DEEM2	 
 )VAY78!		!%!5!5c9V#4#;#;#=> 44S9 "6 " ""B"&"@"@"??"@@#
 %
 	
  	OO""RUXX	s   AE (E<;E<c                    U(       d  g U(       d  g U[        U R                  5      :  a  g U R                  US-
     nUR                  U-   nS n[        U R                  5      R                  5       n U R                  U R                  U R                  XF5      U R                  U5      S9nUR                  S:w  a'  U R                  R                  SUR                  5        g XF-  nXER                  -   n/ nXG:  Ga  U R                  n	U R                  [         :X  a  U R"                  n	[        U	5      R                  5       n
 U R                  U	U R                  XJ5      U R                  U5      S9nU(       d   U$ XJ-  nUR$                  nUR&                  nSUs=::  a  S::  a;  O  O8U R)                  XMU R*                  U   5      nUR                  [-        XUS95        US:  a*  U R)                  XM5      nUR                  [-        XUS95        XM-  nXG:  a  GM  U$ ! [         a"    U R                  R                  SU-  5         GNf = f! [         a$    U R                  R                  SU-  5        S n GNf = f)	Nr9   rZ  zPInvalid IMAGE_DYNAMIC_RELOCATION_TABLE information. Can't read data at RVA: 0x%xzDNo pasring available for IMAGE_DYNAMIC_RELOCATION_TABLE.Version = %d<Invalid relocation information. Can't read data at RVA: 0x%xrC   rG   )rG  symbolrelocations)rF  r  r^  r  )__IMAGE_DYNAMIC_RELOCATION_TABLE_format__r&  r  r  r  r  r  r  rm  r)  #__IMAGE_DYNAMIC_RELOCATION_format__r  r  %__IMAGE_DYNAMIC_RELOCATION64_format__SymbolBaseRelocSize parse_image_base_relocation_listr  r@  )rt   dynamic_value_reloc_table_offset!dynamic_value_reloc_table_sectionrj  r   image_dynamic_reloc_table_structreloc_table_sizerw  r  r  rlc_sizedynamic_rlcr  r  r  s                  r   r  PE.parse_dynamic_relocations  s    00,s4==/AA-- AA EF$$'GG+/($::

&( 	
	/3/C/C>>c4 44S9 0D 0, ,33q8OO""V088 999 i==F||<<CC (//1H#"22MM#0 $ 8 8 = 3  4 #"1 OC ''F,,DFa"CCtGGO $**)*{ z"CCCN#**)*{ KC] i` #"C  	OO""$&)*	> ! #&&(*-. ##s$   29H /I (I	I	*I:9I:c                 $    U R                  X5      $ )r  )r  )rt  r  r  s      r   ru  PE.parse_relocations_directory"  s     44S??r   c                    [        U R                  5      R                  5       nX-   n/ nX:  Ga   U R                  U R                  U R	                  X5      U R                  U5      S9nU(       d   U$ UR                  U R                  R                  :  a+  U R                  R                  SUR                  -  5         U$ UR                  U R                  R                  :  a+  U R                  R                  SUR                  -  5         U$ Uc-  U R                  X-   UR                  UR                  U-
  5      nO-U R                  X-   UR                  UR                  U-
  U5      nUR                  [        XxS95        UR                  (       d   U$ XR                  -  nX:  a  GM  U$ ! [         a$    U R                  R                  SU-  5        S n GNvf = f)NrZ  r  zEInvalid relocation information. VirtualAddress outside of Image: 0x%xz9Invalid relocation information. SizeOfBlock too large: %drG  rP  )r   __IMAGE_BASE_RELOCATION_format__r&  r  r  r  r  r  r  r^  rj  SizeOfImageSizeOfBlockparse_relocationsparse_relocations_with_formatrB  )	rt  r  r  r  r  rw  r  rlcreloc_entriess	            r   r  #PE.parse_image_base_relocation_list'  s   TBBCJJLji**99MM#0 $ 8 8 = +  D ? !!D$8$8$D$DD&&&(+(:(:; 4 - !5!5!A!AA&& __- "  { $ 6 6NC$6$6(8R! !% B BNC$6$6(8RTW! 1TU??  ??"Ci il U ! &&(*-. s   9F3 3*G! G!c           
      t    U R                  X5      nU R                  U5      n/ n[        5       n[        [        [        U5      S-  5      5       H  nU R                  U R                  XHS-  US-   S-   US9n	U	(       d    U$ U	R                  n
U
S-	  nU
S-  nX4U;   a#  U R                  R	                  SX-   -  5          U$ UR                  X45        UR	                  [        XX,U-   S	95        XYR                  5       -  nM     U$ ! [         a$    U R                  R	                  SUS 35        / s $ f = f)
r  Bad RVA in relocation data: 0xrl  rA   r9   rZ  rU   rG  3Overlapping offsets in relocation data at RVA: 0x%x)rG  rF  rI  r  )r  r  r  r  r  rI  r  r%   rF  r  &__IMAGE_BASE_RELOCATION_ENTRY_format__rH  rx  rD  r&  )rt  data_rvar  r  r0   r  rP  offsets_and_typer+  r  rJ  
reloc_typereloc_offsets                r   r  PE.parse_relocationsd  sg   	==0D228<K
 5SY]+,C((;;1Wa1}-' ) E * ) ::DJ&=L)-==&&#&2&8:     ,!;<NN CTWEW
 <<>)K9 -< I  	OO""%CHQ<#PQI	s   "D	 	+D76D7c           	      Z    U R                  X5      nU R                  U5      n[        U5      R                  5       n/ n[        5       n	[        [        [        U5      U-  5      5       H  n
U R                  UXZU-  U
S-   U-   US9nU(       d    U$ UR                  nX;   a#  U R                  R	                  SX-   -  5          U$ U	R                  U5        UR	                  [        XX-   S95        Xg-  nM     U$ ! [         a$    U R                  R	                  SUS 35        / s $ f = f)r  r  rl  r9   rZ  r  )rG  rI  r  )r  r  r  r  r  r  r&  rI  r  r%   rF  r  PageRelativeOffsetrx  rD  )rt  r  r  r  r  r0   r  
entry_sizerP  rY  r+  r  r  s                r   r   PE.parse_relocations_with_format  sO   	==0D228<K
 ,F3::<
%SY345C77:%qJ(>?' 8 E    !33L&&&#&2&8:   KK%NNe|?QR %K/ 62 A  	OO""%CHQ<#PQI	s   "C< <+D*)D*c                    [        U R                  5      R                  5       n/ n[        [	        X#-  5      5       GH	  n U R                  XU-  -   U5      nU R                  U R                  UU R                  XU-  -   5      S9nU(       d    gSnUR                  S:X  a  GOUR                  S:X  Ga  UR                  n	UR                  n
U R                  XU
-    nUSS S:X  Ga  S/ S	Q/nU
[        U5      R                  5       -
  nUS
:  a#  US   R                  SR                  U5      5        U R                  XU	5      nUb  [         R"                  " SSUR$                  -   5      S
   Ul        [)        [*        R,                  " UR.                  UR0                  UR2                  UR4                  UR6                  UR&                  4S95      R9                  SS 5      R;                  5       UR<                  S -   Ul        GO&USS S:X  a]  S/ SQ/nU
[        U5      R                  5       -
  nUS
:  a#  US   R                  SR                  U5      5        U R                  XU	5      nOUR                  S:X  a  UR                  n	UR                  n
U R                  XU
-    nS/ SQ/nU R                  XU	5      nU(       ag  UR@                  S;   aW  U
[        U5      R                  5       -
  nUS
:  a#  US   R                  SR                  U5      5        U R                  XU	5      nUR                  [C        XxS95        GM     U$ ! [         a"    U R                  R                  SU-  5           gf = f)r  z7Invalid debug information. Can't read data at RVA: 0x%xNrZ  r9   rA   rE   s   RSDSCV_INFO_PDB70)z4s,CvSignaturezI,Signature_Data1zH,Signature_Data2zH,Signature_Data3zB,Signature_Data4zB,Signature_Data5z6s,Signature_Data6I,Ager   z{0}s,PdbFileNamez>Q     )fields-Xs   NB10CV_INFO_PDB20)zI,CvHeaderSignaturezI,CvHeaderOffsetru  r  IMAGE_DEBUG_MISC)z
I,DataTypezI,Lengthz	B,UnicodezB,Reserved1r  r   r9   z	{0}s,Data)rG  r  )"r   __IMAGE_DEBUG_DIRECTORY_format__r&  r  r%   r  r  r  r  r  r  Typer]  
SizeOfDatart  r  rG  rH  Signature_Data6Signature_Data6_valuerV  uuidUUIDSignature_Data1Signature_Data2Signature_Data3Signature_Data4Signature_Data5replaceupperAgeSignature_StringUnicoder>  )rt  r  r  dbg_sizedebugr+  r0   dbgdbg_typedbg_type_offsetdbg_type_sizedbg_type_data__CV_INFO_PDB70_format__pdbFileName_size__CV_INFO_PDB20_format_____IMAGE_DEBUG_MISC_format__dbg_type_partial	data_sizes                     r   rt  PE.parse_debug_directory  s    TBBCJJLT_-.C}}Sc>%98D &&55 44Sc>5IJ ' C 
 Hxx1}Q"%"6"6 # $#&E! !!$/ (	0, &	2J(K(R(R(TT % (!+03::.556FG  $330 H  +9? 'H,D,D"D::6   $		(0(@(@(0(@(@(0(@(@(0(@(@(0(@(@(0(F(F,&	!" %WS"-"UW!)a 02 !1$ #2A&'1 (0, &	2J(K(R(R(TT % (!+03::.556FG  $330 H Q"%"6"6 # $#&E! '	0, $(#7#70$  $'//69)'(DELLNO " %q=8;BB + 2 29 = $(#7#78$ LL#>?C /F A ! &&PSVV 	s   M(M87M8c           
         Uc  U/nUc  UnU[         :  a%  U R                  R                  SU[         4-  5        g U R                  U[	        U R
                  5      R                  5       5      nU R                  U R
                  UU R                  U5      S9nUc  U R                  R                  SU-  5        g/ nXR                  5       -  nUR                  UR                  -   n	Sn
X:  a   U R                  R                  SX4-  5        gU =R                  U	-  sl        U R                  [        :  a/  U R                  R                  SU R                  [        4-  5        g/ nSn[        U	5       GH\  nU R                  (       dU  U R                   U R"                  :  a;  S	U l        U R                  R                  S
U R                   U R"                  4-  5        U R%                  U5      nUc"  U R                  R                  SX4-  5          GOSnSnUR&                  S-  S-	  nU(       d  UR&                  nOX>R(                  -   n [+        U U5      nU =R                   UR-                  5       -  sl        U(       aC  US   U:  a:  US   U:  a1  UR/                  5         U R                  R                  SU-  5          GOUUUR-                  5       -   4nUR                  U5        UR0                  (       Ga  X>R2                  -   U;   a    GOU R5                  X>R2                  -   X!U-
  -
  UUS-   XSUR2                  -   /-   S9nU(       d    GOSnU[6        S   :X  Ga*  0 nUR8                   GH  n[;        US5      (       d  M  0 nUR<                  R8                   H  nUb?  [;        US5      (       a.  UR>                  R@                  RB                  b  URD                  c  MG  UR>                  R@                  RF                  nUR>                  R@                  RB                  nURD                  n U R                  UU5      n[I        U[K        U5      S-
  S-  U5        URM                  U5        M     UUR<                  l'        GM     UR                  [Q        UUUUS95        OU RS                  X>R2                  -   5      nU(       a_  U =R                   URB                  -  sl        [U        UUR&                  S-  UR&                  S-	  S9nUR                  [Q        XUUS95        O  OUS:X  a  URV                  [6        S   :X  ar  U(       a  US    n WR<                  R8                  S   R<                  R8                  nU H2  n Sn! U R>                  R@                  n!U!c  M!  U RY                  U!5        M4     XR                  5       -  nGM_     U V"s/ s H  n"U"R[                  5       PM     n#n"U#R]                  5         [_        U5       H  u  nn"U"Ra                  5         M     [c        XxS!9n$U$$ ! [         a!    U R                  R                  SU-  5         gf = f! [         a"    U R                  R                  SU-  5         GNf = f! [         a'    U R                  R                  SUS SU 35         GM!  f = f!    GN'= f!    GN= fs  sn"f )"aX  Parse the resources directory.

Given the RVA of the resources directory, it will process all
its entries.

The root will have the corresponding member of its structure,
IMAGE_RESOURCE_DIRECTORY plus 'entries', a list of all the
entries in the directory.

Those entries will have, correspondingly, all the structure's
members (IMAGE_RESOURCE_DIRECTORY_ENTRY) and an additional one,
"directory", pointing to the IMAGE_RESOURCE_DIRECTORY structure
representing upper layers of the tree. This one will also have
an 'entries' attribute, pointing to the 3rd, and last, level.
Another directory with more entries. Those last entries will
have a new attribute (both 'leaf' or 'data_entry' can be used to
access it). This structure finally points to the resource data.
All the members of this structure, IMAGE_RESOURCE_DATA_ENTRY,
are available as its attributes.
NzNError parsing the resources directory. Excessively nested table depth %d (>%s)zCInvalid resources directory. Can't read directory data at RVA: 0x%xrZ  zDInvalid resources directory. Can't parse directory data at RVA: 0x%xr*   zNError parsing the resources directory. The directory contains %d entries (>%s)zRError parsing the resources directory. The file contains at least %d entries (>%d)TzGResource size 0x%x exceeds file size 0x%x, overlapping resources found.zHError parsing the resources directory, Entry %d is invalid, RVA = 0x%x. r7   r  r   r9   z^Error parsing the resources directory, attempting to read entry name. Entry names overlap 0x%xznError parsing the resources directory, attempting to read entry name. Can't read unicode string at offset 0x%x)rI  leveldirsr  	directoryr0   z2Error parsing resource of type RT_STRING at RVA 0xrl  z with size r6   )rG  r
  idr  i  rQ   )rG  langsublang)rG  r
  r  r0   r$  r*  r  )2MAX_RESOURCE_DEPTHr  r  r  r  #__IMAGE_RESOURCE_DIRECTORY_format__r&  r  r  r  NumberOfNamedEntriesNumberOfIdEntriesr  MAX_RESOURCE_ENTRIESr  r  r  r  parse_resource_entryre  
NameOffsetr  r  r  DataIsDirectoryOffsetToDirectoryrs  RESOURCE_TYPErP  r}  r  r0   rG  r)  r  OffsetToDatarR  r%   updatestringsr:  parse_resource_data_entryr<  Idparse_version_informationr  rg  r)  r  r7  )%rt  r  r  rI  r  r  r0   resource_dirdir_entriesnumber_of_entriesMAX_ALLOWED_ENTRIESstrings_to_postprocesslast_name_begin_endr+  res
entry_nameentry_idname_is_stringustr_offsetentry_directoryr  resource_idresource_stringsresource_langstring_entry_rvastring_entry_sizestring_entry_idstring_entry_datarG  
entry_data
last_entryversion_entriesversion_entryrt_version_structr  string_rvasresource_directory_datas%                                        r   rs  PE.parse_resources_directoryc  sA   . <5DH%%OO"":=BDV<WX 	 ==YtGGHOOQD ++44005 , 

  OO"".034 
 	""$$ --0N0NN 	
 #2OO"":$:;
 ++/@@+..1EEOO"">668LMN
 !#
 #*+C66//$2X2XX 6:2&&' 33>> ++C0C{&&8;>*E JH!hh3:N!88&7 !B4!UJ//:3R3R3TT/ ++A.</2kA /224..7:EG
  $#j&E&E&GG+'
 +11*= """ 333t;"&"@"@444(N+%!)C,A,A!A BB #A # ' }[99 G'6'>'>";<</1,1<1F1F1N1N %2$9+2=&+I+I'4'9'9'@'@'E'E'M'2~~'=$, %2$6$6$=$=$J$J !1 5B4F4F4M4M4R4R 12=..
!-8<(8:K9&$5 !.$5%(%9A%=$C$4!"
 !(/? @E 2OH =MK119S (?V ""("'#"1	 77444 //6;;>/!6%CHHu,<chhRTn"J  &&,#&H:  zcffl(CC!,RJN&0&:&:&B&B1&E&O&O&W&WO *9,0)!0=0B0B0I0I- -8 ::;LM *9 ::<CE ,H -CC,Bqqyy{,BC 67FC  8 #2#
 '&W  	OO"".034 	p % OO**CFQS@ (5 !-$(OO$:$:+11A!0D E00A/B)D%&
 %-!-~! !   DsZ   3X7 A6Y%&Y%7Z-[[[7(Y"!Y"%(ZZ,[	[	[[c                     U R                  U[        U R                  5      R                  5       5      nU R                  U R                  UU R                  U5      S9nU$ ! [         a!    U R
                  R                  SU-  5         gf = f)z0Parse a data entry from the resources directory.zGError parsing a resource directory data entry, the RVA is invalid: 0x%xNrZ  )	r  r  $__IMAGE_RESOURCE_DATA_ENTRY_format__r&  r  r  r  r  r  )rt  r  r0   
data_entrys       r   r  PE.parse_resource_data_entry  s    	 ==YtHHIPPRD ))55005 * 

   	OO""+.13 	s   3A! !(BBc                     U R                  U[        U R                  5      R                  5       5      nU R                  U R                  UU R                  U5      S9nUc  gUR                  S-  Ul        UR                  S-  Ul	        UR                  S-  Ul
        UR                  S-  S-	  Ul        UR                  S-  Ul        U$ ! [         a     gf = f)z5Parse a directory entry from the resources directory.NrZ  r        r  r7   r  )r  r  )__IMAGE_RESOURCE_DIRECTORY_ENTRY_format__r&  r  r  r  re  r  _PE__padr   r  r  r  )rt  r  r0   resources       r   r  PE.parse_resource_entry  s    	==YtMMNUUWD ''::005 ( 
  'mmj8!3mmj0$,$9$9J$F2#M %-%:%:Z%G"-  		s   3C 
CCc                     U R                  UR                  5      nU R                  X"UR                  -    nU R                  U R                  X2S9nUc  gUR                  UR                  5       -   nU R                  U5      nSnU(       a-  UR                  [        UR                  UR                  5      -   nSn Uc  U R!                  USS9nOU R!                  XWU-
  S-	  SS9n Uc+  U R                  R	                  SR                  U5      5        gUb  US	:w  a  [#        U5      S
:  aL  USS
 R%                  S5      n	U	SU	R'                  S5       n	[)        SR                  U	[#        U5      5      5      nU R                  R	                  SR                  UR%                  S5      R+                  SS5      5      5        g[-        U S5      (       d  / U l        Un
Xl        U R.                  R	                  U
5        Uc  SnU R3                  UR                  5       S[#        U5      S-   -  -   UR                  5      nU R                  U R4                  X;S X+-   S9nU(       d  g[-        U S5      (       d  / U l        U R6                  R	                  U5        U R3                  XR                  5       -   UR                  5      n[-        U S5      (       d  / U l        / n U R                  U R:                  X=S X--   S9nUc  U R                  R	                  S5        gUR                  U-   UR                  5       -   n U R!                  U5      nUUl        UR	                  U5        U(       Ga9  UR=                  S5      (       Ga"  UR>                  S;   Ga  UR@                  S:X  Ga  U R3                  UUR                  5       -   S[#        U5      S-   -  -   UR                  5      n/ Ul!         U R                  U RD                  UUS UU-   S9nU(       d  GOUR                  U-   UR                  5       -   n U R!                  U5      nUUl#        0 Ul$        0 Ul%        0 Ul&        URB                  R	                  U5        U R3                  UUR                  5       -   S[#        U5      S-   -  -   UR                  5      nUUURN                  -   :  Ga  U R                  U RP                  UUS UU-   S9nU(       d  GO_UR                  U-   UR                  5       -   n U R!                  U5      nU R                  U5      nU R3                  S[#        U5      S-   -  U-   UR                  5       -   UR                  5      nUR                  U-   n U R!                  UUR@                  S9nU R                  U5      nURN                  S:X  a  UURN                  -   nO)U R3                  URN                  U-   UR                  5      nUURH                  U'   UU4URJ                  U'   [#        U5      [#        U5      4URL                  U'   UUURN                  -   :  a  GM  U R3                  URN                  U-   UR                  5      nUU:X  a  GO?UnUURN                  :  a  GO+GM  GO&U(       Ga  UR=                  S5      (       Ga  UnSUl)        UR>                  S;   Ga  UR@                  S:X  Ga  U R3                  UUR                  5       -   S[#        U5      S-   -  -   UR                  5      n/ Ul*         U R                  U RV                  UUS UU-   S9nU(       d  GOkUR                  U-   UR                  5       -   n U R!                  U5      nUc  GO4URT                  R	                  U5        U R3                  S[#        U5      S-   -  U-   UR                  5       -   UR                  5      nUn UU UR@                  -   :  a  U RY                  UUUS-    S5      n!U RY                  UUS-   US!-    S5      n"US!-  n[[        U![\        5      (       a#  [[        U"[\        5      (       a  US"U!U"4-  0Ul/        UU UR@                  -   :  a  M  U R3                  UURN                  -   UR                  5      nUUURN                  -   ::  a  OGM  U R3                  URN                  U-   UR                  5      nURN                  S:X  d  XRN                  :  a  OGM;  U R8                  R	                  U5        g! [         a7    U R                  R	                  SR                  UR                  5      5         gf = f! [         a"    U R                  R	                  SU-  5         GNf = f! [         a.    U R                  R	                  SR                  U5      5         M  f = f! [         a/    U R                  R	                  SR                  U5      5         GMN  f = f! [         a/    U R                  R	                  SR                  U5      5         GM  f = f! [         a$    U R                  R	                  SUS 35         GM+  f = f! [         a/    U R                  R	                  S R                  U5      5         GM  f = f)#a  Parse version information structure.

The date will be made available in three attributes of the PE object.

VS_VERSIONINFO   will contain the first three fields of the main structure:
    'Length', 'ValueLength', and 'Type'

VS_FIXEDFILEINFO will hold the rest of the fields, accessible as sub-attributes:
    'Signature', 'StrucVersion', 'FileVersionMS', 'FileVersionLS',
    'ProductVersionMS', 'ProductVersionLS', 'FileFlagsMask', 'FileFlags',
    'FileOS', 'FileType', 'FileSubtype', 'FileDateMS', 'FileDateLS'

FileInfo    is a list of all StringFileInfo and VarFileInfo structures.

StringFileInfo structures will have a list as an attribute named 'StringTable'
containing all the StringTable structures. Each of those structures contains a
dictionary 'entries' with all the key / value version information string pairs.

VarFileInfo structures will have a list as an attribute named 'Var' containing
all Var structures. Each Var structure will have a dictionary as an attribute
named 'entry' which will contain the name and value of the Var.
zWError parsing the version information, attempting to read OffsetToData with RVA: 0x{:x}NrZ  asciiencodingr9   zzError parsing the version information, attempting to read VS_VERSION_INFO string. Can't read unicode string at offset 0x%xz"Invalid VS_VERSION_INFO block: {0}s   VS_VERSION_INFOre   z\uz({0} ... ({1} bytes, too long to display) z\00r  r  rA   r  rO  z/Error parsing StringFileInfo/VarFileInfo structz|Error parsing the version information, attempting to read StringFileInfo string. Can't read unicode string at offset 0x{0:x}s   StringFileInfor  r   zyError parsing the version information, attempting to read StringTable string. Can't read unicode string at offset 0x{0:x}z}Error parsing the version information, attempting to read StringTable Key string. Can't read unicode string at offset 0x{0:x}r  zzError parsing the version information, attempting to read StringTable Value string. Can't read unicode string at offset 0xrl  s   VarFileInfoVarFileInfoz}Error parsing the version information, attempting to read VarFileInfo Var string. Can't read unicode string at offset 0x{0:x}rE   z0x%04x 0x%04x)0r  r  r  r  r  r  rt  r)  r  __VS_VERSIONINFO_format__r&  r  r^  rs  r_  r`  r  rF  rJ  rfindrI  r  r}  r  Keydword_align__VS_FIXEDFILEINFO_format__r  rO  __StringFileInfo_format__rX  r  ValueLengthr  __StringTable_format__LangIDrP  rQ  rR  Length__String_format__r
  r  __Var_format__get_word_from_datarU  r%   r  )#rt  version_structstart_offsetr,  versioninfo_structr,  rj  section_endversioninfo_stringexcerptvinfofixedfileinfo_offsetfixedfileinfo_structstringfileinfo_offsetrW  stringfileinfo_structstringfileinfo_stringstringtable_offsetstringtable_structstringtable_stringentry_offsetstring_structr,  
key_offsetvalue_offsetrb  new_stringtable_offsetvarfileinfo_struct
var_offset
var_struct
var_stringvarword_offsetorig_varword_offsetword1word2s#                                      r   r!  PE.parse_version_information  s,   4		33N4O4OPL ==~?R?R0RS "11**H 2 
 %$114F4M4M4OO))+6!003%%w'?'?4 K "
	"%)%=%=' &> &" &*%=%=";!AG &> &" %OO""4;;<NO  ).@DV.V%&,,Tc299'B!"8GMM%$89%&>EE%7!8&"
 OO""4;;&--g6>>ufM
 t-.."$D # '	""5)%!##//%%'!s3E/F/J*KK'' 
  $33,,*+$;  4  
 $t/00$&D! 	$$%9:
 !% 0 0 #>#>#@@''!
 tZ((DM %)$8$8../0(@ %9 %! %,&&E  ++'($++-. 
(,(@(@(M% )>!% LL./ %)>)I)I!* *
 *..&8-99Q> *.)9)9-/6689s#89A=>? '33	*& 9;)5 -1-A-A 77$%7%89(47I(I .B .*  2! +77010779: $

"151I1I+1V. 5G*157*2=?*:=?*:-99@@AST'+'7'7.0779:3'9#:Q#>?@ +77	( )03E3L3LLM -1,@,@ $ 6 6 ( 7,8<,G -A -M $1 % !/ ; ;".!/"/"6"6"8!9 (
&&*&>&>{&K-1-E-Ek-R
 ,0+;+; !SX\ 2".!/"/"6"6"8!9 !/ ; ;	,L +9*E*E*TK&(,(@(@$/M<U<U )A )" 04/G/G/T  -33q8$69K9R9R$R !- 04/?/?$1$8$8<$G$2$?$?0"
 ?D.66s; * ,G.>>sC
 !$C #E
G.>>sCG )03E3L3LLMP 261A1A.558JJ*772. 25GG!-C*-1F1M1MM!A F '+@+K+K, , &;"*7"' '++v5*66!; "&!1!1-,3356s#89A=>? '33	"J .0&* %)%9%9 //$Z[1(4z(A &: &
  *! +77()(//12 $

")-)A)A+)NJ &-!*..55jA)-)9)9Z1!45()(//12 +77	* /=+ +1J4J4JJK %)$;$; (.1:L Mq%E %)$;$; (!);nq>P QST%E +a/N)%55*UC:P:P$.5%.0P4"
 0 +1J4J4JJK  &*%5%5&):)::N<W<W&
 &j6G6G)GG!} B %)$4$4%,,/DD++%! &,,1(,E,EE} @	 	U#o  	OO""CCI6"//D 	R  	OO""58CE	J ! &&<<BF;<O
 h  - " OO22!HHN$/I" ""^ $1 & $ 6 6%LLRF(3M&!" !&&. $1 & $ 6 6%BBMa%R!"
 !&&f  - " OO22!NNTf$/O" ""s   d8 =e< e<  f+ <g& ""h" +i j 8>e98e9<(f('f(+4g#"g#&4hh"4ii)jj4kkc                   ^   T R                  T R                  T R                  U[        T R                  5      R	                  5       5      T R                  U5      S9nU(       d  gU 4S jn T R                  UR                  [        U" UR                  5      UR                  S-  5      5      nT R                  UR                  [        U" UR                  5      UR                  S-  5      5      nT R                  UR                  [        U" UR                  5      UR                  S-  5      5      n/ n	Sn
T R                  UR                  5      n[!        T R"                  5      nU(       a3  UR$                  [!        UR                  5       5      -   UR                  -
  n[&        R(                  " [*        5      nSn[-        [        UR                  [+        US-  5      5      5       GH  nT R/                  X5      nUb%  US-  [!        U5      :  a  T R1                  UU5      nO  gUb  US:X  a  MJ  UU:  a,  UX-   :  a$  T R3                  U5      n T R                  U5      nOU(       a  M  SnSnT R1                  Xo5      nUc  U
S	-  n
U
S::  a  S
n  GO:T R3                  U[4        5      n[7        USS9(       d  S
n  GO T R                  U5      nUUU4==   S	-  ss'   UUU4   S:  a%  T R                  R                  SU SUS S35          O[!        U5      T R8                  :  a6  T R                  R                  SR;                  T R8                  5      5          OpU	R                  [=        T UR>                  U-   T R                  UR                  SU-  -   5      UT R                  UR                  SU-  -   5      UUUUS9	5        GM     U(       d)  T R                  R                  SUR                  S 35        U	 Vs1 s H  nUR@                  iM     nnSn
T R                  UR                  5      n[!        T R"                  5      nU(       a3  UR$                  [!        UR                  5       5      -   UR                  -
  n[&        R(                  " [*        5      nSn[-        [        UR                  [+        US-  5      5      5       GH.  nUUR>                  -   U;  d  M   T R1                  UU5      nUc  U
S	-  n
U
S::  a  S
n  OUS:X  a  MF  Ub   UU:  a  UX-   :  a  T R3                  U5      nOSnUU==   S	-  ss'   UU   T RB                  :  a7  T R                  R                  SR;                  T RB                  U5      5          Oq[!        U5      T R8                  :  a+  T R                  R                  ST R8                   S35          O-U	R                  [=        UR>                  U-   USUS95        GM1     U(       d*  T R                  R                  SUR                  S 35        gU	(       d  URE                  5       (       a  g[G        UU	T R3                  URH                  5      S9$ ! [         a!    T R                  R                  SU-  5         gf = f! [         a!    T R                  R                  SU-  5         gf = f! [         a     GM  f = f! [         aN    U
S	-  n
U
S::  a  S
n   GM.   T R                  U5      n GNA! [         a    U
S	-  n
U
S::  a  S
n    GM`    GML  f = ff = fs  snf ! [         a    Sn GNVf = f)zParse the export directory.

Given the RVA of the export directory, it will process all
its entries.

The exports will be made available as a list of ExportData
instances in the 'IMAGE_DIRECTORY_ENTRY_EXPORT' PE attribute.
rZ  z+Error parsing export directory at RVA: 0x%xNc                 R   > [        TR                  5      TR                  U 5      -
  $ r   )rF  rt  r  )r  rt  s    r   length_until_eof3PE.parse_export_directory.<locals>.length_until_eof  s"    t}}%(@(@(EEEr   rE   rQ   Tr   r9   F)rK  z9Export directory contains more than 10 repeated entries (r  z#02xz). Assuming corrupt.zHExport directory contains more than {} symbol entries. Assuming corrupt.rA   )	r  r  r%  r  r2  r
  r#  r0  r3  zIRVA AddressOfNames in the export directory points to an invalid address: rl  z[Export directory contains more than {} repeated ordinal entries (0x{:x}). Assuming corrupt.z$Export directory contains more than z# ordinal entries. Assuming corrupt.)r  r  r
  r0  zMRVA AddressOfFunctions in the export directory points to an invalid address: )rG  symbolsr
  )%r  !__IMAGE_EXPORT_DIRECTORY_format__r  r  r&  r  r  r  r  AddressOfNamesrr  NumberOfNamesAddressOfNameOrdinalsAddressOfFunctionsNumberOfFunctionsr  rF  rt  r^  collectionsdefaultdictr%   r  rY  get_dword_from_dataget_string_at_rvaMAX_SYMBOL_NAME_LENGTHrQ  r  r  r.  Baser  r  r#  r,  re  )rt  r  r  ro  
export_dirry  address_of_namesaddress_of_name_ordinalsaddress_of_functionsexports#max_failed_entries_before_giving_uprj  r  symbol_counts&export_parsing_loop_completed_normallyrN  symbol_ordinalsymbol_addressforwarder_strr3  symbol_name_addresssymbol_namesymbol_name_offsetexpordinalsr+  s   `                         r   rr  PE.parse_export_directory  s   	--664#I#IJQQS !44S9 . J 
	F	#}}))$Z%>%>?,,q0  (,}}00$Z%E%EF,,q0($ $(==--$Z%B%BC0014$  .0+))**C*CDdmm,&&g&&()*++,  $//415.s:33S19L5MNOA!445MQN)nq.@3$D / "&!9!9(." %1)<
 $#*)D $ 6 6~ F'+'?'?'O$ " $#' "&":":;K"O"*3q836!;=B:00#%;K *+PTU9>6%)%=%=>Q%R"$ ;78A=8k>:;b@&&#}B~d&;;OQ ]#d&=&==&&((.t/F/F(G NN&OOn<#'#;#;"881q5@$ +#'#;#;"55N8JJ$ % 2+%5[ P~ 6OO""&55a8:
 ,337CCKK73.0+))**G*GHdmm,&&g&&()*//0  $//415.Z993QR?R;STUC(H4*%)%=%=>RTW%XN ")71<7:a?AF>!Q& #.&#-&3$($:$:>$JM$(M
 n-2- 043K3KKOO**FFLf 44nG '$*A*AAOO**>2233VX  *# 5 .!"/	_ Vp 6OO""&99!<> :0022''
8
 	
c  	OO""=E 		J  	OO""=E 		Z % . ! 3q836!;=B:)-)A)ABU)V&$ 71<7:a?AF>n 4* % *%)N*s   AY6 -C	Z$ [[$\?+]6(Z! Z!$([[
[! [!$\<\\8,\<2\<7\88\<]]c                 "    X-   S-   S-  US-  -
  $ )NrC   l    re  )rt  r  bases      r   rP  PE.dword_align  s    "j0TJ5FGGr   c                     U R                   R                  nU R                   R                  U R                   R                  -   nX!::  a	  X:  a  X-  nU$ r   )rj  	ImageBaser  )rt  vabegin_of_imageend_of_images       r   normalize_import_vaPE.normalize_import_va  sQ     --77++558L8L8X8XX B$5 B	r   c                    / nSn  U R                  U[        U R                  5      R                  5       5      nU R                  U5      nU R                  U R                  UUS9nU(       a  UR                  5       (       a   U$ SnUR                  S:X  a  U R                  R                  [        S   :X  a  U R                  UR                  5      Ul        U R                  UR                   5      Ul        U R                  UR"                  5      Ul        U R                  UR$                  5      Ul        U R                  UR$                  5      Ul        U R                  UR(                  5      Ul        SnXR                  5       -  n[+        U R,                  5      U-
  n	XR"                  :  d  XR                   :  a$  [/        XR"                  -
  XR                   -
  5      n	/ n
 U R1                  UR"                  UR                   SU	U5      n
US	:  a-  U R
                  R                  S
R3                  U5      5         U$ U
(       d  US-  nGMO  U R6                  [8        :  a1  U R
                  R                  SU R6                  [8        4-  5         U$ U R;                  UR(                  [<        5      n[?        U5      (       d  [A        S5      nU(       ao  U
 HP  nURB                  b  M  [D        RF                  " URI                  5       URJ                  5      nU(       d  MJ  Xl!        MR     UR                  [M        XzUS95        GMG  ! [         a"    U R
                  R                  SU-  5         U$ f = f! [         a?  nU R
                  R                  SR3                  XR4                  5      5         SnAGNSnAff = f)z*Walk and parse the delay import directory.r   Tz5Error parsing the Delay import directory at RVA: 0x%xrZ  Fr   NzSError parsing the Delay import directory. Invalid import data at RVA: 0x{0:x} ({1})rG   zWToo many errors parsing the Delay import directory. Invalid import data at RVA: 0x{0:x}r9   z)Error, too many imported symbols %d (>%s)	*invalid*rG  importsdll)'r  r  (__IMAGE_DELAY_IMPORT_DESCRIPTOR_format__r&  r  r  r  r  r  r#  grAttrsr   r  r  r  	pBoundIATpIATpINT
pUnloadIATphmodszNamerF  rt  rs  parse_importsr  rb  r  MAX_IMPORT_SYMBOLSr  MAX_DLL_LENGTHrJ  rI  r
  	ordlookup	ordLookuplowerr  r  )rt  r  r  import_descsrO  r0   r  import_desccontains_addressesmax_lenimport_datar  r  r  funcnames                  r   rx  PE.parse_delay_import_directory  s     }}dKKLSSU 2237K..==' / K +"8"8":":^ ] "' ##q($$,,=V0WW(,(@(@AVAV(W%#'#;#;K<L<L#M #'#;#;K<L<L#M )-)A)A***& %)$<$<[=S=S$T!%)%=%=k>P>P%Q"%)"%%''C
 $--(;6G%%%/?/?)?c$4$44c<L<L6LMK"00$$$$& Q&&::@&+ 6 3 q **-??&&?224FGH    ((););^LC(--n)F{{*#,#6#6syy{FNN#S#8*2K	 *
 ##"+PSTE  ! &&KsS t }p ! &&@@FsJJ@W s)   3M ))M< (M98M9<
O4O  Oc                    [        U S5      (       a  U R                  c  gUS:X  a-  [        U R                  R                  5      R	                  5       $ US:X  a-  [        U R                  R                  5      R	                  5       $ US:X  a-  [        U R                  R                  5      R	                  5       $ US:X  a-  [        U R                  R                  5      R	                  5       $ [        S5      e)NrK  r  r	   r   r   r   z#Invalid hashing algorithm specified)	r}  rK  r	   r0  r  r   r   r   r  )rt  	algorithms     r   get_rich_header_hashPE.get_rich_header_hash  s    t]++t/?/?/Gt''223==??& ((334>>@@("$**556@@BB("$**556@@BB=>>r   c           	         / n/ SQn[        U S5      (       d  gU R                   GH  n[        UR                  [        5      (       a)  UR                  R                  5       R                  5       nOUR                  R                  5       nUR                  SS5      n[        U5      S:  a  US   U;   a  US   nUR                  R                  5       nUR                   H  nSnUR                  (       dM  [        R                  " XgR                  SS	9nU(       d&  [        S
UR                   SUR                  S 35      eOUR                  nU(       d  Mx  [        U[        5      (       a  UR                  5       nUR                  UR                  5       < SUR                  5       < 35        M     GM     [!        SR#                  U5      R%                  5       5      R'                  5       $ )a  Return the imphash of the PE file.

Creates a hash based on imported symbol names and their specific order within
the executable:
https://www.mandiant.com/resources/blog/tracking-malware-import-hashing

Returns:
    the hexdigest of the MD5 hash of the exported symbols.
)ocxsysr  DIRECTORY_ENTRY_IMPORTr  .r9   r   NT)	make_namezUnable to look up ordinal rB  04xr  )r}  r  rU  r  rW  rJ  r  rsplitrF  r  r
  r  r  r  r  r  r	   r  rk  r  )	rt  impstrsextsr  libnamepartsentry_dll_lowerimpr  s	            r   get_imphashPE.get_imphash  s    $t56600E%))U++))**,224))//+NN3*E5zA~%(d"2(#iioo/O}}xx(22' H $+81S[[QTDUV  $
  #xxHh..'0H'--/8>>;KLM% % 1> 388G$++-.88::r   c                    [        U S5      (       d  g[        U R                  S5      (       d  gU R                  R                   Vs/ s HC  nU(       d  M  UR                  c  M  UR                  R	                  5       R                  5       PME     nn[        U5      S:X  a  g[        SR                  U5      R                  5       5      R                  5       $ s  snf )zReturn the exphash of the PE file.

Similar to imphash, but based on exported symbol names and their specific order.

Returns:
    the hexdigest of the SHA256 hash of the exported symbols.
DIRECTORY_ENTRY_EXPORTr  r{  r   r  )r}  r  r{  r
  rJ  r  rF  r   r  rk  r  )rt  r<   export_lists      r   get_exphashPE.get_exphashJ  s     t566t22I>> 0088
8 $VV $AFFMMO!!#8 	 

 {q chh{+2245??AA
s   CC%,Cc                    / nSn[        U R                  5      R                  5       n  U R                  X5      nU R                  U5      nU R                  U R                  XxS9n	U	(       a  U	R                  5       (       a  GOXR                  5       -  n[        U R                  5      U-
  n
XR                  :  d  XR                  :  a$  [        XR                  -
  XR                  -
  5      n
/ nU(       df   U R                  U	R                  U	R                  U	R                   U
S9nUS
:  a   U R
                  R                  SUS 35        OU(       d  US-  nGM7  U R%                  U	R&                  [(        5      n[+        U5      (       d  [-        S5      nU(       ao  U HP  nUR.                  b  M  [0        R2                  " UR5                  5       UR6                  5      nU(       d  MJ  Xl        MR     UR                  [9        XUS95        GM  U(       d  [;        SS/5      nSnSnU H  nUR<                   H  nU H~  nU(       a  UR.                  (       d  M  UR.                  n[?        UR.                  5      [@        :X  a  UR.                  RC                  S5      nURE                  U5      (       d  My  US-  n  O   US-  nM     M     U[        U5      :X  a!  US:  a  U R
                  R                  S5        U$ ! [         a$    U R
                  R                  SUS 35         GM  f = f! [         a8  nU R
                  R                  SUS SUR"                   S35         S	nAGNFS	nAff = f)z$Walk and parse the import directory.r   z-Error parsing the import directory at RVA: 0xrl  rZ  r  zBError parsing the import directory. Invalid Import data at RVA: 0xz ()NrG   zLToo many errors parsing the import directory. Invalid import data at RVA: 0xr9   r  r  LoadLibraryGetProcAddressr  r   z?Imported symbols contain entries typical of packed executables.)#r  "__IMAGE_IMPORT_DESCRIPTOR_format__r&  r  r  r  r  r  r  r#  rF  rt  OriginalFirstThunk
FirstThunkrs  r  ForwarderChainrb  r  re  r  rJ  rI  r
  r  r  r  r  r  rI  r  rF  rW  rJ  rX  )rt  r  r  rp  r  rO  image_import_descriptor_sizer0   r  r  r  r  r<   r  r  r  suspicious_importssuspicious_imports_counttotal_symbolsimp_dllsuspicious_symbolr
  s                         r   rq  PE.parse_import_directoryc  sT    '033(

&( 	%  }}SG 2237K..77 / K
 +"8"8":":%%''C
 $--(;6G333s=S=S7S888#@V@V:V K "&"4"4#66#..#22#*	 #5 #K ?OO**99<QA "1$K(()9)9>JC(--n)F{{*#,#6#6syy{FNN#S#8*2K	 *
 ##"+PSTI P !$m5E%F!G'($M'%ooF-?)%V[[$%{{,5#);;#5#5g#>D??+<==494! .@ "Q&M . ( )C0B,CC!B&&&U w ! &&CC7K 	F % OO**99<Qr!''!M s)   L :0L3 )L0/L03
M5=-M00M5c                    / nU R                  XU5      nU R                  X$U5      nU(       a  [        U5      S:X  a;  U(       a  [        U5      S:X  a%  U R                  R                  SUS SUS 35        / $ Sn	U(       a  Un	OU(       a  Un	OgSn
SnU R                  [
        :X  a  [        nO%U R                  [        :X  a  [        nSn
S	nO[        nSn[        U	5       GH  u  pSnSnSnSnSnS
nUR                  (       a  UR                  U-  (       a  SnUR                  S-  nSnSnOS
n UR                  U-  nU R                  US5      nU R                  US5      nU R                  UR                  S-   [        5      n[        U5      (       d  [!        S5      nU R#                  UR                  S-   5      nUR'                  5       nU R)                  U5      nX R*                  R,                  -   X-  -   nSn U(       a8  U(       a1  X~   R                  X   R                  :w  a  X   R                  nX   nOSn Uc  Uc  [%        S5      eU[!        S5      :X  a  US:  a  X:X  a  [%        S5      eUS-  nGM  U(       d
  U(       d  GM  UR                  [1        U UUUUUR'                  5       UUUUUUWWS95        GM     U$ ! [$         a     GNf = f! [.         a    Sn Nf = f)zParse the imported symbols.

It will fill a list, which will be available as the dictionary
attribute "imports". Its keys will be the DLL names and the values
of all the symbols imported from that object.
r   z\Damaged Import Table information. ILT and/or IAT appear to be broken. OriginalFirstThunk: 0xrl  z FirstThunk: 0xNrE   r  rM   l    FTr  rA   r  z"Invalid entries, aborting parsing.i  z)Too many invalid names, aborting parsing.r9   )r  r  r"  import_by_ordinalr  r%  hintr
  r#  r  r  hint_name_table_rvathunk_offset	thunk_rva)get_import_tablerF  r  r  r  r  r  r  r  r)  r  r  rY  r  MAX_IMPORT_NAME_LENGTHrQ  rI  r  r  r  r  rj  r  r{  r  )rt  original_first_thunkfirst_thunkforwarder_chainr  r  imported_symbolsiltiattable
imp_offsetaddress_maskr'  num_invalidr+  	tbl_entryimp_ordimp_hintimp_namer#  r  r  r0   r  r  imp_addressr"  	imp_bounds                               r   r  PE.parse_imports  s6     ## .@
 ##K=OP s3x1}sc#h!mOO""))=a(@ A!!,Q1 IEE
!<<33-L\\::/LJ-L
 .L'.NCGHHK"& %&& **\9(,%'55>G#H"&K(-%.7.E.E.T+#}}-@!D#'#:#:4#C#'#9#9%33a79O$  6h??'(~H&*&>&>%33a7'  )88: 44\B	 22<<<s?OO  J!338#9#9SX=S=S#S # 6 6I!$J $I 8#3#$HII
 1[>)%+*<'(STTq (( ''%.#-*; ''0'@'@'B%%$/' +,?%1"+[ /@  A ) $  ! 	!s+   0BK >K?K
KKK+*K+c                    / nU R                   [        :X  a  [        nU R                  nO9U R                   [        :X  a  [
        nU R                  nO[        nU R                  n[        U5      R                  5       nSnSn	Sn
Sn[        5       n[        5       nUnU(       Ga  Ub&  XU-   :  a  U R                  R                  S5         U$ U R                  [        :  a1  U R                  R                  SU R                  [        4-  5         U$ U =R                  S-  sl        X:  a  / $ UR                  5       U:  a  / $ UR                  5       U:  a  / $ Sn U R                  X5      nU(       d  [#        W5      U:w  a  U R                  R                  S
U-  5        g U R%                  UUU R'                  U5      S9nU(       a  U R)                  UR*                  5      Ul        U R)                  UR,                  5      Ul        U R)                  UR.                  5      Ul        U R)                  UR0                  5      Ul        U(       aA  UR*                  U:  a1  UR*                  U::  a!  U R                  R                  SU-  5         U$ U(       aZ  UR*                  (       aI  UR*                  nUU-  (       a  US-  S:  a  / $ O'UU	:  a  UnOUnUU;   a  US-  nUR3                  U5        U(       a  UR5                  5       (       a   U$ UUR                  5       -  nUR                  U5        U(       a  GM  U$ ! [          a    S	n GNf = f)Nr           r[   r   z9Error parsing the import table. Entries go beyond bounds.z$Excessive number of imports %d (>%s)r9   FTz9Error parsing the import table. Invalid data at RVA: 0x%xrZ  z\Error parsing the import table. AddressOfData overlaps with THUNK_DATA for THUNK at RVA 0x%xr  r  )r  r  r  __IMAGE_THUNK_DATA_format__r  r  __IMAGE_THUNK_DATA64_format__r  r&  rn  r  r  r  r  r|  r  r  rF  r  r  r  r  r!  r   r  rx  r#  )rt  r  r  r  r  r'  r  expected_sizeMAX_ADDRESS_SPREADADDR_4GBMAX_REPEATED_ADDRESSESrepeated_addressaddresses_of_data_set_64addresses_of_data_set_32	start_rvafailedr0   
thunk_dataaddr_of_datathe_sets                       r   r  PE.get_import_tablel  s`   
 <<33-L55F\\::/L77F
 .L55F!&)002(!##-< #-< 	%#Z1G*G&&O B  **-??&&:224FGH t q ''1,'  9	
 (,,.1CC	',,.1CC	F}}S8 Tm3&&RUXX --$*B*B3*G . J
 "+/+C+C,,,
( .2-E-E...
* '+&>&>z?R?R&S
#%)%=%=j>P>P%Q
" ,,	9,,3&&(+.0
 > ; j66)77,. $j069!	 : $x/":":#w.(A-(KK-!6!6!8!8 	 :$$&&CLL$I cL M ! s   M   MMc                    Ub  U R                   nU R                  U5        U R                   SS nU R                   GH5  nUR                  S:X  a  UR                  S:X  a  M&  UR                  nU R                  UR                  U R                  R                  5      nU R                  UR                  U R                  R                  U R                  R                  5      nU[        U R                   5      :  d9  U[        U R                   5      :  d   Xg-   [        U R                   5      :  d  X:  a  M  U[        U5      -
  n	U	S:  a	  USU	-  -  nOU	S:  a  USU	 nXER                  5       -  nGM8     Ub  WU l         U$ )aD  Returns the data corresponding to the memory layout of the PE file.

The data includes the PE header and the sections loaded at offsets
corresponding to their relative virtual addresses. (the VirtualAddress
section header member).
Any offset in this data corresponds to the absolute memory address
ImageBase+offset.

The optional argument 'max_virtual_address' provides with means of limiting
which sections are processed.
Any section with their VirtualAddress beyond this value will be skipped.
Normally, sections with values beyond this range are just there to confuse
tools. It's a common trick to see in packed executables.

If the 'ImageBase' optional argument is supplied, the file's relocations
will be applied to the image by calling the 'relocate_image()' method. Beware
that the relocation information is applied permanently.
Nr   rA  )rt  relocate_imager  r`  r_  ri  r]  rj  rk  ro  r^  rp  rF  r  )
rt  max_virtual_addressr  original_datamapped_datarj  srdprdrb  r   s
             r   get_memory_mapped_imagePE.get_memory_mapped_image  so   ,   !MMM	* mmA&}}G ''1,1F1F!1K''C++(($*>*>*L*LC "&!=!=&&$$55$$22" c$--((T]]++9s4==11%</#k2BBN!u~55!#)/>:++--KA %H  )DMr   c                    / n[        U S5      (       a  U R                  R                   H  n[        US5      (       d  M  UR                  R                   H  n[        US5      (       d  M  [        UR                  S5      (       d  M3  UR                  R                  (       d  MP  [        UR                  R                  R                  5       5       H  nUR                  U5        M     M     M     U$ )zReturns a list of all the strings found withing the resources (if any).

This method will scan all entries in the resources directory of the PE, if
there is one, and will return a [] with the strings.

An empty list will be returned otherwise.
DIRECTORY_ENTRY_RESOURCEr  r  )r}  r  rP  r  r  r3  r  r  )rt  resources_stringsres_typer.  
res_strings        r   get_resources_stringsPE.get_resources_strings9  s     4344 99AA8[11'/'9'9'A'A";<< '(=(=y I I$/$9$9$A$A$A26$/$9$9$A$A$H$H$J3"J %6$<$<Z$H3" (B B ! r   c                 "   U R                  U5      nU(       a  X-   nOSnU(       dY  U[        U R                  5      :  a  U R                  X $ U[        U R                  5      :  a  U R                  X $ [	        S5      eUR                  X5      $ )zGet data regardless of the section where it lies on.

Given a RVA and the size of the chunk to retrieve, this method
will find the section where the data lies and return the data.
Nz-data at RVA can't be fetched. Corrupt header?)r  rF  r  rt  r  r  )rt  r  r  r  rw  s        r   r  PE.get_dataU  s     ##C(,CCS%%{{3++ S''}}S-- OPPzz#&&r   c           
      f   U R                  U5      nU(       d  U R                  (       aq  [        U R                   Vs/ s HH  nU R                  UR                  U R
                  R                  U R
                  R                  5      PMJ     sn5      nX:  a  U$ gU$ UR                  U5      $ s  snf )z.Get the RVA corresponding to this file offset.N)	r  r  rr  ro  r^  rj  rp  rk  r  )rt  r  r  
lowest_rvas       r   r  PE.get_rva_from_offsetw  s     &&v.}}  "& "/A 44,, 00AA 00>>
 "/	
 & "M$$V,,-s   AB.c                     U R                  U5      nU(       d+  U[        U R                  5      :  a  U$ [        SUS S35      eUR	                  U5      $ )zGet the file offset corresponding to this RVA.

Given a RVA , this method will find the section where the
data lies and return the offset within the file.
zdata at RVA 0xrl  z can't be fetched)r  rF  rt  r  r  )rt  r  r  s      r   r  PE.get_offset_from_rva  sW     ##C( S''
.Q7H IJJ$$S))r   c                     Uc  gU R                  U5      nU(       d!  U R                  SU R                  XU-    5      $ U R                  SUR                  XS95      $ )z1Get an ASCII string located at the given address.Nr   )r  )r  r  rt  r  )rt  r  r  r  s       r   r  PE.get_string_at_rva  s^     ;##C(,,Qc*DT0UVV((AJJsJ,NOOr   c                 n    U[        U5      :  a  gX!S n[        U[        5      (       a  [        U5      $ U$ )r  r   N)rF  rU  ri  rW  )rt  r  r0   r  s       r   get_bytes_from_dataPE.get_bytes_from_data  s5    CIMa##8Or   c                 `    U R                  X5      nUR                  S5      nUS:  a  USU nU$ )zGet an ASCII string from data.rA  r   N)r)  r2  )rt  r  r0   r  rw  s        r   r  PE.get_string_from_data  s6    $$V2ffUm!8$3Ar   c                 n   US:X  a  gU R                  US5      nUS-  n[        US5      nU R                  X5      nSn UR                  SUS-   5      nUS:X  aD  [        U5      nXu:  d  Xr:X  a  [        U5      S-	  nO1X@R                  X-   X'-
  5      -  nUS-
  nUnOUS-  S:X  a  US-  nOMp  [        R
                  " SR                  U5      US	US-   5      nS
R                  [        [        U5      5      n	U(       a  [        U	R                  US5      5      $ [        U	R                  SS5      5      $ )z3Get an Unicode string located at the given address.r   r   rA   r9   rg   r*  r  z<{:d}HNr  r
   r  )r  rr  r2  rF  rG  rH  r  r  maprL  rI  rk  )
rt  r  r  rJ  r0   	requested
null_indexdata_lengthuchrsr  s
             r   r  PE.get_string_u_at_rva  sA    ? }}S!$ 	q

C(	}}S,
;
Q?JR!$i*k.G!$TaJ c&79QRR&]
&	a1$q 
 $ hooj94@P*q.;QRGGCUO$QXXh(;<=='#6788r   c                 ^    U R                    H  nUR                  U5      (       d  M  Us  $    g)z1Get the section containing the given file offset.N)r  r  )rt  r  rj  s      r   r  PE.get_section_by_offset  s-     }}G&&v.. % r   c                     U R                   b,  U R                   R                  U5      (       a  U R                   $ U R                   H#  nUR                  U5      (       d  M  X l         Us  $    g)z-Get the section containing the given address.N)r  r  r  )rt  r  rj  s      r   r  PE.get_section_by_rva  sb    
 --911>>sCC999}}G##C((5<2 %
 r   c                 "    U R                  5       $ r   )	dump_infor{  s    r   r  
PE.__str__  s    ~~r   c                     [        U S5      $ )z.Checks if the PE file has relocation directoryDIRECTORY_ENTRY_BASERELOC)r}  r{  s    r   
has_relocsPE.has_relocs  s    t899r   c                 ^    [        U S5      (       a  U R                  R                  (       a  gg)NDIRECTORY_ENTRY_LOAD_CONFIGTF)r}  r@  r  r{  s    r   has_dynamic_relocsPE.has_dynamic_relocs  s%    4677//CCr   c                 4    [        U R                  US95        g)z=Print all the PE header information in a human readable from.rI  N)rB  r9  )rt  rJ  s     r   
print_infoPE.print_info  s    dnnhn/0r   c                 /   Uc
  [        5       nU R                  5       nU(       a;  UR                  S5        U H$  nUR                  U5        UR	                  5         M&     UR                  S5        UR                  U R                  R                  5       5        UR	                  5         UR                  S5        UR                  U R                  R                  5       5        UR	                  5         UR                  S5        UR                  U R                  R                  5       5        [        [        S5      nUR                  S5        / n[        U5       H7  n[        U R                  US   5      (       d  M#  UR                  US   5        M9     UR                  S	R!                  U5      5        UR	                  5         [#        U S
5      (       aG  U R$                  b:  UR                  S
5        UR                  U R$                  R                  5       5        [        [&        S5      nUR                  S5        / n[        U5       H7  n[        U R$                  US   5      (       d  M#  UR                  US   5        M9     UR                  S	R!                  U5      5        UR	                  5         UR                  S5        [        [(        S5      n	U R*                   GH  n
UR                  U
R                  5       5        UR                  S5        / n[        U	5       H,  n[        XS   5      (       d  M  UR                  US   5        M.     UR                  S	R!                  U5      5        UR                  SR-                  U
R/                  5       5      5        [0        b.  UR                  SR-                  U
R3                  5       5      5        [4        b"  UR                  SU
R7                  5       -  5        [8        b"  UR                  SU
R;                  5       -  5        [<        b"  UR                  SU
R?                  5       -  5        UR	                  5         GM     [#        U S
5      (       a}  [#        U R$                  S5      (       ab  UR                  S5        U R$                  R@                   H'  nUc  M  UR                  UR                  5       5        M)     UR	                  5         [#        U S5      (       Gay  [C        U RD                  5       GH_  u  p[G        U RD                  5      S:  a  UR                  SUS-    35        OUR                  S5        Ub  UR                  UR                  5       5        UR	                  5         [#        U S5      (       a<  UR                  U RH                  U   R                  5       5        UR	                  5         [#        U S5      (       d  M  [G        U RJ                  5      U:  d  M  U RJ                  U    GHV  nUR                  UR                  5       5        UR	                  5         [#        US5      (       Ga  URL                   H  nUR                  5        Vs/ s H  nUR                  SU-   5      PM       nUR                  SR-                  URN                  RQ                  US5      5      5        UR	                  5         [        [S        URT                  RW                  5       5      5       HJ  nUR                  S R-                  US   RQ                  US5      US   RQ                  US5      5      5        ML     M     UR	                  5         GMa  [#        US!5      (       d  GMu  URX                   H  n[#        US"5      (       d  M  UR                  5        Vs/ s H  nUR                  SU-   5      PM       nUR                  S R-                  [S        URZ                  R]                  5       5      S   RQ                  S#S5      [S        URZ                  R_                  5       5      S   5      5        M     UR	                  5         GMY     GMb     [#        U S$5      (       Gac  UR                  S%5        UR                  U R`                  Rb                  R                  5       5        UR	                  5         UR                  S&S'-  5        U R`                  Rd                   H  nURf                  c  M  [i        S(5      nURj                  (       a  URj                  nUR                  S)URl                  URf                  URQ                  U5      4-  5        URn                  (       a<  UR                  S*R-                  URn                  RQ                  US5      5      5        M  UR	                  5         M     UR	                  5         [#        U S+5      (       Gaf  UR                  S,5        U Rp                   GHD  nUR                  URb                  R                  5       5        URr                  (       dc  UR                  S-R-                  U Ru                  URb                  Rv                  5      RQ                  US5      5      5        UR	                  5         UR	                  5         URr                   GHr  nURx                  S.L a  URj                  b_  UR                  S/R-                  URz                  RQ                  S#5      URj                  RQ                  S#5      URl                  5      5        OUR                  S0R-                  URz                  RQ                  S#5      URl                  5      5        O`UR                  S1R-                  URz                  RQ                  US5      URj                  RQ                  US5      UR|                  5      5        UR~                  (       a-  UR                  S2R-                  UR~                  5      5        GMb  UR	                  5         GMu     UR	                  5         GMG     [#        U S35      (       Ga   UR                  S45        U R                   H  nUR                  URb                  R                  5       5        UR                  S5R-                  URj                  RQ                  US5      5      5        UR	                  5         URT                   Hx  nUR                  URb                  R                  5       S65        UR                  S5R-                  URj                  RQ                  US5      5      S65        UR	                  5         Mz     GM     [#        U S75      (       Ga  UR                  S85        U R                   GHd  nUR                  URb                  R                  5       5        UR	                  5         URr                   GH  nURx                  S.L aF  UR                  S9R-                  URz                  RQ                  US5      URl                  5      5        O`UR                  S:R-                  URz                  RQ                  US5      URj                  RQ                  US5      UR|                  5      5        UR~                  (       a,  UR                  S2R-                  UR~                  5      5        M  UR	                  5         GM	     UR	                  5         GMg     [#        U S;5      (       Ga  UR                  S<5        UR                  U R                  Rb                  R                  5       5        U R                  RT                   GH  nURj                  b3  URj                  RQ                  US5      nUR                  S=U S>3S?5        OX[        R                  URb                  R                  S@5      nUR                  SAURb                  R                  SB SCU SD3S?5        UR                  URb                  R                  5       S?5        [#        USE5      (       Ga)  UR                  UR                  Rb                  R                  5       S65        UR                  RT                   GH  nURj                  b3  URj                  RQ                  S#S5      nUR                  S=U S>3SF5        O+UR                  SAURb                  R                  SB S>3SF5        UR                  URb                  R                  5       SF5        [#        USE5      (       d  M  UR                  UR                  Rb                  R                  5       SG5        UR                  RT                   GH  n[#        USH5      (       d  M  UR                  SIUR                  R                  UR                  R                  [        R                  UR                  R                  SJ5      [        UR                  R                  UR                  R                  5      4-  SG5        UR                  URb                  R                  5       SK5        UR                  UR                  Rb                  R                  5       SL5        GM     [#        UR                  SM5      (       d  GM)  UR                  R                  (       d  GMG  UR                  SNSK5        [S        [        UR                  R                  RW                  5       5      5       HG  u  nnUR                  SOR-                  UUR                  SPSQ5      RQ                  SR5      5      SL5        MI     GM     UR	                  5         GM     UR	                  5         [#        U SS5      (       a  U R                  (       ao  U R                  Rb                  (       aT  UR                  ST5        UR                  U R                  Rb                  R                  5       5        UR	                  5         [#        U SU5      (       a  U R                  (       ao  U R                  Rb                  (       aT  UR                  SV5        UR                  U R                  Rb                  R                  5       5        UR	                  5         [#        U SW5      (       a  UR                  SX5        U R                   H  nUR                  URb                  R                  5       5         UR                  SY[        URb                  R                     -   5        UR	                  5         URZ                  (       d  M  UR                  URZ                  R                  5       S65        UR	                  5         M     U R                  5       (       a  UR                  S[5        U R                   H  nUR                  URb                  R                  5       5        URT                   H9  n  UR                  S\U R                  [        U R                     S]S 4-  S65        M;     UR	                  5         M     [#        U S_5      (       a  [G        U R                  5      S:  a  UR                  S`5        U R                   Hx  n!UR                  U!Rb                  R                  5       5        [#        U!Sa5      (       d  M?  U!R                  c  MN  UR                  U!R                  R                  5       S65        Mz     UR                  5       $ s  snf s  snf ! [         a8    UR                  SZR-                  URb                  R                  5      5         GN:f = f! [         a0    UR                  S^U R                  U R                  4-  S65         GM  f = f)bz>Dump all the PE header information into human readable string.NParsing Warningsr  r  r   r  r~  r   r  rj  r  zDllCharacteristics: PE Sectionsr{  z!Entropy: {0:f} (Min=0.0, Max=8.0)zMD5     hash: {0}zSHA-1   hash: %szSHA-256 hash: %szSHA-512 hash: %sr  Directoriesr  r9   zVersion Information Version Informationr  rO  r  z  z  LangID: {0}r
   z    {0}: {1}r  r  r  r  Exported symbolsz%-10s   %-10s  %sr  RVAre  Nonez%-10d 0x%08X    %sz forwarder: {0}r  Imported symbolsz  Name -> {0}Tz*{0}.{1} Ordinal[{2}] (Imported by Ordinal)z&{0} Ordinal[{1}] (Imported by Ordinal)z{0}.{1} Hint[{2:d}]z Bound: 0x{0:08X}DIRECTORY_ENTRY_BOUND_IMPORTBound importszDLL: {0}rE   DIRECTORY_ENTRY_DELAY_IMPORTDelay Imported symbolsz({0} Ordinal[{1:d}] (Imported by Ordinal)z{0}.{1} Hint[{2}]r  Resource directoryzName: []rA   r  zId: [0xr  z] (r  r  rI   rM   r0   z\--- LANG [%d,%d][%s,%s]r;  rQ   rU   r  z	[STRINGS]z{0:6d}: {1}unicode-escaper   rH  DIRECTORY_ENTRY_TLSTLSr@  LOAD_CONFIGDIRECTORY_ENTRY_DEBUGDebug informationzType: zType: 0x{0:x}(Unknown)Base relocationsz%08Xh %sr6   z0x%08X 0x%x(Unknown)DIRECTORY_ENTRY_EXCEPTIONz"Unwind data for exception handlingr  )\r  r  r  r  r  r  r  r7  r  r   r\  r  rx  sortedr0  r  r  r}  rj  r  r|  r  r  r  r	   r  r   r  r   r  r   r  r  r)  r  rF  r  rO  r  rU  rJ  r3  rP  r	  r  r  rT  r  r  rG  r{  r  rI  r
  r  r0  r  r  r  re  r  r  r  r  rP  rR  r  r  r=  r   r  r0   r  r  r<  rC  r  rk  rW  r@  rZ  
DEBUG_TYPEr  r  r=  r<  r  RELOCATION_TYPErF  r]  r  r  )"rt  r7  rJ  warningsrC  r  ra  r[  r"  r~  rj  r  r+  vinfo_entryr  rX  r  	str_entry	var_entryexportr
  moduler  bound_imp_descbound_imp_refr  res_type_idr.  r0  r  r  
base_relocrelocr  s"                                     r   r9  PE.dump_info  s~    <6D$$&OO./#g&  " $ 	%t++-.%t++-.&t'',,./$%:MJ;'Dt''a11T!W% ( 	dii&'4*++0D0D0POO-.NN4//4467$2!<%
! 	'(45Dt++T!W55T!W% 6 	dii&'&&'>M}}GNN7<<>*HHYE}-7G,,LLa) . MM$))E*+MM3::7;N;N;PQ 1889M9M9OPQ073H3H3JJK!073J3J3LLM!073J3J3LLM' %* 4*++  "21
 1
 OOM*!11@@	(NN9>>#34 A 4)**$-d.A.A$B t**+a/OO&:37)$DEOO$9:*NN;#3#3#56  "4!344NN4#8#8#=#B#B#DE$$&4,,T]]1Cc1I!%s!3uzz|4((*"5-88,1,=,=HP Xtd{!; X $$3$:$:(0(>(>,46I)*%&!" !% 0 0 217X=M=M=S=S=U8V1WI$(MM(6(=(=,5aL,?,?08:M-. -6aL,?,?08:M-.	)*	%& 2X ->. !,,.$UE22-2YY	#*9g#>#> 5>NN4D%&4DD )-dTk(B4D%& %)MM(6(=(=,01E1E1G,H,K,R,R079L-. -11G1G1I,J1,M	)*%& .7 !,,.] "4 %C| 4122OO./NN466==BBDEMM-0JJK55==>>-V9D{{%{{HH,!>>6>>4;;x;PQR ''-44 & 0 0 7 7BU V ((*! >$ 4122OO./55v}}1134~~HH'.. 226==3E3EFMM (*= $$&  "$nnF//47!;;2 HH L S S$*JJ$5$5g$>$*KK$6$6w$?$*NN!" !HH H O O$*JJ$5$5g$>!" 188 &

 1 1(<O P & 2 28=P Q & ||&9&@&@&NO((*9 -:   "U 6X 4788OOO,"&"C"C~4499;<%%&++228=PQ
   "%3%;%;MNN=#7#7#<#<#>BMM")))..55h@ST 	 $$& &< #D( 4788OO45;;v}}1134  "$nnF//47FMM &

 1 1(<O P & /66 &

 1 1(<O P & 2 28=P Q & ||&9&@&@&NO((*) -*   "5 <8 4344OO01NN488??DDFG 99AA==,#==//:MNDMM!$q)
 #0"3"3HOO4F4F"LKMM!(//"4"4Q!7s;-qI
 x335q98[11NN8#5#5#<#<#A#A#CQG'/'9'9'A'A&++7#.#3#3#:#:7DW#XD MM")$q 1 !
 !MMGK4F4F4I4I!3LA*NPQR{'9'9'>'>'@!D";<< NN;+@+@+G+G+L+L+NPQR1<1F1F1N1N#*=&#A#A$(MM(C,9,>,>,C,C,9,>,>,F,F,0HH0=0B0B0G0G-. -F0=0B0B0G0G0=0B0B0J0J-.
+*)* )*%& %)NN=3G3G3L3L3NPR$S$(NN=3E3E3L3L3Q3Q3SUW$X% 2O( !((=(=y I I$/$9$9$A$A$A $k2 >7;$*;+@+@+H+H+N+N+P$Q8"OC %)MM(5(<(<,/,6,=,=0@BT-..4fWo	)* )+%&8"Q (Bj   "U BX  D/00((((// OOE"NN433::??AB D788000077 OOM*NN4;;BBGGIJ4011OO/011szz01TMM(Z

-H"HI   "999NN399>>#3Q7$$& 2 ??OO./"<<
z005578'//E&%))_UZZ5PQSQT5U)VV 0   " = D566D223a7OO@A44ryy~~/02|,,1JNN2==#5#5#7; 5
 }}g	 !Y6%&n   TMM":"A"A#**//"RST" $ 2eii5LLa s>   A]&
A]+
U/A]0Y5A^5]0>A^2^1A^2^55A_/_.A_/c           	         0 nU R                  5       nU(       a  X!S'   U R                  R                  5       US'   U R                  R                  5       US'   U R                  R                  5       US'   [        [        S5      n/ US'   U H:  n[        U R                  US   5      (       d  M#  US   R                  US   5        M<     [        U S5      (       a*  U R                  b  U R                  R                  5       US'   [        [        S
5      n/ US'   U H:  n[        U R                  US   5      (       d  M#  US   R                  US   5        M<     / US'   [        [        S5      nU R                   H  nUR                  5       nUS   R                  U5        / US'   U H/  n[        XtS   5      (       d  M  US   R                  US   5        M1     UR                  5       US'   [        b  UR!                  5       US'   ["        b  UR%                  5       US'   [&        b  UR)                  5       US'   [*        c  M  UR-                  5       US'   M     [        U S5      (       ao  [        U R                  S5      (       aT  / US'   [/        U R                  R0                  5       H,  u  pU
c  M
  US   R                  U
R                  5       5        M.     [        U S5      (       Gaa  / US'   [/        U R2                  5       GHB  u  p/ nUR                  UR                  5       5        [        U S5      (       a,  UR                  U R4                  U	   R                  5       5        [        U S5      (       Ga  [7        U R8                  5      U	:  Ga  / nUR                  U5        U R8                  U	    GHw  nUR                  UR                  5       5        [        US5      (       a  0 nUR:                   Hf  nUR=                  UR                  5       5        UR>                  US'   [A        URB                  RE                  5       5       H  nUS   UUS   '   M     Mh     UR                  U5        M  [        US5      (       d  M  URF                   H  n0 n[        US5      (       d  M  UR=                  UR                  5       5        [A        URH                  RK                  5       5      S   U[A        URH                  RM                  5       5      S   '   UR                  U5        M     GMz     US   R                  U5        GME     [        U S5      (       a  / US'   US   R                  U RN                  RP                  R                  5       5        U RN                  RR                   Hy  n0 nURT                  bS  URW                  URX                  URT                  URZ                  S .5        UR\                  (       a  UR\                  US!'   US   R                  U5        M{     [        U S"5      (       a  / US#'   U R^                   H  n/ nUS#   R                  U5        UR                  URP                  R                  5       5        UR`                   H  n0 nURb                  S$L a  URd                  US%'   URX                  US&'   O-URd                  US%'   URZ                  US''   URf                  US('   URh                  (       a  URh                  US)'   UR                  U5        M     M     [        U S*5      (       a  / US+'   U Rj                   H  n0 nUS+   R                  U5        URW                  URP                  R                  5       5        URZ                  US%'   URB                   H=  n0 nURW                  URP                  R                  5       5        URZ                  US%'   M?     M     [        U S,5      (       a  / US-'   U Rl                   H  n/ nUS-   R                  U5        UR                  URP                  R                  5       5        UR`                   H  n0 nURb                  S$L a  URd                  US%'   URX                  US&'   O-URd                  US%'   URZ                  US''   URf                  US('   URh                  (       a  URh                  US)'   UR                  U5        M     M     [        U S.5      (       Ga  / US/'   US/   R                  U Rn                  RP                  R                  5       5        U Rn                  RB                   GH  n0 n URZ                  b  URZ                  U S''   OCURP                  Rp                  [r        Ru                  URP                  Rp                  S05      4U S1'   U RW                  URP                  R                  5       5        US/   R                  U 5        [        US25      (       d  M  / n!U!R                  URv                  RP                  R                  5       5        US/   R                  U!5        URv                  RB                   GH  n"0 n#U"RZ                  b  U"RZ                  U#S''   OU"RP                  Rp                  U#S1'   U#RW                  U"RP                  R                  5       5        U!R                  U#5        [        U"S25      (       d  M  / n$U$R                  U"Rv                  RP                  R                  5       5        U!R                  U$5        U"Rv                  RB                   GH  n%[        U%S35      (       d  M  0 n&U%Rx                  Rz                  U&S4'   U%Rx                  R|                  U&S5'   [~        Ru                  U%Rx                  Rz                  S65      U&S7'   [        U%Rx                  Rz                  U%Rx                  R|                  5      U&S8'   U&RW                  U%RP                  R                  5       5        U&RW                  U%Rx                  RP                  R                  5       5        U$R                  U&5        GM     [        U"Rv                  S95      (       d  GM$  U"Rv                  R                  (       d  GMB  [A        U"Rv                  R                  RE                  5       5       H6  u  n	n'U$R                  U'R                  S:S;5      R                  S<5      5        M8     GM     GM     [        U S=5      (       aS  U R                  (       aB  U R                  RP                  (       a'  U R                  RP                  R                  5       US>'   [        U S?5      (       aS  U R                  (       aB  U R                  RP                  (       a'  U R                  RP                  R                  5       US@'   [        U SA5      (       a  / USB'   U R                   H  n(0 n)USB   R                  U)5        U)RW                  U(RP                  R                  5       5        [        Ru                  U(RP                  R                  U(RP                  R                  5      U)SC'   M     U R                  5       (       a  / USD'   U R                   H  n*/ n+USD   R                  U+5        U+R                  U*RP                  R                  5       5        U*RB                   H?  n,0 n-U+R                  U-5        U,R                  U-SE'    [        U,R                     SFS	 U-SC'   MA     M     U$ ! [         a    U,R                  U-SC'    Me  f = f)Gz5Dump all the PE header information into a dictionary.rG  r  r  r   r  rl  r   rj  Nr  r  rH  r{  EntropyMD5SHA1SHA256SHA512r  rI  r  rJ  r  rO  r  rU  r9   r  r  r  rK  rL  r0  r  rO  TDLLr  re  HintBoundrP  rQ  rR  rS  r  rT  r  r   r  r0   r<  r>  r;  	LANG_NAMESUBLANG_NAMEr  rV  r   rH  rW  rX  r@  rY  rZ  r[  r  r\  rM  r6   )Or  r  rW  r  r   r\  r  r0  r  r}  rj  r  r|  r  r  r	   r  r   r  r   r  r   r  r)  r  r  r  rF  rO  r  r  rU  r3  rP  r	  r  r  r  rT  r  rG  r{  r  r  r  r
  r0  r  r  r  r  r  r  rP  rR  r  r   r  r=  r  r0   r  r  r<  rC  r  rk  rJ  rW  r@  rZ  r_  r  r=  r<  r  r`  rF  r  ).rt  rW  ra  r  r[  r"  r~  rj  section_dictr+  r  vs_vinfoversion_info_listfileinfo_listr  stringtable_dictrX  rc  rd  var_dictre  export_dictrf  import_listr  symbol_dictrg  bound_imp_desc_dictrh  bound_imp_ref_dictmodule_listr  resource_type_dictdirectory_listr.  resource_id_dictresource_id_listr0  resource_lang_dictr  r  dbg_dictrj  base_reloc_listrk  
reloc_dicts.                                                 r   rW  PE.dump_dict  s    	$$&,4()"&//";";"=	,"&//";";"=	,#'#3#3#=#=#?	- $%:MJ	'Dt''a11'"))$q'2   4*++0D0D0P+/+?+?+I+I+KI'($2!<%
! +-	&'-Dt++T!W55./66tAw? . $&	- &'>M}}G",,.Lm$++L9$&L!%7G,, )00a9 & '.&9&9&;L#&-&:&:&<U#'.'<'<'>V$!)0)@)@)BX&!)0)@)@)BX&! %$ 4*++  "21
 1
 (*Im$"+D,@,@,O,O"P(m,33I4G4G4IJ #Q 4)**/1I+,!*4+>+>!?$&!!((););)=>4!344%,,T-B-B3-G-Q-Q-ST4,,T]]1Cc1I$&M%,,];!%s!3%,,U__->?"5-88/1,,1,=,= - 4 4X5G5G5I J=E__ 0 :15h6F6F6L6L6N1OIENq\$4Yq\$B 2P ->
 *001AB$UE22-2YY	+-#*9g#>#>$1$8$89L9L9N$OPT(1(>(>(@Q&&'Q)HT)//2F2F2H-I!-L$M %2$8$8$B .7 "4, /0778IJA "@D 4122,.I()()00++22<<> 55== >>-&&'-~~#)>>$*KK ''393C3CK0,-44[A > 4122,.I()55 ,-44[A""6==#:#:#<=$nnF"$K//47-3ZZE*17I.-3ZZE*.4kkF+.4kkF+||/5||G,&&{3 -	 6$ 4788)+Io&"&"C"C&(#/*112EF#**>+@+@+J+J+LM-;-@-@#E*%3%;%;M)+&&--m.B.B.L.L.NO0=0B0B&u- &< #D 478824I./;; 23::;G""6==#:#:#<=$nnF"$K//47-3ZZE*17I.-3ZZE*.4kkF+.4kkF+||/5||G,&&{3 - <& 4344.0I*+*+22--44>>@ !99AA%'"==,19&v. !**%))(//*<*<cB0&t,
 #))(//*C*C*EF./667IJ8[11%'N"))(*<*<*C*C*M*M*OP23::>J'/'9'9'A'A+-(&++77B7G7G,V45@5G5G5J5J,T2(//0B0B0L0L0NO&--.>?";<</1,,33 + 5 5 < < F F H +112BC1<1F1F1N1N#*=&#A#A9;$6ANASASAXAX$6v$> )6(:(:(B(B %7(1%& GKhh(5(:(:(?(?G&$6{$C
 )B(5(:(:(?(?(5(:(:(B(B)& %7(6%& %7$=$=(5(<(<(F(F(H%& %7$=$=(5(:(:(A(A(K(K(M%& %5$;$;<N$O- 2O0 !((=(=y I I$/$9$9$A$A$A7;$/$9$9$A$A$G$G$I8"OC %5$;$;(2(9(9,<>P)**0&/%&8"[ (B' BT D/00((((//#77>>HHJIe D788000077 0077AAC  4011-/I)*11-.55h?

 4 4 67#->>#**//3::??#S 	 2 ??,.I()"<<
"$,-44_E&&z'8'8'B'B'DE'//E!#J#**:6(-		Ju%8-<UZZ-H-M
6* 0	 =  $ 8-2ZZ
6*8s   ;}};:};c                 F     U R                  U5      $ ! [         a     gf = f)z;Gets the physical address in the PE file from an RVA value.N)r  r  r  s     r   get_physical_by_rvaPE.get_physical_by_rva  s*    	++C00 		s    
  c                 6    [         R                  " SUS-  5      $ )zMReturn a four byte string representing the double word value (little endian).r+      rG  r1  )rt  dwords     r   get_data_from_dwordPE.get_data_from_dword  s    {{4!344r   c                 x    US-   S-  [        U5      :  a  g[        R                  " SXS-  US-   S-   5      S   $ )zConvert four bytes of data to a double word (little endian)

'offset' is assumed to index into a dword array. So setting it to
N will return a dword out of the data starting at offset N*4.

Returns None if the data can't be turned into a double word.
r9   rE   Nrj  r   rF  rG  rH  rt  r0   r  s      r   r  PE.get_dword_from_data  D     QJ!c$i'}}T4
fqjA5E#FGJJr   c                 h     U R                  U R                  US5      S5      $ ! [         a     gf = f)zReturn the double word value at the given RVA.

Returns None if the value can't be read, i.e. the RVA can't be mapped
to a file offset.
rE   r   N)r  r  r  r  s     r   r  PE.get_dword_at_rva  s7    	++DMM#q,A1EE 		s   !$ 
11c                 ~    US-   [        U R                  5      :  a  gU R                  U R                  XS-    S5      $ )zFReturn the double word value at the given file offset. (little endian)rE   Nr   )rF  rt  r  r  s     r   get_dword_from_offsetPE.get_dword_from_offset  <     A:DMM**''fz(JANNr   c                 B    U R                  XR                  U5      5      $ )zLSet the double word value at the file offset corresponding to the given RVA.)set_bytes_at_rvar  )rt  r  r  s      r   set_dword_at_rvaPE.set_dword_at_rva      $$S*B*B5*IJJr   c                 B    U R                  XR                  U5      5      $ )z3Set the double word value at the given file offset.)r&  r  )rt  r  r  s      r   r$  PE.set_dword_at_offset      ''0H0H0OPPr   c                 0    [         R                  " SU5      $ )zFReturn a two byte string representing the word value. (little endian).r  r  rt  rJ  s     r   get_data_from_wordPE.get_data_from_word      {{4&&r   c                 x    US-   S-  [        U5      :  a  g[        R                  " SXS-  US-   S-   5      S   $ )zConvert two bytes of data to a word (little endian)

'offset' is assumed to index into a word array. So setting it to
N will return a dword out of the data starting at offset N*2.

Returns None if the data can't be turned into a word.
r9   rA   Nr  r   r  r  s      r   rY  PE.get_word_from_data#  r  r   c                 l     U R                  U R                  U5      SS S5      $ ! [         a     gf = f)zReturn the word value at the given RVA.

Returns None if the value can't be read, i.e. the RVA can't be mapped
to a file offset.
NrA   r   )rY  r  r  r  s     r   get_word_at_rvaPE.get_word_at_rva1  s<    	**4==+=bq+A1EE 		   #& 
33c                 ~    US-   [        U R                  5      :  a  gU R                  U R                  XS-    S5      $ )z?Return the word value at the given file offset. (little endian)rA   Nr   )rF  rt  rY  r  s     r   get_word_from_offsetPE.get_word_from_offset=  s<     A:DMM**&&t}}Vqj'I1MMr   c                 B    U R                  XR                  U5      5      $ )zESet the word value at the file offset corresponding to the given RVA.)r  r  )rt  r  rJ  s      r   set_word_at_rvaPE.set_word_at_rvaE  s    $$S*A*A$*GHHr   c                 B    U R                  XR                  U5      5      $ )z,Set the word value at the given file offset.)r&  r  )rt  r  rJ  s      r   r1  PE.set_word_at_offsetI  s    ''0G0G0MNNr   c                 0    [         R                  " SU5      $ )zMReturn an eight byte string representing the quad-word value (little endian).<Qr  r  s     r   get_data_from_qwordPE.get_data_from_qwordQ  r  r   c                 x    US-   S-  [        U5      :  a  g[        R                  " SXS-  US-   S-   5      S   $ )zConvert eight bytes of data to a word (little endian)

'offset' is assumed to index into a word array. So setting it to
N will return a dword out of the data starting at offset N*8.

Returns None if the data can't be turned into a quad word.
r9   rM   Nr  r   r  r  s      r   get_qword_from_dataPE.get_qword_from_dataU  r  r   c                 l     U R                  U R                  U5      SS S5      $ ! [         a     gf = f)zReturn the quad-word value at the given RVA.

Returns None if the value can't be read, i.e. the RVA can't be mapped
to a file offset.
NrM   r   )r  r  r  r  s     r   get_qword_at_rvaPE.get_qword_at_rvac  s<    	++DMM#,>r,BAFF 		r  c                 ~    US-   [        U R                  5      :  a  gU R                  U R                  XS-    S5      $ )zDReturn the quad-word value at the given file offset. (little endian)rM   Nr   )rF  rt  r  r  s     r   get_qword_from_offsetPE.get_qword_from_offseto  r  r   c                 B    U R                  XR                  U5      5      $ )zJSet the quad-word value at the file offset corresponding to the given RVA.)r  r  )rt  r  qwords      r   set_qword_at_rvaPE.set_qword_at_rvaw  r  r   c                 B    U R                  XR                  U5      5      $ )z1Set the quad-word value at the given file offset.)r&  r  )rt  r  r  s      r   set_qword_at_offsetPE.set_qword_at_offset{  r  r   c                     [        U[        5      (       d  [        S5      eU R                  U5      nU(       d  gU R	                  X25      $ )zOverwrite, with the given string, the bytes at the file offset corresponding
to the given RVA.

Return True if successful, False otherwise. It can fail if the
offset is outside the file's boundaries.
data should be of type: bytesF)rU  rW  	TypeErrorr  r&  )rt  r  r0   r  s       r   r  PE.set_bytes_at_rva  sD     $&&;<<))#.''55r   c                     [        U[        5      (       d  [        S5      eSUs=::  a  [        U R                  5      :  a  O  gU R                  X5        gg)zOverwrite the bytes at the given file offset with the given string.

Return True if successful, False otherwise. It can fail if the
offset is outside the file's boundaries.
r  r   FT)rU  rW  r  rF  rt  set_data_bytesrt  r  r0   s      r   r&  PE.set_bytes_at_offset  sR     $&&;<<+T]]++  -  r   r  r0   c                     [        U R                  [        5      (       d  [        U R                  5      U l        X R                  X[        U5      -   & g r   )rU  rt  ri  rF  r  s      r   r  PE.set_data_bytes  s8    $--33%dmm4DM59fD	12r   c                 P   U R                    H  nU R                  UR                  U R                  R                  5      nX!R
                  -   nU[        U R                  5      :  d  M\  U[        U R                  5      :  d  Mw  U R                  X!R                  5       5        M     g)zUUpdate the PE image content with any individual section data that has been
modified.
N)
r  ri  r]  rj  rk  r_  rF  rt  r  r  )rt  rj  section_data_startsection_data_ends       r   merge_modified_section_dataPE.merge_modified_section_data  s    
 }}G!%!:!:(($*>*>*L*L"  24I4II!C$66;KcO < ##$68H8H8JK %r   c                 F   XR                   R                  -
  n[        U R                   R                  5      S:  Gae  U R                   R                  S   R                  (       Ga;  [        U S5      (       d  U R                  [        S   /S9  [        U S5      (       d  U R                  R                  S5        GO[U R                   GHJ  nSnU[        UR                  5      :  d  M!  UR                  U   nUS-  nUR                  [        S	   :X  a  GOUR                  [        S
   :X  a@  U R                  UR                  U R!                  UR                  5      U-   S-	  S-  5        GOUR                  [        S   :X  a=  U R                  UR                  U R!                  UR                  5      U-   S-  5        GO5UR                  [        S   :X  a9  U R#                  UR                  U R%                  UR                  5      U-   5        OUR                  [        S   :X  a  U[        UR                  5      :X  a  GM|  UR                  U   nUS-  nU R                  UR                  U R!                  UR                  5      S-  UR                  -   U-   S-  S-	  5        OOUR                  [        S   :X  a8  U R'                  UR                  U R)                  UR                  5      U-   5        U[        UR                  5      :  a  GM)  GMM     XR                   l        [        U S5      (       a;  U R*                   H+  nUR,                   H  nU=R.                  U-  sl        M     M-     [        U S5      (       a  U R0                  R2                  =R4                  U-  sl        U R0                  R2                  =R6                  U-  sl        U R0                  R2                  =R8                  U-  sl        U R0                  R2                  =R:                  U-  sl        [        U S5      (       Gaf  U R<                  R2                  n	[        U	S5      (       a&  U	R>                  (       a  U	=R>                  U-  sl        [        U	S5      (       a&  U	R@                  (       a  U	=R@                  U-  sl         [        U	S5      (       a&  U	RB                  (       a  U	=RB                  U-  sl!        [        U	S5      (       a&  U	RD                  (       a  U	=RD                  U-  sl"        [        U	S5      (       a&  U	RF                  (       a  U	=RF                  U-  sl#        [        U	S5      (       a&  U	RH                  (       a  U	=RH                  U-  sl$        [        U	S5      (       a&  U	RJ                  (       a  U	=RJ                  U-  sl%        [        U	S5      (       a&  U	RL                  (       a  U	=RL                  U-  sl&        [        U	S5      (       a&  U	RN                  (       a  U	=RN                  U-  sl'        [        U	S5      (       a&  U	RP                  (       a  U	=RP                  U-  sl(        U RR                  [T        :X  a7  [        U	S5      (       a&  U	RV                  (       a  U	=RV                  U-  sl+        [        U	S 5      (       a&  U	RX                  (       a  U	=RX                  U-  sl,        [        U	S!5      (       a&  U	RZ                  (       a  U	=R\                  U-  sl.        [        U	S"5      (       a&  U	R\                  (       a  U	=R\                  U-  sl.        [        U	S#5      (       a(  U	R^                  (       a  U	=R^                  U-  sl/        g$g$g$g$g$g$)%a  Apply the relocation information to the image using the provided image base.

This method will apply the relocation information to the image. Given the new
base, all the relocations will be processed and both the raw data and the
section's data will be fixed accordingly.
The resulting image can be retrieved as well through the method:

    get_memory_mapped_image()

In order to get something that would more closely match what could be found in
memory once the Windows loader finished its work.
rI   rG   r<  rF   r}  zZRelocating image but PE does not have (or pefile cannot parse) a DIRECTORY_ENTRY_BASERELOCr   r9   r   r   r6   r  r   r   r   rB  r   r  rW  r@  LockPrefixTableEditListSecurityCookieSEHandlerTableGuardCFCheckFunctionPointerGuardCFDispatchFunctionPointerGuardCFFunctionTableGuardAddressTakenIatEntryTableGuardLongJumpTargetTableDynamicValueRelocTableCHPEMetadataPointerGuardRFFailureRoutine$GuardRFFailureRoutineFunctionPointer(GuardRFVerifyStackPointerFunctionPointerEnclaveConfigurationPointerN)0rj  r  rF  r  r)  r}  rJ  r  r  r  r<  rP  rF  r`  r  r  r  r  r  r  r  r  r  r  rW  rG  StartAddressOfRawDataEndAddressOfRawDataAddressOfIndexAddressOfCallBacksr@  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  )
rt  new_ImageBaserelocation_differencerk  	entry_idxr  
next_entryr  funcload_configs
             r   r  PE.relocate_image  sk    !.0D0D0N0N N $$3349$$33A6;;;4!<==++!01R!S T ,  4!<==&&9
 ";;E !"I#c%--&88 %i 8!Q	 ::9S)TT "ZZ?;Q+RR !00 %		$($8$8$C&;%<')%* #)!) #ZZ?;P+QQ !00 %		$($8$8$C&;%< #)	!) #ZZ?;T+UU
 !11 %		 $ 5 5eii @"7!8 #ZZ?;T+UU  )C,>> %).y)AJ%NI 00 %		%)%9%9%))%D%J&0nn%5&;%< '1%1
 $&!&	 #ZZ?;R+SS !11 %		 $ 5 5eii @"7!8c $c%--&888 <| .;  * t56666C #(== !, 7 t233((//EE)E ((//CC)C ((//>>BWW>((//BB)B t:;;">>EEK):;;#33//3HH/;
338L8L((,AA(K)9::#22..2GG.K)9::#22..2GG.K)FGG#??;;?TT;K)IJJ#BB>>BWW>K)?@@#88448MM4K)IJJ#BB>>BWW>K)CDD#<<88<QQ8K)ABB#::66:OO6LL$AA-BCC#77337LL3K)@AA#99559NN5K)OPP#HHHH-H K)STT#LLHH-H K)FGG#??;;?TT; @ HW <y < :r   c                 P    U R                   R                  U R                  5       :H  $ r   )rj  CheckSumgenerate_checksumr{  s    r   verify_checksumPE.verify_checksum  s"    ##,,0F0F0HHHr   c                    U R                  5       U l        U R                  R                  5       S-   nSn[	        U R                  5      S-  n[	        U R                  5      SU-
  US:g  -  -   n[        [        US-  5      5       H  nU[        US-  5      :X  a  M  US-   [        US-  5      :X  a;  U(       a4  [        R                  " SU R                  US-  S  SSU-
  -  -   5      S   nO0[        R                  " SU R                  US-  US-  S-    5      S   nX&-  nUS:  d  M  US-  US	-	  -   nM     US
-  US-	  -   nX"S-	  -   nUS
-  nU[	        U R                  5      -   $ )Nrc   r   rE   r9   r  rA  r  r  r5   r  r6   )	rS  rt  rj  r  rF  r  r%   rG  rH  )rt  checksum_offsetr1  	remainderdata_lenrN  r  s          r   r  PE.generate_checksum  st    

 ..>>@4G &*	t}}%!i-IN)KLs8a<()AC!+,,1uX\*+	q1uw/5A	M3JK c4==QQ+KLQOH5 $z1h"nE * v%(b.9R0f$ #dmm,,,r   c                     [         S   nU R                  5       (       d2  U R                  5       (       d  XR                  R                  -  U:X  a  gg)zCheck whether the file is a standard executable.

This will return true only if the file has the IMAGE_FILE_EXECUTABLE_IMAGE flag
set and the IMAGE_FILE_DLL not set and the file does not appear to be a driver
either.
r]   TF)r  is_dllrf  r   rz  )rt  EXE_flags     r   is_exe	PE.is_exe  sF     ))FG ^^%%,,<<<Ir   c                 P    [         S   nXR                  R                  -  U:X  a  gg)ztCheck whether the file is a standard DLL.

This will return true only if the image has the IMAGE_FILE_DLL flag set.
rm   TF)r  r   rz  )rt  DLL_flags     r   r	  	PE.is_dll  s,     ))9:''777HDr   c                 N   [        U S5      (       d  U R                  [        S   /S9  [        U S5      (       d  g[        S5      nUR	                  U R
                   Vs/ s H  o"R                  R                  5       PM     sn5      (       a  g[        S5      nUR	                  U R                   Vs/ s H+  oDR                  R                  5       R                  S5      PM-     sn5      (       a+  U R                  R                  [        S	   [        S
   4;   a  ggs  snf s  snf )zCheck whether the file is a Windows driver.

This will return true only if there are reliable indicators of the image
being a driver.
r  r?   r  F)s   ntoskrnl.exes   hal.dlls   ndis.syss   bootvid.dlls	   kdcom.dllT)s   pages   pagedrA  r   r   )r}  rJ  r  rI  intersectionr  r  r  r  re  rK  rj  	SubsystemSUBSYSTEM_TYPE)rt  system_DLLsr  driver_like_section_namesrj  s        r   rf  PE.is_driver  s   , t566'',-KLM (  t566 T
 ##(,(C(CD(CWW]]_(CD
 
 $'(;$<!$11AEOg\\!((1O
 
   **78?@ ! E Ps   #D32D"c                 |  ^ Sm[        U R                  5      4U4S jjn[        U S5      (       a6  U" U R                  R	                  5       U R
                  R                  45      mU R                   H!  nU" UR                  UR                  45      mM#     [        S   /n[        U R                  R                  5       H:  u  pEXC;   a  M   U" U R                  UR                  5      UR                  45      mM<     [        U R                  5      [#        T5      :  a  [#        T5      $ g! [          a     My  f = f)zgGet the offset of data appended to the file and not contained within
the area described in the headers.r  c                 Z   > [        U 5      U::  a  [        U 5      [        T5      :  a  U $ T$ r   )sum)offset_and_size	file_sizelargest_offset_and_sizes     r   'update_if_sum_is_larger_and_within_fileQPE.get_overlay_data_start_offset.<locals>.update_if_sum_is_larger_and_within_file,  s7     ?#y0S5IC'M 6 '&**r   rj  rD   N)rF  rt  r}  rj  r  r   r  r  r]  r_  r  r)  r  r  r^  r)  r  r	  )rt  r	  rj  skip_directoriesr+  r  r	  s         @r   get_overlay_data_start_offset PE.get_overlay_data_start_offset&  s/    #) (+4=='9	+ 4*++&M((88:$$99'# }}G&M))7+@+@A'# %
 ,,LMN'(<(<(K(KLNC&*Q--i.F.FGX+'	 M t}}$; <<.// ! s   -D--
D;:D;c                 H    U R                  5       nUb  U R                  US $ g)z]Get the data appended to the file and not contained within the area described
in the headers.Nr	  rt  rt  overlay_data_offsets     r   get_overlayPE.get_overlayT  s/     #@@B*==!4!566r   c                 d    U R                  5       nUb  U R                  SU $ U R                  SS $ )zKReturn the just data defined by the PE headers, removing any overlaid data.Nr 	  r!	  s     r   trimPE.trim_  s;     #@@B*==!5"566}}Qr   c                     U[         :  aD  U R                  SL a5  [        U5      (       d%  U R                  R	                  SU-  5        SU l        [        X5      $ )NFz=If FileAlignment > 0x200 it should be a power of 2. Value: %xT)r$   r  rf  r  r  r(   )rt  r&   r'   s      r   ri  PE.adjust_FileAlignmentv  sU    ::))U2<;W;W&&S%' .2*)#>>r   c                     U[         :  a:  X2:w  a5  U R                  SL a&  U R                  R                  SX24-  5        SU l        [	        XU5      $ )NFzAIf FileAlignment(%x) < 0x200 it should equal SectionAlignment(%x)T)r$   r  r  r  r,   )rt  r&   r+   r'   s       r   ro  PE.adjust_SectionAlignment  sV    ::311U:&&W%9: 15-,S^TTr   )r  r   r  rO  r  rj  r  rK  r  r  r  rt  __from_file__resource_size_limit_reached!__resource_size_limit_upperboundsr  __total_import_symbols__total_resource_bytes__total_resource_entries_count
__warningsr  r  r  r  r  r  r  r   )NFF)r   Nr   NFr   )NF)r   N)r   N)r   N)r  )NrH  )r~  r  r  r  r  r  r  r  r  r  r  rd  r  r  r|  r  rC  r>  rM  rQ  rR  rT  rW  rX  r   r  r  r  r  r  r  r  r  r  r  r  r  r  r  &__IMAGE_DYNAMIC_RELOCATION_V2_format__(__IMAGE_DYNAMIC_RELOCATION64_V2_format__r  r  r  MAX_SYMBOL_EXPORT_COUNTrq  r  r  r  r  r  r  r<  r  rD  r  rS  r  rJ  rz  ry  rv  rw  r  ru  r  r  r  rt  rs  r  r  r!  rr  rP  r  rx  r  r  r  rq  r  r  r  r  r  r  r  r  r  r)  r  r  r  r  r  r=  rA  rD  r9  rW  r  r  r  r  r  r  r$  r  rY  r  r  r  r1  r  r  r  r  r  r  r  r&  r%   rW  r  r  r  r  r  r	  r	  rf  r	  r#	  r&	  ri  ro  r  re  r   r   rS  rS  	  s{   BP#2$ '#
"($H!*&F #I'# 0,	*&)%"
+'1-
,(
!
#&!
 VKEN#
%!
($($
.*
C?
	B>>:

&"
($/.*b00,d1-
+'
-)
.*
0,
0,
,(
# 24l
,,n`Ob	 $.5nL^ TY`4DXtun*83
jX#t@
;z*X&PiVu'n	0>s$jG
RH
kZ?.;`B2h^  ] ~BHGR!8 'D->** 1B 	P)9V  :1]~cL	5K
OKQ'K
NIO'K
OKQ6"":S : :L\U|I.-`&8t,\	 .
?$Ur   rS  c                      SS K n SnU R                  SS  (       d  [        U5        g U R                  S   S:X  a  U R                  SS  (       d  U R                  S5        [	        U R                  S   5      nUR
                  R                   HN  n[        [        UR                  R                  UR                  -   5      UR                  UR                  5        MP     g [        [	        U R                  S   5      R                  5       5        g )Nr   z1pefile.py <filename>
pefile.py exports <filename>r9   r  rA   zerror: <filename> required)r  argvrB  exitrS  r  r{  rn  rj  r  r  r
  r  r9  )r  usager  r  s       r   mainr;	    s     E 88AB<e	!		!xx|HH12_,,44CB&&003;;>?3;; 5
 	b!o'')*r   __main__)re   FFr3	  )r  
__author____version____contact__r  r  rG  rj  rG  r  r  r  r  r   typingr   hashlibr   r   r   r	   r   r   r   r  register_errorlookup_errorr%   rF  r   r(   r,   r1   r  r  r  r  r  r  rc  r  r  r6	  r  r  r  r  r  r  r   IMAGE_NUMBEROF_DIRECTORY_ENTRIESr  r  r  r  r=   directory_entry_typesr  image_characteristicsr  section_characteristicsr|  debug_typesr_  subsystem_typesr	  machine_typesr  relocation_typesr`  dll_characteristicsr  r$   r  r  	registersr  r  r  r  r  r  r  r$  r  r  r  resource_typer  r  r<  r  r>  r:   rB  r@  r  rC  rR  r\  rc  rf  rI  rI  rn  r  r  r  r  r  r  r  r  r[  r  r  r  r  r  r,  r.  r7  r:  r<  r>  r@  rB  rD  rM  rO  rQ  rS  rU  rW  r  r  r  r  r  r  r  r  r  r  r#  rp  ascii_lowercaseascii_uppercaser  rG  rJ  rN  rV  rW  ri  boolrQ  rS  r;	  r~  re  r   r   <module>rR	     s)    
%  	                   )6+>+>?Q+R S

" 4& & 4  	
             !        #%   )    % 7 ( 45 & %%:; / b ''>? * +&
" o.%N M*  /0 $ ##67 !& 
  !!23 	& #	         0 ]+_B DhT w
 w-#*L-%%l3".	 $+8.'/*R RH HV I  &; &;T 
	
	
	
	
	
	
	
	
	
	
	
	
	
	
 & 4+ + 4d#( $(VF FREy EP 4e$NX %NXbi,Y i,X% %] ?" ?"DCM C." ."bm = *M  M  "] "Bm - &]  ] i$' i$XO O28N 8;~ ;8;~ ; 
. 
&XN X&Y~ Y&XN X&Y~ Y&Hn H=
 =
@
 
D 
mm    - 
V333fmmC 
 4FK
S%"#
?C
	
 
@VU @VUFl+* zF r   