This repository has been archived by the owner on Mar 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fuzztest_helper.py
executable file
·404 lines (316 loc) · 13.6 KB
/
fuzztest_helper.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#!/usr/bin/env python3
import yaml, sys, subprocess, time
from pathlib import Path
# fuzztest_helper.py
#
# Generates and executes fuzztesting on srsran docker containers
#
# run without arguments to see options
CONTAINER_ACTION_STEP = 3 # ! max number of containers started at once
COMPOSE_DIRECTORY = "./"
LOG_DIRECTORY = "./tests/"
PCAP_DIRECTORY = "./pcaps/"
# colored text stuff
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def mass_generate_compose(startNum, endNum, templateFilename, outputDir):
"""generates composes from [startNum - endNum]
Args:
startNum (int): start val for range
endNum (int): end val for range
"""
for test in range(startNum, endNum + 1):
generate_compose(templateFilename, outputDir, test)
print()
def generate_compose(templateFilename, outputDir, testNumber):
"""generates and writes to file a docker-compose for fuzz testing from template
Args:
templateFilename (str): template file for docker-compose generation
outputDir (str): output directory for docker-compose outputs
testNumber (int): current test number
"""
with open(templateFilename, 'r') as ymlfile:
try:
docker_config = yaml.safe_load(ymlfile)
except yaml.YAMLError as exc:
print(exc)
test_number = str(testNumber) # ! current test iteration
subnet = generate_subnet_string(testNumber) # ! this iterations subnet
# NOTE: using 3 and 5 as we have space for 14 addrs in our subnet, also it wasnt working with 1 and 2
epc_ip = generate_ip(testNumber, 3) # ! 'ip 1'
enb_ip = generate_ip(testNumber, 5) # ! 'ip 2'
output_folder = Path(outputDir)
output_filename = output_folder / ("docker-compose_" + test_number +
".yml")
docker_config['services']['srsue'][
'command'] = 'stdbuf -oL srsue /etc/srsran/ue.conf.fauxrf -f' + test_number
docker_config['services']['srsepc'][
'command'] = 'stdbuf -oL srsepc /etc/srsran/epc.conf --mme.mme_bind_addr=' + epc_ip + ' --spgw.gtpu_bind_addr=' + epc_ip
docker_config['services']['srsepc']['networks']['corenet'][
'ipv4_address'] = epc_ip
docker_config['services']['srsenb'][
'command'] = 'srsenb /etc/srsran/enb.conf.fauxrf --enb.mme_addr=' + epc_ip + ' --enb.gtp_bind_addr=' + enb_ip + ' --enb.s1c_bind_addr=' + enb_ip
docker_config['services']['srsenb']['volumes'][
0] = PCAP_DIRECTORY + test_number + ':/pcaps/'
docker_config['services']['srsenb']['networks']['corenet'][
'ipv4_address'] = enb_ip
docker_config['networks']['corenet']['ipam']['config'][0][
'subnet'] = subnet
# container_name
docker_config['services']['srsepc'][
'container_name'] = 'virtual-srsepc' + test_number
docker_config['services']['srsenb'][
'container_name'] = 'virtual-srsenb' + test_number
docker_config['services']['srsue'][
'container_name'] = 'virtual-srsue' + test_number
with open(output_filename, 'w') as newconf:
yaml.dump(docker_config, newconf, default_flow_style=False)
print("Generated Test# " + f"{testNumber:07d}" + " | Subnet: " + subnet,
end='\r')
def generate_subnet_string(iterationNum):
"""
when given current iteration, generate a subnet range.
each subnet is comprised of a /28 block, thus the last
4 bits of the ip address are available, along with
10.***.*** ranges, so 20 bits are available to us,
and about 1mil subnets can be created
Parameters:
iterationNum (int): Current Test Number
Returns:
subnet_string (str): /28 subnet for current iteration
"""
first = str(iterationNum >> 12) # first 8 bits [0-7]
second = str((iterationNum & 4080) >> 4) # middle 8 bits [8 - 15]
third = str(
16 * (iterationNum & 15)
) # final 4 bits, multiplied by 16 to obtein this iterations subnet
if iterationNum > 1048575: # 20 bit max val
print(
"generate_subnet_string doesnt support more than 1mil iterations")
quit(1)
return "10." + first + "." + second + "." + third + "/28"
def generate_ip(iterationNum, ipNum):
"""
generate a valid ip within this current iteration's
subnet. /28 blocks allow for 15 ips maximum, thus ipNum
cannot be above 15
Parameters:
iterationNum (int): Current Test Number
ipNum (int): ip needed within block, valid values are 1 thru 15
Returns:
ip_address (str): ip within iterations subnet
"""
if ipNum > 16:
print("ipNum too large for a /28 subnet!")
quit(1)
first = str(iterationNum >> 12) # first 8 bits [0-7]
second = str((iterationNum & 4080) >> 4) # middle 8 bits [8 - 15]
# final 4 bits, multiplied by 16 to obtein this iterations subnet,
# then added to ipNum to obtain a unique ip
third = str((16 * (iterationNum & 15)) + ipNum)
return "10." + first + "." + second + "." + third
def print_guide():
"""prints usage instructions and exits script
"""
print("\nSupported commands:\n")
print(
f" [Main Function] Automatically start and stop containers per fuzzing spec:"
)
print(
f" ./fuzztest_helper.py {bcolors.HEADER}fuzz{bcolors.ENDC} <start index> <end index> <(optional)docker-compose directory>\n" # len(args) = 5
)
print(" Generate docker-compose files:")
print(
f" ./fuzztest_helper.py {bcolors.HEADER}generate{bcolors.ENDC} <start index> <end index> <template file> <output dir>\n" # len(args) = 6
)
print(" Start containers from generated compose files:")
print(
f" ./fuzztest_helper.py {bcolors.HEADER}start{bcolors.ENDC} <start index> <end index> <(optional)docker-compose directory>\n" # len(args) = 5
)
print(" Stop containers from generated compose files, generate logs:")
print(
f" ./fuzztest_helper.py {bcolors.HEADER}stop{bcolors.ENDC} <start index> <end index> <(optional)docker-compose directory>\n" # len(args) = 5
)
quit(1)
def start_and_stop_containers(startNum, endNum):
for groupIdx, groupStartIdx in enumerate(
range(startNum, endNum + 1, CONTAINER_ACTION_STEP)):
print(f"{bcolors.HEADER} Fuzzing Group " + str(groupIdx + 1) +
f"{bcolors.ENDC}")
print("!! Starting Group " + str(groupIdx + 1) + " [" +
str(groupStartIdx) + ":" +
str(min((groupStartIdx + CONTAINER_ACTION_STEP), endNum)) +
"] !!")
start_test_containers(
groupStartIdx, min(endNum, groupStartIdx + CONTAINER_ACTION_STEP))
print("!! Stopping container group" + str(groupIdx + 1) + " [" +
str(groupStartIdx) + ":" +
str(min((groupStartIdx + CONTAINER_ACTION_STEP), endNum)) +
"] !!")
stop_test_containers(
groupStartIdx, min(endNum, groupStartIdx + CONTAINER_ACTION_STEP))
print(f"{bcolors.HEADER} Fuzz Group " + str(groupIdx) +
f" Complete, starting next group in 2 seconds {bcolors.ENDC}")
time.sleep(2)
def start_test_containers(startNum, endNum):
"""starts containers in range specified, in groups of CONTAINER_ACTION_STEP
Args:
startNum (int): start index
endNum (int): end index
Returns:
[subprocess.Popen]: array of handles for started processes
"""
popenObjs = []
for currNum in range(startNum, endNum):
popenObjs.append(start_container(currNum))
return popenObjs
def stop_test_containers(startNum, endNum, ignoreCompletion=False):
"""stops containers in range specified and saves logs, in groups of CONTAINER_ACTION_STEP (currently 5)
Args:
startNum (int): start index
endNum (int): end index
ignoreCompletion (boolean, optional, default=False): if True, stops containers without checking whether they've completed their task
"""
for currNum in range(startNum, endNum):
stop_container(currNum, ignoreCompletion)
def stop_container(contNum, ignoreCompletion=False):
"""stops a single container
Args:
contNum (int): container # to stop
ignoreCompletion (boolean, optional, default=False): if True, stops containers without checking whether they've completed their task
"""
numSecWaited = 0
print("Attempting to stop containers")
while not (check_test_completion(contNum) or ignoreCompletion):
numSecWaited += 5
print(f"{bcolors.WARNING}Waiting " + str(numSecWaited) +
"s for test completion on container " + str(contNum) +
F"{bcolors.ENDC}",
end='\r')
time.sleep(5)
print()
save_logs(contNum)
print("closing container" + str(contNum))
subprocess.call([
'docker-compose', '-p', 'srsRAN_' + str(contNum), '-f',
f'{COMPOSE_DIRECTORY}docker-compose_' + str(contNum) + '.yml', 'down',
'-v'
])
print("requested to close container " + str(contNum))
def start_container(contNum):
"""starts a single container
Args:
contNum (int): container # to start
Returns:
[subprocess.Popen]: handle for started process
"""
print("requested to start container " + str(contNum))
handle = subprocess.Popen([
'docker-compose', '-p', 'srsRAN_' + str(contNum), '-f',
f'{COMPOSE_DIRECTORY}docker-compose_' + str(contNum) + '.yml', 'up',
'-d'
],
stdin=None,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
close_fds=True)
return handle
def save_logs(contNum):
"""save docker-compose logs for container to file
Args:
contNum (int): container #
"""
print("saving logs for container " + str(contNum))
output_folder = Path(LOG_DIRECTORY)
output_filename = output_folder / ("testLog_" + str(contNum) + ".txt")
with open(output_filename, "w") as text_file: # ! fix dir
text_file.write(get_logs(contNum))
def check_test_completion(contNum):
"""check if RRCCONNECTIONREQUEST has occured in container
Args:
contNum (int): container #
Returns:
[boolean]: True if connected, False if not
"""
logs = get_logs(contNum)
return "Network attach successful." in logs
def get_logs(contNum):
"""returns logs from container group
Args:
contNum (int): container #
Returns:
[str]: logs from docker-compose
"""
logs = subprocess.run([
'docker-compose', '-p', 'srsRAN_' + str(contNum), '-f',
f'{COMPOSE_DIRECTORY}docker-compose_' + str(contNum) + '.yml', 'logs',
'--no-color'
],
stdout=subprocess.PIPE).stdout.decode('utf-8')
return logs
def range_check_helper_functions(startNum, endNum):
"""When using start container helper function, check if range is large and exit if it is
Args:
startNum (int): start index
endNum (int): end index
"""
if 1 + endNum - startNum > CONTAINER_ACTION_STEP:
print("range too large, try fuzz instead of start")
quit(1)
def main():
if len(sys.argv) <= 2 and sys.argv[1] == "debugging":
return
if len(sys.argv) < 4:
print_guide()
if len(sys.argv) == 5 and (sys.argv[1] == "start" or sys.argv[1] == "stop"
or sys.argv[1] == "fuzz"
or sys.argv[1] == "stopforce"):
print(f"{bcolors.HEADER}COMPOSE DIRECTORY SET: " + sys.argv[4] +
f"{bcolors.ENDC}")
COMPOSE_DIRECTORY = sys.argv[4]
if sys.argv[1] == "generate":
mass_generate_compose(int(sys.argv[2]), int(sys.argv[3]), sys.argv[4],
sys.argv[5])
print(f"{bcolors.OKGREEN}Generated docker-composes [" + sys.argv[2] +
":" + str(int(sys.argv[3])) + f"].{bcolors.ENDC}")
quit(0)
if sys.argv[1] == "start":
print("Starting tests [" + sys.argv[2] + ":" + str(int(sys.argv[3])) +
"].")
range_check_helper_functions(int(sys.argv[2]), int(sys.argv[3]))
start_test_containers(int(sys.argv[2]), int(sys.argv[3]))
print(f"{bcolors.OKGREEN}Containers [" + sys.argv[2] + ":" +
str(int(sys.argv[3])) + f"] Started.{bcolors.ENDC}")
quit(0)
if sys.argv[1] == "stop":
print("Stopping tests [" + sys.argv[2] + ":" + str(int(sys.argv[3])) +
"].")
stop_test_containers(int(sys.argv[2]), int(sys.argv[3]))
print(f"{bcolors.OKGREEN}Containers [" + sys.argv[2] + ":" +
str(int(sys.argv[3])) + f"] Stopped.{bcolors.ENDC}")
quit(0)
if sys.argv[1] == "stopforce":
print("FORCE STOPPING tests [" + sys.argv[2] + ":" +
str(int(sys.argv[3])) + "].")
stop_test_containers(int(sys.argv[2]), int(sys.argv[3]), True)
print(f"{bcolors.OKGREEN}Containers [" + sys.argv[2] + ":" +
str(int(sys.argv[3])) + f"] FORCE Stopped.{bcolors.ENDC}")
quit(0)
if sys.argv[1] == "fuzz":
print("Running full fuzz tests on [" + sys.argv[2] + ":" +
str(int(sys.argv[3])) + "].")
start_and_stop_containers(int(sys.argv[2]), int(sys.argv[3]))
print(f"{bcolors.OKGREEN}Testing [" + sys.argv[2] + ":" +
str(int(sys.argv[3])) + f"] Complete.{bcolors.ENDC}")
quit(0)
print_guide()
if __name__ == "__main__":
main()