-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctionHooks.py
144 lines (104 loc) · 3.64 KB
/
functionHooks.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
import logging
logging.getLogger('angr').setLevel('CRITICAL') #level: WARNING, INFO, NOTSET, DEBUG, ERROR, CRITICAL
import angr, claripy
import sys, os
import IPython
import monkeyhex
class HookClass(angr.SimProcedure):
def run(self, return_values=None):
pass
class HookClassAdd(angr.SimProcedure):
def run(self, return_values=None, symbolic_return=True):
return return_values[0]
def test1(p):
p.hook_symbol('print1', HookClass())
p.hook_symbol('print2', HookClass())
p.unhook_symbol('print2')
state = p.factory.entry_state()
simgr = p.factory.simgr(state)
simgr.run()
print(simgr.deadended[0].posix.dumps(1))
IPython.embed()
def test2(p):
p.hook_symbol('add1', HookClassAdd(return_values=[27]))
p.hook_symbol('add', HookClassAdd())
p.unhook_symbol('add')
state = p.factory.entry_state()
simgr = p.factory.simgr(state)
simgr.run()
print(simgr.deadended[0].posix.dumps(1))
IPython.embed()
def getEntryFunction(project: angr.Project):
cfg = project.analyses.CFGFast()
entry_func = cfg.kb.functions[project.entry]
return entry_func
def getListOfFunctionsInMain(project: angr.Project):
entry_func = getEntryFunction(project)
functions = entry_func.functions_called()
return functions
class Hook1(angr.SimProcedure):
def run(self):
print("add1 hooked")
self.state.regs.rax = claripy.BVS("ret1", 32)
print(self.state.regs.rax)
def test3(p):
functions = getListOfFunctionsInMain(p)
print(functions)
a = functions.pop()
a = functions.pop()
a = functions.pop()
a = functions.pop()
a = functions.pop()
a = functions.pop()
a = functions.pop()
print(a.name, a.addr)
p.hook(a.addr, hook=Hook1(), length=5)
#s = p.analyses.CallingConvention(a, cfg=cfg, analyze_callsites=True)
s = p.analyses.CallingConvention(a, analyze_callsites=True)
print ("Analysed function header: ", s.prototype)
print("| Size of ret:", s.prototype.returnty.with_arch(p.arch).size)
state = p.factory.full_init_state()
simgr = p.factory.simgr(state)
simgr.run()
print(simgr.stashes)
print(simgr.deadended[0].posix.dumps(1))
IPython.embed()
#check for syscall
def test4(p):
functions = getListOfFunctionsInMain(p)
for f in functions:
print(f.name, "sim_procedure?", f.is_simprocedure, "syscall?", f.is_syscall, f.is_plt)
#with plt and simprocedure we can catch all functions that do not need to be simulated
IPython.embed()
def test5(p):
#p.hook(a.addr, hook=Hook1(), length=5)
#a = p.loader.find_symbol('add1')
cfg = p.analyses.CFGFast()
a = cfg.kb.functions['add']
s = p.analyses.CallingConvention(a, cfg=cfg, analyze_callsites=True)
#s = p.analyses.CallingConvention(a, analyze_callsites=True)
print ("Analysed function header: ", s.prototype)
IPython.embed()
print("| Size of ret:", s.prototype.returnty.with_arch(p.arch).size)
p.hook_symbol('add', Hook1())
state = p.factory.full_init_state()
simgr = p.factory.simgr(state)
simgr.run()
print(simgr.stashes)
print(simgr.deadended[0].posix.dumps(1))
IPython.embed()
#test calling convention recovery
def test6(p):
cfg = p.analyses.CFGFast()
a = p.analyses.CompleteCallingConventions(recover_variables=True, force=True, cfg=cfg, analyze_callsites=True)
print(a.kb.functions["add1"].prototype)
IPython.embed()
#TODO TEST MORE HEADER DETECTION
def main(argv):
if len(argv) < 2:
return
prog_name = argv[1]
p = angr.Project(prog_name, auto_load_libs=False)
test4(p)
if __name__ == '__main__':
main(sys.argv)