-
Notifications
You must be signed in to change notification settings - Fork 39
/
rapidast.py
executable file
·304 lines (244 loc) · 10.9 KB
/
rapidast.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
#!/usr/bin/env python3
import argparse
import logging
import os
import pprint
import re
import sys
from datetime import datetime
from typing import Any
from typing import Dict
from urllib import request
import yaml
from dotenv import load_dotenv
import configmodel.converter
import scanners
from exports.defect_dojo import DefectDojo
from exports.google_cloud_storage import GoogleCloudStorage
from utils import add_logging_level
pp = pprint.PrettyPrinter(indent=4)
DEFAULT_CONFIG_FILE = os.path.join(os.path.dirname(__file__), "rapidast-defaults.yaml")
def load_environment(config):
"""Load the environment variables based on the config set in config.environ"""
source = config.get("config.environ.envFile")
if source:
load_dotenv(source)
logging.debug(f"Loaded dotenv from '{source}'")
else:
logging.debug("No environment loaded")
def get_full_result_dir_path(rapidast_config):
# Enforced result layout:
# {config.results_base_dir}/
# {application.shortName}/
# {DAST-<date-time>-RapiDAST-<application.shortName>}/
# <scanner-name>
# This way each runs will get their own directory, and each scanner their own subdir
scan_datetime_str = datetime.now().strftime("%Y%m%d-%H%M%S")
app_name = rapidast_config.get("application.shortName", default="scannedApp")
results_dir_path = os.path.join(
rapidast_config.get("config.base_results_dir", default="./results"),
app_name,
f"DAST-{scan_datetime_str}-RapiDAST-{app_name}",
)
return results_dir_path
def load_config_file(config_file_location: str):
if re.compile(r"^https?://").match(config_file_location):
return request.urlopen(config_file_location)
else:
return open(config_file_location, mode="r", encoding="utf-8")
def load_config(config_file_location: str) -> Dict[str, Any]:
return yaml.safe_load(load_config_file(config_file_location))
# pylint: disable=R0911
# too many return statements
def run_scanner(name, config, args, scan_exporter):
"""given the config `config`, runs scanner `name`.
Returns:
0 for success
1 for failure
(in order to count the number of failure)
"""
# Merge the "general" configuration into the scanner's config
# (but without overwriting anything: scanner's config takes precedence over the general config)
logging.debug(f"Merging general config into {name}'s config")
config.merge(
config.get("general", default={}),
preserve=True,
root=f"scanners.{name}",
)
typ = config.get(f"scanners.{name}.container.type", default="none")
try:
class_ = scanners.str_to_scanner(name, typ)
except ModuleNotFoundError:
logging.error(f"Scanner `{name.split('_')[0]}` of type `{typ}` does not exist")
logging.error(f"Ignoring failed Scanner `name.split('_')[0]` of type `{typ}`")
logging.error(f"Please verify your configuration file: `scanners.{name}`")
return 1
# Part 1: create a instance based on configuration
try:
scanner = class_(config, name)
except OSError as e:
logging.error(f"Caught exception: {e}")
logging.error(f"Ignoring failed Scanner `{name}` of type `{typ}`")
return 1
except RuntimeError as e:
logging.error(f"Caught exception: {e}")
logging.error(f"Ignoring failed Scanner `{name}` of type `{typ}`")
return 1
# Part 2: setup the environment (e.g.: spawn a server)
try:
scanner.setup()
except Exception as excp: # pylint: disable=W0718
logging.error(f"Failed to set up the scanner: {excp}")
scanner.state = scanners.State.ERROR
logging.debug(scanner)
# Part 3: run the actual scan
if scanner.state == scanners.State.READY:
scanner.run()
else:
logging.error(f"scanner {name} is not in READY state: it will not be run")
return 1
# Part 4: Post process
# Even in case of error, some information will be stored in the result directory for troubleshooting
scanner.postprocess()
# Part 5: cleanup
if not scanner.state == scanners.State.PROCESSED:
logging.error(f"Something is wrong. Scanner {name} is not in PROCESSED state: the workdir won't be cleaned up")
return 1
if not args.no_cleanup:
scanner.cleanup()
# Part 6: export to defect dojo, if the scanner is compatible
if scan_exporter and hasattr(scanner, "data_for_defect_dojo"):
logging.info("Exporting results to the Defect Dojo service as configured")
if scan_exporter.export_scan(*scanner.data_for_defect_dojo()) == 1:
logging.error("Exporting results to DefectDojo failed")
return 1
return 0
def dump_redacted_config(config_file_location: str, destination_dir: str) -> bool:
"""
Redacts sensitive parameters from a configuration file and writes the redacted
version to a destination directory
Args:
config_file_location: The file path to the source configuration file
destination_dir: The directory where the redacted configuration file should be saved
"""
logging.info(f"Starting the redaction and dumping process for the configuration file: {config_file_location}")
try:
if not os.path.exists(destination_dir):
os.makedirs(destination_dir)
logging.info(f"Created destination directory: {destination_dir}")
config = yaml.safe_load(load_config_file(config_file_location))
logging.info(f"Redacting sensitive information from configuration {config_file_location}")
for key in config.keys():
if not isinstance(config[key], dict):
continue
if config[key].get("authentication") and config[key]["authentication"].get("parameters"):
for param in config[key]["authentication"]["parameters"]:
config[key]["authentication"]["parameters"][param] = "*****"
dest = os.path.join(destination_dir, os.path.basename(config_file_location))
logging.info(f"Saving redacted configuration to {dest}")
with open(dest, "w", encoding="utf-8") as file:
yaml.dump(config, file)
logging.info("Redacted configuration saved successfully")
return True
except (FileNotFoundError, yaml.YAMLError, IOError) as e:
logging.error(f"Error occurred while dumping redacted config: {e}")
return False
def dump_rapidast_redacted_configs(main_config_file_location: str, destination_dir: str):
"""
Dumps redacted versions of the main and default configuration files to the destination directory.
Args:
main_config_file_location: The file path to the main configuration file.
destination_dir: The directory where the redacted configuration files should be saved.
"""
if not dump_redacted_config(main_config_file_location, destination_dir):
logging.error("Failed to dump configuration. Exiting.")
sys.exit(2)
if os.path.exists(DEFAULT_CONFIG_FILE):
if not dump_redacted_config(DEFAULT_CONFIG_FILE, destination_dir):
logging.error("Failed to dump configuration. Exiting.")
sys.exit(2)
def run():
parser = argparse.ArgumentParser(
description="Runs various DAST scanners against a defined target, as configured by a configuration file."
)
parser.add_argument(
"--log-level",
dest="loglevel",
choices=["debug", "verbose", "info", "warning", "error", "critical"],
default="info",
help="Level of verbosity",
)
parser.add_argument(
"--config",
dest="config_file",
default="./config/config.yaml",
help="URL or Path to YAML config file",
)
parser.add_argument(
"--no-cleanup",
dest="no_cleanup",
help="Scanners to not cleanup their environment. (might be useful for debugging purpose).",
action="store_true",
)
args = parser.parse_args()
args.loglevel = args.loglevel.upper()
add_logging_level("VERBOSE", logging.DEBUG + 5)
logging.basicConfig(format="%(levelname)s:%(message)s", level=args.loglevel)
config_file = parser.parse_args().config_file
logging.debug(f"log level set to debug. Config file: '{config_file}'")
# Load config file
try:
config = configmodel.RapidastConfigModel(yaml.safe_load(load_config_file(config_file)))
except yaml.YAMLError as exc:
raise RuntimeError(f"YAML error in config {config_file}':\n {str(exc)}") from exc
full_result_dir_path = get_full_result_dir_path(config)
dump_rapidast_redacted_configs(config_file, full_result_dir_path)
# Optionally adds default if file exists (will not overwrite existing entries)
if os.path.exists(DEFAULT_CONFIG_FILE):
logging.info(f"Loading defaults from: {DEFAULT_CONFIG_FILE}")
try:
config.merge(yaml.safe_load(load_config_file(DEFAULT_CONFIG_FILE)), preserve=True)
except yaml.YAMLError as exc:
raise RuntimeError(f"YAML error in config {DEFAULT_CONFIG_FILE}':\n {str(exc)}") from exc
# Update to latest config schema if need be
config = configmodel.converter.update_to_latest_config(config)
config.set("config.results_dir", full_result_dir_path)
logging.debug(f"The entire loaded configuration is as follow:\n=====\n{pp.pformat(config)}\n=====")
# Do early: load the environment file if one is there
load_environment(config)
# Prepare an export to Defect Dojo if one is configured.
scan_exporter = None
if config.get("config.googleCloudStorage.bucketName"):
scan_exporter = GoogleCloudStorage(
bucket_name=config.get("config.googleCloudStorage.bucketName", "default-bucket-name"),
app_name=config.get_official_app_name(),
directory=config.get("config.googleCloudStorage.directory", None),
keyfile=config.get("config.googleCloudStorage.keyFile", None),
)
elif config.get("config.defectDojo.url"):
scan_exporter = DefectDojo(
config.get("config.defectDojo.url"),
{
"username": config.get("config.defectDojo.authorization.username", default=""),
"password": config.get("config.defectDojo.authorization.password", default=""),
},
config.get("config.defectDojo.authorization.token"),
config.get("config.defectDojo.ssl", default=True),
)
# Run all scanners
scan_error_count = 0
for name in config.get("scanners"):
logging.info(f"Next scanner: '{name}'")
ret = run_scanner(name, config, args, scan_exporter)
if ret == 1:
logging.info(f"scanner: '{name}' failed")
scan_error_count = scan_error_count + 1
else:
logging.info(f"scanner: '{name}' completed successfully")
if scan_error_count > 0:
logging.warning(f"Number of failed scanners: {scan_error_count}")
sys.exit(2)
else:
sys.exit(0)
if __name__ == "__main__":
run()