12 CollectCodeRelocs(BYTE *pe_buffer,
size_t buffer_size, IN
bool _is64bit, OUT std::set<ULONGLONG> &_relocs)
13 : RelocBlockCallback(_is64bit), relocs(_relocs),
14 peBuffer(pe_buffer), bufferSize(buffer_size)
16 codeSec = getCodeSection(peBuffer, bufferSize);
19 virtual bool processRelocField(ULONG_PTR relocField)
21 if (!codeSec)
return false;
23 ULONGLONG reloc_addr = (relocField - (ULONGLONG)peBuffer);
24 const ULONGLONG code_start = codeSec->VirtualAddress;
25 const ULONGLONG code_end = code_start + codeSec->Misc.VirtualSize;
26 const bool is_in_code = (reloc_addr >= code_start) && (reloc_addr < code_end);
27 if (!is64bit && !is_in_code) {
33 ULONGLONG* relocateAddr = (ULONGLONG*)((ULONG_PTR)relocField);
34 rva = (*relocateAddr);
38 DWORD* relocateAddr = (DWORD*)((ULONG_PTR)relocField);
39 rva = ULONGLONG(*relocateAddr);
46 static PIMAGE_SECTION_HEADER getCodeSection(BYTE *peBuffer,
size_t bufferSize)
49 for (
size_t i = 0; i < sec_count; i++) {
52 if (hdr->VirtualAddress == 0 || hdr->SizeOfRawData == 0) {
55 if (hdr->Characteristics & IMAGE_SCN_MEM_EXECUTE) {
63 std::set<ULONGLONG> &relocs;
64 PIMAGE_SECTION_HEADER codeSec;
73 if (moduleSize == 0) {
76 if (moduleSize == 0)
return 0;
79 std::set<ULONGLONG> relocs;
80 CollectCodeRelocs callback(modulePtr, moduleSize, is64, relocs);
84 if (relocs.size() == 0) {
88 PIMAGE_SECTION_HEADER hdr = CollectCodeRelocs::getCodeSection(modulePtr, moduleSize);
92 const ULONGLONG mask = ~ULONGLONG(0xFFFF);
93 std::map<ULONGLONG, size_t>base_candidates;
95 std::set<ULONGLONG>::iterator itr;
97 for (itr = relocs.begin(); itr != relocs.end(); ++itr) {
98 const ULONGLONG guessed_base = (*itr) & mask;
99 std::map<ULONGLONG, size_t>::iterator found = base_candidates.find(guessed_base);
100 if (found == base_candidates.end()) {
101 base_candidates[guessed_base] = 0;
103 base_candidates[guessed_base]++;
105 ULONGLONG most_frequent = 0;
107 std::map<ULONGLONG, size_t>::iterator mapItr;
108 for (mapItr = base_candidates.begin(); mapItr != base_candidates.end(); ++mapItr) {
109 if (mapItr->second >= max_freq) {
110 most_frequent = mapItr->first;
111 max_freq = mapItr->second;
114 for (itr = relocs.begin(); itr != relocs.end(); ++itr) {
115 ULONGLONG first = *itr;
116 ULONGLONG first_base = first & mask;
117 if (first_base > most_frequent) {
120 ULONGLONG delta = most_frequent - first_base;
121 if (delta < moduleSize) {
Functions related to finding a base to which the module was relocated.
ULONGLONG find_base_candidate(IN BYTE *module_ptr, IN size_t module_size)
PIMAGE_SECTION_HEADER get_section_hdr(IN const BYTE *pe_buffer, IN const size_t buffer_size, IN size_t section_num)
DWORD get_image_size(IN const BYTE *payload)
bool process_relocation_table(IN PVOID modulePtr, IN SIZE_T moduleSize, IN RelocBlockCallback *callback)
bool is64bit(IN const BYTE *pe_buffer)
size_t get_sections_count(IN const BYTE *buffer, IN const size_t buffer_size)
Wrappers over various fields in the PE header. Read, write, parse PE headers.
Operating on PE file's relocations table.