-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdebug.py
473 lines (359 loc) · 12.9 KB
/
debug.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
#
# litefuzz project
#
# debug.py
#
#
import os
import sys
import glob
import shutil
import subprocess32 as subprocess
import time
if(str(sys.platform).startswith('win32')):
from winappdbg import *
import core
import triage
import misc
import config
import settings
from settings import SUCCESS, FAILURE
#
# choose your own adventure 2
#
def main(cmdline):
if(misc.isLinux()):
return gdb(cmdline)
elif(misc.isMac()):
return lldb(cmdline)
else: # winappdbg already handles this for win32
return FAILURE
#
# Linux crash triage with the gdb debugger
#
def gdb(cmdline):
if(config.debug):
print("entering debug.gdb()\n")
target = cmdline[0]
args = cmdline[1:]
debug_timeout = (config.maxtime * settings.DEBUG_TIMEOUT_MULTIPLE)
#
# if we only have a relative name for the target, get the full path for gdb
#
if(os.sep not in target):
target = misc.getExePath(target)
if(config.debug):
print("target=%s, args=%s\n" % (target, args))
debug_cmdline = []
debug_cmdline.append('gdb')
debug_cmdline.append('-q')
debug_cmdline.append('-ex')
debug_cmdline.append('file "' + target + '"')
if(settings.DEBUG_ENV_EFENCE_GLIBC_ENABLE):
debug_cmdline.append('-ex')
#
# set environment LD_PRELOAD /usr/lib/libefence.so
# r ...
#
if(settings.DEBUG_ENV_EFENCE_ENABLE):
debug_cmdline.append('set environment LD_PRELOAD ' + settings.LIBEFENCE_PATH)
else:
debug_cmdline.append('set environment ' + settings.GLIBC_MALLOC_CHECK) # fallback
debug_cmdline.append('-ex')
debug_cmdline.append('shell echo')
debug_cmdline.append('-ex')
run_cmdline = 'run '
if(args != None):
for arg in args:
run_cmdline += arg + ' '
run_cmdline = run_cmdline[:-1]
debug_cmdline.append(run_cmdline)
debug_cmdline.append('-ex')
debug_cmdline.append('bt')
debug_cmdline.append('-ex')
debug_cmdline.append('shell echo')
debug_cmdline.append('-ex')
debug_cmdline.append('info registers')
if(config.golang):
debug_cmdline.append('-ex')
debug_cmdline.append('disas $pc')
else:
# bang exploitable already provides a short disassembly, but it can fail
debug_cmdline.append('-ex')
debug_cmdline.append('x/4i $pc')
debug_cmdline.append('-ex')
debug_cmdline.append('exploitable -v')
debug_cmdline.append('-ex')
debug_cmdline.append('set confirm off')
debug_cmdline.append('-ex')
debug_cmdline.append('quit')
if(config.debug):
print("%s" % debug_cmdline)
if(run(debug_cmdline, debug_timeout) == FAILURE):
if(config.debug):
print("debug.gdb() calling debug.run() failure\n")
sys.exit(FAILURE)
return SUCCESS
#
# Mac crash triage with the lldb debugger
#
# reference some stuff in https://github.com/bnagy/francis/blob/master/exploitaben/exploitaben.py
# it would be nice if one could get exploitaben working for !exploitable-like triage
#
def lldb(cmdline):
if(config.debug):
print("entering debug.lldb()\n")
target = cmdline[0]
args = cmdline[1:]
debug_timeout = (config.maxtime * settings.DEBUG_TIMEOUT_MULTIPLE)
if(config.debug):
print("target=%s, args=%s\n" % (target, args))
debug_cmdline = []
debug_cmdline.append('lldb')
debug_cmdline.append('-o')
debug_cmdline.append('target create "' + cmdline[0] + '"')
if(settings.DEBUG_ENV_GMALLOC_ENABLE):
debug_cmdline.append('-o')
debug_cmdline.append('settings set target.env-vars DYLD_INSERT_LIBRARIES=' + settings.LIBGMALLOC_PATH)
else:
if(settings.DEBUG_ENV_GLIBC_ENABLE):
debug_cmdline.append('-o')
debug_cmdline.append('settings set target.env-vars ' + settings.GLIBC_MALLOC_CHECK) # fallback
debug_cmdline.append('-o')
run_cmdline = 'run '
if(args != None):
for arg in args:
run_cmdline += arg + ' '
run_cmdline = run_cmdline[:-1]
debug_cmdline.append(run_cmdline)
debug_cmdline.append('-o')
# debug_cmdline.append('bt 24')
debug_cmdline.append('bt')
debug_cmdline.append('-o')
debug_cmdline.append('reg read')
debug_cmdline.append('-o')
debug_cmdline.append(settings.DEBUG_DISSASSEMBLE_LLDB)
debug_cmdline.append('-o')
debug_cmdline.append('quit')
if(config.debug):
print("\ndebug_cmdline: %s\n" % debug_cmdline)
if(run(debug_cmdline, debug_timeout) == FAILURE):
if(config.debug):
print("debug.lldb() calling debug.run() failure\n")
sys.exit(FAILURE)
return SUCCESS
#
# debugger execution
#
def run(cmdline, timeout):
if(config.debug):
print("\nentering debug.run()\n")
print("writing debug log to %s\n" % settings.FUZZ_INFO)
if(settings.KILL_EXISTING_PROCESS):
if(config.debug):
print("killing any running processes named %s before running a new one\n" % config.target)
misc.killProcessByName(config.target)
#
# try and prevent the new server from not starting because the port is still occupied
#
if(config.mode == settings.LOCAL_SERVER):
if(misc.checkPort(config.prot, 'localhost', config.port)):
if(config.process != None):
misc.killProcess(config.process.pid)
misc.killProcessByName(config.target)
try:
with open(settings.FUZZ_INFO, 'w') as file:
if(settings.FUZZ_FILE in cmdline):
process = subprocess.Popen(cmdline,
stdin=None,
stdout=file,
stderr=file,
preexec_fn=os.setsid)
else:
if(config.mode == settings.LOCAL): # don't do this for local clients/servers
process = subprocess.Popen(cmdline,
stdin=open(config.current_input),
stdout=file,
stderr=file,
preexec_fn=os.setsid)
else:
process = subprocess.Popen(cmdline,
stdin=None,
stdout=file,
stderr=file,
preexec_fn=os.setsid)
if(config.mode == settings.LOCAL):
(output, error) = process.communicate(timeout=timeout)
if(error):
print("[ERROR] '%s' @ pid=%d: %s\n" % (cmdline, process.pid(), error))
else:
time.sleep(config.maxtime) # works, but no visibility into crashes
except subprocess.TimeoutExpired as error:
if(config.debug):
print("%s\n" % error)
misc.killProcess(process.pid)
except Exception as error:
print("[ERROR] debug.run() failed: %s\n" % error)
try:
file.write("debugger run failed: %s" % error)
except Exception as error:
print("[ERROR] debug.run() failed @ writing about it's failure: %s\n" % error)
return FAILURE
return FAILURE
config.process = process
if(config.insulate):
config.insulate_pid = process.pid
#
# set this here to make sure we're current on the latest debugger runs
#
settings.FUZZ_INFO_STATIC = settings.FUZZ_INFO
if(config.debug):
print("debug.run() %s started @ pid=%d\n" % (cmdline, process.pid))
return SUCCESS
#
# attach to a process
#
# Reference: https://opensource.apple.com/source/lldb/lldb-159/docs/lldb-for-gdb-users.txt.auto.html
#
def attach(debugger, pid):
if(config.debug):
print("entering debug.attach()\n")
settings.KILL_EXISTING_PROCESS = False
debug_timeout = 9999999 # don't... just don't
debug_cmdline = []
if(config.debug):
print("debugger=%s and attaching to pid=%d\n" % (debugger, pid))
if(debugger == 'gdb'):
print("gdb support is unimplemented\n")
return FAILURE
if(debugger == 'lldb'):
debug_cmdline.append('sudo') # usually needed for attaching
debug_cmdline.append('lldb')
debug_cmdline.append('-o')
debug_cmdline.append('attach -p ' + str(pid)) # also attach --name NAME
debug_cmdline.append('-o')
debug_cmdline.append('continue')
debug_cmdline.append('-o')
debug_cmdline.append('bt')
debug_cmdline.append('-o')
debug_cmdline.append('reg read')
#
# command script import lldb.macosx.heap
# malloc_info -S $rax
#
debug_cmdline.append('-o')
debug_cmdline.append(settings.DEBUG_DISSASSEMBLE_LLDB)
debug_cmdline.append('-o')
debug_cmdline.append('detach')
debug_cmdline.append('-o')
debug_cmdline.append('quit')
if(config.debug):
print("\ndebug_cmdline: %s\n" % debug_cmdline)
if(run(debug_cmdline, debug_timeout) == FAILURE):
if(config.debug):
print("debug.attach() calling debug.run() failure\n")
sys.exit(FAILURE)
return SUCCESS
#
# Windows console debugger crash triage with memory dumps via !analyze
#
# (winappdbg already has some !exploitable type analysis)
#
def win32Analyze():
if(config.debug):
print("entering debug.win32Analyze()\n")
time.sleep(5) # delay to make sure WER has time to generate the dump file
crash_dir = os.path.abspath(os.getcwd()) + os.sep + settings.CRASH_DIR
hash = misc.getHash(misc.readBytes(settings.FUZZ_FILE))
if(hash == None):
hash = 'UNKNOWN'
#
# get pid and create paths to both generated dmp file and it's final location
#
dump_file_orig = settings.TMP_DIR + \
os.sep + \
misc.addExtExe(os.path.basename(config.target)) + \
'.' + \
str(config.memdump_pid) + \
'.dmp'
dump_file = crash_dir + \
os.sep + \
misc.addExtExe(os.path.basename(config.target)) + \
'.' + \
str(config.memdump_pid) + \
'_' + \
hash + \
'.dmp'
dump_log = crash_dir + \
os.sep + \
misc.addExtExe(os.path.basename(config.target)) + \
'.' + \
str(config.memdump_pid) + \
'_' + \
hash + \
'.log'
if(config.debug):
print("dump file = %s\n" % dump_file)
print("dump file (orig) = %s\n" % dump_file_orig)
try:
shutil.copy(dump_file_orig, dump_file)
except Exception as error:
if(config.debug):
print("\n[ERROR] debug.win32Analyze() @ copy dump file: %s\n" % error)
return FAILURE
timeout = (config.maxtime * settings.DEBUG_TIMEOUT_MULTIPLE)
cmdline = []
cmdline.append(settings.CONSOLE_DEBUGGER_PATH)
cmdline.append('-kqm')
cmdline.append('-nosqm')
cmdline.append('-c')
cmdline.append('.symfix; .reload; !analyze -v')
cmdline.append('-z')
cmdline.append(dump_file)
#
# pass dump file to console debugger and append output to fuzz info file
#
try:
with open(dump_log, 'w') as file:
process = subprocess.Popen(cmdline,
stdin=None,
stdout=file,
stderr=file)
(output, error) = process.communicate(timeout=timeout)
except subprocess.TimeoutExpired as error:
if(config.debug):
print("%s\n" % error)
misc.killProcess(process.pid)
except Exception as error:
if(config.debug):
print("[ERROR] debug.win32Analyze() failed: %s\n" % error)
return FAILURE
return SUCCESS
#
# Crash handler for win32
#
def win32CrashHandler(event):
event_code = event.get_event_code()
#
# catch interesting crashes
#
if(event_code == win32.EXCEPTION_DEBUG_EVENT and event.is_last_chance()):
if(config.debug):
print("entering win32CrashHandler() with event code %d (last chance)\n" % event_code)
config.crash = True
exception_name = event.get_exception_name()
if(exception_name != 'EXCEPTION_ACCESS_VIOLATION'):
fault_type = exception_name
else:
fault_type = event.get_fault_type()
process = Process(event.get_pid())
cmdline = process.get_command_line()
crash = Crash(event)
info = crash.fullReport()
if(config.debug):
print("current_input: %s\n" % config.current_input)
if(settings.MEMORY_DUMP):
config.memdump_pid = event.get_pid()
triage.win32(fault_type, cmdline, info)