Repository navigation
Expand file tree
/
Copy pathRTTI.cpp
More file actions
346 lines (282 loc) · 10.4 KB
/
Copy pathRTTI.cpp
File metadata and controls
346 lines (282 loc) · 10.4 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
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
#include <iostream>
#include "ManualMapper.hpp"
#include "RTTI.hpp"
#include <span>
std::vector<RTTIDumperX64::VTableInfo> RTTIDumperX64::dump()
{
std::vector<VTableInfo> results;
// Manual map the PE file
PEManualMapper mapper(dllPath_);
if (!mapper.load()) {
std::cerr << "Failed to map PE file" << std::endl;
return results;
}
auto baseAddr = mapper.getBase();
auto moduleSize = mapper.getSize();
return processModule(baseAddr, moduleSize, &mapper);
}
std::vector<RTTIDumperX64::VTableInfo> RTTIDumperX64::processModule(uintptr_t base, size_t size, PEManualMapper* mapper)
{
std::vector<VTableInfo> results;
// Find all type descriptors (only classes starting with C)
auto typeDescriptors = findClassTypeDescriptors(base, size);
std::cout << "Found " << std::dec << typeDescriptors.size() << " class type descriptors" << std::endl;
// Find COLs and vtables
auto memory = std::span(reinterpret_cast<uint32_t*>(base),
size / sizeof(uint32_t));
for (const auto& [tdAddr, className] : typeDescriptors) {
// Calculate RVA of type descriptor
uint32_t tdRVA = static_cast<uint32_t>(tdAddr - base);
// Search for COLs referencing this TD
for (size_t i = 0; i < memory.size() - 6; ++i) {
auto potentialCOL = reinterpret_cast<x64::CompleteObjectLocator*>(
&memory[i]);
// Check signature (1 for x64)
if (potentialCOL->signature != 1) continue;
// Check if it references our type descriptor
if (potentialCOL->pTypeDescriptor != tdRVA) continue;
// Validate other fields
if (!isValidCOL(potentialCOL, base, size)) continue;
// Found valid COL
VTableInfo info;
info.className = className;
info.rttiAddr = reinterpret_cast<uintptr_t>(&memory[i]) - base;
// Find all references to this COL
auto vtableRefs = findAllReferences(base, size,
reinterpret_cast<uintptr_t>(&memory[i]));
// Process each vtable reference
for (const auto& vtRef : vtableRefs) {
VTableData vtData;
vtData.vtableAddr = vtRef - base;
// Extract functions from this vtable
vtData.functions = extractVTableFunctions(vtRef, base, size, mapper);
if (!vtData.functions.empty()) {
info.vtables.push_back(vtData);
}
}
// Extract base classes
if (potentialCOL->pClassDescriptor) {
extractBaseClasses(info, potentialCOL->pClassDescriptor, base, size);
}
if (!info.vtables.empty()) {
results.push_back(info);
}
break;
}
}
return results;
}
std::unordered_map<uintptr_t, std::string> RTTIDumperX64::findClassTypeDescriptors(uintptr_t base, size_t size)
{
std::unordered_map<uintptr_t, std::string> descriptors;
// Search for .?AVC pattern (classes only)
auto memory = reinterpret_cast<uint8_t*>(base);
const char* pattern = ".?AVC";
size_t patternLen = 5;
for (size_t i = 0; i <= size - patternLen; ++i) {
if (std::memcmp(memory + i, pattern, patternLen) == 0) {
// Calculate TypeDescriptor address
uintptr_t nameAddr = base + i;
uintptr_t tdAddr = nameAddr - offsetof(x64::TypeDescriptor, name);
// Demangle and store
std::string className = demangleMSVC(
reinterpret_cast<const char*>(nameAddr));
// Filter out lambda and anonymous classes
if (className.find("lambda") == std::string::npos &&
className.find("<unnamed") == std::string::npos) {
descriptors[tdAddr] = className;
}
}
}
return descriptors;
}
std::vector<uintptr_t> RTTIDumperX64::findAllReferences(uintptr_t base, size_t size, uintptr_t targetAddr)
{
std::vector<uintptr_t> refs;
uintptr_t searchValue = targetAddr;
// Search for direct pointers to COL
auto memory = reinterpret_cast<uintptr_t*>(base);
size_t count = size / sizeof(uintptr_t);
for (size_t i = 0; i < count; ++i) {
if (memory[i] == searchValue) {
// Found a reference, the vtable starts after this pointer
uintptr_t vtableAddr = base + (i * sizeof(uintptr_t)) + sizeof(uintptr_t);
// Validate that this looks like a vtable
if (isValidVTable(vtableAddr, base, size)) {
refs.push_back(vtableAddr);
}
}
}
return refs;
}
bool RTTIDumperX64::isValidVTable(uintptr_t vtableAddr, uintptr_t base, size_t size)
{
// A valid vtable should have at least one function pointer
// pointing to executable code within the module
try {
auto firstFunc = *reinterpret_cast<uintptr_t*>(vtableAddr);
// Check if it points within the module
if (firstFunc < base || firstFunc >= base + size) {
return false;
}
// For cross-platform, we just check if it's within bounds
// since we can't use VirtualQuery
return true;
}
catch (...) {
return false;
}
}
std::vector<uintptr_t> RTTIDumperX64::extractVTableFunctions(uintptr_t vtableAddr, uintptr_t base, size_t size, PEManualMapper* mapper)
{
std::vector<uintptr_t> functions;
auto vtablePtr = reinterpret_cast<uintptr_t*>(vtableAddr);
// Read vtable functions until we hit an invalid pointer
for (size_t i = 0; i < 256; ++i) {
try {
uintptr_t funcAddr = vtablePtr[i];
// Check if it's a valid code pointer within module
if (funcAddr < base || funcAddr >= base + size) {
break; // End of vtable
}
// Check if it points to executable section
if (!mapper->isExecutablePage(funcAddr)) {
break;
}
functions.push_back(funcAddr - base); // Store as RVA
}
catch (...) {
break;
}
}
return functions;
}
bool RTTIDumperX64::isValidCOL(x64::CompleteObjectLocator* col, uintptr_t base, size_t size)
{
// Validate RVAs are within module bounds
if (col->pTypeDescriptor >= size) return false;
if (col->pClassDescriptor && col->pClassDescriptor >= size) return false;
if (col->pSelf >= size) return false;
// Validate self-reference
auto selfAddr = base + col->pSelf;
if (selfAddr != reinterpret_cast<uintptr_t>(col)) return false;
return true;
}
void RTTIDumperX64::extractBaseClasses(VTableInfo& info, uint32_t chdRVA, uintptr_t base, size_t size)
{
try {
auto chd = reinterpret_cast<x64::ClassHierarchyDescriptor*>(
base + chdRVA);
if (!chd->pBaseClassArray || chd->numBaseClasses == 0) return;
if (chd->numBaseClasses > 100) return; // Sanity check
auto bcdArray = reinterpret_cast<uint32_t*>(base + chd->pBaseClassArray);
for (uint32_t i = 0; i < chd->numBaseClasses && i < 10; ++i) {
if (bcdArray[i] >= size) continue;
auto bcd = reinterpret_cast<x64::BaseClassDescriptor*>(
base + bcdArray[i]);
if (bcd->pTypeDescriptor && bcd->pTypeDescriptor < size) {
auto td = reinterpret_cast<x64::TypeDescriptor*>(
base + bcd->pTypeDescriptor);
info.baseClasses.push_back(demangleMSVC(td->name));
}
}
}
catch (...) {
// Ignore errors
}
}
std::string RTTIDumperX64::demangleMSVC(std::string_view mangled)
{
std::string result;
size_t startPos = 0;
if (!mangled.empty() && mangled[0] == '.') {
startPos = 1;
}
if (startPos >= mangled.size() || mangled[startPos] != '?') {
return "Invalid mangled name";
}
size_t pos = startPos + 1;
bool isClass = false;
bool isTemplate = false;
std::vector<std::string> parts;
std::string currentPart;
if (pos < mangled.size() && mangled[pos] == 'A') {
pos++;
if (pos < mangled.size() && mangled[pos] == 'V') {
isClass = true;
pos++;
}
}
if (pos < mangled.size() && mangled[pos] == '?') {
pos++;
if (pos < mangled.size() && mangled[pos] == '$') {
isTemplate = true;
pos++;
}
}
while (pos < mangled.size()) {
if (mangled[pos] == '@') {
if (!currentPart.empty()) {
parts.push_back(currentPart);
currentPart.clear();
}
pos++;
if (pos < mangled.size() && mangled[pos] == '@') {
break;
}
}
else if (mangled[pos] == '$') {
while (pos < mangled.size() && mangled[pos] != '@') {
pos++;
}
}
else {
currentPart += mangled[pos];
pos++;
}
}
if (parts.empty() && !currentPart.empty()) {
parts.push_back(currentPart);
}
if (parts.empty()) {
return "Unknown name";
}
result = "";
for (size_t i = 0; i < parts.size(); ++i) {
if (i > 0) {
result += "::";
}
result += parts[i];
}
if (isTemplate) {
size_t templateStart = mangled.find('$', startPos);
if (templateStart != std::string::npos) {
size_t templateEnd = mangled.find('@', templateStart);
if (templateEnd != std::string::npos) {
std::string templateParams;
size_t paramPos = templateStart + 1;
while (paramPos < templateEnd) {
if (mangled[paramPos] == 'V') {
paramPos++;
std::string param;
while (paramPos < templateEnd && mangled[paramPos] != '@') {
param += mangled[paramPos];
paramPos++;
}
if (!templateParams.empty()) {
templateParams += ", ";
}
templateParams += "class " + param;
}
else {
paramPos++;
}
}
if (!templateParams.empty()) {
result += "<" + templateParams + ">";
}
}
}
}
return result;
}