-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathanalyze.py
executable file
·290 lines (233 loc) · 8.11 KB
/
analyze.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
#!/usr/bin/env python3
import argparse
import csv
import importlib
import jinja2
import json
import logging
import pathlib
import sys
import tomllib as toml
LOGGER = logging.getLogger(__name__)
ANALYZERS_DIR = pathlib.Path(
pathlib.Path(__file__).resolve().parent,
"analyzers"
)
SUPPORTED_SERVICES = []
for path in ANALYZERS_DIR.iterdir():
if path.is_dir() and not path.name.startswith('__'):
SUPPORTED_SERVICES.append(path.name)
LANGUAGE = 'en'
SUPPORTED_FORMATS = ['md', 'json', 'csv']
def analyze_service(service, files, recommendations_file, tool=None):
with open(recommendations_file, 'rb') as f:
recommendations = toml.load(f)
LOGGER.debug(f"importing 'analyzers.{service}'")
module = importlib.import_module(f'analyzers.{service}')
analyzer = module.Analyzer(service, recommendations)
if tool:
analyzer.set_parser(tool)
services = analyzer.analyze(files)
issues_file = pathlib.Path(
pathlib.Path(__file__).resolve().parent,
"config",
"issues",
service,
f"{LANGUAGE}.toml"
)
LOGGER.info(f"loading issues file '{issues_file}'")
if not issues_file.exists():
LOGGER.error("issues file does not exist!")
sys.exit(f"the file '{issues_file}' does not exist!")
with open(issues_file, 'rb') as f:
issue_templates = toml.load(f)
affected_assets = {}
issues = {}
recommendations = []
references = []
info = []
for asset, service in services.items():
if len(service['issues']) == 0:
continue
affected_assets[asset] = []
for issue in service['issues']:
issue.format(issue_templates)
for recommendation in issue.recommendations:
if recommendation not in recommendations:
recommendations.append(recommendation)
for reference in issue.references:
if reference not in references:
references.append(reference)
affected_assets[asset].append(issue.description)
if issue.description not in issues:
issues[issue.description] = []
issues[issue.description].append(asset)
# collect additional (debug) information
if 'info' in service:
for i in service['info']:
if i not in info:
info.append(i)
return {
'services': services,
'affected_assets': affected_assets,
'issues': issues,
'recommendations': recommendations,
'references': references,
'additional_info': info,
}
def get_files(directory, service):
files = {}
for path in directory.glob(f'*/{service}*'):
suffix = path.suffix[1:]
stem = path.stem # the final path component, w/o its suffix
tool = stem.split(',')[-1]
if tool not in files:
files[tool] = {}
if suffix not in files[tool]:
files[tool][suffix] = []
if path not in files[tool][suffix]:
files[tool][suffix].append(str(path))
return files
def render_CSV(services):
delimiter = ','
header = ['asset', 'issues']
csv.writer(sys.stdout, delimiter=delimiter, quoting=csv.QUOTE_MINIMAL).writerow(header)
for identifier, service in services.items():
for issue in service['issues']:
row = [identifier, issue.description]
csv.writer(sys.stdout, delimiter=delimiter, quoting=csv.QUOTE_MINIMAL).writerow(row)
def process(args):
if not args.input.exists():
sys.exit(f"the specified directory '{args.input}' does not exist!")
if args.service == '?':
services = {}
for service in SUPPORTED_SERVICES:
files = get_files(args.input, service)
if len(files):
services[service] = files.keys()
print("scan results relating to the following services are available for analysis.")
print("the name of the scanner is shown in parenthesis.\n")
for service, tools in services.items():
print(f"* {service} ({', '.join(tools)})")
else:
logging.basicConfig(
format = '%(levelname)s: %(message)s',
filename = f'analyzer-{args.service}.log',
filemode = 'w',
encoding = 'utf-8',
level = logging.DEBUG
)
LOGGER.debug(f"requested to analyze '{args.service}'")
files = get_files(args.input, args.service)
global LANGUAGE
LANGUAGE = args.language
LOGGER.debug(f"using language '{LANGUAGE}'")
if args.recommendations:
recommendations_file = args.recommendations
LOGGER.info(f"user specified recommendations file: '{args.recommendations}'")
if not recommendations_file.exists():
LOGGER.error("the recommendations file does not exist!")
sys.exit(f"the recommendations file '{recommendations_file}' does not exist!")
else:
recommendations_file = pathlib.Path(
pathlib.Path(__file__).resolve().parent,
"config",
"recommendations",
args.service,
"default.toml"
)
LOGGER.info(f"using default recommendations file: '{recommendations_file}'")
if not recommendations_file.exists():
LOGGER.error("the recommendations file does not exist!")
sys.exit(f"the default recommendations file '{recommendations_file}' does not exist!")
analysis = analyze_service(
args.service,
files,
recommendations_file,
tool = args.tool
)
analysis['recommendations_file'] = recommendations_file
template_file = None
if args.template:
LOGGER.info(f"user specified template file: '{args.template}'")
if not args.template.exists():
LOGGER.error("the template file does not exist!")
sys.exit(f"the specified template file '{args.template}' does not exist!")
else:
template_file = args.template.resolve()
else:
LOGGER.info(f"user specified format: '{args.fmt}'")
if args.fmt == 'json':
print(json.dumps(analysis['services']))
elif args.fmt == 'csv':
render_CSV(analysis['services'])
else:
template_file = pathlib.Path(
pathlib.Path(__file__).resolve().parent,
"config",
"templates",
f"default.{LANGUAGE}.md"
)
LOGGER.info(f"using default template file: '{template_file}'")
if not template_file.exists():
LOGGER.error("the template file does not exist!")
sys.exit(f"the default template file '{template_file}' does not exist!")
if template_file:
env = jinja2.Environment(
loader = jinja2.FileSystemLoader(template_file.parent),
trim_blocks = True,
autoescape = False
)
template = env.get_template(template_file.name)
rendered_analysis = template.render(analysis).strip()
print(rendered_analysis)
def main():
parser = argparse.ArgumentParser(
description = "Analyze and summarize the results of specific tools previously run by the scanner of the recon tool suite (i.e. 'scan')."
)
parser.add_argument(
'service',
choices = ['?'] + sorted(SUPPORTED_SERVICES),
help = "specify the service that should be analyzed. use '?' to list services available for analysis."
)
parser.add_argument(
'-t', '--tool',
metavar = 'name',
help = "specify the tool whose results are to be parsed"
)
parser.add_argument(
'-r', '--recommendations',
metavar = 'path',
type = pathlib.Path,
help = "path to the recommendations document (default: '/path/to/recon/config/recommendations/<service>/default.toml')"
)
parser.add_argument(
'-i', '--input',
metavar = 'path',
type = pathlib.Path,
default = './recon',
help = "path to the root directory that holds the results to be analysed (default: './recon')"
)
parser.add_argument(
'-l', '--language',
metavar = 'code',
default = LANGUAGE,
help = f"specify the language in which the analysis should be printed (default: '{LANGUAGE}')"
)
parser.add_argument(
'-f', '--format',
dest = 'fmt',
metavar = 'code',
choices = SUPPORTED_FORMATS,
default = SUPPORTED_FORMATS[0],
help = f"specify the output format of the analysis (choices: {SUPPORTED_FORMATS}; default: '{SUPPORTED_FORMATS[0]}')"
)
parser.add_argument(
'--template',
metavar = 'path',
type = pathlib.Path,
help = "path to the Jinja2 template for the analysis; this option overrides '-f/--format'"
)
process(parser.parse_args())
if __name__ == '__main__':
main()