Parse the imported symbols.
| 254 | |
| 255 | // Parse the imported symbols. |
| 256 | BOOL cobf::parse_imports(PIMAGE_IMPORT_DESCRIPTOR p_imports, size_t imports_size) |
| 257 | { |
| 258 | // Filling up the imported modules array. |
| 259 | while (imports_size >= sizeof(IMAGE_IMPORT_DESCRIPTOR) && p_imports->Name) |
| 260 | { |
| 261 | // Getting the thunk array offset. |
| 262 | DWORD dll_name = p_imports->Name; |
| 263 | DWORD orig_th = p_imports->OriginalFirstThunk; |
| 264 | DWORD first_th = p_imports->FirstThunk; |
| 265 | PSIZE_T p_orig_th, p_first_th; |
| 266 | PCHAR p_dll_name; size_t sym_idx = 0; |
| 267 | |
| 268 | // Convert the pointers. |
| 269 | if ((orig_th && !this->rva_to_ptr(orig_th, (PVOID*)&p_orig_th)) |
| 270 | || !this->rva_to_ptr(first_th, (PVOID*)&p_first_th) |
| 271 | || !this->rva_to_ptr(dll_name, (PVOID*)&p_dll_name)) |
| 272 | { |
| 273 | // Cannot get the thunks or the name. |
| 274 | return FALSE; |
| 275 | }; |
| 276 | |
| 277 | // Get the dll name. |
| 278 | this->pe_mods.push_back({ p_dll_name }); |
| 279 | auto& dll_mod = this->pe_mods.back(); |
| 280 | transform(dll_mod.dll_name.begin(), dll_mod.dll_name.end(), |
| 281 | dll_mod.dll_name.begin(), ::toupper); |
| 282 | |
| 283 | // Loop the imports. |
| 284 | PSIZE_T p_thunk = orig_th ? p_orig_th : p_first_th; |
| 285 | while (*p_thunk) |
| 286 | { |
| 287 | // Needed attributes. |
| 288 | DWORD fth_rva = first_th + (DWORD)sym_idx * sizeof(size_t); |
| 289 | DWORD oth_off = (DWORD)((PBYTE)p_thunk - this->pe_rawf.data()); |
| 290 | |
| 291 | // Try to insert it. |
| 292 | if (!this->insert_import(this->pe_mods.back(), dll_name, *p_thunk, fth_rva, oth_off)) |
| 293 | return FALSE; |
| 294 | |
| 295 | // Calculate the next one. |
| 296 | p_thunk++; |
| 297 | sym_idx++; |
| 298 | }; |
| 299 | |
| 300 | // Move to the next module. |
| 301 | imports_size -= sizeof(IMAGE_IMPORT_DESCRIPTOR); |
| 302 | p_imports++; |
| 303 | }; |
| 304 | |
| 305 | // Done. |
| 306 | return TRUE; |
| 307 | }; |
| 308 | |
| 309 | // Insert a parsed import. |
| 310 | BOOL cobf::insert_import(cmod& dll_mod, DWORD dll_off, size_t th_sym, DWORD fth_rva, DWORD oth_off) |
no test coverage detected