-
Notifications
You must be signed in to change notification settings - Fork 0
/
vulnerability-report-generator.py
338 lines (301 loc) · 11.9 KB
/
vulnerability-report-generator.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
import json
import os
from collections import Counter, defaultdict
def read_json_file(file_path):
try:
with open(file_path, 'r', encoding='utf-8') as file:
return json.load(file)
except json.JSONDecodeError as e:
raise ValueError(f"Error decoding JSON from file {file_path}: {e}")
except Exception as e:
raise ValueError(f"Error reading file {file_path}: {e}")
def extract_severity_counts(data):
if not isinstance(data, dict):
raise ValueError("Expected data to be a dictionary with library names as keys.")
severity_counter = Counter()
vulnerabilities_by_severity = defaultdict(list)
for library, details in data.items():
if 'vulnerabilities' in details:
for vulnerability in details['vulnerabilities']:
severity = vulnerability.get('severity')
summary = vulnerability['identifiers'].get('summary', 'N/A')
fixes = vulnerability.get('info', [])
fix_links = ', '.join(f'<a href="{fix}" target="_blank">{fix}</a>' for fix in fixes)
if severity:
severity_counter[severity] += 1
vulnerabilities_by_severity[severity].append({
'library': library,
'severity': severity,
'summary': summary,
'fix': fix_links
})
return severity_counter, vulnerabilities_by_severity
def generate_html_from_json(severity_counts, vulnerabilities_by_severity, output_file):
severity_data = [{"severity": k, "count": v} for k, v in severity_counts.items()]
color_map = {
'low': '#28a745',
'medium': '#ffc107',
'high': '#dc3545',
'critical': '#dc3545'
}
html_content = f'''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vulnerability Report</title>
<script src="https://d3js.org/d3.v7.min.js"></script>
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap" rel="stylesheet">
<style>
body {{
font-family: 'Roboto', sans-serif;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
align-items: center;
background-color: #f5f5f5;
}}
h1 {{
margin-top: 20px;
color: #333;
}}
#pie-chart {{
margin-top: 10px;
width: 90%;
max-width: 450px;
height: 450px;
}}
table {{
width: 90%;
max-width: 1000px;
margin-top: 30px;
border-collapse: collapse;
background-color: #fff;
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
border-spacing: 0;
}}
th, td {{
border: 1px solid #ddd;
padding: 12px;
text-align: left;
word-wrap: break-word;
}}
th {{
background-color: #007BFF;
color: white;
}}
tr:nth-child(even) {{
background-color: #f2f2f2;
}}
tr:hover {{
background-color: #ddd;
}}
.arc text {{
font: 12px sans-serif;
text-anchor: middle;
}}
.table-container {{
width: 90%;
max-width: 1200px;
margin: 0 auto;
}}
.low-severity {{ border-left: 5px solid #28a745; }}
.medium-severity {{ border-left: 5px solid #ffc107; }}
.high-severity {{ border-left: 5px solid #dc3545; }}
.critical-severity {{ border-left: 5px solid #dc3545; }}
th.library, td.library {{ width: 25%; }}
th.severity, td.severity {{ width: 15%; }}
th.summary, td.summary {{ width: 40%; }}
th.fix, td.fix {{ width: 20%; }}
#top-button {{
position: fixed;
bottom: 20px;
right: 20px;
display: none;
font-size: 18px;
background-color: #007BFF;
color: white;
border: none;
padding: 10px 15px;
border-radius: 5px;
cursor: pointer;
}}
#top-button:hover {{
background-color: #0056b3;
}}
@media (max-width: 768px) {{
table {{
width: 100%;
}}
}}
</style>
</head>
<body>
<h1>Vulnerability Report</h1>
<!-- Pie Chart -->
<div id="pie-chart"></div>
<script>
// Data for Pie Chart
const data = {json.dumps(severity_data)};
// Define colors
const colorMap = {json.dumps(color_map)};
// Dimensions
const width = document.getElementById('pie-chart').clientWidth;
const height = width;
const margin = 40;
// Radius
const radius = Math.min(width, height) / 2 - margin;
// Append the SVG object
const svg = d3.select("#pie-chart")
.append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
// Color scale
const color = d3.scaleOrdinal()
.domain(data.map(d => d.severity))
.range(Object.values(colorMap));
// Compute the position of each group on the pie
const pie = d3.pie()
.value(d => d.count);
const data_ready = pie(data);
// Shape helper to build arcs
const arc = d3.arc()
.innerRadius(0)
.outerRadius(radius);
// Build the pie chart
svg
.selectAll('pieces')
.data(data_ready)
.enter()
.append('path')
.attr('d', arc)
.attr('fill', d => color(d.data.severity))
.attr("stroke", "white")
.style("stroke-width", "2px")
.style("opacity", 0.7)
.on("click", function(event, d) {{
window.location.hash = d.data.severity + "-severity";
}});
// Add labels
svg
.selectAll('pieces')
.data(data_ready)
.enter()
.append('text')
.text(d => d.data.severity)
.attr("transform", d => "translate(" + arc.centroid(d) + ")")
.style("text-anchor", "middle")
.style("font-size", 15);
</script>
<!-- Vulnerabilities Tables -->
<div class="table-container">
<h2 id="low-severity">Low Severity Vulnerabilities</h2>
<table class="low-severity">
<tr>
<th class="library">Library</th>
<th class="summary">Summary</th>
<th class="fix">Fix</th>
</tr>
{''.join(f'''
<tr>
<td class="library">{vul['library']}</td>
<td class="summary">{vul['summary']}</td>
<td class="fix">{vul['fix']}</td>
</tr>
''' for vul in vulnerabilities_by_severity.get('low', []))}
</table>
<h2 id="medium-severity">Medium Severity Vulnerabilities</h2>
<table class="medium-severity">
<tr>
<th class="library">Library</th>
<th class="summary">Summary</th>
<th class="fix">Fix</th>
</tr>
{''.join(f'''
<tr>
<td class="library">{vul['library']}</td>
<td class="summary">{vul['summary']}</td>
<td class="fix">{vul['fix']}</td>
</tr>
''' for vul in vulnerabilities_by_severity.get('medium', []))}
</table>
<h2 id="high-severity">High Severity Vulnerabilities</h2>
<table class="high-severity">
<tr>
<th class="library">Library</th>
<th class="summary">Summary</th>
<th class="fix">Fix</th>
</tr>
{''.join(f'''
<tr>
<td class="library">{vul['library']}</td>
<td class="summary">{vul['summary']}</td>
<td class="fix">{vul['fix']}</td>
</tr>
''' for vul in vulnerabilities_by_severity.get('high', []))}
</table>
<h2 id="critical-severity">Critical Severity Vulnerabilities</h2>
<table class="critical-severity">
<tr>
<th class="library">Library</th>
<th class="summary">Summary</th>
<th class="fix">Fix</th>
</tr>
{''.join(f'''
<tr>
<td class="library">{vul['library']}</td>
<td class="summary">{vul['summary']}</td>
<td class="fix">{vul['fix']}</td>
</tr>
''' for vul in vulnerabilities_by_severity.get('critical', []))}
</table>
</div>
<!-- Scroll to Top Button -->
<button id="top-button" onclick="scrollToTop()">^_^</button>
<script>
function scrollToTop() {{
window.scrollTo({{
top: 0,
behavior: 'smooth'
}});
}}
// Show the button when the user scrolls down
window.onscroll = function() {{
const topButton = document.getElementById('top-button');
if (document.body.scrollTop > 100 || document.documentElement.scrollTop > 100) {{
topButton.style.display = "block";
}} else {{
topButton.style.display = "none";
}}
}};
</script>
</body>
</html>
'''
if os.path.isdir(output_file):
raise ValueError(f"{output_file} is a directory. Please provide a file path.")
with open(output_file, 'w', encoding='utf-8') as file:
file.write(html_content)
print(f"HTML file generated: {output_file}")
def main():
print(r"""
_____ _ _ _ _ _ _ _____ _
| | |_ _| |___ ___ ___ ___| |_|_| |_| |_ _ _ | __ |___ ___ ___ ___| |_
| | | | | | | -_| _| .'| . | | | | _| | | | -| -_| . | . | _| _|
\___/|___|_|_|_|___|_| |__,|___|_|_|_|_| |_ | |__|__|___| _|___|_| |_|
|___| |_|""")
input_file = input("Enter the path to the JSON file: ")
output_file = input("Enter the path for the output HTML file: ")
try:
data = read_json_file(input_file)
severity_counts, vulnerabilities_by_severity = extract_severity_counts(data)
except ValueError as e:
print(e)
return
generate_html_from_json(severity_counts, vulnerabilities_by_severity, output_file)
if __name__ == "__main__":
main()