-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathgdb_engine.py
304 lines (215 loc) · 6.59 KB
/
gdb_engine.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
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
# shadow - De Mysteriis Dom jemalloc
import resource
try:
import gdb
except ImportError:
raise Exception('[shadow] gdb_engine is only usable from within gdb')
# cache
cache_dword_size = None
cache_page_size = None
cache_int_size = None
cache_offsets = {}
cache_values= {}
cache_sizes = {}
def to_int(val):
sval = str(val)
start = sval.find('0x')
if start != -1:
end = sval.find(':')
if end == -1:
end = sval.find('\n')
if end == -1:
return int(sval[start:], 16)
else:
return int(sval[start:end], 16)
else:
return int(sval[start:end], 16)
elif sval.startswith('unsigned int'):
return int(sval[len('unsigned int'):])
else:
return int(sval)
def buf_to_le(buf):
# this function is from seanhn's tcmalloc_gdb
tmp = 0
for i in range(0, len(buf)):
tmp |= (ord(buf[i]) << i * 8)
return tmp
def buf_to_val(buf):
val = 0
for i in range(0, len(buf)):
val |= (buf[i] << i * 8)
return val
def get_page_size():
global cache_page_size
if not cache_page_size:
cache_page_size = resource.getpagesize()
return cache_page_size
def get_xul_version():
return gdb.parse_and_eval('gToolkitVersion')
def get_arch_running():
arch = gdb.selected_frame().architecture().name()
if 'aarch64' == arch:
return 'Aarch64'
elif 'arm' in arch:
return 'ARM'
elif 'i386' == arch:
return 'x86'
elif 'x86-64' in arch:
return 'x86-64'
def get_arch():
# try to use GDB's architecture API first. If the program is not running
# this will throw an exception but so will `execute("info proc start")`
try:
return get_arch_running()
except gdb.error as e:
pass
# get the start of text
text_addr = None
for l in execute("info proc stat").split("\n"):
if l.startswith("Start of text:"):
text_addr = int(l.split(":")[1], 16)
break
# raise exception?
if text_addr is None:
return None
e_machine = read_memory(text_addr + 0x12 , 2)
if e_machine == 3:
return "x86"
if e_machine == 0x28:
return "ARM"
if e_machine == 0x3E:
return "x86-64"
if e_machine == 0xB7:
return "Aarch64"
# raise exception?
return None
def get_dword_size():
global cache_dword_size
if not cache_dword_size:
arch = get_arch()
if arch in ["x86", "ARM"]:
cache_dword_size = 4
if arch in ["x86-64", "Aarch64"]:
cache_dword_size = 8
return cache_dword_size
def int_size():
global cache_int_size
if not cache_int_size:
cache_int_size = 4
return cache_int_size
def offsetof(struct_name, member_name):
global cache_offsets
k = struct_name + "." + member_name
if k not in cache_offsets:
expr = '(size_t)&(((%s *)0)->%s) - (size_t)((%s *)0)' % \
(struct_name, member_name, struct_name)
cache_offsets[k] = to_int(gdb.parse_and_eval(expr))
return cache_offsets[k]
def sizeof(type_name):
global cache_sizes
k = type_name
if k not in cache_sizes:
cache_sizes[k] = to_int(gdb.parse_and_eval('sizeof(%s)' % (type_name)))
return cache_sizes[k]
def get_value(symbol, ignore_cache=False):
global cache_values
k = symbol
# stripped libc gdb fix
if symbol in ['arenas', 'je_arenas', 'chunk_rtree',
'g_thread_list', 'je_tcache_bin_info',
'tcache_bin_info']:
# fuck gdb
symbol = '*((unsigned long int *) &%s)' % symbol
if ignore_cache:
return gdb.parse_and_eval(symbol)
if k not in cache_values:
cache_values[k] = gdb.parse_and_eval(symbol)
return cache_values[k]
def addressof(symbol):
return get_value('&' + symbol)
def eval_expr(expr):
return gdb.parse_and_eval(expr)
def execute(expr):
return gdb.execute(expr, to_string = True)
def read_memory(addr, size):
proc = gdb.selected_inferior()
return buf_to_le(proc.read_memory(addr, size))
def read_bytes(addr, size):
proc = gdb.selected_inferior()
return bytearray(proc.read_memory(addr, size))
def read_bytearray(addr, size):
proc = gdb.selected_inferior()
return bytearray(proc.read_memory(addr, size))
# todo: check endianess
def read_dwords(addr, size):
proc = gdb.selected_inferior()
dword_size = get_dword_size()
ndwords = size * dword_size
bytearr = read_bytes(addr, ndwords)
dwords = []
for i in range(0, ndwords, dword_size):
dword = bytearr[i]
for j in range(1, dword_size):
dword += bytearr[i+j] << j * 8
dwords.append(dword)
return dwords
def read_dword(addr):
return read_dwords(addr, 1)[0]
def dword_from_buf(buf, off):
dword = 0
for i in range(0, get_dword_size()):
dword |= buf[off+i] << i * 8
return dword
def int_from_buf(buf, off):
dword = 0
for i in range(0, int_size()):
dword |= buf[off+i] << i * 8
return dword
def read_struct_member(buf, struct_name, member_name, size):
off = offsetof(struct_name, member_name)
val_bytes = buf[off:off+size]
if size > get_dword_size():
return val
val = 0
for i in range(0, size):
val |= (val_bytes[i] << i * 8)
return val
def search(start_addr, end_addr, dword):
search_expr = 'find 0x%x, 0x%x, 0x%s'
results = []
if dword.startswith('0x'):
dword = dword[len('0x'):]
search_str = search_expr % (start_addr, end_addr, dword)
out_str = gdb.execute(search_str, to_string = True)
str_results = out_str.split('\n')
for str_result in str_results:
if str_result.startswith('0x'):
results.append((str_result, start_addr))
return results
def modules_dict():
"""
modules_dict[objfile] = (start, end)
"""
modules_dict = {}
for ln in execute('info proc mappings').split('\n'):
# [start, end, size, offset, objfile]
l = ln.split()
# skip if objfile is missing
if len(l) < 4:
continue
# skip the first line
if l[0] == 'Start':
continue
#todo: filter out [stack], [vsyscall], etc?
if len(l) < 5:
objfile = l[0]
else:
objfile = l[4]
objfile = objfile.split("/")[-1]
start = int(l[0], 16)
end = int(l[1], 16)
if objfile not in modules_dict:
modules_dict[objfile] = [(start, end),]
else:
modules_dict[objfile].append((start, end))
return modules_dict