15 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 LOG_ERROR(
"Cannot fill imports into 64-bit PE via 32-bit loader.");
29 IMAGE_THUNK_DATA32* desc =
reinterpret_cast<IMAGE_THUNK_DATA32*
>(origFirstThunkPtr);
30 DWORD* call_via =
reinterpret_cast<DWORD*
>(firstThunkPtr);
33 LOG_ERROR(
"Cannot fill imports into 32-bit PE via 64-bit loader.");
40 template <
typename T_FIELD,
typename T_IMAGE_THUNK_DATA>
41 bool processThunks_tpl(LPSTR lib_name, T_IMAGE_THUNK_DATA* desc, T_FIELD* call_via, T_FIELD ordinal_flag)
47 bool is_by_ord = (desc->u1.Ordinal & ordinal_flag) != 0;
49 FARPROC hProc =
nullptr;
51 T_FIELD raw_ordinal = desc->u1.Ordinal & (~ordinal_flag);
52 LOG_DEBUG(
"raw ordinal: 0x%llx.", (
unsigned long long)raw_ordinal);
53 hProc =
funcResolver->resolve_func(lib_name, MAKEINTRESOURCEA(raw_ordinal));
57 PIMAGE_IMPORT_BY_NAME by_name = (PIMAGE_IMPORT_BY_NAME)((ULONGLONG)
modulePtr + desc->u1.AddressOfData);
58 LPSTR func_name =
reinterpret_cast<LPSTR
>(by_name->Name);
60 hProc = this->
funcResolver->resolve_func(lib_name, func_name);
63 LOG_ERROR(
"Could not resolve the function.");
66 (*call_via) =
reinterpret_cast<T_FIELD
>(hProc);
88 virtual bool processThunks(LPSTR libName, ULONG_PTR origFirstThunkPtr, ULONG_PTR va)
93 const ULONGLONG module_base =
reinterpret_cast<ULONGLONG
>(this->
vBuf);
94 if (va < module_base) {
97 if (va >= module_base + this->
vBufSize) {
120 virtual bool processThunks(LPSTR lib_name, ULONG_PTR origFirstThunkPtr, ULONG_PTR firstThunkPtr)
123 IMAGE_THUNK_DATA64* desc =
reinterpret_cast<IMAGE_THUNK_DATA64*
>(origFirstThunkPtr);
124 ULONGLONG* call_via =
reinterpret_cast<ULONGLONG*
>(firstThunkPtr);
130 IMAGE_THUNK_DATA32* desc =
reinterpret_cast<IMAGE_THUNK_DATA32*
>(origFirstThunkPtr);
131 DWORD* call_via =
reinterpret_cast<DWORD*
>(firstThunkPtr);
137 template <
typename T_FIELD,
typename T_IMAGE_THUNK_DATA>
138 bool processThunks_tpl(LPSTR lib_name, T_IMAGE_THUNK_DATA* desc, T_FIELD* call_via, T_FIELD ordinal_flag)
145 const bool is_by_ord = (desc->u1.Ordinal & ordinal_flag) != 0;
149 T_FIELD raw_ordinal = desc->u1.Ordinal & (~ordinal_flag);
150 func =
new ExportedFunc(short_name, IMAGE_ORDINAL64(raw_ordinal));
153 PIMAGE_IMPORT_BY_NAME by_name = (PIMAGE_IMPORT_BY_NAME)((ULONGLONG)
modulePtr + desc->u1.AddressOfData);
154 LPSTR func_name =
reinterpret_cast<LPSTR
>(by_name->Name);
155 WORD ordinal = by_name->Hint;
156 func =
new ExportedFunc(short_name, func_name, ordinal);
172template <
typename T_FIELD,
typename T_IMAGE_THUNK_DATA>
177 T_FIELD *thunks = (T_FIELD*)((ULONGLONG)modulePtr + thunk_addr);
178 T_FIELD *callers = (T_FIELD*)((ULONGLONG)modulePtr + call_via);
180 for (
size_t index = 0;
true; index++) {
181 if (!
validate_ptr(modulePtr, module_size, &callers[index],
sizeof(T_FIELD))) {
184 if (!
validate_ptr(modulePtr, module_size, &thunks[index],
sizeof(T_FIELD))) {
187 if (callers[index] == 0) {
191 LPVOID thunk_ptr = &thunks[index];
192 T_IMAGE_THUNK_DATA* desc =
reinterpret_cast<T_IMAGE_THUNK_DATA*
>(thunk_ptr);
193 if (!
validate_ptr(modulePtr, module_size, desc,
sizeof(T_IMAGE_THUNK_DATA))) {
196 if (!desc->u1.Function) {
199 T_FIELD ordinal_flag = (
sizeof(T_FIELD) ==
sizeof(ULONGLONG)) ? IMAGE_ORDINAL_FLAG64 : IMAGE_ORDINAL_FLAG32;
200 bool is_by_ord = (desc->u1.Ordinal & ordinal_flag) != 0;
202 PIMAGE_IMPORT_BY_NAME by_name = (PIMAGE_IMPORT_BY_NAME)((ULONGLONG)modulePtr + desc->u1.AddressOfData);
203 if (!
validate_ptr(modulePtr, module_size, by_name,
sizeof(IMAGE_IMPORT_BY_NAME))) {
208 if (!callback->processThunks(lib_name, (ULONG_PTR)&thunks[index], (ULONG_PTR)&callers[index])) {
218 bool isAllFilled =
true;
220 const bool is64 =
is64bit((BYTE*)modulePtr);
221 IMAGE_IMPORT_DESCRIPTOR* lib_desc =
nullptr;
223 for (
size_t i = 0;
true; i++) {
224 lib_desc = &first_desc[i];
225 if (!
validate_ptr(modulePtr, module_size, lib_desc,
sizeof(IMAGE_IMPORT_DESCRIPTOR))) {
228 if (!lib_desc->OriginalFirstThunk && !lib_desc->FirstThunk) {
231 LPSTR lib_name = (LPSTR)((ULONGLONG)modulePtr + lib_desc->Name);
236 DWORD call_via = lib_desc->FirstThunk;
237 DWORD thunk_addr = lib_desc->OriginalFirstThunk;
239 thunk_addr = lib_desc->FirstThunk;
241 LOG_DEBUG(
"Imported Lib: 0x%lx : 0x%lx : 0x%lx", lib_desc->FirstThunk, lib_desc->OriginalFirstThunk, lib_desc->Name);
242 bool all_solved =
false;
259 if (moduleSize == 0) {
262 if (moduleSize == 0)
return false;
264 IMAGE_DATA_DIRECTORY *importsDir =
get_directory_entry((BYTE*)modulePtr, IMAGE_DIRECTORY_ENTRY_IMPORT);
268 const DWORD impAddr = importsDir->VirtualAddress;
269 IMAGE_IMPORT_DESCRIPTOR *first_desc = (IMAGE_IMPORT_DESCRIPTOR*)(impAddr + (ULONG_PTR)modulePtr);
270 if (!
peconv::validate_ptr(modulePtr, moduleSize, first_desc,
sizeof(IMAGE_IMPORT_DESCRIPTOR))) {
273 return process_dlls(modulePtr, moduleSize, first_desc, callback);
279 if (moduleSize == 0)
return false;
281 bool is64 =
is64bit((BYTE*)modulePtr);
282 bool is_loader64 =
false;
286 if (is64 != is_loader64) {
287 LOG_ERROR(
"Loader/Payload bitness mismatch.");
292 if (!func_resolver) {
303 bool is_terminated =
false;
304 for (
size_t i = 0; i < MAX_PATH; i++) {
308 char next_char = *lib_name;
309 if (next_char ==
'\0') {
310 is_terminated =
true;
313 if (next_char < 0x20 || next_char >= 0x7E) {
318 return is_terminated;
322 template <
typename FIELD_T>
323 bool _has_valid_import_table(
const PBYTE modulePtr,
size_t moduleSize,
size_t maxCount = 0)
325 IMAGE_DATA_DIRECTORY* importsDir =
get_directory_entry((BYTE*)modulePtr, IMAGE_DIRECTORY_ENTRY_IMPORT);
326 if (!importsDir)
return false;
328 const DWORD impAddr = importsDir->VirtualAddress;
329 if (impAddr == 0 || impAddr >= moduleSize) {
332 IMAGE_IMPORT_DESCRIPTOR* lib_desc =
nullptr;
333 size_t parsedSize = 0;
334 size_t valid_records = 0;
336 bool is_terminated =
false;
337 while (parsedSize < moduleSize) {
339 if (maxCount != 0 && valid_records >= maxCount) {
342 lib_desc = (IMAGE_IMPORT_DESCRIPTOR*)((ULONG_PTR)impAddr + parsedSize + (ULONG_PTR)modulePtr);
346 parsedSize +=
sizeof(IMAGE_IMPORT_DESCRIPTOR);
348 if (!lib_desc->OriginalFirstThunk && !lib_desc->FirstThunk) {
349 is_terminated =
true;
352 LPSTR lib_name = (LPSTR)((ULONGLONG)modulePtr + lib_desc->Name);
355 DWORD call_via = lib_desc->FirstThunk;
356 DWORD thunk_addr = lib_desc->OriginalFirstThunk;
357 if (!thunk_addr) thunk_addr = lib_desc->FirstThunk;
359 FIELD_T* thunks = (FIELD_T*)((ULONGLONG)modulePtr + thunk_addr);
362 FIELD_T* callers = (FIELD_T*)((ULONGLONG)modulePtr + call_via);
367 return is_terminated && (valid_records > 0);
374 return _has_valid_import_table<ULONGLONG>(modulePtr, moduleSize, maxCount);
376 return _has_valid_import_table<DWORD>(modulePtr, moduleSize, maxCount);
#define MASK_TO_DWORD(val)
CollectImportsCallback(BYTE *_modulePtr, size_t _moduleSize, std::map< DWORD, ExportedFunc * > &_thunkToFunc)
bool processThunks_tpl(LPSTR lib_name, T_IMAGE_THUNK_DATA *desc, T_FIELD *call_via, T_FIELD ordinal_flag)
virtual bool processThunks(LPSTR lib_name, ULONG_PTR origFirstThunkPtr, ULONG_PTR firstThunkPtr)
std::map< DWORD, ExportedFunc * > & thunkToFunc
virtual bool processThunks(LPSTR libName, ULONG_PTR origFirstThunkPtr, ULONG_PTR va)
CollectThunksCallback(BYTE *_vBuf, size_t _vBufSize, std::set< DWORD > &_fields)
std::set< DWORD > & fields
FillImportThunks(BYTE *_modulePtr, size_t _moduleSize, t_function_resolver *func_resolver)
bool processThunks_tpl(LPSTR lib_name, T_IMAGE_THUNK_DATA *desc, T_FIELD *call_via, T_FIELD ordinal_flag)
virtual bool processThunks(LPSTR lib_name, ULONG_PTR origFirstThunkPtr, ULONG_PTR firstThunkPtr)
t_function_resolver * funcResolver
ImportThunksCallback(BYTE *_modulePtr, size_t _moduleSize)
bool process_dlls(BYTE *modulePtr, size_t module_size, IMAGE_IMPORT_DESCRIPTOR *first_desc, IN ImportThunksCallback *callback)
bool process_imp_functions_tpl(BYTE *modulePtr, size_t module_size, LPSTR lib_name, DWORD call_via, DWORD thunk_addr, IN ImportThunksCallback *callback)
Parsing and filling the Import Table.
#define LOG_DEBUG(fmt,...)
#define LOG_ERROR(fmt,...)
bool has_valid_import_table(const PBYTE modulePtr, size_t moduleSize, size_t max_count=0)
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)
DWORD get_image_size(IN const BYTE *payload)
bool collect_thunks(IN BYTE *modulePtr, IN SIZE_T moduleSize, OUT std::set< DWORD > &thunk_rvas)
bool is64bit(IN const BYTE *pe_buffer)
bool is_valid_import_name(const PBYTE modulePtr, const size_t moduleSize, LPSTR lib_name)
IMAGE_DATA_DIRECTORY * get_directory_entry(IN const BYTE *pe_buffer, IN DWORD dir_id, IN bool allow_empty=false)
bool collect_imports(IN BYTE *modulePtr, IN SIZE_T moduleSize, OUT ImportsCollection &collection)
std::string get_dll_shortname(const std::string &str)
bool load_imports(BYTE *modulePtr, t_function_resolver *func_resolver=nullptr)