-
Notifications
You must be signed in to change notification settings - Fork 24
/
framework.py
262 lines (197 loc) · 8.27 KB
/
framework.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
#!/usr/bin/env python2
# FFW - Fuzzing For Worms
# Author: Dobin Rutishauser
import logging
import argparse
import os
import glob
import sys
import inspect
from network import replay
from network.interceptor import Interceptor
from clientfuzzer import clientfuzzermaster
from verifier import verifier
from verifier import minimizer
from uploader import uploader
from network import tester
from network import proto_vnc
from basicmode import basicmaster
from honggmode.honggmaster import HonggMaster
from configmanager import ConfigManager
import utils
# https://stackoverflow.com/questions/9321741/printing-to-screen-and-writing-to-a-file-at-the-same-time
def setupLoggingWithFile(config):
# set up logging to file - see previous section for more details
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
datefmt='%m-%d %H:%M',
filename='ffw-debug.log',
filemode='w')
# define a Handler which writes INFO messages or higher to the sys.stderr
console = logging.StreamHandler()
console.setLevel(logging.WARN)
# set a format which is simpler for console use
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
# tell the handler to use this format
console.setFormatter(formatter)
# add the handler to the root logger
logging.getLogger('').addHandler(console)
# slaves will setup their own logging
config["DebugWithFile"] = True
def setupLoggingStandard():
logging.basicConfig(format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
datefmt='%m-%d %H:%M')
def realMain(config):
parser = argparse.ArgumentParser("Fuzzing For Worms")
group = parser.add_mutually_exclusive_group()
group.add_argument('--intercept', help='Intercept and record network communication', action="store_true")
group.add_argument('--test', help='Test intercepted network communication', action="store_true")
group.add_argument('--fuzz', help='Perform fuzzing', action="store_true")
group.add_argument('--clientfuzz', help='Perform client fuzzing', action="store_true")
group.add_argument('--honggmode', help='Perform honggfuze based fuzzing', action="store_true")
group.add_argument('--verify', help='Verify crashes', action="store_true")
group.add_argument('--minimize', help='Minimize crashes', action="store_true")
group.add_argument('--replay', help='Replay a crash', action="store_true")
group.add_argument('--upload', help='Upload verified crashes', action="store_true")
parser.add_argument('--debug', help="More error messages, only one process", action="store_true")
parser.add_argument('--gui', help='Fuzzer: Use ncurses gui', action="store_true")
parser.add_argument('--processes', help='Fuzzer: How many paralell processes', type=int)
# TODO: make this mode specific
parser.add_argument("--honggcov", help="Select Honggfuzz coverage: hw/sw", default="sw")
parser.add_argument('--listenport', help='Intercept: Listener port', type=int)
parser.add_argument('--targetport', help='Intercept/Replay: Port to be used for the target server', type=int)
parser.add_argument('--file', help="Verify/Replay: Specify file to be used")
parser.add_argument('--url', help="Uploader: url")
parser.add_argument('--basic_auth_user', help='Uploader: basic auth user')
parser.add_argument('--basic_auth_password', help='Uploader: basic auth password')
parser.add_argument('--adddebuglogfile', help='Will write a debug log file', action="store_true")
parser.add_argument('--config', help='Config file')
parser.add_argument('--basedir', help='FFW base directory')
args = parser.parse_args()
# wtf is this
configManager = ConfigManager()
if config is None:
basedir = None
configFile = None
if args.config is None:
maybeConfigFile = os.getcwd() + "/config.py"
if os.path.isfile( maybeConfigFile ):
configFile = maybeConfigFile
else:
print "No config specified. Either start via fuzzing.py, or with --config <configfile>"
return False
else:
configFile = args.config
if not args.basedir:
# https://stackoverflow.com/questions/50499/how-do-i-get-the-path-and-name-of-the-file-that-is-currently-executing
basedir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
else:
basedir = args.basedir
print "Basedir: " + basedir
print "Config file: " + configFile
config = configManager.loadConfigByFile(configFile, basedir)
if config is None:
print "Invalid config"
return False
if not configManager.checkRequirements(config):
print "Requirements not met."
return
if args.processes:
config["processes"] = args.processes
if args.debug:
logging.basicConfig(level=logging.DEBUG)
config["processes"] = 1
config["debug"] = True
else:
config["debug"] = False
if args.adddebuglogfile:
setupLoggingWithFile(config)
else:
setupLoggingStandard()
if args.intercept:
ffwIntercept(config, args)
if args.test:
ffwTest(config, args)
if args.fuzz:
ffwBasicFuzz(configManager, args)
if args.clientfuzz:
ffwClientFuzz(configManager, args)
if args.honggmode:
ffwHonggmode(configManager, args)
if args.verify:
ffwVerify(config, args)
if args.minimize:
ffwMinimize(config, args)
if args.replay:
ffwReplay(config, args)
if args.upload:
ffwUpload(config, args)
def ffwIntercept(config, args):
interceptorPort = 10000
targetPort = 20000
if args.listenport:
interceptorPort = args.listenport
if args.targetport:
targetPort = args.targetport
else:
targetPort = config["target_port"]
print("Interceptor listen on port: " + str(interceptorPort))
print("Target server port: " + str(targetPort))
interceptor = Interceptor(config)
interceptor.doIntercept(interceptorPort, targetPort)
def ffwTest(config, args):
t = tester.Tester(config)
t.test()
def ffwBasicFuzz(configManager, args):
config = configManager.config
if not configManager.checkFuzzRequirements(config, 'basic'):
return False
utils.setupTmpfs(config, enable=True)
basicmaster.doFuzz(config, args.gui)
utils.setupTmpfs(config, enable=False)
def ffwHonggmode(configManager, args):
config = configManager.config
if not configManager.checkFuzzRequirements(config, 'hongg'):
return False
if args.honggcov == "hw" or config["honggcov"] == "hw":
config["honggmode_option"] = "--linux_perf_bts_edge"
if os.geteuid() != 0:
logging.error('"--honggcov hw" hardware coverage requires root')
return
elif args.honggcov == "sw" or config["honggcov"] == "sw":
config["honggmode_option"] = None # sw is default
else:
config["honggmode_option"] = None
honggMaster = HonggMaster(config)
utils.setupTmpfs(config, enable=True)
honggMaster.doFuzz()
utils.setupTmpfs(config, enable=False)
def ffwClientFuzz(configManager, args):
config = configManager.config
if not configManager.checkFuzzRequirements(config):
return False
clientfuzzermaster.doFuzz(config, args.gui)
def ffwVerify(config, args):
v = verifier.Verifier(config)
if args.file:
v.verifyFile(args.file)
else:
v.verifyOutDir()
def ffwMinimize(config, args):
mini = minimizer.Minimizer(config)
mini.minimizeOutDir()
def ffwReplay(config, args):
replayer = replay.Replayer(config)
targetPort = None
if not args.file:
print "Use --file to specify a file to be replayed"
return False
if not args.targetport:
targetPort = config['target_port']
replayer.replayFile(targetPort, args.file)
def ffwUpload(config, args):
if args.basic_auth_user and args.basic_auth_password:
u = uploader.Uploader(config, args.url, args.basic_auth_user, args.basic_auth_password)
else:
u = uploader.Uploader(config, args.url, None, None)
u.uploadVerifyDir()