-
Notifications
You must be signed in to change notification settings - Fork 1
/
idatocpp.py
391 lines (360 loc) · 18.8 KB
/
idatocpp.py
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import argparse
import json
import csv
import re
import os
parser = argparse.ArgumentParser(description='IDA .h and .cpp wrapper file generator.')
parser.add_argument('-db', required=True, metavar='path', type=str,
help='The path to IDA generated database using plugin-sdk tool.')
parser.add_argument('-iclass', required=True, metavar='name', type=str,
help='The class name in the IDB.')
parser.add_argument('--rcalls', action='store_true',
help='Adds <className>_Reversed wrappers for virtual functions.')
parser.add_argument('--pdtypes', action='store_true',
help='Function parameter types will be extracted from the demangled name rather than the function prototype.')
args = parser.parse_args()
reversedWrappers = args.rcalls
extractTypesFromDemangledName = args.pdtypes
className = args.iclass
structFilePath = args.db + "\\structs\\gtaout." + className + ".json"
variablesFilePath = args.db + "\\plugin-sdk.out.variables.csv"
functionsFilePath = args.db + "\\plugin-sdk.out.functions.csv"
class_members_h = ""
static_vars_h = ""
static_vars_cpp = ""
functions_h = ""
functions_cpp = ""
inject_hooks_cpp = ""
def replace_all(text, dic):
for i, j in dic.items():
text = re.sub(i,j,text)
return text
def GetProperType(text):
if text.strip() == "char":
return "std::int8_t"
replacements = {
r'\bunsigned __int8\b':'std::uint8_t',
r'\bunsigned __int16\b':'std::uint16_t',
r'\bunsigned __int32\b':'std::uint32_t',
r'\b__int8\b':'std::int8_t',
r'\b__int16\b':'std::int16_t',
r'\b__int32\b':'std::int32_t',
r'\b_BYTE\b':'std::uint8_t',
r'\bword\b':'std::int16_t',
r'\b_WORD\b':'std::int16_t',
r'\bWORD\b':'std::int16_t',
r'\bsigned int\b':'std::int32_t',
r'\bunsigned int\b':'std::uint32_t',
r'\bint\b':'std::int32_t',
r'\bdword\b':'std::int32_t',
r'\b_DWORD\b':'std::int32_t',
r'\bstruct\b':'',
r'\b_BOOL1\b':'bool',
}
return replace_all(text, replacements).strip()
def GetTypeData(theType):
if '[' in theType:
realType = theType.split("[")[0].replace(" ", "")
result = re.findall(r"\[\s*\+?(-?\d+)\s*\]", theType)
if len(result) > 0:
for i in range(len(result)):
result[i] = int(result[i])
return { "type" : GetProperType(realType), "arrays" : result}
elif '*' in theType:
realType = theType.split("*")[0].strip()
return { "type" : GetProperType(realType), "isPointer" : True }
return { "type" : GetProperType(theType) }
structInfo = None
isStaticClass = False
if not os.path.isfile(structFilePath):
isStaticClass = True
else:
with open(structFilePath) as f:
structInfo = json.load(f)
for member in structInfo["members"]:
typeData = GetTypeData(member["type"])
if "arrays" in typeData:
totalElements1 = typeData['arrays'][0]
arrayLength = len(typeData['arrays'])
if arrayLength == 1:
class_members_h += " %s %s[%d];\n" % (typeData['type'], member["name"], totalElements1)
if arrayLength == 2:
totalElements2 = typeData['arrays'][1]
class_members_h += " %s %s[%d][%d];\n" % (typeData['type'], member["name"], totalElements1, totalElements2)
elif "isPointer" in typeData:
class_members_h += " %s* %s;\n" % (typeData['type'], member["name"])
else:
class_members_h += " %s %s;\n" % (typeData['type'], member["name"])
classIdentifier = className + "::"
if os.path.isfile(variablesFilePath):
with open(variablesFilePath,'rt', encoding='UTF-8') as f:
data = csv.reader(f)
for row in data:
address = row[0]
demangledName = row[3]
theType = row[4]
sizeInBytes = row[6]
findIndex = demangledName.find(classIdentifier)
if findIndex == 0:
#print (str(findIndex) + " | " + demangledName + " | type: " + theType + " | sizeInBytes: " + str(sizeInBytes))
variableName = demangledName.strip(classIdentifier)
staticVarData = GetTypeData(theType)
if "arrays" in staticVarData:
totalElements1 = staticVarData['arrays'][0]
arrayLength = len(staticVarData['arrays'])
if arrayLength == 1:
static_vars_h += " static %s(&%s)[%d];\n" % (staticVarData['type'], variableName, totalElements1)
static_vars_cpp += "%s(&%s)[%d] = *(%s(*)[%d])%s;\n" % (staticVarData['type'],
demangledName, totalElements1, staticVarData['type'], totalElements1, address)
if arrayLength == 2:
totalElements2 = staticVarData['arrays'][1]
static_vars_h += " static %s(&%s)[%d][%d];\n" % (staticVarData['type'], variableName, totalElements1, totalElements2)
static_vars_cpp += "%s(&%s)[%d][%d] = *(%s(*)[%d][%d])%s;\n" % (staticVarData['type'],
demangledName, totalElements1, totalElements2, staticVarData['type'], totalElements1, totalElements2, address)
elif "isPointer" in staticVarData:
static_vars_h += " static %s*& %s;\n" % (staticVarData['type'], variableName)
static_vars_cpp += "%s*& %s = *(%s**)%s;\n" % (staticVarData['type'], demangledName, staticVarData['type'], address)
else:
static_vars_h += " static %s& %s;\n" % (staticVarData['type'], variableName)
static_vars_cpp += "%s& %s = *(%s*)%s;\n" % (staticVarData['type'], demangledName, staticVarData['type'], address)
#print("\n")
#print (static_vars_h)
#print("\n")
#print (static_vars_cpp)
functionsList = []
with open(functionsFilePath,'rt', encoding='UTF-8') as f:
data = csv.reader(f)
for row in data:
demangledName = row[3]
findIndex = demangledName.find(classIdentifier)
if findIndex == 0:
vtableIndex = int(row[13])
row[13] = vtableIndex
functionsList.append(row)
functionsList.sort(key=lambda row: row[13]) # sort by vtable index
def GetParameterTypesFromDemangledName(demangledName):
demangledName = demangledName.strip()
startBracketIndex = demangledName.find('(')
if startBracketIndex != -1:
lastCharIndex = len(demangledName) - 1
if demangledName[lastCharIndex] == ')':
textWithinBrackets = demangledName[startBracketIndex+1:lastCharIndex].strip()
if textWithinBrackets == "":
return []
parameterTypes = textWithinBrackets.split(',')
if len(parameterTypes) == 1 and parameterTypes[0] == "void":
return []
return parameterTypes
return []
def IsConstructorFunction(gtaClassName, functionName, classIdentifier):
functionName = functionName.strip(" ").lower()
if gtaClassName.lower() in functionName or "constructor" in functionName:
return True
return False
def IsDestructorFunction(gtaClassName, functionName, classIdentifier):
functionName = functionName.strip(" ").lower()
destructorName = "~" + gtaClassName.lower()
if destructorName in functionName or "destructor" in functionName:
return True
return False
processedFunctions = {}
supportedCallingConventions = ["cdecl", "thiscall", "stdcall", "fastcall"]
def GenerateFunctionCode(constructorsOnly = False, destructorsOnly = False, constructorWrappersOnly = False, virtualFunctionsOnly = False):
global functionsList
global classIdentifier
global args
global functions_h
global functions_cpp
global isStaticClass
global processedFunctions
global inject_hooks_cpp
global extractTypesFromDemangledName
virtual_functions_h = ""
virtual_functions_cpp = ""
for i in range(len(functionsList)):
if i in processedFunctions:
continue
row = functionsList[i]
address = row[0].strip()
demangledName = row[3].strip()
functionType = GetProperType(row[4].strip())
callingConvention = row[5].strip()
returnType = GetProperType(row[6])
parameterNames = row[8].strip()
vtableIndex = int(row[13])
fullFunctionName = demangledName.split("(")[0]
functionName = fullFunctionName.replace(classIdentifier, "").split("(")[0]
if callingConvention not in supportedCallingConventions:
raise Exception("calling convention '%s' not handled for function '%s'" % (callingConvention, fullFunctionName))
paramTypesExtractedFromDemangledName = False
parameterTypes = []
if extractTypesFromDemangledName:
if "<" in demangledName:
print ("%s: `pdtypes` option ignored due to template usage" % fullFunctionName)
else:
parameterTypes = GetParameterTypesFromDemangledName(demangledName)
if len(parameterTypes) > 0:
paramTypesExtractedFromDemangledName = True
else:
parameterTypes = row[7].strip()
if parameterTypes != "":
parameterTypes = parameterTypes.split("~")
else:
parameterTypes = []
if parameterNames != "":
parameterNames = parameterNames.split("~")
else:
parameterNames = []
if returnType == "" and not isStaticClass:
print ("%s: Skipping, return type is empty" % fullFunctionName)
continue
if constructorsOnly and not IsConstructorFunction(className, functionName, classIdentifier):
continue
if destructorsOnly and not IsDestructorFunction(className, functionName, classIdentifier):
continue
if constructorWrappersOnly and not IsConstructorFunction(className, functionName, classIdentifier):
continue
if virtualFunctionsOnly and vtableIndex == -1:
continue
# we need this check to avoid duplicates because the destructor function can also be a virtual function
if not destructorsOnly and IsDestructorFunction(className, functionName, classIdentifier):
continue
if not constructorsOnly: # this check is needed to make constructorWrappersOnly option generate code
processedFunctions[i] = True
if callingConvention == "thiscall" or callingConvention == "fastcall":
# remove the first parameter type and name (this pointer)
if paramTypesExtractedFromDemangledName:
del parameterNames[0]
else:
del parameterNames[0]
del parameterTypes[0]
parameterTypesStr = ""
parameterNamesStr = ""
paramterTypesNamesStr = ""
totalParameters = len(parameterTypes)
for i in range(totalParameters):
theType = GetProperType(parameterTypes[i]).strip()
theName = parameterNames[i].strip()
parameterTypesStr += theType
parameterNamesStr += theName
paramterTypesNamesStr += "%s %s" % (theType, theName)
if i != totalParameters-1:
parameterTypesStr += ", "
parameterNamesStr += ", "
paramterTypesNamesStr += ", "
if constructorsOnly and IsConstructorFunction(className, functionName, classIdentifier):
inject_hooks_cpp += " HookInstall(%s, &%s::Constructor);\n" % (address, className)
functions_h += " %s(%s);\n" % (className, paramterTypesNamesStr)
functions_cpp += "%s::%s(%s)\n{\n}\n\n" % (className, className, paramterTypesNamesStr)
continue
if destructorsOnly and IsDestructorFunction(className, functionName, classIdentifier):
inject_hooks_cpp += " HookInstall(%s, &%s::Destructor);\n" % (address, className)
functions_h += " ~%s();\n" % className
functions_cpp += "%s::~%s()\n{\n}\n\n" % (className, className)
functions_h += " %s* Destructor();\n" % className
functions_cpp += "%s* %s::Destructor()\n{\n this->%s::~%s();\n return this;\n}\n\n" % (className, className, className, className)
break # break the loop because a class should not have more than 1 destructor
if constructorWrappersOnly and IsConstructorFunction(className, functionName, classIdentifier):
functions_h += " %s* Constructor(%s);\n" % (className, paramterTypesNamesStr)
functions_cpp += "%s* %s::Constructor(%s)\n{\n this->%s::%s(%s);\n return this;\n}\n\n" % (className, className, paramterTypesNamesStr, className, className, parameterNamesStr)
continue
elif vtableIndex != -1:
if reversedWrappers:
inject_hooks_cpp += " HookInstall(%s, &%s_Reversed);\n" % (address, fullFunctionName)
functions_h += " %s %s(%s) override;\n" % (returnType, functionName, paramterTypesNamesStr)
functions_cpp += "%s %s(%s)\n{\n" % (returnType, fullFunctionName, paramterTypesNamesStr)
if reversedWrappers:
functions_cpp += "#ifdef USE_DEFAULT_FUNCTIONS\n"
virtual_functions_h += " %s %s_Reversed(%s);\n" % (returnType, functionName, paramterTypesNamesStr)
virtual_functions_cpp += "%s %s_Reversed(%s)\n{\n}\n\n" % (returnType, fullFunctionName, paramterTypesNamesStr)
elif callingConvention == "fastcall":
inject_hooks_cpp += " HookInstall(%s, &%s);\n" % (address, fullFunctionName)
functions_h += " static %s __fastcall %s(%s);\n" % (returnType, functionName, paramterTypesNamesStr)
functions_cpp += "%s __fastcall %s(%s)\n{\n" % (returnType, fullFunctionName, paramterTypesNamesStr)
elif callingConvention == "stdcall":
inject_hooks_cpp += " HookInstall(%s, &%s);\n" % (address, fullFunctionName)
functions_h += " static %s __stdcall %s(%s);\n" % (returnType, functionName, paramterTypesNamesStr)
functions_cpp += "%s __stdcall %s(%s)\n{\n" % (returnType, fullFunctionName, paramterTypesNamesStr)
elif callingConvention == "cdecl":
inject_hooks_cpp += " HookInstall(%s, &%s);\n" % (address, fullFunctionName)
functions_h += " static %s %s(%s);\n" % (returnType, functionName, paramterTypesNamesStr)
functions_cpp += "%s %s(%s)\n{\n" % (returnType, fullFunctionName, paramterTypesNamesStr)
else:
inject_hooks_cpp += " HookInstall(%s, &%s);\n" % (address, fullFunctionName)
functions_h += " %s %s(%s);\n" % (returnType, functionName, paramterTypesNamesStr)
functions_cpp += "%s %s(%s)\n{\n" % (returnType, fullFunctionName, paramterTypesNamesStr)
parameterNamesCommaStr = ""
if totalParameters > 0:
parameterTypesStr = ", %s" % parameterTypesStr
if callingConvention == "thiscall" or callingConvention == "fastcall":
parameterNamesCommaStr = ", %s" % parameterNamesStr
else:
parameterNamesCommaStr = parameterNamesStr
if callingConvention == "thiscall":
if returnType == "void":
functions_cpp += " return plugin::CallMethod<%s, %s*%s>(this%s);\n" % (
address, className, parameterTypesStr, parameterNamesCommaStr)
else:
functions_cpp += " return plugin::CallMethodAndReturn<%s, %s, %s*%s>(this%s);\n" % (returnType,
address, className, parameterTypesStr, parameterNamesCommaStr)
elif callingConvention == "fastcall":
if returnType == "void":
functions_cpp += " return plugin::FastCall<%s, %s*%s>(this%s);\n" % (
address, className, parameterTypesStr, parameterNamesCommaStr)
else:
functions_cpp += " return plugin::FastCallAndReturn<%s, %s, %s*%s>(this%s);\n" % (returnType,
address, className, parameterTypesStr, parameterNamesCommaStr)
elif callingConvention == "stdcall" or isStaticClass:
if returnType == "void":
functions_cpp += " return plugin::StdCall<%s%s>(%s);\n" % (
address, parameterTypesStr, parameterNamesCommaStr)
else:
functions_cpp += " return plugin::StdCallAndReturn<%s, %s%s>(%s);\n" % (returnType,
address, parameterTypesStr, parameterNamesCommaStr)
elif callingConvention == "cdecl" or isStaticClass:
if returnType == "void":
functions_cpp += " return plugin::Call<%s%s>(%s);\n" % (
address, parameterTypesStr, parameterNamesCommaStr)
else:
functions_cpp += " return plugin::CallAndReturn<%s, %s%s>(%s);\n" % (returnType,
address, parameterTypesStr, parameterNamesCommaStr)
else:
raise Exception("calling convention '%s' not handled for function '%s'" % (callingConvention, fullFunctionName))
if vtableIndex != -1 and reversedWrappers:
functions_cpp += "#else\n return %s_Reversed(%s);\n#endif\n}\n\n" % (fullFunctionName, parameterNamesStr)
else:
functions_cpp += "}\n\n"
if virtualFunctionsOnly and reversedWrappers and not isStaticClass:
functions_h += "\nprivate:\n" + virtual_functions_h + "public:\n"
functions_cpp += "\n" + virtual_functions_cpp
GenerateFunctionCode(constructorsOnly = True)
GenerateFunctionCode(destructorsOnly = True)
functions_h += "private:\n"
GenerateFunctionCode(constructorWrappersOnly = True)
functions_h += "public:\n"
GenerateFunctionCode(virtualFunctionsOnly = True)
GenerateFunctionCode()
functions_h = " static void InjectHooks();\n\n" + functions_h
functions_cpp = "void %s::InjectHooks()\n{\n%s}\n\n%s" % (className, inject_hooks_cpp, functions_cpp)
final_code_h = "class %s {\npublic:\n" % (className)
if not isStaticClass:
final_code_h += class_members_h
if static_vars_h != "":
final_code_h += "\n%s\n%s\n}\n\n" % (static_vars_h, functions_h)
else:
final_code_h += "\n%s\n};\n\n" % functions_h
if not isStaticClass:
final_code_h += "VALIDATE_SIZE(%s, %s);\n" % (className, structInfo["size"])
if static_vars_cpp != "":
final_code_cpp = '#include "StdInc.h"\n\n%s\n%s' % (static_vars_cpp, functions_cpp)
else:
final_code_cpp = '#include "StdInc.h"\n\n%s' % functions_cpp
def createOutputFile(filePath, contents):
file = open(filePath,"w+")
file.write(contents)
file.close()
outputPath = "output"
if not os.path.exists(outputPath):
os.mkdir(outputPath)
createOutputFile("%s\\%s.h" % (outputPath, className), final_code_h)
createOutputFile("%s\\%s.cpp" % (outputPath, className), final_code_cpp)