-
Notifications
You must be signed in to change notification settings - Fork 9
/
run.py
213 lines (180 loc) · 6.27 KB
/
run.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
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
#
# litefuzz project
#
# run.py
#
import os
import sys
import signal
import shutil
import subprocess32 as subprocess
import time
from time import time as timer
if(str(sys.platform).startswith('win32')):
from winappdbg import *
try:
FileNotFoundError # py3
except NameError:
FileNotFoundError = IOError # py2
import triage
import debug
import misc
import config
import settings
from settings import SUCCESS, FAILURE
#
# choose your own adventure
#
def main(cmdline):
if(misc.isUnix()):
return unix(cmdline)
elif(misc.isWin32()):
return windows(cmdline)
else:
return FAILURE
#
# Linux/Mac binary execution support
#
def unix(cmdline):
if(config.debug):
print("current_input: %s\n" % config.current_input)
print("cmdline: %s\n" % cmdline)
if(config.current_input == None):
print("[ERROR] current_input cannot be None\n")
return FAILURE
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)
startTime = timer()
if(config.debug):
print("\n[INFO] unix.run() @ starting target process: %s\n" % cmdline)
#
# capture stdout/stderr in a file
#
try:
with open(settings.FUZZ_OUTPUT, 'w') as file:
if('FUZZ' in cmdline):
process = subprocess.Popen(cmdline,
stdin=None,
stdout=file,
stderr=file,
preexec_fn=os.setsid,
env=config.env)
else:
process = subprocess.Popen(cmdline,
stdin=open(config.current_input),
stdout=file,
stderr=file,
preexec_fn=os.setsid,
env=config.env)
if(config.debug):
print("unix.run() %s started @ pid=%d\n" % (cmdline, process.pid))
#
# do not timeout insulated apps (interactive apps)
# and use sleep() instead of a process timeout if
# client or server fuzzing is in progress
#
if(config.insulate == False):
if(config.mode == settings.LOCAL):
(output, error) = process.communicate(timeout=config.maxtime)
if(error):
print("[ERROR] '%s' @ pid=%d: %s\n" % (cmdline, process.pid(), error))
else:
time.sleep(config.maxtime)
except subprocess.TimeoutExpired as error:
if(config.debug):
print("%s\n" % error)
misc.killProcess(process.pid)
except FileNotFoundError as error:
print("[ERROR] run.unix() @ main run cmdline: %s" % error)
sys.exit(FAILURE)
except IOError as error:
print("[INFO] run.unix() @ write(FUZZ_OUTPUT): %s" % error)
return FAILURE
except Exception as error:
print("[ERROR] run.unix() failed: %s" % error)
return FAILURE
config.process = process
#
# keep track of execution times for stats
#
if(len(config.exec_times) <= settings.MAX_AVG_EXEC):
config.exec_times.append(timer() - startTime)
#
# only do this for local apps in run.unix() because local
# client and server runs call this in their own functions
# because the process may not finish until after connection
#
if(config.mode == settings.LOCAL):
misc.checkForCrash(cmdline)
return SUCCESS
#
# make sure each process properly goes away after timeout
#
def win32KillAll(dbg):
if(config.debug):
print("entering run.win32KillAll()\n")
for pid in dbg.get_debugee_pids():
try:
dbg.detach(pid)
dbg.kill(pid)
except:
misc.killProcess(pid) # last ditch effort
# pass
#
# Win32 binary execution support
#
# (based on https://winappdbg.readthedocs.io/en/latest/MoreExamples.html)
#
# note: stdin fuzzing not supported on win32
#
def windows(cmdline):
if(config.debug):
print("entering run.windows() with cmdline %s\n" % cmdline)
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)
with Debug(debug.win32CrashHandler, bKillOnExit = True) as dbg:
try:
dbg.execv(cmdline)
System.set_kill_on_exit_mode(True)
except Exception as error:
print("\n[ERROR] run.windows() @ main run: %s\n" % error)
sys.exit(FAILURE)
config.dbg32 = dbg # so we can kill the eg. local server process as needed
#
# let local network servers join the '9 club
#
if(config.mode == settings.LOCAL_SERVER):
maxtime = 9999999 # don't stop, running
else:
maxtime = config.maxtime
timeout = timer() + maxtime
while(dbg and timer() < timeout):
try:
dbg.wait(1000)
except:
continue
try:
try:
dbg.dispatch()
except Exception as error:
if(config.debug):
print("[INFO] run.windows() @ dbg dispatch: %s" % error)
finally:
try:
dbg.cont()
except Exception as error:
if(config.debug):
print("[INFO] run.windows() @ dbg continue: %s" % error)
try:
win32KillAll(dbg)
except Exception as error:
if(config.debug):
print("[INFO] run.windows() @ dbg continue: %s" % error)
# pass
return SUCCESS