-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.py
206 lines (181 loc) · 6.42 KB
/
run.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
import argparse
import os
import pathlib
from typing import Generator, Optional, Tuple
import structlog
from dotenv import load_dotenv
from wafp.__main__ import main as run
from wafp.fuzzers import loader as fuzzers_loader
from wafp.targets import loader as targets_loader
logger = structlog.get_logger()
load_dotenv()
BASIC_SCHEMATHESIS = (
"schemathesis:AllChecks",
"schemathesis:Default",
"schemathesis:LessPreProcessing",
"schemathesis:Negative",
"schemathesis:NegativeNoSwarm",
"schemathesis:NoFormats",
"schemathesis:NoMutations",
)
COMBINATIONS = {
"age_of_empires_2_api:Default": {
"fuzzers": [
"api_fuzzer",
"got_swag",
"restler",
*BASIC_SCHEMATHESIS,
],
},
# "age_of_empires_2_api:Linked": {
# "fuzzers": ["schemathesis:StatefulNew", "schemathesis:StatefulOld"],
# },
"cccatalog_api:Default": {
"fuzzers": ["got_swag", "restler", *BASIC_SCHEMATHESIS],
},
# "cccatalog_api:Linked": {
# "fuzzers": ["schemathesis:StatefulNew"],
# },
"covid19_japan_web_api:Default": {
"fuzzers": [
"api_fuzzer",
"cats",
"restler",
*BASIC_SCHEMATHESIS,
],
},
# "covid19_japan_web_api:Linked": {
# "fuzzers": ["schemathesis:StatefulNew", "schemathesis:StatefulOld"],
# },
"disease_sh:Default": {"fuzzers": ["api_fuzzer", "cats", *BASIC_SCHEMATHESIS]},
# "disease_sh:Linked": {"fuzzers": ["schemathesis:StatefulNew", "schemathesis:StatefulOld"]},
"httpbin": {
"fuzzers": ["api_fuzzer", *BASIC_SCHEMATHESIS],
},
"jupyter_server:Default": {
"fuzzers": ["cats", "restler", *BASIC_SCHEMATHESIS],
},
# "jupyter_server:Linked": {
# "fuzzers": ["schemathesis:StatefulNew", "schemathesis:StatefulOld"],
# },
"jupyterhub:Default": {
"fuzzers": [*BASIC_SCHEMATHESIS],
},
# "jupyterhub:Linked": {
# "fuzzers": ["schemathesis:StatefulNew", "schemathesis:StatefulOld"],
# },
"mailhog": {"fuzzers": ["api_fuzzer", "cats", "restler", *BASIC_SCHEMATHESIS]},
"open_fec:Default": {
"fuzzers": [
"api_fuzzer",
"cats",
"fuzz_lightyear",
"got_swag",
"restler",
*BASIC_SCHEMATHESIS,
"swagger_fuzzer",
],
},
# "open_fec:Linked": {
# "fuzzers": ["schemathesis:StatefulNew", "schemathesis:StatefulOld"],
# },
"opentopodata": {
"fuzzers": ["api_fuzzer", "cats", "restler", *BASIC_SCHEMATHESIS],
},
"otto_parser": {"fuzzers": [*BASIC_SCHEMATHESIS]},
"pslab_webapp": {
"fuzzers": ["api_fuzzer", "cats", "fuzz_lightyear", "restler", *BASIC_SCHEMATHESIS],
},
"pulpcore": {
"fuzzers": [
"api_fuzzer",
"cats",
"got_swag",
*BASIC_SCHEMATHESIS,
"tnt_fuzzer",
],
},
"request_baskets:Default": {"fuzzers": ["api_fuzzer", "cats", "restler", *BASIC_SCHEMATHESIS]},
# "request_baskets:Linked": {"fuzzers": ["schemathesis:StatefulNew", "schemathesis:StatefulOld"]},
"restler_demo:Default": {
"fuzzers": ["got_swag", "restler", *BASIC_SCHEMATHESIS],
},
# "restler_demo:Linked": {
# "fuzzers": ["schemathesis:StatefulNew", "schemathesis:StatefulOld"],
# },
"worklog:Default": {
"fuzzers": ["api_fuzzer", "restler", *BASIC_SCHEMATHESIS],
},
# "worklog:Linked": {
# "fuzzers": ["schemathesis:StatefulNew", "schemathesis:StatefulOld"],
# },
# "gitlab": {"fuzzers": ["schemathesis:Default"]},
}
def split_name(name: str) -> Tuple[str, Optional[str]]:
return tuple(name.split(":", 1) + [None])[:2] # type:ignore
def get_env_var_prefix(target: str) -> str:
name, _ = split_name(target)
return name.upper()
def get_sentry_dsn_env_var_name(target: str) -> str:
prefix = get_env_var_prefix(target)
return f"{prefix}_SENTRY_DSN"
def get_sentry_dsn(target: str) -> Optional[str]:
env_var_name = get_sentry_dsn_env_var_name(target)
return os.getenv(env_var_name)
def is_match(value: str, expected: str) -> bool:
name, variant = split_name(value)
expected_name, expected_variant = split_name(expected)
return name == expected_name and (expected_variant is None or expected_variant == variant)
def expand_options(options: Generator[str, None, None]) -> list[str]:
output = []
for option in options:
output.append(option)
name, variant = split_name(option)
if variant is not None:
output.append(name)
return output
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument(
"--output-dir",
action="store",
required=True,
type=str,
)
parser.add_argument(
"--iterations",
action="store",
default=30,
type=int,
)
parser.add_argument("--fuzzer", choices=expand_options(fuzzers_loader.get_all_variants()), help="Fuzzer to run")
parser.add_argument("--target", choices=expand_options(targets_loader.get_all_variants()), help="Target to run")
return parser.parse_args()
def run_single(fuzzer: str, target: str, iteration: int, output_dir: pathlib.Path, sentry_dsn: Optional[str]) -> None:
final_dir = output_dir / f"{fuzzer}-{target}-{iteration}"
if final_dir.exists():
print("The output directory exists! Skipping", final_dir)
return
args = [fuzzer, target, "--build", f"--output-dir={final_dir}"]
if sentry_dsn is not None:
args.append(f"--sentry-dsn={sentry_dsn}")
run(args)
def main() -> None:
args = parse_args()
assert args.iterations >= 0, "The number of iterations should be a positive integer"
output_dir = pathlib.Path(args.output_dir).absolute()
for target, data in COMBINATIONS.items():
if args.target and not is_match(target, args.target):
continue
sentry_dsn = get_sentry_dsn(target)
for fuzzer in data.get("fuzzers", ()):
if args.fuzzer and not is_match(fuzzer, args.fuzzer):
continue
if sentry_dsn:
logger.info("Sentry is installed")
else:
logger.warn("Sentry is not installed")
for iteration in range(1, args.iterations + 1):
run_single(fuzzer, target, iteration, output_dir, sentry_dsn)
if __name__ == "__main__":
main()