-
Notifications
You must be signed in to change notification settings - Fork 2
/
probe.py
172 lines (134 loc) · 4.9 KB
/
probe.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
"""
Copyright notice
================
Copyright (C) 2010
Lorenzo Martignoni <[email protected]>
Roberto Paleari <[email protected]>
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
ProcessTap is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
"""
import event
import probeexp
_probes = {
event.FUNCTION_ENTRY : [],
event.FUNCTION_EXIT : [],
event.SYSCALL_ENTRY : [],
event.SYSCALL_EXIT : [],
event.MEMORY_READ : [],
event.MEMORY_WRITE : [],
event.MEMORY_EXECUTE : [],
}
def probe_not_callable():
raise Exception("[!] Probe is not callable")
class probe:
def __init__(self, event, cond = None):
if cond is None:
cond = probeexp.ProbeConstant(True)
elif not isinstance(cond, probeexp.ProbeExpression):
raise Exception("[!] Invalid function specification %s" % str(cond))
self.event = event
self.condition = cond
self.callback = None
self.enabled = True
def __call__(self, callback = None):
self.callback = callback
_probes[self.event] += [self]
return probe_not_callable
def __str__(self):
return "%s %s" % (self.condition, self.callback.__name__)
def eval_condition(self, ev, env):
return eval(self.condition.actualize(ev, env))
def gen_filter(self, t):
return self.condition.generateFilter(t)
def run_callback(self, ev, env):
if self.eval_condition(ev, env):
env.event = ev
return self.callback(env)
else:
return
class function_entry(probe):
def __init__(self, cond = None):
probe.__init__(self, event.FUNCTION_ENTRY, cond)
class function_exit(probe):
def __init__(self, cond = None):
probe.__init__(self, event.FUNCTION_EXIT, cond)
class syscall_entry(probe):
def __init__(self, cond = None):
probe.__init__(self, event.SYSCALL_ENTRY, cond)
class syscall_exit(probe):
def __init__(self, cond = None):
probe.__init__(self, event.SYSCALL_EXIT, cond)
class memory_read(probe):
def __init__(self, cond = None):
probe.__init__(self, event.MEMORY_READ, cond)
class memory_write(probe):
def __init__(self, cond = None):
probe.__init__(self, event.MEMORY_WRITE, cond)
class memory_execute(probe):
def __init__(self, cond = None):
probe.__init__(self, event.MEMORY_EXECUTE, cond)
def show_probes():
print "[*] Loaded probes:"
for ev, pp in _probes.iteritems():
if not pp: continue
print " [*] %s" % event.event2str[ev]
for p in pp:
if p.enabled:
print " [+] %s" % p
else:
print " [-] %s" % p
def run_probes(ev, env):
for probe in _probes[ev.type]:
probe.run_callback(ev, env)
def enabled_probes():
ee = 0
for e, pp in _probes.iteritems():
if pp:
ee |= e
return ee
def filters(t):
f = []
for e, pp in _probes.iteritems():
for p in pp:
f += p.gen_filter(t)
return f
if __name__ == "__main__":
import symbol
from probeexp import function_name, syscall_num, syscall_name, memory_size, process_id
symbol.init("")
@function_entry(function_name == "xxx")
@function_entry(function_name == "fopen")
def wrap_function_entry(env):
print "Calling wrap_function_entry()"
@function_exit()
def wrap_function_exit(env):
print "Calling wrap_function_exit()"
@syscall_entry(syscall_name >> ["open", "close"])
def wrap_syscall_entry(env):
print "Calling wrap_syscall_entry()"
@syscall_exit()
def wrap_syscall_exit(env):
print "Calling wrap_syscall_exit()"
@memory_read()
def wrap_mem_read(env):
print "Calling wrap_mem_read()"
@memory_write(memory_size == 1)
def wrap_mem_write(env):
print "Calling wrap_mem_write()"
@function_exit((process_id == 20) & (function_name == "malloc"))
def wrap_function_exit2(env):
print "Calling wrap_function_exit2()"
show_probes()
e = event.function_entry(pid = 20, tid = 15, inst = 0xbadbabe, stack = 0xdeadbeef, funcaddr = 0xcafebabe)
print "[*] Dispatching event %s" % e
run_probes(e, None)
e = event.syscall_entry(pid = 20, tid = 15, inst = 0xbadbabe, stack = 0xdeadbeef, sysno = 18)
print "[*] Dispatching event %s" % e
run_probes(e, None)