-
Notifications
You must be signed in to change notification settings - Fork 38
/
tfuzz_fuzzer.py
208 lines (160 loc) · 6.83 KB
/
tfuzz_fuzzer.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
import logging
import shutil
import threading
import time
import os
import re
from fuzzer import Fuzzer as SFFuzzer
import angr
from .tprogram import TProgram
from .executor import Executor
from .utils import replace_input_placeholder
logger = logging.getLogger("tfuzz.tfuzz_fuzzer")
class Fuzzer(object):
'''
Wrapper class if shellphish fuzzer, which is, in turn,
a python wrapper for AFL.
'''
def __init__(self, tprogram, seed_files, workdir, target_opts=None,
input_placeholder='@@', afl_opts=None):
self.tprogram = tprogram
self.target_opts = target_opts
self.workdir = workdir
self.stat_file = os.path.join(self.workdir,
tprogram.program_name, 'stat')
self.stat = {}
self.crashing_inputs = []
self.tmout_inputs = []
if seed_files == None:
seed_files = self.__prepare_predefined_seeds()
seeds = []
logger.debug("Starting to classify the seeds for %s", tprogram.program_name)
for sf in seed_files:
logger.debug("Trying running %s", sf)
ret = self.__get_exec_result_on_input(sf)
if ret == 2:
# the seed results in crash
self.crashing_inputs.append(sf)
elif ret == 1:
# the seed results in timeout
self.tmout_inputs.append(sf)
else:
seeds.append(file(sf).read())
logger.debug("Classifying the seeds for %s ended", tprogram.program_name)
use_qemu = False
if tprogram.is_cgc():
use_qemu = True
logger.debug("fuzzing %s with opts: %s", self.tprogram.program_path,
str(target_opts))
self._fuzzer = SFFuzzer(self.tprogram.program_path, self.workdir,
seeds=seeds, qemu=use_qemu,
create_dictionary=False,
target_opts=target_opts, extra_opts=afl_opts)
# save the seed files resulting in crashes and timeout
self.save_crash_and_tmout_inputs()
def __prepare_predefined_seeds(self):
_pre_seeds_dir = os.path.join(self.workdir, '_pre_seeds')
_pre_seed_file = os.path.join(_pre_seeds_dir, 'seed-0')
if not os.path.exists(_pre_seeds_dir):
os.makedirs(_pre_seeds_dir)
with open(_pre_seed_file, 'w') as f:
f.write('fuzz')
return [_pre_seed_file]
def __str__(self):
return "<Fuzzer:(tprogram:%s, workdir:%s)>" % \
(self.tprogram, self.workdir)
def __repr__(self):
return self.__str__()
def start(self):
self.stat['status'] = 'running'
self._fuzzer.start()
def stop(self):
self._fuzzer.kill()
self.stat['status'] = 'stopped'
self.write_stat()
def __find_generated_files(self, afl_instance, subdir):
if not os.path.exists(os.path.join(self._fuzzer.out_dir,
afl_instance, subdir)):
raise ValueError("subdir: '%s' in afl instance '%s' does not exist"\
% (subdir, afl_instance))
subdir = os.path.join(self._fuzzer.out_dir, afl_instance, subdir)
generated_files = filter(lambda x: x.startswith('id:'),
os.listdir(subdir))
return map(lambda x: os.path.join(subdir, x), generated_files)
def generated_inputs(self, afl_instance='fuzzer-master'):
return self.__find_generated_files(afl_instance, 'queue')
def crashes_found(self, afl_instance='fuzzer-master'):
return self.__find_generated_files(afl_instance, 'crashes')
def crash_seeds(self):
cs_dir = os.path.join(self._fuzzer.job_dir, 'crashing_seeds')
cs_files = filter(lambda x: x.startswith('crash_seed_'),
os.listdir(cs_dir))
return map(lambda x: os.path.join(cs_dir, x), cs_files)
def timeout_seeds(self):
timeout_seed_dir = os.path.join(self._fuzzer.job_dir, 'tmout_seeds')
timeout_seed_files = filter(lambda x: x.startswith('tmout_seed_'),
os.listdir(cs_dir))
return map(lambda x: os.path.join(cs_dir, x), cs_files)
def failed_to_start(self):
afl_log_file = os.path.join(self._fuzzer.job_dir, 'fuzzer-master.log')
if not os.path.exists(afl_log_file):
return True
ansi_escape = re.compile(r'\x1b[^m]*m')
with open(afl_log_file, 'r') as f:
for l in f.readlines():
l = l.strip()
ansi_escape.sub('', l)
if "[-] PROGRAM ABORT :" in l:
return True
return False
def __get_exec_result_on_input(self, input_file):
args = replace_input_placeholder(self.target_opts, input_file)
# here we hard-code the max time
# to run a target program by 1 sec
executor = Executor(self.tprogram.program_path,
target_opts=args, timeout=1)
if executor.tmout:
return 1
if executor.crash:
return 2
return 0
def save_crash_and_tmout_inputs(self, additional_crash_input_files=None,
additional_tmout_input_files=None):
ci_dir = os.path.join(self._fuzzer.job_dir, 'crashing_seeds')
tmout_dir = os.path.join(self._fuzzer.job_dir, 'tmout_seeds')
if not os.path.exists(ci_dir):
os.makedirs(ci_dir)
if not os.path.exists(tmout_dir):
os.makedirs(tmout_dir)
if additional_crash_input_files != None:
for c in additional_crash_input_files:
self.crashing_inputs.append(c)
if additional_tmout_input_files != None:
for c in additional_tmout_input_files:
self.tmout_inputs.append(c)
idx = 0
for i in self.crashing_inputs:
shutil.copyfile(i, os.path.join(ci_dir,
'crash_seed_' + str(idx)))
idx = idx + 1
idx = 0
for i in self.tmout_inputs:
shutil.copyfile(i, os.path.join(tmout_dir,
'tmout_seed_' + str(idx)))
idx = idx + 1
def resuming(self):
return self._fuzzer.resuming
def is_stuck(self):
try:
pending_favs = self._fuzzer.stats['fuzzer-master']['pending_favs']
except:
return False
return int(pending_favs) == 0
def write_stat(self):
if not os.path.exists(os.path.dirname(self.stat_file)):
os.makedirs(os.path.dirname(self.stat_file))
with open(self.stat_file, 'w') as f:
for key, val in self.stat.items():
f.write("%s:%s\n" % (key, val))
def __del__(self):
self.write_stat()