-
Notifications
You must be signed in to change notification settings - Fork 2
/
StackArgumentsSearcher.py
101 lines (86 loc) · 3.26 KB
/
StackArgumentsSearcher.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
#Stack arguments searcher
#@author dzik
#@category Diablo 2
#@keybinding
#@menupath
#@toolbar
import json
import time
from ghidra.util.exception import CancelledException, InvalidInputException
from ghidra.program.model.listing import VariableFilter
from ghidra.program.model.listing import ParameterImpl
from ghidra.program.model.symbol import SourceType
from ghidra.app.services import DataTypeManagerService
# thanks weiry6922
def getDataTypeManagerByName(name):
tool = state.getTool()
service = tool.getService(DataTypeManagerService)
dataTypeManagers = service.getDataTypeManagers()
for manager in dataTypeManagers:
managerName = manager.getName()
if name in managerName:
return manager
return None
# thanks weiry6922
def findDataTypeByNameInDataManager(nameDT, nameDTM):
manager = getDataTypeManagerByName(nameDTM)
allDataTypes = manager.getAllDataTypes()
while allDataTypes.hasNext():
dataType = allDataTypes.next()
dataTypeName = dataType.getName()
if dataTypeName.startswith(nameDT):
return dataType
return None
# thanks weiry6922
def findDataTypeByName(name):
dt = findDataTypeByNameInDataManager(name, currentProgram.name)
if dt == None:
dt = findDataTypeByNameInDataManager(name, u"BuiltInTypes")
if dt == None:
dt = findDataTypeByNameInDataManager(name, u"windows_vs12_32")
return dt
def main():
start = time.time()
monitor.initialize(currentProgram.getFunctionManager().getFunctionCount())
c = 0
for func in currentProgram.functionManager.getFunctions(1):
if "{}".format(func.getEntryPoint()) == "00681a48":
break
if func.hasCustomVariableStorage():
func.setCallingConvention("unknown")
monitor.incrementProgress(1)
monitor.setShowProgressValue(True)
found = False
args = func.getParameters(VariableFilter.STACK_VARIABLE_FILTER)
argsSize = 0
for arg in args:
a = arg.getLength()
if a < 4:
a = 4
argsSize = argsSize + a
argcount = len(args)
retcount = 0
retSize = 0
for inst in currentProgram.listing.getInstructions(func.getBody(), True):
if '{:02X}'.format(inst.getUnsignedByte(0)) == "C2":
retSize = inst.getUnsignedShort(1)
retcount = retSize / 4
found = True
break;
if found:
if argsSize != retSize:
if argsSize < retSize:
if not func.hasCustomVariableStorage():
for i in range(retcount - argcount):
dt = findDataTypeByName("undefined4")
p = ParameterImpl(None, dt, currentProgram)
func.addParameter(p, SourceType.USER_DEFINED)
c = c + 1
print("Function 0x{} has defined {}/{} stack arguments".format(func.getEntryPoint(), argcount, retcount))
print("Found {} functions with wrong stack arguments count".format(c))
end = time.time()
print(end - start)
try:
main()
except CancelledException:
pass