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 std::cerr <<
"[!] Cannot fill imports into 64 bit PE via 32 bit loader!\n";
29 IMAGE_THUNK_DATA32* desc =
reinterpret_cast<IMAGE_THUNK_DATA32*
>(origFirstThunkPtr);
30 DWORD* call_via =
reinterpret_cast<DWORD*
>(firstThunkPtr);
33 std::cerr <<
"[!] Cannot fill imports into 32 bit PE via 64 bit loader!\n";
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);
53 std::cout <<
"raw ordinal: " << std::hex << raw_ordinal << std::endl;
59 PIMAGE_IMPORT_BY_NAME by_name = (PIMAGE_IMPORT_BY_NAME)((ULONGLONG)
modulePtr + desc->u1.AddressOfData);
60 LPSTR func_name =
reinterpret_cast<LPSTR
>(by_name->Name);
62 std::cout <<
"name: " << func_name << std::endl;
68 std::cerr <<
"Could not resolve the function!" << std::endl;
72 (*call_via) =
reinterpret_cast<T_FIELD
>(hProc);
94 virtual bool processThunks(LPSTR libName, ULONG_PTR origFirstThunkPtr, ULONG_PTR va)
99 const ULONGLONG module_base =
reinterpret_cast<ULONGLONG
>(this->
vBuf);
100 if (va < module_base) {
103 if (va > module_base + this->
vBufSize) {
126 virtual bool processThunks(LPSTR lib_name, ULONG_PTR origFirstThunkPtr, ULONG_PTR firstThunkPtr)
129 IMAGE_THUNK_DATA64* desc =
reinterpret_cast<IMAGE_THUNK_DATA64*
>(origFirstThunkPtr);
130 ULONGLONG* call_via =
reinterpret_cast<ULONGLONG*
>(firstThunkPtr);
136 IMAGE_THUNK_DATA32* desc =
reinterpret_cast<IMAGE_THUNK_DATA32*
>(origFirstThunkPtr);
137 DWORD* call_via =
reinterpret_cast<DWORD*
>(firstThunkPtr);
143 template <
typename T_FIELD,
typename T_IMAGE_THUNK_DATA>
144 bool processThunks_tpl(LPSTR lib_name, T_IMAGE_THUNK_DATA* desc, T_FIELD* call_via, T_FIELD ordinal_flag)
146 if (call_via ==
nullptr) {
151 const bool is_by_ord = (desc->u1.Ordinal & ordinal_flag) != 0;
155 T_FIELD raw_ordinal = desc->u1.Ordinal & (~ordinal_flag);
156 func =
new ExportedFunc(short_name, IMAGE_ORDINAL64(raw_ordinal));
159 PIMAGE_IMPORT_BY_NAME by_name = (PIMAGE_IMPORT_BY_NAME)((ULONGLONG)
modulePtr + desc->u1.AddressOfData);
160 LPSTR func_name =
reinterpret_cast<LPSTR
>(by_name->Name);
161 WORD ordinal = by_name->Hint;
162 func =
new ExportedFunc(short_name, func_name, ordinal);
178template <
typename T_FIELD,
typename T_IMAGE_THUNK_DATA>
183 T_FIELD *thunks = (T_FIELD*)((ULONGLONG)modulePtr + thunk_addr);
184 T_FIELD *callers = (T_FIELD*)((ULONGLONG)modulePtr + call_via);
186 for (
size_t index = 0;
true; index++) {
187 if (!
validate_ptr(modulePtr, module_size, &callers[index],
sizeof(T_FIELD))) {
190 if (!
validate_ptr(modulePtr, module_size, &thunks[index],
sizeof(T_FIELD))) {
193 if (callers[index] == 0) {
197 LPVOID thunk_ptr = &thunks[index];
198 T_IMAGE_THUNK_DATA* desc =
reinterpret_cast<T_IMAGE_THUNK_DATA*
>(thunk_ptr);
199 if (!
validate_ptr(modulePtr, module_size, desc,
sizeof(T_IMAGE_THUNK_DATA))) {
202 if (desc->u1.Function == NULL) {
205 T_FIELD ordinal_flag = (
sizeof(T_FIELD) ==
sizeof(ULONGLONG)) ? IMAGE_ORDINAL_FLAG64 : IMAGE_ORDINAL_FLAG32;
206 bool is_by_ord = (desc->u1.Ordinal & ordinal_flag) != 0;
208 PIMAGE_IMPORT_BY_NAME by_name = (PIMAGE_IMPORT_BY_NAME)((ULONGLONG)modulePtr + desc->u1.AddressOfData);
209 if (!
validate_ptr(modulePtr, module_size, by_name,
sizeof(IMAGE_IMPORT_BY_NAME))) {
214 if (!callback->processThunks(lib_name, (ULONG_PTR)&thunks[index], (ULONG_PTR)&callers[index])) {
224 bool isAllFilled =
true;
226 std::cout <<
"---IMP---" << std::endl;
228 const bool is64 =
is64bit((BYTE*)modulePtr);
229 IMAGE_IMPORT_DESCRIPTOR* lib_desc =
nullptr;
231 for (
size_t i = 0;
true; i++) {
232 lib_desc = &first_desc[i];
233 if (!
validate_ptr(modulePtr, module_size, lib_desc,
sizeof(IMAGE_IMPORT_DESCRIPTOR))) {
236 if (lib_desc->OriginalFirstThunk == NULL && lib_desc->FirstThunk == NULL) {
239 LPSTR lib_name = (LPSTR)((ULONGLONG)modulePtr + lib_desc->Name);
244 DWORD call_via = lib_desc->FirstThunk;
245 DWORD thunk_addr = lib_desc->OriginalFirstThunk;
246 if (thunk_addr == NULL) {
247 thunk_addr = lib_desc->FirstThunk;
250 std::cout <<
"Imported Lib: " << std::hex << lib_desc->FirstThunk <<
" : " << std::hex << lib_desc->OriginalFirstThunk <<
" : " << lib_desc->Name << std::endl;
252 size_t all_solved =
false;
264 printf(
"---------\n");
271 if (moduleSize == 0) {
274 if (moduleSize == 0)
return false;
276 IMAGE_DATA_DIRECTORY *importsDir =
get_directory_entry((BYTE*)modulePtr, IMAGE_DIRECTORY_ENTRY_IMPORT);
280 const DWORD impAddr = importsDir->VirtualAddress;
281 IMAGE_IMPORT_DESCRIPTOR *first_desc = (IMAGE_IMPORT_DESCRIPTOR*)(impAddr + (ULONG_PTR)modulePtr);
282 if (!
peconv::validate_ptr(modulePtr, moduleSize, first_desc,
sizeof(IMAGE_IMPORT_DESCRIPTOR))) {
285 return process_dlls(modulePtr, moduleSize, first_desc, callback);
291 if (moduleSize == 0)
return false;
293 bool is64 =
is64bit((BYTE*)modulePtr);
294 bool is_loader64 =
false;
298 if (is64 != is_loader64) {
299 std::cerr <<
"[ERROR] Loader/Payload bitness mismatch.\n";
304 if (!func_resolver) {
319 char next_char = *lib_name;
320 if (next_char ==
'\0')
break;
322 if (next_char <= 0x20 || next_char >= 0x7E) {
332 IMAGE_DATA_DIRECTORY *importsDir =
get_directory_entry((BYTE*)modulePtr, IMAGE_DIRECTORY_ENTRY_IMPORT);
333 if (importsDir == NULL)
return false;
335 const DWORD impAddr = importsDir->VirtualAddress;
337 IMAGE_IMPORT_DESCRIPTOR* lib_desc = NULL;
338 DWORD parsedSize = 0;
339 size_t valid_records = 0;
342 lib_desc = (IMAGE_IMPORT_DESCRIPTOR*)(impAddr + parsedSize + (ULONG_PTR)modulePtr);
346 parsedSize +=
sizeof(IMAGE_IMPORT_DESCRIPTOR);
348 if (lib_desc->OriginalFirstThunk == NULL && lib_desc->FirstThunk == NULL) {
351 LPSTR lib_name = (LPSTR)((ULONGLONG)modulePtr + lib_desc->Name);
354 DWORD call_via = lib_desc->FirstThunk;
355 DWORD thunk_addr = lib_desc->OriginalFirstThunk;
356 if (thunk_addr == NULL) thunk_addr = lib_desc->FirstThunk;
358 DWORD *thunks = (DWORD*)((ULONGLONG)modulePtr + thunk_addr);
361 DWORD *callers = (DWORD*)((ULONGLONG)modulePtr + call_via);
367 return (valid_records > 0);
#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
virtual FARPROC resolve_func(LPCSTR lib_name, LPCSTR func_name)=0
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.
bool has_valid_import_table(const PBYTE modulePtr, size_t moduleSize)
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)