-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfoozzer.py
346 lines (276 loc) · 9.58 KB
/
foozzer.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
#!/usr/bin/env python3
"""
A cross platform fuzzing framework primarily targeting GUI applications.
usage: foozzer.py [-h] [--verbose] [-L] -i I -o O -D D -m MUTATOR -r RUNNER -- RUNNER_ARGS
runner_args
Options:
-h
--help show help message and exit
-v
--verbose increase output verbosity (can be given multiple times)
-L describe available plugins
-i I input directory
-o O output directory
-D D Dr.Memory bin directory
-m M mutator to use
-r R runner to use
RUNNER_ARGS arguments passed to selected runner module
"""
# Copyright (c) 2020 tick <tickelton@gmail.com>
# SPDX-License-Identifier: ISC
import os
import sys
import shutil
import argparse
import importlib
import pkgutil
import logging
from time import sleep
from subprocess import PIPE, STDOUT, Popen
from threading import Thread
from queue import Queue, Empty
from typing import List, Dict, Tuple, Any, TextIO, Union, Sequence, Optional, Generator
import foozzer.mutators
import foozzer.runners
ON_POSIX = os.name == 'posix'
# binaries
if ON_POSIX:
DRMEMORY_BIN = 'drmemory'
else:
DRMEMORY_BIN = 'drmemory.exe'
DRMEMORY_PARAMS = '-batch'
# misc constants
RUNFILE = 'foozzer.run'
PAUSEFILE = 'foozzer.pause'
LOG_OUTFILE = 'log.txt'
# logging configuration
logger = logging.getLogger()
handler = logging.StreamHandler()
formatter = logging.Formatter(
'%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.ERROR)
def enqueue_output(out: TextIO, queue: 'Queue[str]') -> None:
"""Helper function for non-blocking reading of child STDOUT."""
for line in iter(out.readline, ''):
queue.put(line)
out.close()
def clear_queue(queue: 'Queue[str]', outfile: TextIO) -> None:
"""Helper function for non-blocking reading of child STDOUT."""
while True:
# non-blocking readline
try:
line = queue.get_nowait()
except Empty:
break
else:
outfile.write(line)
def startall(queue: 'Queue[str]', drmemory_bin: str, target_cmdline: List[str]) -> Tuple['Popen[str]', Thread]:
"""Starts fuzzee child process and thread for STDOUT queue."""
logger.debug(
'startall: drmemory_bin=%s target_cmdline=%s',
drmemory_bin,
target_cmdline
)
drmem = Popen(
[drmemory_bin, DRMEMORY_PARAMS, '--'] + target_cmdline,
stdout=PIPE,
stderr=STDOUT,
bufsize=1,
universal_newlines=True,
close_fds=ON_POSIX
)
qthread = Thread(target=enqueue_output, args=(drmem.stdout, queue))
#qthread.daemon = True
qthread.start()
sleep(1)
if drmem.poll() is not None:
logger.error('SOMETHING WENT WRONG!!')
qthread.join()
sys.exit(1)
return drmem, qthread
def stop_processes(target: str) -> None:
"""Stops fuzzee and Dr.Memrory processes, if running."""
if ON_POSIX:
os.system('pkill {}'.format(target))
else:
os.system('taskkill /t /im {}'.format(target))
sleep(2)
os.system('taskkill /t /im drmemory.exe')
def stopall(qthread: Thread, target: str) -> None:
"""Stops child processes and queue thread."""
stop_processes(target)
sleep(5)
qthread.join()
class ActionListPlugins(argparse.Action):
"""Argparser helper class to show plugins descriptions."""
def __init__(self, option_strings, dest, const, **kwargs):
self._descriptions = const
super().__init__(option_strings, dest, **kwargs)
def __call__(self, parser, namespace, values, option_string):
for plugin_type in self._descriptions:
print('\n{}:\n'.format(plugin_type))
for k, val in self._descriptions[plugin_type].items():
print(' {} : {}'.format(k, val))
print('')
sys.exit(0)
def iter_namespace(ns_pkg):
"""Helper function for plugin discovery."""
return pkgutil.iter_modules(ns_pkg.__path__, ns_pkg.__name__ + ".")
# TODO: Replace 'Any' with proper type
def discover_plugins(namespc) -> Dict[str, Tuple[str, Any]]:
"""
Discovers mutator and runner plugins.
Retrieves entry points of the modules and descriptions for help texts.
"""
plugins = {}
for finder, name, ispkg in iter_namespace(namespc):
try:
plugins.update(importlib.import_module(name).get_module_info()) # type: ignore
except AttributeError:
# If the module does not provide a get_module_info function
# it is probably an abstract base class or utility library.
# Anyways, since in that case we have no way to determine its
# correct entry point, we just ignore it.
pass
return plugins
def do_parse_args(args, mutators, runners) -> argparse.Namespace:
"""Argument parsing helper function."""
parser = argparse.ArgumentParser()
parser.add_argument('--verbose', '-v', action='count', default=0)
parser.add_argument(
'-L',
nargs=0,
action=ActionListPlugins,
help='describe available plugins',
const={
'Mutators': {n: mutators[n][0] for n in mutators},
'Runners': {n: runners[n][0] for n in runners},
}
)
parser.add_argument(
'-i',
required=True,
help='input directory'
)
parser.add_argument(
'-o',
required=True,
help='output directory'
)
parser.add_argument(
'-D',
required=True,
help='Dr.Memory bin directory'
)
parser.add_argument(
'-m',
required=True,
choices = [m for m in mutators],
help='mutator to use'
)
parser.add_argument(
'-r',
required=True,
choices = [m for m in runners],
help='runner to use'
)
parser.add_argument(
'-f',
help='filename as seen by target process'
)
parser.add_argument('runner_args', nargs=argparse.REMAINDER)
return parser.parse_args(args)
def main(args=None) -> None:
"""foozzer.py main function"""
mutators = discover_plugins(foozzer.mutators)
runners = discover_plugins(foozzer.runners)
args = do_parse_args(args, mutators, runners)
if args.verbose == 1:
logger.setLevel(logging.WARNING)
elif args.verbose == 2:
logger.setLevel(logging.INFO)
elif args.verbose > 2:
logger.setLevel(logging.DEBUG)
runner_class = runners[args.r][1]
runner = runner_class(args.runner_args[1:])
target_process = runner.get_process_name()
input_mutator = mutators[args.m][1]
stop_processes(target_process)
queue: 'Queue[str]' = Queue()
drmem, qthread = startall(queue, os.path.join(args.D, DRMEMORY_BIN), runner.get_cmdline())
logger.info('Opening logfile')
log_outfile = open(os.path.join(args.o, LOG_OUTFILE), 'a')
runfile_path = os.path.join(args.o, RUNFILE)
pausefile_path = os.path.join(args.o, PAUSEFILE)
mutator = input_mutator(args.i, args.o)
i = 0
logger.debug('Clearing queue initially')
clear_queue(queue, log_outfile)
stop_processes(target_process)
runner.setup()
log_outfile.flush()
logger.info('MAINLOOP START')
for input_file, state_msg in mutator:
if not os.path.isfile(runfile_path):
logger.info('Stopping due to missing run file: %s', runfile_path)
break
logger.debug('clearing queue')
clear_queue(queue, log_outfile)
logger.debug('checking if Dr.Memory is still running')
if drmem.poll() is not None:
logger.info('RESTARTING Dr.Memory')
stopall(qthread, target_process)
drmem, qthread = startall(
queue,
os.path.join(args.D, DRMEMORY_BIN),
runner.get_cmdline()
)
runner.setup()
if os.path.isfile(pausefile_path):
logger.info('pausing...')
while os.path.isfile(pausefile_path):
sleep(1)
if args.f:
# If a specific filename was requested for the target
# appliction with the argument '-f', copy the file the
# mutator generated to that destination.
shutil.copyfile(
os.path.join(args.o, input_file),
os.path.join(args.o, args.f)
)
input_file = args.f
logger.debug('Iteration: %s\n', state_msg)
log_outfile.flush()
log_outfile.write('FOOZZER: Iteration: {}\n'.format(state_msg))
log_outfile.flush()
# TODO: make it clear that an unsuccessful run requires a
# reset of the fuzzing process, e.g. by returning
# some meaningful value like 'RUNNER_{FAILURE|RESTART}'...
if not runner.run(input_file):
log_outfile.write('Resetting after Runner error')
logger.warning('Resetting after Runner error')
clear_queue(queue, log_outfile)
if drmem.poll() is None:
drmem.terminate()
stopall(qthread, target_process)
drmem, qthread = startall(
queue,
os.path.join(args.D, DRMEMORY_BIN),
runner.get_cmdline()
)
runner.setup()
i += 1
logger.info('MAINLOOP END')
if drmem.poll() is None:
logger.debug('terminating')
drmem.terminate()
stopall(qthread, target_process)
clear_queue(queue, log_outfile)
log_outfile.flush()
log_outfile.write('FOOZZER: FINISHED')
log_outfile.close()
logger.info('FINISHED')
if __name__ == '__main__':
main()