-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathpe_mapper.cpp
More file actions
232 lines (207 loc) · 5.24 KB
/
Copy pathpe_mapper.cpp
File metadata and controls
232 lines (207 loc) · 5.24 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include "pe_mapper.hpp"
#include <fstream>
PEMemoryMapper::PEMemoryMapper(const std::string& path)
{
/* Open file */
this->binary_.open(path, std::ios::binary);
/* Parse optional header */
IMAGE_DOS_HEADER dos_header;
binary_.seekg(0);
binary_.read(reinterpret_cast<char*>(&dos_header), sizeof(dos_header));
IMAGE_NT_HEADERS nt_headers;
binary_.seekg(dos_header.e_lfanew);
binary_.read(reinterpret_cast<char*>(&nt_headers), sizeof(nt_headers));
/* Set base address */
base_address_ = nt_headers.OptionalHeader.ImageBase;
/* Set memory size */
memory_size_ = nt_headers.OptionalHeader.SizeOfImage;
this->memory_.resize(memory_size_, 0);
binary_.seekg(0);
this->map_sections();
}
std::vector<uint8_t> PEMemoryMapper::read_from_va(uint64_t va, size_t size) const
{
uint64_t offset = va - this->base_address_;
if (offset + size > this->memory_size_)
{
return { };
}
return std::vector<uint8_t>(this->memory_.begin() + offset, this->memory_.begin() + offset + size);
}
void PEMemoryMapper::write_to_va(uint64_t va, const std::vector<uint8_t>& data)
{
uint64_t offset = va - this->base_address_;
if (offset + data.size() > this->memory_size_)
{
return;
}
std::copy(data.begin(), data.end(), this->memory_.begin() + offset);
}
bool PEMemoryMapper::is_va_mapped(uint64_t va) const
{
uint64_t offset = va - this->base_address_;
if (offset >= this->memory_.size())
{
return false;
}
return true;
}
uint8_t* PEMemoryMapper::get_data_pointer(uint64_t va)
{
uint64_t offset = va - this->base_address_;
if (offset >= this->memory_.size())
{
return nullptr;
}
return this->memory_.data() + offset;
}
std::vector<uint8_t>& PEMemoryMapper::get_memory()
{
return this->memory_;
}
uintptr_t PEMemoryMapper::sigscan(const char* pattern)
{
auto base = this->memory_.data();
std::string signature = this->hex_to_bytes(pattern);
u_char first = static_cast<u_char>(signature.at(0));
u_char* end = (base + memory_size_) - signature.length();
for (; base < end; ++base)
{
if (*base != first)
continue;
u_char* bytes = base;
auto sig = (u_char*)signature.c_str();
for (; *sig; ++sig, ++bytes)
{
if (*sig == '?')
continue;
if (*bytes != *sig)
goto end;
}
return (uintptr_t)base;
end:;
}
return NULL;
}
std::string PEMemoryMapper::hex_to_bytes(std::string hex)
{
std::string bytes;
std::erase_if(hex, isspace);
for (uint32_t i = 0; i < hex.length(); i += 2)
{
if (static_cast<u_char>(hex[i]) == '?')
{
bytes += '?';
i -= 1;
continue;
}
u_char byte = static_cast<u_char>(std::strtol(hex.substr(i, 2).c_str(), nullptr, 16));
bytes += byte;
}
return bytes;
}
void PEMemoryMapper::map_sections()
{
// Read DOS Header
IMAGE_DOS_HEADER dos_header = { };
if (!read_struct(dos_header, 0))
{
return;
}
if (dos_header.e_magic != IMAGE_DOS_SIGNATURE)
{
return;
}
// Read NT Headers
DWORD pe_offset = dos_header.e_lfanew;
DWORD signature = 0;
if (!read_struct(signature, pe_offset))
{
return;
}
if (signature != IMAGE_NT_SIGNATURE)
{
return;
}
// Read File Header
IMAGE_FILE_HEADER file_header = { };
if (!read_struct(file_header, pe_offset + sizeof(DWORD)))
{
return;
}
// Read and verify Optional Header
size_t optional_header_offset = pe_offset + sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER);
WORD magic = 0;
if (!read_struct(magic, optional_header_offset))
{
return;
}
// Handle PE32 vs PE32+
if (magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)
{
IMAGE_OPTIONAL_HEADER32 optional_header = { };
if (!read_struct(optional_header, optional_header_offset))
{
return;
}
base_address_ = optional_header.ImageBase;
memory_size_ = optional_header.SizeOfImage;
}
else if (magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
{
IMAGE_OPTIONAL_HEADER64 optional_header = { };
if (!read_struct(optional_header, optional_header_offset))
{
return;
}
base_address_ = optional_header.ImageBase;
memory_size_ = optional_header.SizeOfImage;
}
else
{
return;
}
// Allocate memory with proper size checks
if (memory_size_ == 0 || memory_size_ > MAX_REASONABLE_SIZE)
{
return;
}
memory_.resize(memory_size_, 0);
// Calculate section headers offset
size_t section_headers_offset = pe_offset +
sizeof(DWORD) +
sizeof(IMAGE_FILE_HEADER) +
file_header.SizeOfOptionalHeader;
// Map each section
for (int i = 0; i < file_header.NumberOfSections; i++)
{
IMAGE_SECTION_HEADER section_header = { };
if (!read_struct(section_header, section_headers_offset + (i * sizeof(IMAGE_SECTION_HEADER))))
{
return;
}
// Validate section header
if (section_header.VirtualAddress >= memory_size_ ||
section_header.Misc.VirtualSize > memory_size_ ||
section_header.VirtualAddress + section_header.Misc.VirtualSize > memory_size_)
{
return;
}
// Calculate correct sizes
size_t virtual_size = section_header.Misc.VirtualSize;
if (virtual_size == 0)
{
virtual_size = section_header.SizeOfRawData;
}
size_t copy_size = min(static_cast<size_t>(section_header.SizeOfRawData), virtual_size);
// Validate raw data
if (section_header.PointerToRawData + copy_size > get_file_size())
{
return;
}
// Copy section data
binary_.seekg(section_header.PointerToRawData);
binary_.read(reinterpret_cast<char*>(memory_.data() + section_header.VirtualAddress),
copy_size);
}
}