forked from vic4key/Vutils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSample.PEFile.h
More file actions
143 lines (113 loc) · 3.17 KB
/
Sample.PEFile.h
File metadata and controls
143 lines (113 loc) · 3.17 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
#pragma once
#include "Sample.h"
#define SEPERATOR() std::tcout << _T("----------------------------------------") << std::endl;
DEF_SAMPLE(PEFile)
{
#ifdef _WIN64
#define PROCESS_NAME _T("x64dbg.exe")
#else // _WIN32
#define PROCESS_NAME _T("x32dbg.exe")
#endif // _WIN64
auto PIDs = vu::NameToPID(PROCESS_NAME);
assert(!PIDs.empty());
vu::CProcess process;
process.Attach(PIDs.back());
auto module = process.GetModuleInformation();
vu::CPEFileT<vu::peX> pe(module.szExePath);
vu::VUResult result = pe.Parse();
if (result != vu::VU_OK)
{
std::tstring s;
if (result == 7)
{
s = _T(" (Used wrong type data for the current PE file format)");
}
if (result == 8)
{
s = _T(" (The curent type data was not supported)");
}
std::tcout << _T("PE -> Parse -> Failure") << vu::LastError() << s << std::endl;
return 1;
}
void* pBase = pe.GetpBase();
if (pBase == nullptr)
{
std::tcout << _T("PE -> GetpBase -> Failure") << vu::LastError() << std::endl;
return 1;
}
SEPERATOR()
auto sections = pe.GetSetionHeaders();
if (sections.size() == 0)
{
std::tcout << _T("PE -> GetSetionHeaderList -> Failure") << vu::LastError() << std::endl;
return 1;
}
for (auto section: sections)
{
printf(
"%+10s %08X %08X %08X %08X\n",
section->Name,
section->PointerToRawData,
section->SizeOfRawData,
section->VirtualAddress,
section->Misc.VirtualSize
);
}
SEPERATOR()
auto pPEHeader = pe.GetpPEHeader();
assert(pPEHeader != nullptr);
std::cout << std::hex << pe.Offset2RVA(0x00113a92) << std::endl;
std::cout << std::hex << pe.RVA2Offset(0x00115292) << std::endl;
auto IIDs = pe.GetImportDescriptors();
assert(!IIDs.empty());
for (auto IID: IIDs)
{
printf("%+20s %08X %08X %08X %08X\n",
((char*)pBase + pe.RVA2Offset(IID->Name)),
IID->Name,
IID->FirstThunk,
IID->OriginalFirstThunk,
IID->Characteristics
);
}
SEPERATOR()
auto modules = pe.GetImportModules();
assert(!modules.empty());
for (auto e: modules)
{
printf("%08X, '%s'\n", e.IIDID, e.Name.c_str());
}
SEPERATOR()
auto functions = pe.GetImportFunctions();
assert(!functions.empty());
// for (auto e : functions)
// {
// auto s = vu::FormatA("IIDID = %08X, Hint = %04X, '%s'", e.IIDID, e.Hint, e.Name.c_str());
// std::cout << s << std::endl;
// }
// SEPERATOR()
auto pModule = pe.FindImportModule("KERNEL32.DLL");
if (pModule != nullptr)
{
printf("%08X, '%s'\n", pModule->IIDID, pModule->Name.c_str());
}
SEPERATOR()
auto pfn = pe.FindImportFunction("GetLastError");
if (pfn != nullptr)
{
printf("%08X, %04X, '%s'\n", pfn->IIDID, pfn->Hint, pfn->Name.c_str());
}
SEPERATOR()
for (const auto& Entry : pe.GetRelocationEntries())
{
auto Value = vu::peX(0);
vu::RPM(process.Handle(), LPVOID(vu::peX(module.modBaseAddr) + Entry.RVA), &Value, sizeof(Value));
#ifdef _WIN64
auto fmt = _T("%llX : %llX -> %llX");
#else // _WIN32
auto fmt = _T("%08X : %08X -> %08X");
#endif // _WIN64
std::tcout << vu::Fmt(fmt, Entry.RVA, Entry.Value, Value) << std::endl;
}
return vu::VU_OK;
}