GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — pythonfuzz/pythonfuzz/fuzzer.py at master · fuzzitdev/pythonfuzz

This repository was archived by the owner on Apr 30, 2021. It is now read-only.

Expand file tree

/

fuzzer.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
161 lines (140 loc) · 5.52 KB
/

fuzzer.py

Copy path

File metadata and controls

161 lines (140 loc) · 5.52 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import os
import sys
import time
import sys
import psutil
import hashlib
import logging
import functools
import multiprocessing as mp
from pythonfuzz import corpus, tracer
logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))
logging.getLogger().setLevel(logging.DEBUG)
SAMPLING_WINDOW = 5 # IN SECONDS
try:
lru_cache = functools.lru_cache
except:
import functools32
lru_cache = functools32.lru_cache
def worker(target, child_conn, close_fd_mask):
# Silence the fuzzee's noise
class DummyFile:
"""No-op to trash stdout away."""
def write(self, x):
pass
logging.captureWarnings(True)
logging.getLogger().setLevel(logging.CRITICAL)
if close_fd_mask & 1:
sys.stdout = DummyFile()
if close_fd_mask & 2:
sys.stderr = DummyFile()
sys.settrace(tracer.trace)
while True:
buf = child_conn.recv_bytes()
try:
target(buf)
except Exception as e:
print("Exception: %r\n" % (e,))
logging.exception(e)
child_conn.send(e)
break
else:
child_conn.send_bytes(b'%d' % tracer.get_coverage())
class Fuzzer(object):
def __init__(self,
target,
dirs=None,
exact_artifact_path=None,
rss_limit_mb=2048,
timeout=120,
regression=False,
max_input_size=4096,
close_fd_mask=0,
runs=-1,
mutators_filter=None,
dict_path=None):
self._target = target
self._dirs = [] if dirs is None else dirs
self._exact_artifact_path = exact_artifact_path
self._rss_limit_mb = rss_limit_mb
self._timeout = timeout
self._regression = regression
self._close_fd_mask = close_fd_mask
self._corpus = corpus.Corpus(self._dirs, max_input_size, mutators_filter, dict_path)
self._total_executions = 0
self._executions_in_sample = 0
self._last_sample_time = time.time()
self._total_coverage = 0
self._p = None
self.runs = runs
def help_mutators(self):
print("Mutators currently available (and their types):")
active_mutators = [mutator.__class__ for mutator in self._corpus.mutators]
for mutator in corpus.mutator_classes:
active = mutator in active_mutators
indicator = '-' if not active else ' '
print(" {}{:<60s} [{}]".format(indicator, mutator.name, ', '.join(sorted(mutator.types))))
print("\nMutators prefixed by '-' are currently disabled.")
def log_stats(self, log_type):
rss = (psutil.Process(self._p.pid).memory_info().rss + psutil.Process(os.getpid()).memory_info().rss) / 1024 / 1024
endTime = time.time()
execs_per_second = int(self._executions_in_sample / (endTime - self._last_sample_time))
self._last_sample_time = time.time()
self._executions_in_sample = 0
logging.info('#{} {} cov: {} corp: {} exec/s: {} rss: {} MB'.format(
self._total_executions, log_type, self._total_coverage, self._corpus.length, execs_per_second, rss))
return rss
def write_sample(self, buf, prefix='crash-'):
m = hashlib.sha256()
m.update(buf)
if self._exact_artifact_path:
crash_path = self._exact_artifact_path
else:
crash_path = prefix + m.hexdigest()
logging.info('sample written to {}'.format(crash_path))
if len(buf) < 200:
try:
logging.info('sample = {}'.format(buf.hex()))
except AttributeError:
logging.info('sample = {!r}'.format(buf))
with open(crash_path, 'wb') as f:
f.write(buf)
def start(self):
logging.info("#0 READ units: {}".format(self._corpus.length))
parent_conn, child_conn = mp.Pipe()
self._p = mp.Process(target=worker, args=(self._target, child_conn, self._close_fd_mask))
self._p.start()
while True:
if self.runs != -1 and self._total_executions >= self.runs:
self._p.terminate()
logging.info('did %d runs, stopping now.', self.runs)
break
buf = self._corpus.generate_input()
parent_conn.send_bytes(bytes(buf))
if not parent_conn.poll(self._timeout):
self._p.terminate()
logging.info("=================================================================")
logging.info("timeout reached. testcase took: {}".format(self._timeout))
self.write_sample(buf, prefix='timeout-')
break
try:
total_coverage = int(parent_conn.recv_bytes())
except ValueError:
self.write_sample(buf)
break
self._total_executions += 1
self._executions_in_sample += 1
rss = 0
if total_coverage > self._total_coverage:
self._total_coverage = total_coverage
self._corpus.put(buf)
rss = self.log_stats("NEW")
else:
if (time.time() - self._last_sample_time) > SAMPLING_WINDOW:
rss = self.log_stats('PULSE')
if rss > self._rss_limit_mb:
logging.info('MEMORY OOM: exceeded {} MB. Killing worker'.format(self._rss_limit_mb))
self.write_sample(buf)
self._p.kill()
break
self._p.join()
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.