10 ListImportNames(BYTE* _modulePtr,
size_t _moduleSize, std::map<std::string, DWORD> &name_to_addr)
15 virtual bool processThunks(LPSTR lib_name, ULONG_PTR origFirstThunkPtr, ULONG_PTR firstThunkPtr)
18 IMAGE_THUNK_DATA64* desc =
reinterpret_cast<IMAGE_THUNK_DATA64*
>(origFirstThunkPtr);
19 ULONGLONG* call_via =
reinterpret_cast<ULONGLONG*
>(firstThunkPtr);
20 return processThunks_tpl<ULONGLONG, IMAGE_THUNK_DATA64>(lib_name, desc, call_via, IMAGE_ORDINAL_FLAG64);
22 IMAGE_THUNK_DATA32* desc =
reinterpret_cast<IMAGE_THUNK_DATA32*
>(origFirstThunkPtr);
23 DWORD* call_via =
reinterpret_cast<DWORD*
>(firstThunkPtr);
24 return processThunks_tpl<DWORD, IMAGE_THUNK_DATA32>(lib_name, desc, call_via, IMAGE_ORDINAL_FLAG32);
28 template <
typename T_FIELD,
typename T_IMAGE_THUNK_DATA>
29 bool processThunks_tpl(LPSTR lib_name,
const T_IMAGE_THUNK_DATA* desc,
const T_FIELD* call_via,
const T_FIELD ordinal_flag)
31 const DWORD call_via_rva =
static_cast<DWORD
>((ULONG_PTR)call_via - (ULONG_PTR)this->
modulePtr);
32 LOG_DEBUG(
"via RVA: 0x%lx", call_via_rva);
33 const bool is_by_ord = (desc->u1.Ordinal & ordinal_flag) != 0;
35 const PIMAGE_IMPORT_BY_NAME by_name = (PIMAGE_IMPORT_BY_NAME)((ULONGLONG)
modulePtr + desc->u1.AddressOfData);
37 LOG_ERROR(
"Invalid pointer to IMAGE_IMPORT_BY_NAME");
40 const LPSTR func_name =
reinterpret_cast<LPSTR
>(by_name->Name);
42 LOG_ERROR(
"Invalid pointer to function name");
56 std::map<std::string, DWORD> name_to_addr;
60 std::map<std::string, DWORD>::iterator found = name_to_addr.find(
"_CorExeMain");
61 if (found != name_to_addr.end())
return found->second;
63 found = name_to_addr.find(
"_CorDllMain");
64 if (found != name_to_addr.end())
return found->second;
69BYTE*
search_jump(BYTE *buf,
size_t buf_size,
const DWORD cor_exe_main_thunk,
const ULONGLONG img_base)
73 const size_t jmp_size = 2;
74 const BYTE jmp_pattern[jmp_size] = { 0xFF, 0x25 };
76 const size_t arg_size =
sizeof(DWORD);
77 if ((jmp_size + arg_size) > buf_size) {
80 const size_t end_offset = buf_size - (jmp_size + arg_size);
82 for (
size_t i = end_offset;
86 if (buf[i] == jmp_pattern[0] && buf[i + 1] == jmp_pattern[1]) {
87 DWORD* addr = (DWORD*)(&buf[i + jmp_size]);
88 DWORD rva =
static_cast<DWORD
>((*addr) - img_base);
89 if (rva == cor_exe_main_thunk) {
94 LOG_WARNING(
"Mismatch: 0x%lx vs _CorExeMain: 0x%lx", rva, cor_exe_main_thunk);
103 if (!pe_buffer)
return false;
111 LOG_INFO(
".NET payload may require Entry Point correction. Current EP: 0x%lx", (
unsigned long)ep_rva);
116 BYTE* sec_ptr = (BYTE*)((ULONG_PTR)pe_buffer + sec_hdr->VirtualAddress);
122 if (!cor_exe_main_thunk) {
125 BYTE* jump_ptr =
search_jump(sec_ptr, sec_hdr->SizeOfRawData, cor_exe_main_thunk, img_base);
129 size_t offset = (ULONG_PTR)jump_ptr - (ULONG_PTR)pe_buffer;
131 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, const T_IMAGE_THUNK_DATA *desc, const T_FIELD *call_via, const 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_ERROR(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)
bool is_valid_string(LPVOID modulePtr, const size_t moduleSize, const CHAR_T *name_ptr, const size_t max_len=260)
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.