-
Notifications
You must be signed in to change notification settings - Fork 0
/
PeInfo.cpp
486 lines (435 loc) · 15.3 KB
/
PeInfo.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#include "ExceptionThrow.h"
#include "utils.h"
#include "PeInfo.h"
#include <filesystem>
namespace cchips {
namespace fs = std::filesystem;
const std::map<std::pair<std::size_t, std::string>, FileDetector::file_format> FileDetector::_magic_format_map = {
{{0, "MZ"}, FileDetector::file_format::format_pe},
{{0, "ZM"}, FileDetector::file_format::format_pe},
{{0, "\x7F""ELF"}, FileDetector::file_format::format_elf},
{{0, ":"}, FileDetector::file_format::format_intel_hex},
{{0, "\xFE""\xED""\xFA""\xCE"}, FileDetector::file_format::format_macho}, // Mach-O
{{0, "\xFE""\xED""\xFA""\xCF"}, FileDetector::file_format::format_macho}, // Mach-O
{{0, "\xCE""\xFA""\xED""\xFE"}, FileDetector::file_format::format_macho}, // Mach-O
{{0, "\xCF""\xFA""\xED""\xFE"}, FileDetector::file_format::format_macho}, // Mach-O
{{0, "\xCA""\xFE""\xBA""\xBE"}, FileDetector::file_format::format_macho} // Mach-O fat binary
};
const std::map<std::pair<std::size_t, std::string>, FileDetector::file_format> FileDetector::_unknown_format_map = {
{{0, "\x7""\x1""\x64""\x00"}, FileDetector::file_format::format_unknown}, // a.out
{{0, "PS-X EXE"}, FileDetector::file_format::format_unknown}, // PS-X
{{257, "ustar"}, FileDetector::file_format::format_unknown} // tar
};
const std::map<FileDetector::file_format, std::string> FileDetector::_file_format_map = {
{format_undetectable, "undetectable"},
{format_unknown, "unknown"},
{format_pe, "pe"},
{format_dos, "dos"},
{format_elf, "elf"},
{format_coff, "coff"},
{format_macho, "macho"},
{format_intel_hex, "intel_hex"},
{format_raw, "raw"}
};
byte_array_buffer::int_type byte_array_buffer::underflow()
{
if (current_ == end_)
{
return traits_type::eof();
}
return traits_type::to_int_type(*current_);
}
byte_array_buffer::int_type byte_array_buffer::uflow()
{
if (current_ == end_)
{
return traits_type::eof();
}
return traits_type::to_int_type(*current_++);
}
byte_array_buffer::int_type byte_array_buffer::pbackfail(int_type ch)
{
if (current_ == begin_ || (ch != traits_type::eof() && ch != current_[-1]))
{
return traits_type::eof();
}
return traits_type::to_int_type(*--current_);
}
std::streamsize byte_array_buffer::showmanyc()
{
assert(std::less_equal<const std::uint8_t *>()(current_, end_));
return end_ - current_;
}
std::streampos byte_array_buffer::seekoff(
std::streamoff off,
std::ios_base::seekdir way,
std::ios_base::openmode which)
{
if (way == std::ios_base::beg)
{
current_ = begin_ + off;
}
else if (way == std::ios_base::cur)
{
current_ += off;
}
else if (way == std::ios_base::end)
{
current_ = end_;
}
if (current_ < begin_ || current_ > end_)
{
return -1;
}
return current_ - begin_;
}
std::streampos byte_array_buffer::seekpos(
std::streampos sp,
std::ios_base::openmode which)
{
current_ = begin_ + sp;
if (current_ < begin_ || current_ > end_)
{
return -1;
}
return current_ - begin_;
}
FileDetector::file_format FileDetector::DetectFileFormat(const std::uint8_t* base_address, size_t size)
{
size_t magic_size = 0;
for (const auto &formatMap : { _magic_format_map, _unknown_format_map })
{
for (const auto &item : formatMap)
{
magic_size = (std::max)(magic_size, item.first.first + item.first.second.length());
}
}
std::string magic;
tls_check_struct *tls = check_get_tls();
if (tls) {
tls->active = 1;
if (setjmp(tls->jb) == 0) {
magic.resize(magic_size);
memcpy(&magic[0], base_address, magic_size);
tls->active = 0;
}
else {
//execption occur
tls->active = 0;
return file_format::format_undetectable;
}
}
for (const auto &item : _unknown_format_map)
{
if (HasSubstringOnPosition(magic, item.first.second, item.first.first))
{
return file_format::format_unknown;
}
}
for (const auto &item : _magic_format_map)
{
if (HasSubstringOnPosition(magic, item.first.second, item.first.first))
{
switch (item.second)
{
case file_format::format_pe:
return IsPEHead(base_address, size) ? file_format::format_pe : file_format::format_dos;
default:
return item.second;
}
}
}
return file_format::format_unknown;
}
FileDetector::file_format FileDetector::DetectFileFormat(const std::string& path) {
file_format format = file_format::format_undetectable;
std::vector<std::uint8_t> context_buffer;
if (!LoadFile(path, context_buffer))
return format;
return DetectFileFormat(&context_buffer[0], context_buffer.size());
}
std::string FileDetector::GetFileFormat(const std::uint8_t* base_address, size_t size)
{
FileDetector::file_format format = DetectFileFormat(base_address, size);
return _file_format_map.at(format);
}
std::string FileDetector::GetFileFormat(const std::string& path)
{
std::vector<std::uint8_t> context_buffer;
if (!LoadFile(path, context_buffer))
return _file_format_map.at(file_format::format_undetectable);
return GetFileFormat(&context_buffer[0], context_buffer.size());
}
bool FileDetector::HasSubstringOnPosition(const std::string &str,
const std::string &withWhat, std::string::size_type position)
{
return (position < str.length()) && (str.length() - position >= withWhat.length()) &&
(str.compare(position, withWhat.length(), withWhat) == 0);
}
PeLib::PeFile* FileDetector::ReadImageFile(const std::string& path)
{
if (path.empty()) {
return nullptr;
}
if (!fs::exists(path)) {
return nullptr;
}
if (!fs::is_regular_file(path)) {
return nullptr;
}
PeLib::PeFile32 pefile(path);
// Attempt to read the DOS file header.
if (pefile.readMzHeader() != PeLib::ERROR_NONE)
{
return nullptr;
}
// Verify the DOS header
if (!pefile.mzHeader().isValid())
{
return nullptr;
}
// Read PE header. Note that at this point, we read the header as if
// it was 32-bit PE file.
if (pefile.readPeHeader() != PeLib::ERROR_NONE)
{
return nullptr;
}
if (!pefile.peHeader().isValid())
{
return nullptr;
}
WORD machine = pefile.peHeader().getMachine();
WORD magic = pefile.peHeader().getMagic();
// jk2012-02-20: make the PEFILE32 be the default return value
if ((machine == PeLib::PELIB_IMAGE_FILE_MACHINE_AMD64
|| machine == PeLib::PELIB_IMAGE_FILE_MACHINE_IA64)
&& magic == PeLib::PELIB_IMAGE_NT_OPTIONAL_HDR64_MAGIC)
{
return new PeLib::PeFile64(path);
}
else
{
return new PeLib::PeFile32(path);
}
}
PeLib::PeFile* FileDetector::ReadImageMemory(std::istream& istream)
{
PeLib::PeFile32 pefile(istream, PeLib::PeFileT<32>::loading_memory);
// Attempt to read the DOS file header.
if (pefile.readMzHeader() != PeLib::ERROR_NONE)
{
return nullptr;
}
// Verify the DOS header
if (!pefile.mzHeader().isValid())
{
return nullptr;
}
// Read PE header. Note that at this point, we read the header as if
// it was 32-bit PE file.
if (pefile.readPeHeader() != PeLib::ERROR_NONE)
{
return nullptr;
}
if (!pefile.peHeader().isValid())
{
return nullptr;
}
WORD machine = pefile.peHeader().getMachine();
WORD magic = pefile.peHeader().getMagic();
// jk2012-02-20: make the PEFILE32 be the default return value
if ((machine == PeLib::PELIB_IMAGE_FILE_MACHINE_AMD64
|| machine == PeLib::PELIB_IMAGE_FILE_MACHINE_IA64)
&& magic == PeLib::PELIB_IMAGE_NT_OPTIONAL_HDR64_MAGIC)
{
return new PeLib::PeFile64(istream, PeLib::PeFileT<64>::loading_memory);
}
else
{
return new PeLib::PeFile32(istream, PeLib::PeFileT<32>::loading_memory);
}
}
bool FileDetector::IsPEHead(const std::uint8_t* base_address, size_t size)
{
DWORD signature = 0;
byte_array_buffer ba_buffer(base_address, size);
std::istream istream(&ba_buffer);
tls_check_struct *tls = check_get_tls();
if (tls) {
tls->active = 1;
if (setjmp(tls->jb) == 0) {
std::unique_ptr<PeLib::PeFile> file(ReadImageMemory(istream));
if (!file) return false;
file->readMzHeader();
file->readPeHeader();
switch (file->getBits())
{
case 32:
signature = static_cast<PeLib::PeFileT<32>*>(file.get())->peHeader().getNtSignature();
break;
case 64:
signature = static_cast<PeLib::PeFileT<64>*>(file.get())->peHeader().getNtSignature();
break;
default:;
}
tls->active = 0;
}
else {
//execption occur
tls->active = 0;
return false;
}
}
return signature == _pe_header_signature_def || signature == _pe_header_little_endian_signature_def;
}
bool FileDetector::IsPEHead(const std::string& path)
{
std::vector<std::uint8_t> context_buffer;
if (!LoadFile(path, context_buffer))
return nullptr;
return IsPEHead(&context_buffer[0]);
}
const std::uint8_t* FileDetector::GetEntryPoint(const std::uint8_t* base_address, size_t size)
{
file_format format = DetectFileFormat(base_address, size);
switch (format)
{
case file_format::format_pe:
{
byte_array_buffer ba_buffer(base_address, _default_header_size_def);
std::istream istream(&ba_buffer);
std::unique_ptr<PeLib::PeFile> file(ReadImageMemory(istream));
if (!file) return false;
file->readMzHeader();
file->readPeHeader();
switch (file->getBits())
{
case 32:
return (base_address + static_cast<PeLib::PeFileT<32>*>(file.get())->peHeader().getAddressOfEntryPoint());
case 64:
return (base_address + static_cast<PeLib::PeFileT<64>*>(file.get())->peHeader().getAddressOfEntryPoint());
default:
;
}
return nullptr;
}
break;
case format_dos:
case format_elf:
case format_coff:
case format_macho:
default:
break;
}
return nullptr;
}
const std::uint8_t* FileDetector::GetEntryPoint(const std::string& path)
{
std::vector<std::uint8_t> context_buffer;
if (!LoadFile(path, context_buffer))
return nullptr;
return GetEntryPoint(&context_buffer[0]);
}
const size_t FileDetector::GetSizeOfImage(const std::uint8_t* base_address, size_t size)
{
file_format format = DetectFileFormat(base_address);
switch (format)
{
case file_format::format_pe:
{
byte_array_buffer ba_buffer(base_address, size);
std::istream istream(&ba_buffer);
std::unique_ptr<PeLib::PeFile> file(ReadImageMemory(istream));
if (!file) return false;
file->readMzHeader();
file->readPeHeader();
switch (file->getBits())
{
case 32:
return static_cast<PeLib::PeFileT<32>*>(file.get())->peHeader().getSizeOfImage();
case 64:
return static_cast<PeLib::PeFileT<64>*>(file.get())->peHeader().getSizeOfImage();
default:
;
}
return 0;
}
break;
case format_dos:
case format_elf:
case format_coff:
case format_macho:
default:
break;
}
return 0;
}
const size_t FileDetector::GetSizeOfImage(const std::string& path)
{
std::vector<std::uint8_t> context_buffer;
if (!LoadFile(path, context_buffer))
return 0;
return GetSizeOfImage(&context_buffer[0]);
}
std::unique_ptr<CapInsn> FileDetector::GetAsmInstruction(std::uint8_t* insn_addr)
{
ASSERT(insn_addr);
if (!insn_addr) return 0;
if (!cchips::GetCapstoneImplment().IsValid())
return nullptr;
tls_check_struct *tls = check_get_tls();
if (tls) {
tls->active = 1;
if (setjmp(tls->jb) == 0) {
cs_insn *insn = nullptr;
size_t count =
cs_disasm_ex(cchips::GetCapstoneImplment().GetCapHandle(), reinterpret_cast<const uint8_t*>(insn_addr), 16, reinterpret_cast<uintptr_t>(insn_addr), 1, &insn);
if (count == 0) return nullptr;
if (!insn) return nullptr;
std::unique_ptr<CapInsn> cap_insn = std::make_unique<CapInsn>(insn, count);
return cap_insn;
tls->active = 0;
}
else {
//execption occur
tls->active = 0;
}
}
return nullptr;
}
bool FileDetector::LoadFile(const std::string& path, std::vector<std::uint8_t>& context_buffer)
{
if (path.empty()) {
return false;
}
if (!fs::exists(path)) {
return false;
}
if (!fs::is_regular_file(path)) {
return false;
}
context_buffer.resize(0);
auto filesize = fs::file_size(path);
if (filesize == static_cast<uintmax_t>(-1) || filesize == 0) {
return false;
}
std::ifstream infile;
infile.open(path, std::ios::in | std::ios::binary | std::ios::ate);
if (!infile.is_open()) {
return false;
}
infile.seekg(0, std::ios::beg);
context_buffer.resize(filesize);
if (context_buffer.size() != filesize) {
context_buffer.resize(0);
return false;
}
infile.read((char*)&context_buffer[0], filesize);
auto readed = infile.tellg();
if (!readed) {
context_buffer.resize(0);
return false;
}
return true;
}
} // namespace cchips