Repository navigation
Expand file tree
/
Copy pathManualMapper.cpp
More file actions
136 lines (109 loc) · 4.55 KB
/
Copy pathManualMapper.cpp
File metadata and controls
136 lines (109 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "ManualMapper.hpp"
#include <iostream>
#include <fstream>
bool PEManualMapper::load()
{
// Read file
std::ifstream file(filePath_, std::ios::binary | std::ios::ate);
if (!file) {
std::cerr << "Failed to open file: " << filePath_ << std::endl;
return false;
}
size_t fileSize = file.tellg();
file.seekg(0);
auto fileBuffer = std::make_unique<uint8_t[]>(fileSize);
file.read(reinterpret_cast<char*>(fileBuffer.get()), fileSize);
file.close();
// Parse DOS header
auto* dosHeader = reinterpret_cast<PE::IMAGE_DOS_HEADER*>(fileBuffer.get());
if (dosHeader->e_magic != PE::IMAGE_DOS_SIGNATURE) {
std::cerr << "Invalid DOS signature" << std::endl;
return false;
}
// Parse NT headers
auto* ntHeaders = reinterpret_cast<PE::IMAGE_NT_HEADERS64*>(
fileBuffer.get() + dosHeader->e_lfanew);
if (ntHeaders->Signature != PE::IMAGE_NT_SIGNATURE) {
std::cerr << "Invalid NT signature" << std::endl;
return false;
}
if (ntHeaders->OptionalHeader.Magic != PE::IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
std::cerr << "Not a 64-bit PE file" << std::endl;
return false;
}
imageSize_ = ntHeaders->OptionalHeader.SizeOfImage;
preferredBase_ = ntHeaders->OptionalHeader.ImageBase;
// Allocate memory for the mapped image
mappedImage_ = new uint8_t[imageSize_]();
mappedBase_ = reinterpret_cast<uintptr_t>(mappedImage_);
// Copy headers
std::memcpy(mappedImage_, fileBuffer.get(),
ntHeaders->OptionalHeader.SizeOfHeaders);
// Map sections
auto* sectionHeader = reinterpret_cast<PE::IMAGE_SECTION_HEADER*>(
reinterpret_cast<uint8_t*>(ntHeaders) + sizeof(PE::IMAGE_NT_HEADERS64));
for (int i = 0; i < ntHeaders->FileHeader.NumberOfSections; ++i) {
if (sectionHeader[i].SizeOfRawData > 0) {
std::memcpy(
mappedImage_ + sectionHeader[i].VirtualAddress,
fileBuffer.get() + sectionHeader[i].PointerToRawData,
sectionHeader[i].SizeOfRawData
);
}
// Mark section characteristics for validation
if (sectionHeader[i].Characteristics & PE::IMAGE_SCN_MEM_EXECUTE) {
// Mark as executable section
for (uint32_t j = sectionHeader[i].VirtualAddress;
j < sectionHeader[i].VirtualAddress + sectionHeader[i].Misc.VirtualSize;
j += 4096) {
if (j < imageSize_) {
executablePages_.insert(j / 4096);
}
}
}
}
// Process relocations if base address changed
if (mappedBase_ != preferredBase_) {
if (!processRelocations(ntHeaders)) {
std::cerr << "Failed to process relocations" << std::endl;
return false;
}
}
std::cout << "PE file loaded successfully" << std::endl;
std::cout << "Image size: " << imageSize_ << " bytes" << std::endl;
std::cout << "Mapped at: 0x" << std::hex << mappedBase_ << std::endl;
return true;
}
bool PEManualMapper::isExecutablePage(uintptr_t addr) const
{
uintptr_t offset = addr - mappedBase_;
return executablePages_.count(offset / 4096) > 0;
}
bool PEManualMapper::processRelocations(PE::IMAGE_NT_HEADERS64* ntHeaders)
{
auto& relocDir = ntHeaders->OptionalHeader.DataDirectory[5]; // IMAGE_DIRECTORY_ENTRY_BASERELOC
if (!relocDir.VirtualAddress || !relocDir.Size) {
return true; // No relocations
}
int64_t delta = static_cast<int64_t>(mappedBase_) - static_cast<int64_t>(preferredBase_);
auto* reloc = reinterpret_cast<PE::IMAGE_BASE_RELOCATION*>(
mappedImage_ + relocDir.VirtualAddress);
auto* relocEnd = reinterpret_cast<PE::IMAGE_BASE_RELOCATION*>(
reinterpret_cast<uint8_t*>(reloc) + relocDir.Size);
while (reloc < relocEnd && reloc->SizeOfBlock) {
uint32_t count = (reloc->SizeOfBlock - sizeof(PE::IMAGE_BASE_RELOCATION)) / sizeof(uint16_t);
auto* typeOffset = reinterpret_cast<uint16_t*>(reloc + 1);
for (uint32_t i = 0; i < count; ++i) {
uint16_t type = (typeOffset[i] >> 12) & 0xF;
uint16_t offset = typeOffset[i] & 0xFFF;
if (type == PE::IMAGE_REL_BASED_DIR64) {
auto* patchAddr = reinterpret_cast<uint64_t*>(
mappedImage_ + reloc->VirtualAddress + offset);
*patchAddr += delta;
}
}
reloc = reinterpret_cast<PE::IMAGE_BASE_RELOCATION*>(
reinterpret_cast<uint8_t*>(reloc) + reloc->SizeOfBlock);
}
return true;
}