-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedMetaClassAnalyzer.py
More file actions
603 lines (496 loc) · 22.7 KB
/
RedMetaClassAnalyzer.py
File metadata and controls
603 lines (496 loc) · 22.7 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
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
# Does all kind of cool stuff to make analyzing meta classes easier. Check the changelog/code for detailed features.
# @author Nyan Cat
# @category A_Red
# @keybinding
# @menupath
# @toolbar
# Features (Newest at bottom)
# Rename metaClass/vtable pointers in __got.
# Find all references to safeMetaCast and retype variables according to the arguments fed.
# Add missing meta structs.
# Add many long fields to ATIController in order to ease the REing of its structure.
# Set up meta/vtable structs to display function name on vtable calls.
# Create vtable stubs in order to ease the REing of its structure.
# pyright: reportMissingImports=false
# pyright: reportUndefinedVariable=false
import array
import binascii
import json
import os
from java.util import ArrayList
from ghidra.program.model.symbol import SourceType
from ghidra.app.decompiler import DecompileOptions
from ghidra.app.decompiler import DecompInterface
from ghidra.util.task import ConsoleTaskMonitor
# I literally found this by looking at Ghidra's source code (RetypeLocalAction.java)
from ghidra.program.model.pcode import HighFunctionDBUtil
# DataTypeEditorManager.java
# CreatePointerAction.java
from ghidra.program.model.data import CategoryPath, StructureDataType, PointerDataType, FunctionDefinitionDataType
# FindReferencesToField.java -> LocationReferencesPlugin.java -> GenericCompositeDataTypeProgramLocation.java -> ReferenceUtils.java
# Java.
from ghidra.program.model.data import DataTypeConflictHandler
# Used to recreate FunctionDefinitionDataType from signature string
from ghidra.program.model.data import GenericCallingConvention, ParameterDefinitionImpl
# Used to update existing functions from signature string
from ghidra.program.model.listing import ParameterImpl
from ghidra.program.model.listing.Function import FunctionUpdateType
# Used for handling getAddress failure
from ghidra.program.model.address import AddressFormatException
verbose = False
# If importVtables is not empty, then *only* meta classes in the list will have their vtable processed
importVtables = []
# Computed by comparing the address of the actual function to the pointer in the vtable
# The first function in the vtable is always one of the destructors
# dyldOffset = 0x7dff20000000
dyldOffset = 0
# When set to True, discover the return type of functions by decompiling them
# Warning: It's slow and may take more than 10 minutes on large programs.
discoverReturnType = True
def makeByteArr(length):
return array.array('b', b'\x00' * length)
def readMem(address, length, littleEndian=True):
memVal = makeByteArr(length)
mem.getBytes(address, memVal)
if littleEndian:
memVal = memVal[::-1]
return binascii.hexlify(memVal)
def getCommentAtPtr(ptr, ptrSize=8, commentType=3):
addr = getAddress(readMem(ptr, ptrSize))
return codeManager.getComment(commentType, addr)
# https://github.com/HackOvert/GhidraSnippets
def getAddress(offset, applyDyldOffset=False):
if applyDyldOffset:
offset = int(offset, 16) + dyldOffset
offset = hex(offset)[2:].replace("L", "")
return currentProgram.getAddressFactory().getDefaultAddressSpace().getAddress(offset)
def getDataType(typeName, typeManager):
matches = ArrayList()
typeManager.findDataTypes(typeName, matches)
if len(matches) > 1:
print("Warning: Using the first " + typeName)
elif len(matches) == 0:
return None
return matches[0]
def makePtrTypeName(typeName, ptrLevel):
return typeName + " *" * ptrLevel
def ensureDataType(typeName, typeManager, ptrLevel, forceStructSize):
# Because I kept on misusing this function somehow
assert "*" not in typeName
ret = getDataType(makePtrTypeName(typeName, ptrLevel), typeManager)
if ret is not None:
# Don't touch other types
if not isinstance(ret, StructureDataType):
return ret
if ptrLevel == 0 and forceStructSize is not None and ret.getLength() != forceStructSize:
if verbose:
print(" Resizing {}/{} from {} bytes to {} bytes".format(
ret.getCategoryPath(), ret.getName(), ret.getLength(), forceStructSize))
ret.replaceWith(StructureDataType(
ret.getCategoryPath(), ret.getName(), forceStructSize))
return ret
# Create our own data type
if ptrLevel == 0:
targetLen = forceStructSize if forceStructSize is not None else 0
if verbose:
print("Creating {}-bytes struct data type {}".format(targetLen, typeName))
ret = StructureDataType(CategoryPath(
"/AMDGen/Structs"), typeName, targetLen, typeManager)
else:
prevType = ensureDataType(
typeName, typeManager, ptrLevel - 1, forceStructSize)
if verbose:
print("Creating pointer data type " + makePtrTypeName(typeName, ptrLevel))
ret = PointerDataType(prevType, typeManager)
ret = typeManager.addDataType(ret, DataTypeConflictHandler.REPLACE_HANDLER)
return ret
def strToFunc(funcStr, suffix=""):
funcName = funcStr.split("(")[0].split(" ")[-1]
funcType = FunctionDefinitionDataType(CategoryPath(
"/AMDGen/FuncSigns"), funcName + "_sign" + suffix)
callType = funcStr.split(" ")[0]
funcType.setGenericCallingConvention(callTypes.get(callType, GenericCallingConvention.stdcall))
retType = funcStr.split(" ")[1]
funcType.setReturnType(ensureDataType(retType.replace(
"*", ""), typeManager, retType.count("*"), None))
paraStrs = funcStr.split("(")[1].split(")")[0].split(", ")
if (paraStrs[0] == "void"):
return funcType
parameters = []
for paraStr in paraStrs:
try:
typeName, paraName = paraStr.split(" ")
paraType = ensureDataType(typeName.replace(
"*", ""), typeManager, typeName.count("*"), None)
parameters.append(ParameterDefinitionImpl(paraName, paraType, ""))
except Exception:
pass
funcType.setArguments(parameters)
return funcType
def funcToStr(curFunc):
funcSign = curFunc.getSignature(False)
funcStr = funcSign.getCallingConventionName()
funcStr += " " + curFunc.getReturnType().getName() # Return type
funcStr += " " + str(curFunc) # Function name
protoStr = funcSign.getPrototypeString(False)
funcStr += "(" + protoStr.split("(")[-1].replace(" *", "*") # Parameters
return funcStr
def DBIndex(index):
return str(index).zfill(3)
def askVtablePreference(title, dbStr, progStr):
print(" {}\n DB: {}\n Prog: {}".format(title, dbStr, progStr))
global globalVtablePreference, classVtablePreference
if globalVtablePreference is not None:
return globalVtablePreference
if classVtablePreference is not None:
return classVtablePreference
modes = ["Prog and update DB",
"Prog but don't update DB", "DB and update Prog"]
optionTemplates = [
"Always use {}", "Use {} in this meta class", "Use {} just for this conflict"]
options = []
for mode in modes:
for template in optionTemplates:
options.append(template.format(mode))
choice = options.index(askChoice(title,
"Which one should I use? (See console for details)",
options, None))
if choice % 3 == 0:
globalVtablePreference = modes[choice // 3]
return globalVtablePreference
elif choice % 3 == 1:
classVtablePreference = modes[choice // 3]
return classVtablePreference
elif choice % 3 == 2:
return modes[choice // 3]
globalVtablePreference = None
classVtablePreference = None
# Change directory to script location
abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
os.chdir(dname)
state = getState()
mem = currentProgram.getMemory()
funcManager = currentProgram.getFunctionManager()
codeManager = currentProgram.getCodeManager()
typeManager = currentProgram.getDataTypeManager()
symbolTable = currentProgram.getSymbolTable()
print("Renaming pointers in __got...")
got = mem.getBlock("__got")
print("Found __got at {}~{}".format(got.getStart(), got.getEnd()))
metaTypeNames = set(importVtables)
addr = got.getStart()
renameCount = 0
# This step SHOULD NOT be skipped because it populates the metaTypeNames set
while True:
name = getCommentAtPtr(addr)
if name is None:
break
elif "etaClass" in name:
name = name.replace("::", "__")
metaTypeNames.add(name.split("__")[0])
pass
elif "vtable" in name:
metaTypeNames.add(name.split(" ")[2])
name = "{}__vtable".format(name.split(" ")[2])
else:
break
renameCount += 1
symbol = symbolTable.createLabel(addr, name, SourceType.ANALYSIS)
addr = addr.add(8)
print("Renamed {} symbols".format(renameCount))
options = DecompileOptions()
# Prevent line wrapping
options.setMaxWidth(10000)
monitor = ConsoleTaskMonitor()
ifc = DecompInterface()
ifc.setOptions(options)
ifc.openProgram(currentProgram)
print("")
print("Retyping variables that uses OSMetaClassBase::safeMetaCast...")
metaCast = None
for func in funcManager.getFunctions(True):
if "safeMetaCast" in func.getName():
metaCast = func
break
if metaCast is None:
print("Warning: safeMetaCast not found, skipping variable retype")
else:
print("Found safeMetaCast at {}".format(metaCast.getEntryPoint()))
refs = getReferencesTo(metaCast.getEntryPoint())
print("Found {} references to safeMetaCast".format(len(refs)))
todoFuncs = []
for xref in refs:
func = funcManager.getFunctionContaining(xref.getFromAddress())
if func is not None and func not in todoFuncs:
todoFuncs.append(func)
print("Found {} functions that uses safeMetaCast".format(len(todoFuncs)))
retypeCounts = []
for iter in range(25):
if len(importVtables) != 0:
print("Skipping variable retype")
break
print("Running pass {}/{}".format(iter + 1, 25))
retypeCount = 0
for func in todoFuncs:
try:
results = ifc.decompileFunction(func, 0, monitor)
code = results.getDecompiledFunction().getC().split('\n')
except AttributeError:
continue
funcName = [x for x in code if "::" in x][0][3:-3]
castRefs = [
x for x in code if "OSMetaClassBase::safeMetaCast(" in x]
for castRef in castRefs:
try:
parts = castRef.strip().split(" = ")
assert len(parts) == 2 and not any(
x in parts[0] for x in "()&! ")
varName = parts[0]
try:
className = parts[1].split(
"safeMetaCast(")[-1].split(")")[-2].strip()
except IndexError:
raise AssertionError
if className.endswith("__metaClass"):
className = className[:-len("__metaClass")]
elif "::" in className:
className = className.split("::")[0].replace("&", "")
elif className == "&gMetaClass":
# Class of "this"
className = funcName.split("::")[0]
else:
raise AssertionError
metaTypeNames.add(className)
try:
varSymbol = results.getHighFunction().getLocalSymbolMap().getNameToSymbolMap()[
str(varName)]
except KeyError:
raise AssertionError
dataType = ensureDataType(className, typeManager, 1, None)
if varSymbol.getDataType() != dataType:
if verbose:
print(" Retyping {} from {} to {} in {}".format(
varName, varSymbol.getDataType().getName(), makePtrTypeName(className, 1), funcName))
retypeCount += 1
# Here goes nothing...
HighFunctionDBUtil.updateDBVariable(
varSymbol, None, dataType, SourceType.ANALYSIS)
except AssertionError:
pass
# print("Warning: Error processing code")
# print(" " * 4 + castRef.strip())
print("Retyped {} variables".format(retypeCount))
retypeCounts.append(retypeCount)
if retypeCount == 0 or sum(retypeCounts[-5:]) == retypeCounts[-1] * 5:
break
print("")
print("Setting up meta struct basic structure...")
demanglerPath = typeManager.getCategory(CategoryPath("/Demangler"))
dataTypes = demanglerPath.getDataTypes()
print("Found {} data types directly under /Demangler".format(len(dataTypes)))
metaDataTypes = []
for dataType in dataTypes:
name = dataType.getName()
if "*" not in name and ("_t" not in name or name in metaTypeNames):
metaDataTypes.append(dataType)
blacklist = ["mach_timespec", "longlong", "IOPCIAddressSpace", "IOACPIAddress"]
newStructs = typeManager.getCategory(CategoryPath("/AMDGen/Structs"))
if newStructs is not None:
for dataType in newStructs.getDataTypes():
name = dataType.getName()
if "*" not in name and "_vtableStruct" not in name and not name.startswith("FuncDef") and name not in blacklist:
metaDataTypes.append(dataType)
print("Found {} meta structs".format(len(metaDataTypes)))
ulong = ensureDataType("ulong", typeManager, 0, None)
ulongPtr = ensureDataType("ulong", typeManager, 1, None)
callTypes = {
"__stdcall": GenericCallingConvention.stdcall,
"__cdecl": GenericCallingConvention.cdecl,
"__fastcall": GenericCallingConvention.fastcall,
"__thiscall": GenericCallingConvention.thiscall,
"__vectorcall": GenericCallingConvention.vectorcall
}
try:
vtableDB = json.load(open("vtableDB.json"))
except Exception:
print("Warning: Failed to load vtableDB.json!")
vtableDB = {}
print("")
print("Creating and assigning vtable structs...")
for i in range(len(metaDataTypes)):
print("{}/{}:".format(i + 1, len(metaDataTypes)))
dataType = metaDataTypes[i]
name = dataType.getName()
if len(importVtables) != 0 and name not in importVtables:
print(" Skipping " + name)
continue
vtable = {}
classVtablePreference = None
# Find vtable address
vtableAddr = None
func = next((v for v in funcManager.getFunctions(
True) if name == v.getName()), None)
if func is not None:
try:
results = ifc.decompileFunction(func, 0, monitor)
code = results.getDecompiledFunction().getC().splitlines()
matches = [v for v in code if "this->vtable = " in v or "this = " in v]
if len(matches) != 0:
arg = matches[0].split(" ")[-1].split(")")[-1].replace("&", "").strip()
valid = arg.startswith("DAT_") or arg.startswith(
"PTR_") or arg.startswith(name + "_")
extractedAddr = getAddress(
arg.split("_")[-1][:-1].replace(";", "")) if valid else None
if vtableAddr is None:
vtableAddr = extractedAddr
elif vtableAddr != extractedAddr:
print(" Error: Conflicting vtable addresses for {}".format(name))
print(vtableAddr, code)
raise AssertionError
except AttributeError:
pass
if vtableAddr is not None:
# Analyze vtable
print(" Analyzing {} vtable at {}...".format(name, vtableAddr))
addr = vtableAddr
i = 0
if name not in vtableDB:
vtableDB[name] = {}
funcOccurances = {}
while True:
try:
ptrAddr = getAddress(readMem(addr, 8), applyDyldOffset=True)
if ptrAddr.getOffset() == 0:
break
except AddressFormatException:
break
func = funcManager.getFunctionContaining(ptrAddr)
if func is not None:
if discoverReturnType:
try:
code = ifc.decompileFunction(func, 0, monitor).getDecompiledFunction().getC()
signLine = [x.strip() for x in code.split("\n") if "/*" not in x and len(x.strip()) != 0][0]
retType = " ".join(signLine.split("(")[0].split(" ")[:-1])
# Remove call convention annotation, if any
for callConvention in ["MSABI", "syscall", "__thiscall"]:
retType = retType.replace(" {}".format(callConvention), "")
retTypeName = retType.replace("*", "").strip()
retTypePtrLevel = retType.count("*")
func.setReturnType(ensureDataType(retTypeName, typeManager, retTypePtrLevel, None), SourceType.ANALYSIS)
except Exception:
print(" Warning: Failed to discover return type of " + str(func))
funcName = str(func)
funcStr = funcToStr(func)
funcSign = func.getSignature(False)
if vtableDB[name].get(DBIndex(i), funcStr) != funcStr:
title = "Merge conflict on {} field_{}".format(
name, DBIndex(i))
prefer = askVtablePreference(
title, vtableDB[name][DBIndex(i)], funcStr)
print(" About to use {}".format(prefer))
if prefer == "Prog and update DB":
vtableDB[name][DBIndex(i)] = funcStr
elif prefer == "Prog but don't update DB":
pass
elif prefer == "DB and update Prog":
funcStr = vtableDB[name][DBIndex(i)]
funcName = funcStr.split("(")[0].split(" ")[-1]
funcSign = strToFunc(funcStr)
func.setCallingConvention(funcStr.split(" ")[0])
func.setReturnType(
funcSign.getReturnType(), SourceType.ANALYSIS)
if "::" in funcName:
func.setCallFixup(funcName.split("::")[0])
func.setName(funcName.split("::")
[-1], SourceType.ANALYSIS)
# ParameterImpl vs ParameterDefinition smh
paras = ArrayList()
for para in funcSign.getArguments():
paras.add(ParameterImpl(para.getName(),
para.getDataType(), currentProgram))
func.replaceParameters(
paras, FunctionUpdateType.DYNAMIC_STORAGE_ALL_PARAMS, True, SourceType.ANALYSIS)
else:
# If this happens I made a typo lol
raise AssertionError
else:
vtableDB[name][DBIndex(i)] = funcStr
vtable[i] = funcName
if funcName not in funcOccurances:
funcOccurances[funcName] = 0
occurId = funcOccurances[funcName]
funcOccurances[funcName] += 1
funcType = FunctionDefinitionDataType(CategoryPath(
"/AMDGen/FuncSigns"), funcName + "_sign_" + str(occurId), funcSign)
typeManager.addDataType(
funcType, DataTypeConflictHandler.REPLACE_HANDLER)
addr = addr.add(8)
i += 1
if discoverReturnType and i % 100 == 0:
print(" Processed {} functions".format(i))
if dyldOffset != 0 and len(symbolTable.getSymbols(addr)) != 0:
break
vtableDB[name]["length"] = max(
vtableDB[name].get("length", 0), len(vtable))
vtable["length"] = vtableDB[name]["length"]
funcOccurances = {}
if name in vtableDB.keys():
length = vtableDB[name]["length"]
for i in range(length):
if i in vtable:
continue
funcStr = vtableDB[name].get(DBIndex(i))
if funcStr is not None:
funcName = funcStr.split("(")[0].split(" ")[-1]
if funcName not in funcOccurances:
funcOccurances[funcName] = 0
occurId = funcOccurances[funcName]
funcOccurances[funcName] += 1
typeManager.addDataType(strToFunc(
funcStr, "_" + str(occurId)), DataTypeConflictHandler.REPLACE_HANDLER)
vtable[i] = funcName
vtable["length"] = max(vtable.get("length", 0), length)
vtableDB[name]["length"] = vtable["length"]
if len(vtable) == 0:
print(" Warning: Failed to find vtable for {}".format(name))
continue
print(" Creating {}_vtableStruct with {} entries...".format(
name, vtable["length"]))
vtableStruct = ensureDataType(name + "_vtableStruct", typeManager, 0, 0)
vtableStruct.deleteAll()
funcOccurances = {}
for i in range(vtable["length"]):
funcName = vtable.get(i)
if funcName is None:
funcName = "field_" + str(i).zfill(3)
vtableStruct.add(ulongPtr, 8, funcName,
"Generated by RedMetaClassAnalyzer.py")
else:
if funcName not in funcOccurances:
funcOccurances[funcName] = 0
occurId = funcOccurances[funcName]
funcOccurances[funcName] += 1
signPtr = ensureDataType(
funcName + "_sign_" + str(occurId), typeManager, 1, None)
vtableStruct.add(signPtr, 8, funcName,
"Generated by RedMetaClassAnalyzer.py")
# Ensure the DataType in typeManager is updated
typeManager.addDataType(
vtableStruct, DataTypeConflictHandler.REPLACE_HANDLER)
vtableStructPtr = ensureDataType(
name + "_vtableStruct", typeManager, 1, None)
if dataType.isZeroLength():
dataType.add(vtableStructPtr, 8, "vtable",
"Generated by RedMetaClassAnalyzer.py")
# Ensure the DataType in typeManager is updated
typeManager.addDataType(
dataType, DataTypeConflictHandler.REPLACE_HANDLER)
elif dataType.getComponentContaining(0) is None or dataType.getComponentContaining(0).getFieldName() != "0":
dataType.replaceAtOffset(
0, vtableStructPtr, 8, "vtable", "Generated by RedMetaClassAnalyzer.py")
# Ensure the DataType in typeManager is updated
typeManager.addDataType(
dataType, DataTypeConflictHandler.REPLACE_HANDLER)
with open("vtableDB.json", "w") as f:
json.dump(vtableDB, f, sort_keys=True, indent=4)