11 ListImportNames(BYTE* _modulePtr,
size_t _moduleSize, std::map<std::string, DWORD> &name_to_addr)
16 virtual bool processThunks(LPSTR lib_name, ULONG_PTR origFirstThunkPtr, ULONG_PTR firstThunkPtr)
19 IMAGE_THUNK_DATA64* desc =
reinterpret_cast<IMAGE_THUNK_DATA64*
>(origFirstThunkPtr);
20 ULONGLONG* call_via =
reinterpret_cast<ULONGLONG*
>(firstThunkPtr);
23 IMAGE_THUNK_DATA32* desc =
reinterpret_cast<IMAGE_THUNK_DATA32*
>(origFirstThunkPtr);
24 DWORD* call_via =
reinterpret_cast<DWORD*
>(firstThunkPtr);
29 template <
typename T_FIELD,
typename T_IMAGE_THUNK_DATA>
30 bool processThunks_tpl(LPSTR lib_name, T_IMAGE_THUNK_DATA* desc, T_FIELD* call_via, T_FIELD ordinal_flag)
32 DWORD call_via_rva =
static_cast<DWORD
>((ULONG_PTR)call_via - (ULONG_PTR)this->
modulePtr);
33 LOG_DEBUG(
"via RVA: 0x%lx", call_via_rva);
34 bool is_by_ord = (desc->u1.Ordinal & ordinal_flag) != 0;
36 PIMAGE_IMPORT_BY_NAME by_name = (PIMAGE_IMPORT_BY_NAME)((ULONGLONG)
modulePtr + desc->u1.AddressOfData);
37 LPSTR func_name =
reinterpret_cast<LPSTR
>(by_name->Name);
49 std::map<std::string, DWORD> name_to_addr;
53 std::map<std::string, DWORD>::iterator found = name_to_addr.find(
"_CorExeMain");
54 if (found != name_to_addr.end())
return found->second;
56 found = name_to_addr.find(
"_CorDllMain");
57 if (found != name_to_addr.end())
return found->second;
62BYTE*
search_jump(BYTE *buf,
size_t buf_size,
const DWORD cor_exe_main_thunk,
const ULONGLONG img_base)
66 const size_t jmp_size = 2;
67 const BYTE jmp_pattern[jmp_size] = { 0xFF, 0x25 };
69 const size_t arg_size =
sizeof(DWORD);
70 if ((jmp_size + arg_size) > buf_size) {
73 const size_t end_offset = buf_size - (jmp_size + arg_size);
75 for (
size_t i = end_offset;
79 if (buf[i] == jmp_pattern[0] && buf[i + 1] == jmp_pattern[1]) {
80 DWORD* addr = (DWORD*)(&buf[i + jmp_size]);
81 DWORD rva =
static_cast<DWORD
>((*addr) - img_base);
82 if (rva == cor_exe_main_thunk) {
87 LOG_WARNING(
"Mismatch: 0x%lx vs _CorExeMain: 0x%lx", rva, cor_exe_main_thunk);
96 if (!pe_buffer)
return false;
105 LOG_INFO(
".NET payload may require Entry Point correction. Current EP: 0x%lx", (
unsigned long)ep_rva);
110 BYTE* sec_ptr = (BYTE*)((ULONG_PTR)pe_buffer + sec_hdr->VirtualAddress);
116 if (!cor_exe_main_thunk) {
119 BYTE* jump_ptr =
search_jump(sec_ptr, sec_hdr->SizeOfRawData, cor_exe_main_thunk, img_base);
123 size_t offset = (ULONG_PTR)jump_ptr - (ULONG_PTR)pe_buffer;
125 LOG_INFO(
"Found possible Entry Point: 0x%lx", (
unsigned long)offset);
virtual bool processThunks(LPSTR lib_name, ULONG_PTR origFirstThunkPtr, ULONG_PTR firstThunkPtr)
bool processThunks_tpl(LPSTR lib_name, T_IMAGE_THUNK_DATA *desc, T_FIELD *call_via, T_FIELD ordinal_flag)
ListImportNames(BYTE *_modulePtr, size_t _moduleSize, std::map< std::string, DWORD > &name_to_addr)
std::map< std::string, DWORD > & nameToAddr
ImportThunksCallback(BYTE *_modulePtr, size_t _moduleSize)
bool fix_dot_net_ep(BYTE *pe_buffer, size_t pe_buffer_size)
BYTE * search_jump(BYTE *buf, size_t buf_size, const DWORD cor_exe_main_thunk, const ULONGLONG img_base)
DWORD find_corexemain(BYTE *buf, size_t buf_size)
#define LOG_DEBUG(fmt,...)
#define LOG_INFO(fmt,...)
#define LOG_WARNING(fmt,...)
bool update_entry_point_rva(IN OUT BYTE *pe_buffer, IN DWORD ep)
DWORD get_entry_point_rva(IN const BYTE *pe_buffer)
ULONGLONG get_image_base(IN const BYTE *pe_buffer)
bool process_import_table(IN BYTE *modulePtr, IN SIZE_T moduleSize, IN ImportThunksCallback *callback)
bool validate_ptr(IN const void *buffer_bgn, IN size_t buffer_size, IN const void *field_bgn, IN size_t field_size)
PIMAGE_SECTION_HEADER get_section_hdr(IN const BYTE *pe_buffer, IN const size_t buffer_size, IN size_t section_num)
bool is64bit(IN const BYTE *pe_buffer)
Master include for LibPEConv.