forked from trailofbits/pe-parse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
378 lines (310 loc) · 11.1 KB
/
main.cpp
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#include <algorithm>
#include <iostream>
#include <limits>
#include <memory>
#include <climits>
#include <cstring>
#include <parser-library/parse.h>
using ParsedPeRef =
std::unique_ptr<peparse::parsed_pe, void (*)(peparse::parsed_pe *)>;
ParsedPeRef openExecutable(const std::string &path) noexcept {
// The factory function does not throw exceptions!
ParsedPeRef obj(peparse::ParsePEFromFile(path.data()),
peparse::DestructParsedPE);
if (!obj) {
return ParsedPeRef(nullptr, peparse::DestructParsedPE);
}
return obj;
}
enum class AddressType {
PhysicalOffset,
RelativeVirtualAddress,
VirtualAddress
};
bool convertAddress(ParsedPeRef &pe,
std::uint64_t address,
AddressType source_type,
AddressType destination_type,
std::uint64_t &result) noexcept {
if (source_type == destination_type) {
result = address;
return true;
}
std::uint64_t image_base_address = 0U;
if (pe->peHeader.nt.FileHeader.Machine == peparse::IMAGE_FILE_MACHINE_AMD64) {
image_base_address = pe->peHeader.nt.OptionalHeader64.ImageBase;
} else {
image_base_address = pe->peHeader.nt.OptionalHeader.ImageBase;
}
struct SectionAddressLimits final {
std::uintptr_t lowest_rva;
std::uintptr_t lowest_offset;
std::uintptr_t highest_rva;
std::uintptr_t highest_offset;
};
auto L_getSectionAddressLimits = [](void *N,
peparse::VA secBase,
std::string &secName,
peparse::image_section_header s,
peparse::bounded_buffer *data) -> int {
static_cast<void>(secBase);
static_cast<void>(secName);
static_cast<void>(data);
SectionAddressLimits *section_address_limits =
static_cast<SectionAddressLimits *>(N);
section_address_limits->lowest_rva =
std::min(section_address_limits->lowest_rva,
static_cast<std::uintptr_t>(s.VirtualAddress));
section_address_limits->lowest_offset =
std::min(section_address_limits->lowest_offset,
static_cast<std::uintptr_t>(s.PointerToRawData));
std::uintptr_t sectionSize;
if (s.SizeOfRawData != 0) {
sectionSize = s.SizeOfRawData;
} else {
sectionSize = s.Misc.VirtualSize;
}
section_address_limits->highest_rva =
std::max(section_address_limits->highest_rva,
static_cast<std::uintptr_t>(s.VirtualAddress + sectionSize));
section_address_limits->highest_offset =
std::max(section_address_limits->highest_offset,
static_cast<std::uintptr_t>(s.PointerToRawData + sectionSize));
return 0;
};
SectionAddressLimits section_address_limits = {
std::numeric_limits<std::uintptr_t>::max(),
std::numeric_limits<std::uintptr_t>::max(),
std::numeric_limits<std::uintptr_t>::min(),
std::numeric_limits<std::uintptr_t>::min()};
IterSec(pe.get(), L_getSectionAddressLimits, §ion_address_limits);
switch (source_type) {
case AddressType::PhysicalOffset: {
if (address >= section_address_limits.highest_offset) {
return false;
}
if (destination_type == AddressType::RelativeVirtualAddress) {
struct CallbackData final {
bool found;
std::uint64_t address;
std::uint64_t result;
};
auto L_inspectSection = [](void *N,
peparse::VA secBase,
std::string &secName,
peparse::image_section_header s,
peparse::bounded_buffer *data) -> int {
static_cast<void>(secBase);
static_cast<void>(secName);
static_cast<void>(data);
std::uintptr_t sectionBaseOffset = s.PointerToRawData;
std::uintptr_t sectionEndOffset = sectionBaseOffset;
if (s.SizeOfRawData != 0) {
sectionEndOffset += s.SizeOfRawData;
} else {
sectionEndOffset += s.Misc.VirtualSize;
}
auto callback_data = static_cast<CallbackData *>(N);
if (callback_data->address >= sectionBaseOffset &&
callback_data->address < sectionEndOffset) {
callback_data->result = s.VirtualAddress + (callback_data->address -
s.PointerToRawData);
callback_data->found = true;
return 1;
}
return 0;
};
CallbackData callback_data = {false, address, 0U};
IterSec(pe.get(), L_inspectSection, &callback_data);
if (!callback_data.found) {
return false;
}
result = callback_data.result;
return true;
} else if (destination_type == AddressType::VirtualAddress) {
std::uint64_t rva = 0U;
if (!convertAddress(pe,
address,
source_type,
AddressType::RelativeVirtualAddress,
rva)) {
return false;
}
result = image_base_address + rva;
return true;
}
return false;
}
case AddressType::RelativeVirtualAddress: {
if (address < section_address_limits.lowest_rva) {
result = address;
return true;
} else if (address >= section_address_limits.highest_rva) {
return false;
}
if (destination_type == AddressType::PhysicalOffset) {
struct CallbackData final {
bool found;
std::uint64_t address;
std::uint64_t result;
};
auto L_inspectSection = [](void *N,
peparse::VA secBase,
std::string &secName,
peparse::image_section_header s,
peparse::bounded_buffer *data) -> int {
static_cast<void>(secBase);
static_cast<void>(secName);
static_cast<void>(data);
std::uintptr_t sectionBaseAddress = s.VirtualAddress;
std::uintptr_t sectionEndAddress =
sectionBaseAddress + s.Misc.VirtualSize;
auto callback_data = static_cast<CallbackData *>(N);
if (callback_data->address >= sectionBaseAddress &&
callback_data->address < sectionEndAddress) {
callback_data->result =
s.PointerToRawData +
(callback_data->address - sectionBaseAddress);
callback_data->found = true;
return 1;
}
return 0;
};
CallbackData callback_data = {false, address, 0U};
IterSec(pe.get(), L_inspectSection, &callback_data);
if (!callback_data.found) {
return false;
}
result = callback_data.result;
return true;
} else if (destination_type == AddressType::VirtualAddress) {
result = image_base_address + address;
return true;
}
return false;
}
case AddressType::VirtualAddress: {
if (address < image_base_address) {
return false;
}
std::uint64_t rva = address - image_base_address;
return convertAddress(pe,
rva,
AddressType::RelativeVirtualAddress,
destination_type,
result);
}
default: {
return false;
}
}
}
int main(int argc, char *argv[]) {
if (argc != 3 || (argc == 2 && std::strcmp(argv[1], "--help") == 0)) {
std::cout << "PE address conversion utility from Trail of Bits\n";
std::cout << "Usage:\n\tpeaddrconv /path/to/executable.exe address\n\n";
std::cout << "The <address> parameter is always interpreted as hex!\n";
return 1;
}
const char *executable_path = argv[1];
const char *address_as_string = argv[2];
char *last_parsed_char = nullptr;
errno = 0;
std::uint64_t address =
std::strtoull(address_as_string, &last_parsed_char, 16);
if (address == 0U && *last_parsed_char != 0) {
std::cout << "Invalid address specified\n";
return 1;
} else if (address == ULLONG_MAX && errno == ERANGE) {
std::cout << "The address you specified is too big\n";
return 1;
}
auto pe = openExecutable(executable_path);
if (!pe) {
std::cout << "Failed to open the executable\n\n";
std::cout << "Error: " << peparse::GetPEErr() << " ("
<< peparse::GetPEErrString() << ")\n";
std::cout << "Location: " << peparse::GetPEErrLoc() << "\n";
return 1;
}
std::uint64_t image_base_address = 0U;
if (pe->peHeader.nt.FileHeader.Machine == peparse::IMAGE_FILE_MACHINE_AMD64) {
image_base_address = pe->peHeader.nt.OptionalHeader64.ImageBase;
} else {
image_base_address = pe->peHeader.nt.OptionalHeader.ImageBase;
}
std::cout << "Image base address: 0x" << std::hex << image_base_address
<< "\n";
std::cout << "Converting address 0x" << std::hex << address << "...\n\n";
std::uint64_t result = 0U;
std::cout << "as Physical offset (off)\n";
std::cout << " to rva:\t";
if (convertAddress(pe,
address,
AddressType::PhysicalOffset,
AddressType::RelativeVirtualAddress,
result)) {
std::cout << "0x" << std::hex << result;
} else {
std::cout << "-";
}
std::cout << "\n";
std::cout << " to va:\t";
if (convertAddress(pe,
address,
AddressType::PhysicalOffset,
AddressType::VirtualAddress,
result)) {
std::cout << "0x" << std::hex << result;
} else {
std::cout << "-";
}
std::cout << "\n\n";
std::cout << "as Relative virtual address (rva)\n";
std::cout << " to off:\t";
if (convertAddress(pe,
address,
AddressType::RelativeVirtualAddress,
AddressType::PhysicalOffset,
result)) {
std::cout << "0x" << std::hex << result;
} else {
std::cout << "-";
}
std::cout << "\n";
std::cout << " to va:\t";
if (convertAddress(pe,
address,
AddressType::RelativeVirtualAddress,
AddressType::VirtualAddress,
result)) {
std::cout << "0x" << std::hex << result;
} else {
std::cout << "-";
}
std::cout << "\n\n";
std::cout << "as Virtual address (va)\n";
std::cout << " to off:\t";
if (convertAddress(pe,
address,
AddressType::VirtualAddress,
AddressType::PhysicalOffset,
result)) {
std::cout << "0x" << std::hex << result;
} else {
std::cout << "-";
}
std::cout << "\n";
std::cout << " to rva:\t";
if (convertAddress(pe,
address,
AddressType::VirtualAddress,
AddressType::RelativeVirtualAddress,
result)) {
std::cout << "0x" << std::hex << result;
} else {
std::cout << "-";
}
std::cout << "\n";
return 0;
}