-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgenerate.py
executable file
·391 lines (326 loc) · 13.4 KB
/
generate.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
#!/usr/bin/env python3
#
# A simple, self-contained report generator for penetration testing reports.
#
# Lauritz Holtmann, (c) 2022 - 2023
#
import io
import os
import re
import sys
import yaml
import pdfkit
import argparse
import datetime
import markdown
import xlsxwriter
import matplotlib.pyplot as plt
from cvss import CVSS3
from datetime import date
from string import Template
# Constants
content_dir = "content/"
findings_dir = "findings/"
output_dir = "output/"
boilerplate_dir = "boilerplate/"
page_break = '\n\n<div style = "display:block; clear:both; page-break-after:always;"></div>\n\n'
# Global Variables
config = {}
findings = []
report_md = ""
report_html = ""
findings_list = ""
cover_location = ""
generated_piechart = ""
total_findings = critical_findings = high_findings = medium_findings = low_findings = none_findings = 0
# Set Base-URL to current working directory
# Makes including images to report more easy by simply referencing images/test.png
report_md += "<base href=\"file://{}/\">\n\n".format(os.getcwd())
def init():
"""Initialize the report generator, load config from config.yaml"""
global config
# Parse Config
with open('config.yaml') as f:
config = yaml.load(f, Loader=yaml.FullLoader)
print("Config options: {}".format(config))
f.close()
def generate_report():
"""Generate the PDF report"""
global report_html
# Generate Markdown Report
generate_markdown_report()
# Generate PDF Report
generate_pdf_report(report_html)
def generate_markdown_report():
"""Generate the Markdown report from the Markdown template and findings"""
global config, content_dir, cover_location, findings, findings_dir, report_md, report_html, total_findings, critical_findings, high_findings, medium_findings, low_findings, none_findings
# Glue: Collect files and build Markdown report
with open(content_dir + 'introduction.md') as f:
report_md += f.read()
report_md += page_break
f.close()
with open(content_dir + 'scope.md') as f:
report_md += f.read()
report_md += page_break
f.close()
with open(content_dir + 'technical-details.md') as f:
report_md += f.read()
report_md += page_break
f.close()
# Insert Placeholders
report_md = report_md.format(
title = config["title"],
author = config["author"],
customer = config["customer"],
critical_findings = "{critical_findings}",
high_findings = "{high_findings}",
medium_findings = "{medium_findings}",
low_findings = "{low_findings}",
piechart = "{piechart}",
findings_list = "{findings_list}"
)
# Process Findings
process_findings()
# Determine Statistics and Render Pie Chart
print("Generating Pie Chart...")
## Data for the pie chart
# Optional: Include informational findnigs
#labels = ['Critical', 'High', 'Medium', 'Low', 'None']
#sizes = [critical_findings, high_findings, medium_findings, low_findings, none_findings]
#colors = ['violet', 'red', 'orange', 'yellow', 'green']
labels = ['Critical', 'High', 'Medium', 'Low']
sizes = [critical_findings, high_findings, medium_findings, low_findings]
colors = ['violet', 'red', 'orange', 'yellow']
## Set font size and padding for legend
plt.rcParams['font.size'] = 12
plt.rcParams['legend.fontsize'] = 12
plt.rcParams['font.family'] = 'sans-serif'
plt.rcParams['font.sans-serif'] = ['avenir']
plt.rcParams["figure.autolayout"] = True
## Create the pie chart as an SVG in memory
fig, ax = plt.subplots()
ax.pie(sizes, labels=None, colors=colors, autopct=lambda pct: f"{pct:.1f}%" if pct > 0 else '')
ax.axis('equal')
### Set legend
plt.subplots_adjust(left=0.1, right=0.5)
ax.legend(labels, loc='center left', bbox_to_anchor=(1, 0.5), title='Distribution of Findings by Severity')
leg = ax.get_legend()
leg._legend_box.align = "left"
svg_io = io.BytesIO()
plt.savefig(svg_io, format='svg')
svg_io.seek(0)
generated_piechart = svg_io.getvalue().decode('utf-8')
## Create the detailed table of findings
generated_table_of_findings = ""
for counter,finding in enumerate(findings):
# Fill Template
generated_table_of_findings += "* <b style='display:inline-block;width:100px'>{}</b> #PEN{}{:04d}:\t{} ([CWE-{}](https://cwe.mitre.org/data/definitions/{}.html))\n".format(finding["cvss_severity"], date.today().year, counter+1, finding["title"], finding["CWE-ID"], finding["CWE-ID"])
# Insert Placeholders
report_md = report_md.format(
critical_findings = critical_findings,
high_findings = high_findings,
medium_findings = medium_findings,
low_findings = low_findings,
piechart = "{piechart}",
findings_list = generated_table_of_findings
)
# Append processed findings to report
for counter,finding in enumerate(findings):
print("Appending finding {}...".format(finding["title"]))
# Fill Template
report_md += finding_markdown(finding, "#PEN{}{:04d}".format(date.today().year, counter+1))
# Append Conclusion and Appendix
with open(content_dir + 'conclusion.md') as f:
report_md += f.read()
report_md += page_break
f.close()
with open(content_dir + 'appendix.md') as f:
report_md += f.read()
report_md += page_break
f.close()
# Render Markdown: Convert to main report to HTML
print("Render Markdown to HTML...")
report_html = markdown.markdown(report_md, extensions=['fenced_code', 'codehilite', 'tables'])
cover_location = "temp/cover_processed.html"
with open(boilerplate_dir + 'cover.html') as f:
cover_processed = Template(f.read()).safe_substitute(title=config["title"], author=config["author"], date=datetime.datetime.now().strftime("%Y-%m-%d"), customer=config["customer"])
f.close()
with open(cover_location, 'w') as f:
f.write(cover_processed)
f.close()
# Insert inlined SVG
report_html = report_html.replace("{piechart}", generated_piechart)
def finding_markdown(finding, finding_id = "TBD"):
"""Generate Markdown for a single finding"""
temp = """
### {}: {}
---
| Asset | CWE | Severity (CVSS v3.1 Base Score) | CVSS v3.1 Vector |
|---------------|----------------------------------------------------------|---------------------------------|----------------------------------------------------------------------------------------------|
| {} | [{}]({}) | {} ({}) | *{}* |
---
{}
""".format(
finding_id,
finding["title"],
finding["asset"],
finding["CWE-ID"],
finding["CWE-Link"],
finding["cvss_severity"],
finding["cvss_score"],
finding["cvss_vector"],
finding["description"]
)
return temp + page_break
def process_findings():
"""Process all findings and generate statistics"""
global config, findings, findings_dir, total_findings, critical_findings, high_findings, medium_findings, low_findings, none_findings
# Iterate over finding MD files, preprocess
for file in os.listdir(findings_dir):
if file.endswith(".md"):
filename = os.fsdecode(file)
with open(findings_dir + filename) as f:
print("Processing finding {}...".format(filename))
finding = {}
# Map finding description from MD file
finding["description"] = f.read()
f.close()
# Parse Properties from Header Section
re_search = re.search(r"<!--[\r\n]([\s\S]*)[\r\n]-->", finding["description"])
properties_yaml = re_search.group(1)
properties = yaml.load(properties_yaml, Loader=yaml.FullLoader)
# Cleanup: Remove properties
finding["description"] = finding["description"].replace(re_search.group(0), "")
# Map Properties
finding["title"] = properties["title"]
finding["asset"] = properties["asset"]
finding["CWE-ID"] = properties["CWE-ID"]
finding["CWE-Link"] = properties["CWE-Link"]
if "finding_id" in properties:
finding["finding_id"] = properties["finding_id"]
# calculate CVSS score and severity
cvss_vector = "CVSS:3.1/AV:{}/AC:{}/PR:{}/UI:{}/S:{}/C:{}/I:{}/A:{}".format(properties["cvss"]["AV"], properties["cvss"]["AC"], properties["cvss"]["PR"], properties["cvss"]["UI"], properties["cvss"]["S"], properties["cvss"]["C"], properties["cvss"]["I"],properties["cvss"]["A"])
c = CVSS3(cvss_vector)
finding["cvss_vector"] = c.clean_vector()
finding["cvss_score"] = c.scores()[0]
finding["cvss_severity"] = c.severities()[0]
findings.append(finding)
else:
print("File {} does not have correct file type .md".format(file))
# Sort findings, CVSS Score descending
def useScore(elem):
return elem["cvss_score"]
findings.sort(key=useScore,reverse=True)
total_findings = len(findings)
critical_findings = len([finding for finding in findings if finding["cvss_severity"] == "Critical"])
high_findings = len([finding for finding in findings if finding["cvss_severity"] == "High"])
medium_findings = len([finding for finding in findings if finding["cvss_severity"] == "Medium"])
low_findings = len([finding for finding in findings if finding["cvss_severity"] == "Low"])
none_findings = len([finding for finding in findings if finding["cvss_severity"] == "None"])
def generate_excel_report():
"""Generate Excel Report"""
global config, findings, output_dir
# Write findings to Excel file
print("Generating Excel file...")
excel_report = xlsxwriter.Workbook(output_dir + 'report.xlsx')
excel_report_sheet = excel_report.add_worksheet("Findings")
bold = excel_report.add_format({'bold': True})
table_header = excel_report.add_format({'bold': True, 'bg_color': '#c8c8cf'})
# Title
excel_report_sheet.write(0, 0, "Pentest Report: {}".format(config["title"]), bold)
excel_report_sheet.write(1, 0, "Author: {}".format(config["author"]))
excel_report_sheet.write(2, 0, "Date: {}".format(datetime.datetime.now().strftime("%Y-%m-%d")))
# Table Header
excel_report_sheet.write(4, 0, "Finding-ID", table_header)
excel_report_sheet.write(4, 1, "Severity", table_header)
excel_report_sheet.write(4, 2, "Asset", table_header)
excel_report_sheet.write(4, 3, "Title", table_header)
# Findings
row = 5
col = 0
for counter,finding in enumerate(findings):
excel_report_sheet.write(row, col, "#PEN{}{:04d}".format(date.today().year,counter+1), bold)
excel_report_sheet.write(row, col + 1, "{} ({})".format(finding["cvss_severity"], finding["cvss_score"]))
excel_report_sheet.write(row, col + 2, finding["asset"])
excel_report_sheet.write(row, col + 3, finding["title"])
row += 1
excel_report.close()
def generate_pdf_report(report_html, mode = "report", filename = "finding.md"):
"""Generate PDF Report from HTML"""
global boilerplate_dir, cover_location, output_dir
# Generate PDF
toc = {
'xsl-style-sheet': boilerplate_dir + 'toc.xsl'
}
options = {
'--header-html': boilerplate_dir + 'header.html',
'--footer-html': boilerplate_dir + 'footer.html',
#'footer-right': '[page] of [topage]',
'footer-right': '[page]',
'footer-font-name': 'avenir next',
'footer-font-size': '10',
'margin-bottom': '1.25cm',
'margin-top': '2.5cm',
'header-spacing': '-5',
'encoding': "UTF-8",
'page-size': 'A4',
"enable-local-file-access": None
}
css = boilerplate_dir + "report.css"
print("Generating PDF...")
if mode == "report":
pdfkit.from_string(report_html, output_dir+'report.pdf', options=options, css=css, toc=toc, cover=cover_location, cover_first=True)
elif mode == "findings":
pdfkit.from_string(report_html, output_dir+filename, options=options, css=css)
def all():
"""Generate all reports"""
generate_report()
generate_excel_report()
def print_findings():
global config, findings, findings_dir, total_findings, critical_findings, high_findings, medium_findings, low_findings, none_findings
print("Processed {} findings:".format(total_findings))
print("Critical: {}".format(critical_findings))
print("High: {}".format(high_findings))
print("Medium: {}".format(medium_findings))
print("Low: {}".format(low_findings))
print("None: {}".format(none_findings))
print("Findings:")
for finding in findings:
print("++++++++++++")
print("Title: {}".format(finding["title"]))
print("Asset: {}".format(finding["asset"]))
print("Severity: {}".format(finding["cvss_severity"]))
print("CVSS Score: {}".format(finding["cvss_score"]))
print("")
def generate_findings_reports():
"""Generate separate report files for all findings"""
global config, findings
for counter,finding in enumerate(findings):
if "finding_id" in finding:
finding_id = finding["finding_id"]
else:
finding_id = "PEN{}{:04d}".format(date.today().year,counter+1)
print("Generating report for finding #{}...".format(finding_id))
finding_markdown_temp = "<base href=\"file://{}/\">\n\n".format(os.getcwd()) + finding_markdown(finding, finding_id)
finding_html = markdown.markdown(finding_markdown_temp, extensions=['fenced_code', 'codehilite', 'tables'])
generate_pdf_report(finding_html, mode = "findings", filename = "finding_{}.pdf".format(finding_id))
################################################
if __name__ == '__main__':
init()
# Parse arguments
parser = argparse.ArgumentParser(description='Render a pentest report.')
parser.add_argument('--all', default=False, action='store_true', help='Generate all reports from scratch.')
parser.add_argument('--view_findings', default=False, action='store_true', help='Print all findings.')
parser.add_argument('--findings_only', default=False, action='store_true', help='Generate separate report files for all findings.')
if len(sys.argv) == 1:
parser.print_help(sys.stderr)
sys.exit(1)
args = parser.parse_args()
if args.all:
all()
if args.view_findings:
process_findings()
print_findings()
if args.findings_only:
process_findings()
generate_findings_reports()