-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathVMTDir.h
222 lines (196 loc) · 4.13 KB
/
VMTDir.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
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
/*
* vmtdir.h - Proof of concept for a Delphi Obfuscator
*
* Copyright (c) 2005 Sebastian Porst ([email protected])
* All rights reserved.
*
* This software is licensed under the zlib/libpng License.
* For more details see http://www.opensource.org/licenses/zlib-license.php
*/
#ifndef VMTPARSER_H
#define VMTPARSER_H
#include "helpers.h"
#include <PeLib.h>
template<typename T>
bool cmpncs(T s1, T s2);
/**
* Stores the property info of a VMT.
**/
struct PropInfo
{
unsigned int PropType;
unsigned int GetProc;
unsigned int SetProc;
unsigned int StoredProc;
int Index;
int Default;
short int NameIndex;
std::string* name;
unsigned int nameoffset;
std::string* type;
unsigned int typeoffset;
PropInfo()
{
name = 0;
type = 0;
}
};
/**
* Stores the method info of a VMT.
**/
struct MethodInfo
{
unsigned short int id;
unsigned int va;
std::string* name;
unsigned int nameoffset;
MethodInfo()
{
name = 0;
}
};
struct FieldInfo
{
unsigned int nameoffset;
std::string* name;
FieldInfo()
{
name = 0;
}
};
/**
* Stores information about a virtual method table.
**/
struct VMT
{
unsigned int offset;
unsigned int nameoffset;
std::string* name;
unsigned int parentvmt;
VMT* parent;
std::vector<VMT*> children;
std::vector<PropInfo> typeinfo;
std::vector<MethodInfo> methods;
std::vector<FieldInfo> fields;
unsigned int vmtSelfPtr;
unsigned int vmtIntfTable;
unsigned int vmtAutoTable;
unsigned int vmtInitTable;
unsigned int vmtTypeInfo;
unsigned int vmtFieldTable;
unsigned int vmtMethodTable;
unsigned int vmtDynamicTable;
unsigned int vmtClassName;
unsigned int vmtInstanceSize;
unsigned int vmtParent;
unsigned int vmtSafeCallException;
unsigned int vmtAfterConstruction;
unsigned int vmtBeforeDestruction;
unsigned int vmtDispatch;
unsigned int vmtDefaultHandler;
unsigned int vmtNewInstance;
unsigned int vmtFreeInstance;
unsigned int vmtDestroy;
VMT()
{
name = 0;
parent = 0;
}
~VMT()
{
for (unsigned int i=0;i<children.size();i++)
{
delete children[i];
}
}
};
typedef std::vector<VMT*> VMTDir;
void readVMTs(PeLib::PeFile32& pefile, VMTDir& vmtparser);
VMT* handleCollections(VMT* vmt, const VMTDir& vmtdir);
std::string* getAttributeType(const VMT* vmt, const std::string& name, const VMTDir& vmtdir);
/**
* Used when searching VMTs by method name.
**/
struct FindByMethodName
{
static VMT* find(VMT* vmt, const std::string& methodname)
{
for (unsigned int i=0;i<vmt->methods.size();i++)
{
if (cmpncs(*vmt->methods[i].name, methodname))
{
return vmt;
}
}
return 0;
}
};
/**
* Used when searching VMTs by property name.
**/
struct FindByPropertyName
{
static VMT* find(VMT* vmt, const std::string& propertyname)
{
for (unsigned int i=0;i<vmt->typeinfo.size();i++)
{
if (*vmt->typeinfo[i].name == propertyname)
{
return vmt;
}
}
return 0;
}
};
/**
* Used when searching VMTs by field name.
**/
struct FindByFieldName
{
static VMT* find(VMT* vmt, const std::string& fieldname)
{
for (unsigned int i=0;i<vmt->fields.size();i++)
{
if (*vmt->fields[i].name == fieldname)
{
return vmt;
}
}
return 0;
}
};
/**
* Searches upwards through the VMT hierarchy for a VMT with a given value.
* @param vmt The VMT where the search begins.
* @param value The value to search for.
* @return The VMT with the given value. If no such VMT was found the
* return value is 0.
**/
template<typename T>
VMT* findVMT(VMT* vmt, const std::string& value)
{
VMT* vmt2 = T::find(vmt, value);
if (vmt2) return vmt2;
else if (vmt->parent) return findVMT<T>(vmt->parent, value);
else return 0;
}
/**
* Searches through a vector and returns the found element.
* @param v The vector.
* @param value The string value of the element to search for.
* @return The found element or 0 if no element was found.
**/
template<typename T>
std::string* getVMTAttribute(const std::vector<T>& v, const std::string& value)
{
for (unsigned int i=0;i<v.size();++i)
{
// std::cout << *v[i].name << std::endl;
if (*v[i].name == value)
{
return v[i].name;
}
}
return 0;
}
#endif