62 if (entriesNum == 0) {
67 for (i = 0; i < entriesNum; i++) {
71 DWORD offset = entry->
Offset;
72 DWORD type = entry->
Type;
78 printf(
"[-] Not supported relocations format at %d: %d\n", (
int)i, (
int)type);
82 DWORD reloc_field = page + offset;
83 if (reloc_field >= moduleSize) {
85 printf(
"[-] Malformed field: %lx\n", reloc_field);
92 std::cout <<
"[-] Failed processing reloc field at: " << std::hex << reloc_field <<
"\n";
104 if (relocDir == NULL) {
106 std::cout <<
"[!] WARNING: no relocation table found!\n";
110 if (!
validate_ptr(modulePtr, moduleSize, relocDir,
sizeof(IMAGE_DATA_DIRECTORY))) {
111 std::cerr <<
"[!] Invalid relocDir pointer\n";
114 DWORD maxSize = relocDir->Size;
115 DWORD relocAddr = relocDir->VirtualAddress;
116 bool is64b =
is64bit((BYTE*)modulePtr);
118 IMAGE_BASE_RELOCATION* reloc = NULL;
120 DWORD parsedSize = 0;
121 while (parsedSize < maxSize) {
122 reloc = (IMAGE_BASE_RELOCATION*)(relocAddr + parsedSize + (ULONG_PTR)modulePtr);
123 if (!
validate_ptr(modulePtr, moduleSize, reloc,
sizeof(IMAGE_BASE_RELOCATION))) {
125 std::cerr <<
"[-] Invalid address of relocations\n";
129 if (reloc->SizeOfBlock == 0) {
132 size_t entriesNum = (reloc->SizeOfBlock - 2 *
sizeof(DWORD)) /
sizeof(WORD);
133 DWORD page = reloc->VirtualAddress;
137 std::cerr <<
"[-] Invalid address of relocations block\n";
141 if (!
process_reloc_block(block, entriesNum, page, modulePtr, moduleSize, is64b, callback)) {
146 parsedSize += reloc->SizeOfBlock;
bool process_reloc_block(BASE_RELOCATION_ENTRY *block, SIZE_T entriesNum, DWORD page, PVOID modulePtr, SIZE_T moduleSize, bool is64bit, RelocBlockCallback *callback)