-
Notifications
You must be signed in to change notification settings - Fork 2
/
VTDump.java
225 lines (210 loc) · 6.43 KB
/
VTDump.java
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
//Dump a classes VTable to C++ code you can copy into your class generated with reclass
//@author DisabledMallis, belohnung
//@category Symbol
//@keybinding
//@menupath
//@toolbar
import ghidra.app.script.GhidraScript;
import ghidra.program.model.util.*;
import ghidra.program.model.reloc.*;
import ghidra.program.model.data.*;
import ghidra.program.model.block.*;
import ghidra.program.model.symbol.*;
import ghidra.program.model.scalar.*;
import ghidra.program.model.mem.*;
import ghidra.program.model.listing.*;
import ghidra.program.model.lang.*;
import ghidra.program.model.pcode.*;
import ghidra.program.model.address.*;
import java.util.*;
import java.util.regex.Pattern;
import java.util.List;
public class VTDump extends GhidraScript {
final List<String> funcNames = new ArrayList<String>();
public static final Pattern MOJANG_CLASS_STANDARD = Pattern.compile(".*[A-Z].*");
public static final String BASIC_STRING_TYPE = "basic_string";
public static final String UNDEFINED_FUNCTION = "UndefinedFunc_";
public static final String POINTER_TYPE = "*";
public static final String VOID_POINTER_TYPE = "void" + POINTER_TYPE;
public static final String STD_STRING_POINTER_TYPE = "std::string" + POINTER_TYPE;
public static final String TYPE_PLACEHOLDER = "{TYPE}";
public static final String NAME_PLACEHOLDER = "{NAME}";
public static final String TYPE_NAME_PLACEHOLDER = TYPE_PLACEHOLDER + " " + NAME_PLACEHOLDER;
public static final boolean DEBUG = false;
public String undefinedNearestPrimitive(String undefinedType) {
if(undefinedType.contains("undefined")) {
if(undefinedType.equals("undefined")) {
return "void*";
}
else {
int size = Integer.parseInt(undefinedType.substring(9));
switch(size) {
case 1:
return "uint8_t";
case 2:
return "uint16_t";
case 4:
return "uint32_t";
case 8:
return "uint64_t";
}
}
}
return undefinedType;
}
public Address deref(Address addr) throws Exception {
switch(addr.getPointerSize()) {
case 4:
return addr.getNewAddress(getInt(addr));
case 8:
default:
return addr.getNewAddress(getLong(addr));
}
}
public void run() throws Exception {
Address firstInTable = currentAddress;
if(DEBUG){
println("Beginning of VTable: " + firstInTable);
println("Test: " + deref(firstInTable));
}
Address nextInTable = firstInTable;
int maxIter = 500;
int currentIter = 0;
Set<String> outputArray = new LinkedHashSet<String>();
// Get all func names and gen vtable code
while (getFunctionAt(deref(nextInTable)) != null) {
Function function = getFunctionAt(deref(nextInTable));
if (function == null) {
println("Func is null");
break;
}
if(DEBUG){
println("Current: " + nextInTable + " Func name: " + function.getName());
}
/*
* Return types
*/
String returnType = function.getReturnType().getName().replace(" " + POINTER_TYPE, POINTER_TYPE);
// Sanitize return type
// Make string return types not ugly
if (returnType.contains(BASIC_STRING_TYPE)) {
returnType = STD_STRING_POINTER_TYPE;
}
// if the return type contains a capital, it cannot be primitive and must be a
// class by mojang's standards (or so it seems)
if (MOJANG_CLASS_STANDARD.matcher(returnType).matches()) {
// all classes are returned as pointers, so this must be one, right?
if (!returnType.contains(POINTER_TYPE)) {
returnType += POINTER_TYPE;
}
// Add class definition because it may not be defined already
returnType = "class " + returnType;
}
if(returnType.contains("undefined")) {
returnType = undefinedNearestPrimitive(returnType);
}
/*
* Func name
*/
// Sanitize function name
String funcName = function.getName();
// if funcname contains a @ its mangled, prob dont need it anyway so lets just
// give a generic ass name. Same goes for < and > cuz templates long and ugly
String[] badFuncNames = { "@", "<", ">", "`", "'" };
for (String chr : badFuncNames) {
if (funcName.contains(chr)) {
funcName = UNDEFINED_FUNCTION + currentIter;
}
}
//Check if it is a destructor
if(funcName.startsWith("~")) {
returnType = "";
}
/*
* Parameters
*/
String paramsStr = "";
Parameter[] params = function.getParameters();
int count = 0;
for (Parameter param : params) {
String type = param.getFormalDataType().getName();
if (funcName.contains(UNDEFINED_FUNCTION)) {
type = VOID_POINTER_TYPE;
}
String name = param.getName();
if (count == 0) {
count++;
continue;
}
if (MOJANG_CLASS_STANDARD.matcher(type).matches()) {
if (!type.contains(POINTER_TYPE)) {
type += POINTER_TYPE;
}
type = "class " + type;
}
type = type.replace(" " + POINTER_TYPE, POINTER_TYPE);
if(type.contains("undefined")) {
type = undefinedNearestPrimitive(type);
}
paramsStr += TYPE_NAME_PLACEHOLDER.replace(TYPE_PLACEHOLDER, type).replace(NAME_PLACEHOLDER, name);
if (params.length != count + 1) {
paramsStr += ", ";
}
// println("Count: "+count+" size: "+params.length);
count++;
}
//Return a default value so the code compiles
String returnCode = "";
if(returnType != null && !returnType.equals(""))
returnType += " ";
switch(returnType) {
case "bool":
returnCode = "return false;";
break;
case "short":
case "int":
case "long":
returnCode = "return 0;";
break;
case "float":
returnCode = "return 0.0f;";
break;
case "double":
returnCode = "return 0.0;";
break;
default:
returnCode = "";
}
if (returnType.contains(POINTER_TYPE)) {
returnCode = "return nullptr;";
}
String line = "virtual " + returnType + funcName + "(" + paramsStr + ") {" + returnCode + "}";
if(!outputArray.add(line)) {
int counter = 0;
while (!outputArray.add(line)) {
counter++;
line = "virtual " + returnType + funcName + "_" + counter + "(" + paramsStr + ") {" + returnCode + "}";
}
}
nextInTable = nextInTable.add(nextInTable.getPointerSize());
maxIter--;
if (maxIter < 0) {
println("FAILED");
break;
}
/*
* boolean yesOrNo = askYesNo("yes or no", "is this a yes/no question?");
* println("Yes or No? " + yesOrNo);
*
* if(!yesOrNo){ break; }
*/
currentIter++;
continue;
}
String outputString = "\n";
for (String line : outputArray){
outputString += line + "\n";
}
println(outputString);
}
}