/
FuzzingInTheLarge.py
executable file
·781 lines (521 loc) · 19.1 KB
/
FuzzingInTheLarge.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# "Fuzzing in the Large" - a chapter of "The Fuzzing Book"
# Web site: https://www.fuzzingbook.org/html/FuzzingInTheLarge.html
# Last change: 2023-01-07 15:55:19+01:00
#
# Copyright (c) 2021-2023 CISPA Helmholtz Center for Information Security
# Copyright (c) 2018-2020 Saarland University, authors, and contributors
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
r'''
The Fuzzing Book - Fuzzing in the Large
This file can be _executed_ as a script, running all experiments:
$ python FuzzingInTheLarge.py
or _imported_ as a package, providing classes, functions, and constants:
>>> from fuzzingbook.FuzzingInTheLarge import <identifier>
but before you do so, _read_ it and _interact_ with it at:
https://www.fuzzingbook.org/html/FuzzingInTheLarge.html
The Python `FuzzManager` package allows for programmatic submission of failures from a large number of (fuzzed) programs. One can query crashes and their details, collect them into buckets to ensure they will be treated the same, and also retrieve coverage information for debugging both programs and their tests.
For more details, source, and documentation, see
"The Fuzzing Book - Fuzzing in the Large"
at https://www.fuzzingbook.org/html/FuzzingInTheLarge.html
'''
# Allow to use 'from . import <module>' when run as script (cf. PEP 366)
if __name__ == '__main__' and __package__ is None:
__package__ = 'fuzzingbook'
# Fuzzing in the Large
# ====================
if __name__ == '__main__':
print('# Fuzzing in the Large')
if __name__ == '__main__':
# We use the same fixed seed as the notebook to ensure consistency
import random
random.seed(2001)
from . import Fuzzer
## Synopsis
## --------
if __name__ == '__main__':
print('\n## Synopsis')
## Collecting Crashes from Multiple Fuzzers
## ----------------------------------------
if __name__ == '__main__':
print('\n## Collecting Crashes from Multiple Fuzzers')
from graphviz import Digraph
if __name__ == '__main__':
g = Digraph()
server = 'Crash Server'
g.node('Crash Database', shape='cylinder')
for i in range(1, 7):
g.edge('Fuzzer ' + repr(i), server)
g.edge(server, 'Crash Database')
g
## Running a Crash Server
## ----------------------
if __name__ == '__main__':
print('\n## Running a Crash Server')
### Excursion: Setting up the Server
if __name__ == '__main__':
print('\n### Excursion: Setting up the Server')
import os
import sys
import shutil
if __name__ == '__main__':
if 'CI' in os.environ:
# Can't run this in our continuous environment,
# since it can't run a headless Web browser
sys.exit(0)
if __name__ == '__main__':
if os.path.exists('FuzzManager'):
shutil.rmtree('FuzzManager')
if __name__ == '__main__':
import os
os.system(f'git clone https://github.com/uds-se/FuzzManager')
if __name__ == '__main__':
import os
os.system(f'{sys.executable} -m pip install -r FuzzManager/server/requirements.txt > /dev/null')
if __name__ == '__main__':
import os
os.system(f'{sys.executable} -m pip install --upgrade celery > /dev/null 2>&1')
if __name__ == '__main__':
import os
os.system(f'cd FuzzManager; {sys.executable} server/manage.py migrate > /dev/null')
if __name__ == '__main__':
import os
os.system(f'(cd FuzzManager; echo "from django.contrib.auth import get_user_model; User = get_user_model(); User.objects.create_superuser(\'demo\', \'demo@fuzzingbook.org\', \'demo\')" | {sys.executable} server/manage.py shell)')
import subprocess
import sys
if __name__ == '__main__':
os.chdir('FuzzManager')
result = subprocess.run(['python',
'server/manage.py',
'get_auth_token',
'demo'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
os.chdir('..')
err = result.stderr.decode('ascii')
if len(err) > 0:
print(err, file=sys.stderr, end="")
if __name__ == '__main__':
token = result.stdout
token = token.decode('ascii').strip()
token
if __name__ == '__main__':
assert len(token) > 10, "Invalid token " + repr(token)
if __name__ == '__main__':
home = os.path.expanduser("~")
conf = os.path.join(home, ".fuzzmanagerconf")
if __name__ == '__main__':
fuzzmanagerconf = """
[Main]
sigdir = /home/example/fuzzingbook
serverhost = 127.0.0.1
serverport = 8000
serverproto = http
serverauthtoken = %s
tool = fuzzingbook
""" % token
if __name__ == '__main__':
with open(conf, "w") as file:
file.write(fuzzmanagerconf)
from pygments.lexers.configs import IniLexer
from .bookutils import print_file
if __name__ == '__main__':
print_file(conf, lexer=IniLexer())
### End of Excursion
if __name__ == '__main__':
print('\n### End of Excursion')
### Excursion: Starting the Server
if __name__ == '__main__':
print('\n### Excursion: Starting the Server')
from multiprocess import Process # type: ignore
import subprocess
def run_fuzzmanager():
def run_fuzzmanager_forever():
os.chdir('FuzzManager')
proc = subprocess.Popen(['python', 'server/manage.py',
'runserver'],
stdout=subprocess.PIPE,
stdin=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True)
while True:
line = proc.stdout.readline()
print(line, end='')
fuzzmanager_process = Process(target=run_fuzzmanager_forever)
fuzzmanager_process.start()
return fuzzmanager_process
if __name__ == '__main__':
fuzzmanager_process = run_fuzzmanager()
import time
if __name__ == '__main__':
time.sleep(2)
### End of Excursion
if __name__ == '__main__':
print('\n### End of Excursion')
### Logging In
if __name__ == '__main__':
print('\n### Logging In')
if __name__ == '__main__':
fuzzmanager_url = "http://127.0.0.1:8000"
if __name__ == '__main__':
from IPython.display import display, Image
from .bookutils import HTML, rich_output
from .GUIFuzzer import start_webdriver # minor dependency
if __name__ == '__main__':
gui_driver = start_webdriver(headless=True, zoom=1.2)
if __name__ == '__main__':
gui_driver.set_window_size(1400, 600)
if __name__ == '__main__':
gui_driver.get(fuzzmanager_url)
if __name__ == '__main__':
Image(gui_driver.get_screenshot_as_png())
from selenium.webdriver.common.by import By
if __name__ == '__main__':
username = gui_driver.find_element(By.NAME, "username")
username.send_keys("demo")
if __name__ == '__main__':
password = gui_driver.find_element(By.NAME, "password")
password.send_keys("demo")
if __name__ == '__main__':
login = gui_driver.find_element(By.TAG_NAME, "button")
login.click()
time.sleep(1)
if __name__ == '__main__':
Image(gui_driver.get_screenshot_as_png())
## Collecting Crashes
## ------------------
if __name__ == '__main__':
print('\n## Collecting Crashes')
if __name__ == '__main__':
import os
os.system(f'git clone https://github.com/uds-se/simply-buggy')
if __name__ == '__main__':
import os
os.system(f'(cd simply-buggy && make)')
from .bookutils import print_file
if __name__ == '__main__':
print_file("simply-buggy/simple-crash.cpp")
if __name__ == '__main__':
print_file("simply-buggy/simple-crash.fuzzmanagerconf", lexer=IniLexer())
if __name__ == '__main__':
import os
os.system(f'simply-buggy/simple-crash')
import subprocess
if __name__ == '__main__':
cmd = ["simply-buggy/simple-crash"]
if __name__ == '__main__':
result = subprocess.run(cmd, stderr=subprocess.PIPE)
stderr = result.stderr.decode().splitlines()
crashed = False
for line in stderr:
if "ERROR: AddressSanitizer" in line:
crashed = True
break
if crashed:
print("Yay, we crashed!")
else:
print("Move along, nothing to see...")
### Program Configurations
if __name__ == '__main__':
print('\n### Program Configurations')
if __name__ == '__main__':
sys.path.append('FuzzManager')
if __name__ == '__main__':
from FTB.ProgramConfiguration import ProgramConfiguration # type: ignore
if __name__ == '__main__':
configuration = ProgramConfiguration.fromBinary('simply-buggy/simple-crash')
(configuration.product, configuration.platform)
### Crash Info
if __name__ == '__main__':
print('\n### Crash Info')
if __name__ == '__main__':
from FTB.Signatures.CrashInfo import CrashInfo # type: ignore
if __name__ == '__main__':
cmd = ["simply-buggy/simple-crash"]
result = subprocess.run(cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
if __name__ == '__main__':
stderr = result.stderr.decode().splitlines()
stderr[0:3]
if __name__ == '__main__':
stdout = result.stdout.decode().splitlines()
stdout
if __name__ == '__main__':
crashInfo = CrashInfo.fromRawCrashData(stdout, stderr, configuration)
print(crashInfo)
### Collector
if __name__ == '__main__':
print('\n### Collector')
if __name__ == '__main__':
from Collector.Collector import Collector # type: ignore
if __name__ == '__main__':
collector = Collector()
if __name__ == '__main__':
collector.submit(crashInfo)
### Inspecting Crashes
if __name__ == '__main__':
print('\n### Inspecting Crashes')
if __name__ == '__main__':
gui_driver.refresh()
if __name__ == '__main__':
Image(gui_driver.get_screenshot_as_png())
if __name__ == '__main__':
crash = gui_driver.find_element(By.XPATH, '//td/a[contains(@href,"/crashmanager/crashes/")]')
crash.click()
time.sleep(1)
if __name__ == '__main__':
Image(gui_driver.get_screenshot_as_png())
## Crash Buckets
## -------------
if __name__ == '__main__':
print('\n## Crash Buckets')
if __name__ == '__main__':
Image(gui_driver.get_screenshot_as_png())
if __name__ == '__main__':
create = gui_driver.find_element(By.XPATH, '//a[contains(@href,"/signatures/new/")]')
create.click()
time.sleep(1)
if __name__ == '__main__':
gui_driver.set_window_size(1400, 1200)
if __name__ == '__main__':
Image(gui_driver.get_screenshot_as_png())
if __name__ == '__main__':
save = gui_driver.find_element(By.NAME, "submit_save")
save.click()
time.sleep(1)
### Crash Signatures
if __name__ == '__main__':
print('\n### Crash Signatures')
if __name__ == '__main__':
gui_driver.set_window_size(1400, 800)
Image(gui_driver.get_screenshot_as_png())
### Coarse-Grained Signatures
if __name__ == '__main__':
print('\n### Coarse-Grained Signatures')
if __name__ == '__main__':
print_file("simply-buggy/out-of-bounds.cpp")
import os
import random
import subprocess
import tempfile
import sys
#### Excursion: `escapelines()` implementatipn
if __name__ == '__main__':
print('\n#### Excursion: `escapelines()` implementatipn')
def isascii(s):
return all([0 <= ord(c) <= 127 for c in s])
if __name__ == '__main__':
isascii('Hello,')
def escapelines(bytes):
def ascii_chr(byte):
if 0 <= byte <= 127:
return chr(byte)
return r"\x%02x" % byte
def unicode_escape(line):
ret = "".join(map(ascii_chr, line))
assert isascii(ret)
return ret
return [unicode_escape(line) for line in bytes.splitlines()]
if __name__ == '__main__':
escapelines(b"Hello,\nworld!")
if __name__ == '__main__':
escapelines(b"abc\xffABC")
#### End of Excursion
if __name__ == '__main__':
print('\n#### End of Excursion')
if __name__ == '__main__':
cmd = ["simply-buggy/out-of-bounds"]
# Connect to crash server
collector = Collector()
random.seed(2048)
crash_count = 0
TRIALS = 20
for itnum in range(0, TRIALS):
rand_len = random.randint(1, 1024)
rand_data = bytes([random.randrange(0, 256) for i in range(rand_len)])
(fd, current_file) = tempfile.mkstemp(prefix="fuzztest", text=True)
os.write(fd, rand_data)
os.close(fd)
current_cmd = []
current_cmd.extend(cmd)
current_cmd.append(current_file)
result = subprocess.run(current_cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout = [] # escapelines(result.stdout)
stderr = escapelines(result.stderr)
crashed = False
for line in stderr:
if "ERROR: AddressSanitizer" in line:
crashed = True
break
print(itnum, end=" ")
if crashed:
sys.stdout.write("(Crash) ")
# This reads the simple-crash.fuzzmanagerconf file
configuration = ProgramConfiguration.fromBinary(cmd[0])
# This reads and parses our ASan trace into a more generic format,
# returning us a generic "CrashInfo" object that we can inspect
# and/or submit to the server.
crashInfo = CrashInfo.fromRawCrashData(stdout, stderr, configuration)
# Submit the crash
collector.submit(crashInfo, testCase = current_file)
crash_count += 1
os.remove(current_file)
print("")
print("Done, submitted %d crashes after %d runs." % (crash_count, TRIALS))
if __name__ == '__main__':
gui_driver.get(fuzzmanager_url + "/crashmanager/crashes")
if __name__ == '__main__':
Image(gui_driver.get_screenshot_as_png())
## Collecting Code Coverage
## ------------------------
if __name__ == '__main__':
print('\n## Collecting Code Coverage')
if __name__ == '__main__':
print_file("simply-buggy/maze.cpp")
if __name__ == '__main__':
import os
os.system(f'(cd simply-buggy && make clean && make coverage)')
if __name__ == '__main__':
import os
os.system(f'git clone https://github.com/uds-se/simply-buggy simply-buggy-server ')
if __name__ == '__main__':
import os
os.system(f'cd FuzzManager; {sys.executable} server/manage.py setup_repository simply-buggy GITSourceCodeProvider ../simply-buggy-server')
import random
import subprocess
if __name__ == '__main__':
random.seed(0)
cmd = ["simply-buggy/maze"]
constants = [3735928559, 1111638594]
TRIALS = 1000
for itnum in range(0, TRIALS):
current_cmd = []
current_cmd.extend(cmd)
for _ in range(0, 4):
if random.randint(0, 9) < 3:
current_cmd.append(str(constants[
random.randint(0, len(constants) - 1)]))
else:
current_cmd.append(str(random.randint(-2147483647, 2147483647)))
result = subprocess.run(current_cmd, stderr=subprocess.PIPE)
stderr = result.stderr.decode().splitlines()
crashed = False
if stderr and "secret" in stderr[0]:
print(stderr[0])
for line in stderr:
if "ERROR: AddressSanitizer" in line:
crashed = True
break
if crashed:
print("Found the bug!")
break
print("Done!")
if __name__ == '__main__':
import os
os.system(f'export PATH=$HOME/.cargo/bin:$PATH; grcov simply-buggy/ -t coveralls+ --commit-sha $(cd simply-buggy && git rev-parse HEAD) --token NONE -p `pwd`/simply-buggy/ > coverage.json')
if __name__ == '__main__':
import os
os.system(f'cd FuzzManager; {sys.executable} -mCovReporter --repository simply-buggy --description "Test1" --submit ../coverage.json')
if __name__ == '__main__':
gui_driver.get(fuzzmanager_url + "/covmanager")
if __name__ == '__main__':
Image(gui_driver.get_screenshot_as_png())
if __name__ == '__main__':
first_id = gui_driver.find_element(By.XPATH, '//td/a[contains(@href,"/browse")]')
first_id.click()
time.sleep(1)
if __name__ == '__main__':
Image(gui_driver.get_screenshot_as_png())
if __name__ == '__main__':
maze_cpp = gui_driver.find_element(By.XPATH, "//*[contains(text(), 'maze.cpp')]")
maze_cpp.click()
time.sleep(1)
if __name__ == '__main__':
gui_driver.set_window_size(1400, 1400)
Image(gui_driver.get_screenshot_as_png())
if __name__ == '__main__':
random.seed(0)
cmd = ["simply-buggy/maze"]
# Added the missing constant here
constants = [3735928559, 1111638594, 3405695742]
for itnum in range(0,1000):
current_cmd = []
current_cmd.extend(cmd)
for _ in range(0,4):
if random.randint(0, 9) < 3:
current_cmd.append(str(
constants[random.randint(0, len(constants) - 1)]))
else:
current_cmd.append(str(random.randint(-2147483647, 2147483647)))
result = subprocess.run(current_cmd, stderr=subprocess.PIPE)
stderr = result.stderr.decode().splitlines()
crashed = False
if stderr:
print(stderr[0])
for line in stderr:
if "ERROR: AddressSanitizer" in line:
crashed = True
break
if crashed:
print("Found the bug!")
break
print("Done!")
## Synopsis
## --------
if __name__ == '__main__':
print('\n## Synopsis')
## Lessons Learned
## ---------------
if __name__ == '__main__':
print('\n## Lessons Learned')
if __name__ == '__main__':
fuzzmanager_process.terminate()
if __name__ == '__main__':
gui_driver.quit()
import shutil
if __name__ == '__main__':
for temp_file in ['coverage.json', 'geckodriver.log', 'ghostdriver.log']:
if os.path.exists(temp_file):
os.remove(temp_file)
if __name__ == '__main__':
home = os.path.expanduser("~")
for temp_dir in ['coverage', 'simply-buggy', 'simply-buggy-server',
'simply-buggy-server',
'FuzzManager']:
if os.path.exists(temp_dir):
shutil.rmtree(temp_dir)
## Next Steps
## ----------
if __name__ == '__main__':
print('\n## Next Steps')
## Background
## ----------
if __name__ == '__main__':
print('\n## Background')
## Exercises
## ---------
if __name__ == '__main__':
print('\n## Exercises')
### Exercise 1: Automatic Crash Reporting
if __name__ == '__main__':
print('\n### Exercise 1: Automatic Crash Reporting')