-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrandom_fuzzer.py
313 lines (257 loc) · 12.8 KB
/
random_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
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
# -*- coding: utf-8 -*-
# Copyright (C) 2018 Bruno Melo <[email protected]>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import time
import getopt
import signal
import os
import sys
from collections import OrderedDict
from scapy.all import *
from scapy.contrib.coap import *
from boofuzz import pedrpc
from utils import *
from coap_target import Target
from fuzzer_models import *
##################################################################################################
# Fuzzer Object
##################################################################################################
class Fuzzer():
def __init__(self, targets):
self.targets = targets
self.target_paths = {}
self.fuzz_models = {}
self.info = {}
self.total_tc = 0
def get_total_tc(self):
tc_num = 0
for target_name in self.fuzz_models.keys():
for option_name in self.fuzz_models[target_name].keys():
for model_id, model in self.fuzz_models[target_name][option_name].iteritems():
tc_num += model[1]
return tc_num
def print_table(self):
models = []
tcs = []
for target_name in self.fuzz_models.keys():
for o in self.info[target_name]['active_options']:
tc_num = 0
for k,v in self.fuzz_models[target_name][o].iteritems():
tc_num += v[1]
models.append(len(self.fuzz_models[target_name][o]))
tcs.append(tc_num)
table_str = Table(
Column( "Option Name",
[ o for o in self.info[target_name]['active_options'] ] + ["="*22, "Total"], align=ALIGN.LEFT ),
Column( "Templates/Generators", models + ["="*22, sum(models)] ),
Column( "Test Cases", tcs + ["="*22, sum(tcs)] ),
)
print table_str
self.targets[target_name].summaryfile.write(str(table_str)+'\n\n\n')
##################################################################################################
# Fuzzer Object: Setup Functions
##################################################################################################
def setup(self, target_name):
self.info[target_name] = {}
self.info[target_name]['total_active_models'] = 0
self.info[target_name]['active_options'] = ['header']
self.targets[target_name].pedrpc_connect()
self.targets[target_name].start_target()
time.sleep(1)
# 'R': Random Options (and possibly Random Payload)
self.fuzz_models[target_name] = OrderedDict()
self.fuzz_models[target_name]['header'] = OrderedDict()
# Full Random
self.fuzz_models[target_name]['header']['R'] = [fuzz(CoAP(options=[])), K_RANDOM] # 1
self.fuzz_models[target_name]['header']['R-L'] = [Raw(load=RandEnumKeys(
[ RandBin(i) for i in SeqSingNum(0, 2**16-1 - (20+8), neg=False, overflow_max=False)._choice ]
)), K_RANDOM] # IP header (20) + UDP datagram (8)
self.info[target_name]['total_active_models'] += len(self.fuzz_models[target_name]['header'])
self.total_tc = self.get_total_tc()
self.print_table()
##################################################################################################
# Fuzzer Object: Running Functions
##################################################################################################
def run_model(self, target_name, option_name, model_id, opt_tc, msg, target_path=None):
start = time.time()
total_model_tc = self.fuzz_models[target_name][option_name][model_id][1]
initial_model_crash_count = self.targets[target_name].crash_count
if option_name in RESPONSE_OPTIONS:
max_model_crash = MAX_MODEL_CRASH_RESPONSE_OPTION
elif option_name in PROXY_OPTIONS:
max_model_crash = MAX_MODEL_CRASH_PROXY_OPTION
else:
max_model_crash = MAX_MODEL_CRASH
for count in xrange(total_model_tc):
self.targets[target_name].pre_send(count+1, total_model_tc, opt_tc, self.total_opt_tc, self.total_tc, msg)
if not TARGET_IPV6:
ans, unans = sr(IP(dst=self.targets[target_name].aut_host)/UDP(sport=self.targets[target_name].aut_src_port, dport=self.targets[target_name].aut_port)/str(self.fuzz_models[target_name][option_name][model_id][0])[:65507], verbose=0, timeout=REQUEST_TIMEOUT)
else:
ans, unans = sr(IPv6(dst=self.targets[target_name].aut_host)/UDP(sport=self.targets[target_name].aut_src_port, dport=self.targets[target_name].aut_port)/str(self.fuzz_models[target_name][option_name][model_id][0])[:1452], verbose=0, timeout=REQUEST_TIMEOUT)
time.sleep(INTERVAL_BETWEEN_REQUESTS)
self.targets[target_name].post_send(ans[0] if ans else unans[0], option_name, model_id, target_path, (count+1) == total_model_tc)
opt_tc += 1
if ( (self.targets[target_name].crash_count - initial_model_crash_count) >= max_model_crash ):
self.total_opt_tc -= (total_model_tc - (count+1))
self.total_tc -= (total_model_tc - (count+1))
break
crash_msg = "Crashes for %s model %s: %d (TCs Executed:%d/%d)" %\
('header' if option_name == 'header' else 'option %s' % option_name,
model_id, (self.targets[target_name].crash_count - initial_model_crash_count),
count+1, total_model_tc)
self.targets[target_name].log(crash_msg)
self.targets[target_name].summaryfile.write(crash_msg+'\n')
# Fill out Model Results (MR) file
self.targets[target_name].mrfile.write( "%s\t%s\t%d\t%d\t%d\t%f\n" %\
( option_name, model_id,
(self.targets[target_name].crash_count - initial_model_crash_count),
count+1, total_model_tc, (time.time() - start) ) )
self.targets[target_name].log("="*80)
return opt_tc
def run_option(self, target_name, option_name):
start = time.time()
opt_tc = 1
self.total_opt_tc = 0
for model_id, model in self.fuzz_models[target_name][option_name].iteritems():
self.total_opt_tc += model[1]
initial_opt_crash_count = self.targets[target_name].crash_count
for model_id, model in self.fuzz_models[target_name][option_name].iteritems():
target_path = None
msg = "Fuzzing %s | Model ID: %s" %\
('Header' if option_name == 'header' else 'Option: %s' % option_name,
model_id)
if option_name != 'header' and ("KU" in model_id):
target_path = (self.targets[target_name].known_paths[ int(model_id.split('_')[-1]) ])
msg += " | Target Resource: %s" % target_path
opt_tc = self.run_model(target_name, option_name, model_id, opt_tc, msg, target_path)
crash_msg = "Crashes for %s: %d" %\
('header' if option_name == 'header' else 'option %s' % option_name,
(self.targets[target_name].crash_count - initial_opt_crash_count))
time_msg = "Total time for %s: %.5fs" %\
('header' if option_name == 'header' else 'option %s' % option_name,
(time.time() - start))
self.targets[target_name].log(crash_msg)
self.targets[target_name].log(time_msg)
self.targets[target_name].summaryfile.write(crash_msg+'\n'+time_msg+'\n\n')
self.targets[target_name].log("Total crashes until now: %d" % (self.targets[target_name].crash_count))
self.targets[target_name].log("="*120)
def run(self):
for target_name in self.fuzz_models.keys():
start = time.time()
first_option = True
for option_name in self.fuzz_models[target_name].keys():
if not first_option:
self.targets[target_name].restart_target()
first_option = False
self.run_option(target_name, option_name)
time_msg = "Total Time: %.5fs" % (time.time() - start)
print time_msg
self.targets[target_name].summaryfile.write(time_msg)
self.targets[target_name].stop_target()
##################################################################################################
# Main
##################################################################################################
USAGE = "USAGE: random_fuzzer.py"\
"\n [-t|--target_name tname] Application/System Under Test's Identifier " \
"\n (from target_list.py)" \
"\n [-h|--host ipv4] Process Monitor's Host" \
"\n [-p|--port port] Process Monitor's TCP port"\
"\n [-H|--aut_host aut_ipv4] Application/System Under Test's Host" \
"\n [-P|--aut_port aut_port] Application/System Under Test's UDP port (CoAP dst)"\
"\n [-c|--aut_src_port aut_src_port] CoAP source port (CoAP src)"\
"\n -d|--output_dir dir directory where output files are put "\
"\n (as in 'output/<target_name>')"
ERR = lambda msg: sys.stderr.write("ERR> " + msg + "\n") or sys.exit(1)
if __name__ == "__main__":
# parse command line options.
opts = None
try:
opts, args = getopt.getopt(sys.argv[1:], "t:h:p:H:P:c:d:",
["target_name=", "host=", "port=", "aut_host=", "aut_port=", "aut_src_port=", "output_dir="] )
except getopt.GetoptError:
ERR(USAGE)
target_name = None
host = None
port = None
aut_host = None
aut_port = None
aut_src_port = None
output_dir = None
for opt, arg in opts:
if opt in ("-t", "--target_name"):
target_name = arg
if opt in ("-h", "--host"):
host = arg
if opt in ("-p", "--port"):
port = int(arg)
if opt in ("-H", "--aut_host"):
aut_host = arg
if opt in ("-P", "--aut_port"):
aut_port = int(arg)
if opt in ("-t", "--aut_src_port"):
aut_src_port = int(arg)
if opt in ("-d", "--output_dir"):
output_dir = arg
if not output_dir or not target_name:
ERR(USAGE)
if not os.path.isdir(output_dir):
ERR("output_dir must be an existing directory")
if not host or host == "-1":
host = PROCMON_DEFAULT_DST_HOST
if not port or port == -1:
port = PROCMON_DEFAULT_DST_PORT
if not aut_host or aut_host == "-1":
aut_host = COAP_AUT_DEFAULT_DST_HOST
if not aut_port or aut_port == -1:
aut_port = COAP_AUT_DEFAULT_DST_PORT
if not aut_src_port or aut_src_port == -1:
aut_src_port = COAP_AUT_DEFAULT_SRC_PORT
my_seed = ord(os.urandom(1))
random.seed(my_seed)
print "Using %d as seed" % my_seed
# Retrieve SUT-specific configuration from target_list.py
target_info = get_target_info_from_target_name(target_name, aut_host, aut_port)
try:
target_env = target_info['env']
except KeyError:
target_env = {}
# Pass specified target parameters to the PED-RPC server
target = Target(
name=target_name,
aut_host=aut_host,
aut_port=aut_port,
aut_src_port=aut_src_port,
aut_heartbeat_path=target_info['heartbeat_path'],
aut_default_uris=target_info['default_uris'],
aut_strings=target_info.get('strings', []),
procmon=pedrpc.Client(host, port),
procmon_options={
'start_commands': [target_info['start_cmd']],
'time_to_settle': target_info['time_to_settle'],
'env': target_env,
},
output_dir=output_dir
)
bind_layers(UDP, CoAP, sport=aut_port)
bind_layers(UDP, CoAP, dport=aut_port)
bind_layers(UDP, CoAP, sport=aut_src_port)
bind_layers(UDP, CoAP, dport=aut_src_port)
mf = Fuzzer({ target_name: target })
mf.targets[target_name].summaryfile.write("Using %d as seed\n\n" % my_seed)
# Signal Handler defined here so we can access the files by closure
def signal_handler(signal, frame):
print "\nSIGINT Received"
mf.targets[target_name].stop_target()
sys.exit(signal)
signal.signal(signal.SIGINT, signal_handler)
mf.setup(target_name)
mf.run()