-
Notifications
You must be signed in to change notification settings - Fork 28
/
PortableExecutable.h
105 lines (77 loc) · 1.86 KB
/
PortableExecutable.h
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
//A class to parse PE files
#pragma once
#define WIN32_LEAN_AND_MEAN
// WIN32_FAT_AND_STUPID
#include "Handle.h"
#include "Support.h"
#include <string>
#include <string_view>
#include <vector>
#include <windows.h>
struct PEThunkData
{
IMAGE_THUNK_DATA uThunkData;
bool bIsOrdinal;
DWORD Address;
//bIsOrdinal
DWORD Ordinal;
//!bIsOrdinal
std::string Name;
WORD wWord;
};
struct PEImport
{
IMAGE_IMPORT_DESCRIPTOR uDesc;
std::string Name;
std::vector<PEThunkData> vecThunkData;
};
class PortableExecutable
{
private:
std::string Filename;
// basic pe structure;
IMAGE_DOS_HEADER uDOSHeader;
IMAGE_NT_HEADERS uPEHeader;
// sections
std::vector<IMAGE_SECTION_HEADER> vecPESections;
// imports
std::vector<PEImport> vecImports;
FileHandle Handle;
public:
PortableExecutable(std::string_view filename) : Filename(filename) {
if(!Filename.empty()) {
Handle = FileHandle(_fsopen(Filename.c_str(), "rb", _SH_DENYNO));
}
if(!this->Handle || !this->ReadFile()) {
throw_lasterror_or(ERROR_BAD_EXE_FORMAT, Filename);
}
};
auto GetFilename() const noexcept {
return Filename.c_str();
}
// pe
auto const& GetDOSHeader() const noexcept {
return uDOSHeader;
}
auto const& GetPEHeader() const noexcept {
return uPEHeader;
}
// sections
auto const& GetSections() const noexcept {
return vecPESections;
}
auto const& GetImports() const noexcept {
return vecImports;
}
// helpers
auto const& GetImageBase() const noexcept {
return this->GetPEHeader().OptionalHeader.ImageBase;
}
DWORD VirtualToRaw(DWORD dwAddress) const noexcept;
bool ReadBytes(DWORD dwRawAddress, size_t Size, void* Dest) const noexcept;
bool ReadCString(DWORD dwRawAddress, std::string& result) const;
IMAGE_SECTION_HEADER const* FindSection(
std::string_view name) const noexcept;
private:
bool ReadFile();
};