-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent.py
275 lines (248 loc) · 9.17 KB
/
agent.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
from parser import parse_instance
import kafka
import fuzzer
import parser
import flask
from flask import Flask
from flask import request, jsonify
from kafka import KafkaProducer
import os
import sys
import json
import os.path as path
import subprocess
import warnings
import datetime
import signal
from distutils import util
import argparse
from time import sleep
from json import dumps
from multiprocessing import Process, Value
'''
Fuzzer instance class
'''
fuzz_instance = ""
crash_list = ""
queue_list = ""
with open("config/agent.cfg" , "r") as infile :
config_data = json.load(infile)
app = flask.Flask(__name__)
app.config["DEBUG"] = bool(util.strtobool(os.getenv('FUZZING_AGENT_DEBUG', 'True')))
process_state = Value('b', True)
def output_data(data, topic) :
if bool(util.strtobool(os.getenv('FUZZING_AGENT_DEBUG', 'True'))) :
print(data)
else :
ip = os.getenv('KAFKA_ENDPOINT', '127.0.0.1:9092')
producer = KafkaProducer(bootstrap_servers=ip,value_serializer=lambda x: dumps(x).encode('utf-8'))
producer.send(topic, value=data)
'''
Builds the fuzzing instance class and the deploy string for the subprocess
'''
def deploy_string(fuzz, testing_dir, findings_dir, binary, config, profile) :
global fuzz_instance
if not fuzz_instance :
fuzz_instance = fuzzer.Fuzzer(fuzz,testing_dir,findings_dir,binary,config,profile)
def deploy(fuzzer):
for each in fuzzer.deploy_string :
p = subprocess.Popen(each, stdout=subprocess.DEVNULL)
'''
Creates a new test case file with the given input for the fuzzing
'''
@app.route('/add_testcase', methods=['GET'])
def add_testcase() :
testcase = request.args.get('file')
global fuzz_instance
file_name = max([int(each) for each in os.listdir(fuzz_instance.testing_dir)])
if not file_name :
file_name = 0
file_name = file_name + 1
f = open(fuzz_instance.testing_dir + "/" + str(file_name), "w")
f.write(testcase)
f.close()
return "Testcase added."
@app.route('/kill', methods=['GET'])
def kill():
global fuzz_instance
global p
stat = json.loads(stats(fuzz_instance, ""))
for each in stat :
pid = int(each["fuzzer_pid"])
os.kill(pid, signal.SIGKILL)
p.terminate()
os.kill(os.getpid(), signal.SIGTERM)
return "Fuzzer killed."
@app.route('/pause', methods=['GET'])
def pause():
global fuzz_instance
global process_state
stat = json.loads(stats(fuzz_instance, ""))
for each in stat :
pid = int(each["fuzzer_pid"])
os.kill(pid, signal.SIGSTOP)
process_state.value = False
return "Fuzzer paused."
@app.route('/resume', methods=['GET'])
def resume():
global fuzz_instance
global process_state
stat = json.loads(stats(fuzz_instance, ""))
for each in stat :
pid = int(each["fuzzer_pid"])
os.kill(pid, signal.SIGCONT)
process_state.value = True
return "Fuzzer resumed."
'''
Provides a json formatted output of fuzzing stats of all the fuzzers deployed. Detailed
'''
def stats(fuzzer, dataset):
stats = []
for each in fuzzer.deploy_string :
dir_stat = {}
slave_dir = each[6]
path = fuzzer.findings_dir + "/" + slave_dir + "/fuzzer_stats"
if os.path.isfile(path) :
for line in open(path, "r"):
key = line.split(":")[0]
value = line.split(":")[1]
key = key.strip().replace(" ", "")
if key != "command_line" :
value = value.strip().replace(" ", "")
time_list = ["last_crash", "last_path", "last_update", "start_time"]
if key in time_list :
value = datetime.datetime.fromtimestamp(int(value))
if dataset:
if key in dataset:
dir_stat[key] = str(value)
else:
dir_stat[key] = str(value)
stats.append(dir_stat)
return json.dumps(stats, indent=4, sort_keys=True)
def crashes(fuzzer) :
global crash_list
crash = []
for each in fuzzer.deploy_string :
thread_crash = []
slave_dir = each[6]
path = fuzzer.findings_dir + "/" + slave_dir + "/crashes"
if os.path.exists(path) :
crashdir_list = os.listdir(path)
for each in crashdir_list :
dir_crash = {}
id = each.split(",")[0]
grammar = each[len(id) + 1 : ]
dir_crash["id"] = id.strip("id:").lstrip("0")
dir_crash["grammar"] = grammar
dir_crash["path"] = path + "/" + each
dir_crash["size"] = os.path.getsize(dir_crash["path"])
thread_crash.append(dir_crash)
crash.append(thread_crash)
crash_list = crash
return json.dumps(crash, indent=4, sort_keys=True)
def queues(fuzzer) :
global queue_list
queues = []
for each in fuzzer.deploy_string :
thread_queues = []
slave_dir = each[6]
path = fuzzer.findings_dir + "/" + slave_dir + "/queue"
if os.path.exists(path) :
crashdir_list = os.listdir(path)
for each in crashdir_list :
dir_crash = {}
id = each.split(",")[0]
grammar = each[len(id) + 1 : ]
dir_crash["id"] = id.strip("id:").lstrip("0")
dir_crash["grammar"] = grammar
dir_crash["path"] = path + "/" + each
dir_crash["size"] = os.path.getsize(dir_crash["path"])
thread_queues.append(dir_crash)
queues.append(thread_queues)
queue_list = queues
return json.dumps(queues, indent=4, sort_keys=True)
def push_report():
global fuzz_instance
data = stats(fuzz_instance, "")
output_data(data, config_data["topics"]["report"])
def push_compressed_report():
dataset = ["last_crash", "last_path", "last_update", "start_time", "execs_per_sec",
"unique_crashes", "unique_hangs", "execs_done", "paths_favored", "paths_total"]
global fuzz_instance
data = stats(fuzz_instance, dataset)
output_data(data, config_data["topics"]["comp_report"])
def push_queue():
global queue_list, fuzz_instance
if not queue_list :
data = queues(fuzz_instance)
else :
diff = []
old_data = queue_list.copy()
new_data = json.loads(queues(fuzz_instance))
for id, value in enumerate(old_data):
diff = [i for i in new_data[id] + value if i not in new_data[id] or i not in value]
data = json.dumps(diff, indent=4, sort_keys=True)
if data :
output_data(data, config_data["topics"]["queue"])
def push_crashes():
global crash_list, fuzz_instance
if not crash_list :
data = crashes(fuzz_instance)
else :
diff = []
old_data = crash_list.copy()
new_data = json.loads(crashes(fuzz_instance))
for id, value in enumerate(old_data):
diff = [i for i in new_data[id] + value if i not in new_data[id] or i not in value]
data = json.dumps(diff, indent=4, sort_keys=True)
if data :
output_data(data, config_data["topics"]["crash"])
def push_testcase():
global fuzz_instance
path = fuzz_instance.testing_dir
testcases = []
data = ""
if os.path.exists(path) :
filedir_list = os.listdir(path)
for each in filedir_list :
dir_testcase = {}
dir_testcase["name"] = each
dir_testcase["path"] = path + "/" + each
dir_testcase["size"] = os.path.getsize(dir_testcase["path"])
f = open(dir_testcase["path"], "r")
dir_testcase["content"] = f.read()
testcases.append(dir_testcase)
data = json.dumps(testcases, indent=4, sort_keys=True)
if data :
output_data(data, config_data["topics"]["testcase"])
def push_global() :
global process_state
global fuzz_instance
while True:
if process_state.value :
parse_instance(fuzz_instance)
push_report()
push_compressed_report()
push_queue()
push_crashes()
push_testcase()
sleep(int(os.getenv('SERVICE_TIMEOUT', 10)))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Fuzzing agent for ASTRID")
parser.add_argument('--fuzzer', '-f', help='Fuzzer to be used for fuzzing (AFL/driller)', required=True)
parser.add_argument('--input', '-i', help='Testcases directory for input')
parser.add_argument('--output', '-o', help='Working output directory for input')
parser.add_argument('--binary', '-b', help='The binary to be used for fuzzing')
parser.add_argument('--config', '-c' ,help='Path to the configuration file')
parser.add_argument('--profile', '-p',help='Execution profile for the agent', required=True)
args = parser.parse_args()
if not args.binary :
args.binary = os.getenv('BINARY_PATH')
deploy_string(args.fuzzer, args.input, args.output, args.binary, args.config, args.profile)
print(fuzz_instance.deploy_string)
deploy(fuzz_instance)
p = Process(target = push_global)
p.start()
port = os.getenv('FLASK_PORT', 5000)
app.run(host="localhost", port=port, use_reloader=False)
p.join()