-
Notifications
You must be signed in to change notification settings - Fork 28
/
PortableExecutable.cpp
155 lines (122 loc) · 3.81 KB
/
PortableExecutable.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
#include "PortableExecutable.h"
#include "Log.h"
#include <algorithm>
#include <cassert>
#include <share.h>
// address without the image base!
DWORD PortableExecutable::VirtualToRaw(DWORD const dwAddress) const noexcept
{
for(auto const& uSection : vecPESections)
{
auto const dwDifference = dwAddress - uSection.VirtualAddress;
if(dwDifference < uSection.SizeOfRawData) {
return uSection.PointerToRawData + dwDifference;
}
}
return 0;
}
bool PortableExecutable::ReadFile()
{
auto const pFile = this->Handle.get();
// dos header
fseek(pFile, 0, SEEK_SET);
fread(&uDOSHeader, sizeof(IMAGE_DOS_HEADER), 1, pFile);
if(uDOSHeader.e_magic != IMAGE_DOS_SIGNATURE) {
return false;
}
// pe header
fseek(pFile, uDOSHeader.e_lfanew, SEEK_SET);
fread(&uPEHeader, sizeof(IMAGE_NT_HEADERS), 1, pFile);
if(uPEHeader.Signature != IMAGE_NT_SIGNATURE) {
return false;
}
// sections
vecPESections.resize(uPEHeader.FileHeader.NumberOfSections);
fread(&vecPESections[0], sizeof(IMAGE_SECTION_HEADER), vecPESections.size(), pFile);
// imports
auto const& Imports = uPEHeader.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT];
if(Imports.Size) {
fseek(pFile, static_cast<long>(VirtualToRaw(Imports.VirtualAddress)), SEEK_SET);
for(;;) {
IMAGE_IMPORT_DESCRIPTOR import_desc;
fread(&import_desc, sizeof(IMAGE_IMPORT_DESCRIPTOR), 1, pFile);
if(!import_desc.Characteristics) {
break;
}
vecImports.emplace_back().uDesc = import_desc;
}
}
for(auto& current_import : vecImports) {
constexpr auto Size = 0x100;
char name_buf[Size];
fseek(pFile, static_cast<long>(VirtualToRaw(current_import.uDesc.Name)), SEEK_SET);
fgets(name_buf, Size, pFile);
current_import.Name = name_buf;
// thunks
fseek(pFile, static_cast<long>(VirtualToRaw(current_import.uDesc.FirstThunk)), SEEK_SET);
for(;;) {
IMAGE_THUNK_DATA thunk_data;
fread(&thunk_data, sizeof(IMAGE_THUNK_DATA), 1, pFile);
if(!thunk_data.u1.AddressOfData) {
break;
}
current_import.vecThunkData.emplace_back().uThunkData = thunk_data;
}
auto thunk_addr = reinterpret_cast<IMAGE_THUNK_DATA*>(current_import.uDesc.FirstThunk);
for(auto& thunk : current_import.vecThunkData) {
thunk.Address = reinterpret_cast<DWORD>(thunk_addr++);
thunk.bIsOrdinal = IMAGE_SNAP_BY_ORDINAL(thunk.uThunkData.u1.Ordinal);
if(thunk.bIsOrdinal) {
thunk.Ordinal = IMAGE_ORDINAL(thunk.uThunkData.u1.Ordinal);
} else {
fseek(pFile, static_cast<long>(VirtualToRaw(thunk.uThunkData.u1.AddressOfData)), SEEK_SET);
fread(&thunk.wWord, 2, 1, pFile);
fgets(name_buf, Size, pFile);
thunk.Name = name_buf;
}
}
}
return true;
}
bool PortableExecutable::ReadBytes(
DWORD const dwRawAddress, size_t const Size, void* const Dest) const noexcept
{
auto const pFile = this->Handle.get();
if(!fseek(pFile, static_cast<long>(dwRawAddress), SEEK_SET)) {
return (fread(Dest, Size, 1, pFile) == 1);
}
return false;
}
bool PortableExecutable::ReadCString(
DWORD const dwRawAddress, std::string& Result) const
{
auto const pFile = this->Handle.get();
if(!fseek(pFile, static_cast<long>(dwRawAddress), SEEK_SET)) {
constexpr size_t sz = 0x100;
char tmpBuf[sz];
tmpBuf[0] = 0;
if(fread(tmpBuf, 1, sz, pFile) == sz) {
tmpBuf[sz - 1] = 0;
Result.assign(tmpBuf);
return true;
}
}
return false;
}
IMAGE_SECTION_HEADER const* PortableExecutable::FindSection(
std::string_view const name) const noexcept
{
assert(name.size() <= IMAGE_SIZEOF_SHORT_NAME);
char buffer[IMAGE_SIZEOF_SHORT_NAME] = {};
std::memcpy(buffer, name.data(), name.size());
auto const found = std::find_if(
vecPESections.cbegin(), vecPESections.cend(),
[&buffer](auto const& section) {
return !memcmp(section.Name, buffer, std::size(buffer));
});
if(found == vecPESections.cend()) {
return nullptr;
} else {
return &(*found);
}
}