-
Notifications
You must be signed in to change notification settings - Fork 1
/
FunctionExporter.py
73 lines (66 loc) · 2.47 KB
/
FunctionExporter.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
#Export function addresses and parameters info
#@author dzik
#@category Diablo 2
#@keybinding
#@menupath
#@toolbar
import json
from ghidra.util.exception import CancelledException, InvalidInputException
def minify(file_name):
file_data = open(file_name, "r", 1).read() # store file info in variable
json_data = json.loads(file_data) # store in json structure
json_string = json.dumps(json_data, separators=(',', ":")) # Compact JSON structure
file_name = str(file_name).replace(".json", "") # remove .json from end of file_name string
new_file_name = "{0}_minify.json".format(file_name)
open(new_file_name, "w+", 1).write(json_string) # open and write json_string to file
def write_json(data, filename='game.json'):
with open(filename,'w') as f:
json.dump(data, f, indent=2)
try:
data = []
o = 0
for func in currentProgram.functionManager.getFunctions(1):
if "{}".format(func.getEntryPoint()) == "00681a48":
break
o = o + 1
e = {
"name" : func.getName(),
"address" : "0x{}".format(func.getEntryPoint()),
"paramcount" : func.getParameterCount(),
"returntype" : "{}".format(func.getReturn().getDataType()),
"ASM" : "",
"jumpback" : "",
"params": [
]
}
#get asm what we replace in case we want to hook function and it's jumpback location
for inst in currentProgram.listing.getInstructions(func.getBody(), True):
if inst.getAddress() >= func.getEntryPoint().add(5):
e["jumpback"] = "0x{}".format(inst.getAddress())
break
e["ASM"] = e["ASM"] + "{}\n".format(inst)
i = 0
for p in func.getParameters():
t = e["params"]
name = p.getName()
type = p.getDataType()
size = type.getLength()
if p.isRegisterVariable():
loc = p.getRegister()
if p.isStackVariable():
loc = "Stack[0x{:02X}]".format(p.getStackOffset())
w = {
"name" : "{}".format(name),
"type" : "{}".format(type),
"location" : "{}".format(loc),
"idx" : i,
"size" : size
}
i = i + 1
t.append(w)
data.append(e)
write_json(data)
minify('game.json')
print("Exported {} functions".format(o))
except CancelledException:
pass