-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathvulnerability_summarizer.py
323 lines (262 loc) · 11.1 KB
/
vulnerability_summarizer.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
import re
from addict import Dict
SEVERITIES = ["LOW", "MODERATE", "HIGH", "CRITICAL"]
def get_max_severity(repo):
max_severity_index = -1
severity = "NONE"
for va_edge in repo.vulnerabilityAlerts.edges:
for v_edge in va_edge.node.securityAdvisory.vulnerabilities.edges:
edge_severity = v_edge.node.severity
severity_index = SEVERITIES.index(edge_severity)
if severity_index > max_severity_index:
max_severity_index = severity_index
severity = edge_severity
return severity
def group_by_severity(repositories):
vulnerable_by_severity = Dict({sev: [] for sev in SEVERITIES})
for repo in repositories:
severity = get_max_severity(repo)
vulnerable_by_severity[severity].append(repo)
return vulnerable_by_severity
def get_next_major(major, majors):
found = False
next_major = None
for major2 in majors:
if not found and major2 > major:
found = True
next_major = major2
return next_major
def get_major_version(version):
"""
Enable checking that 2 versions are within the same
major version
"""
components = re.findall(r"\d+", version)
major = components[0]
return major
def get_uniform_version(version):
"""
Remove any non-numeric characters from required version text
"""
components = re.findall(r"\d+", version)
uniform = ".".join(components)
return uniform
def get_sortable_version(version):
"""
Add leading zeros to version number components to
ensure that 1.10.x is > 1.2.x
"""
version_components = re.findall(r"\d+", version)
print(version_components)
sortable = []
for component in version_components:
sortable.append(format(int(component), "04d"))
return ".".join(sortable)
def get_vulnerability_patches(repo):
patches = []
# log case when extra pages are found
if repo.vulnerabilityAlerts.pageInfo.hasNextPage:
print(f"ERROR: {repo.name} has multiple vulnerabilities")
for v_edge in repo.vulnerabilityAlerts.edges:
package = v_edge.node.securityVulnerability.package.name
print(package)
dependency_file = v_edge.node.vulnerableManifestPath
required_version = get_uniform_version(v_edge.node.vulnerableRequirements)
sortable_version = get_sortable_version(required_version)
print(f"{required_version} == {sortable_version}")
patch_sortable_version = None
patch_version = None
alternative_patched_versions = []
patch_found = False
patchable = False
max_severity = 0
# log case when extra pages are found
if v_edge.node.securityAdvisory.vulnerabilities.pageInfo.hasNextPage:
print(f"ERROR: {repo.name} {package} has more than 20 advisories")
advisories = v_edge.node.securityAdvisory.vulnerabilities.edges
for a_edge in advisories:
advisory = a_edge.node
# Some advisories apply to packages that don't match
# the installed package.
same_package = advisory.package.name == package
severity_index = SEVERITIES.index(advisory.severity)
if severity_index > max_severity:
max_severity = severity_index
if advisory.firstPatchedVersion:
advisory_patch_version = get_uniform_version(
advisory.firstPatchedVersion.identifier
)
advisory_sortable_version = get_sortable_version(advisory_patch_version)
print(advisory_sortable_version)
patch_is_newer = advisory_sortable_version > sortable_version
patchable = same_package and patch_is_newer
if patchable:
patch_found = True
print(f"{advisory_sortable_version} is patchable")
alternative_patched_versions.append(
Dict(
{
"version": advisory_patch_version,
"sortable": advisory_sortable_version,
}
)
)
is_higher_patch = (not patch_version) or (
advisory_sortable_version > patch_sortable_version
)
if is_higher_patch:
patch_version = advisory_patch_version
patch_sortable_version = advisory_sortable_version
alternative_patched_versions.sort(key=lambda patch: patch["sortable"])
if patch_found:
patches.append(
Dict(
{
"package": package,
"dependency_file": dependency_file,
"current_version": required_version,
"patch_available": patch_found,
"patch_version": patch_version,
"alternative_patched_versions": alternative_patched_versions,
"sortable_version": patch_sortable_version,
"severity": SEVERITIES[max_severity],
"severity_index": max_severity,
}
)
)
else:
patches.append(
Dict(
{
"package": package,
"dependency_file": dependency_file,
"current_version": required_version,
"patch_available": patch_found,
"patch_version": required_version,
"alternative_patched_versions": alternative_patched_versions,
"sortable_version": sortable_version,
"severity": SEVERITIES[max_severity],
"severity_index": max_severity,
}
)
)
print(patches)
return patches
def get_consolidated_alternate_versions(versions1, versions2):
"""
If multiple vulnerabilities exist for the same package you
should patch to a version which fixes both.
This means the higher if the same major version exists in
both lists.
"""
# print(versions1)
# print(versions2)
alternates = {}
# get versions indexed by major version
majors1 = {get_major_version(version.version): version for version in versions1}
majors2 = {get_major_version(version.version): version for version in versions2}
# print(majors1)
# print(majors2)
# get list of present major versions
majors1_list = [major for major in majors1.keys()]
majors2_list = [major for major in majors2.keys()]
# print(majors1_list)
# print(majors2_list)
all_majors = list(set(majors1_list + majors2_list))
all_majors.sort()
# print(all_majors)
for major in all_majors:
alternate = None
if major in majors1_list and major in majors2_list:
if majors1[major]["sortable"] > majors2[major]["sortable"]:
alternate = majors1[major]
else:
alternate = majors2[major]
elif major in majors1_list and major not in majors2_list:
next_major = get_next_major(major, majors2_list)
if next_major:
alternate = majors2[next_major]
elif major not in majors1_list and major in majors2_list:
next_major = get_next_major(major, majors1_list)
if next_major:
alternate = majors1[next_major]
if alternate:
alternates[alternate.version] = alternate
alternate_list = [alternate for version, alternate in alternates.items()]
alternate_list.sort(key=lambda alternate: alternate["sortable"])
# alternate_versions = [alternate.version for alternate in alternate_list]
return alternate_list
def get_package_patches(repo):
"""
Consolidate results if there are multiple vulnerabilities
for the same package
"""
vulnerability_patches = get_vulnerability_patches(repo)
package_patches = Dict({})
for patch in vulnerability_patches:
if patch.package in package_patches:
current_package_patch = package_patches[patch.package]
current_alternates = current_package_patch.alternative_patched_versions
new_alternates = patch.alternative_patched_versions
# Show highest version patch for multiple vulnerabilities
if patch.sortable_version > current_package_patch.sortable_version:
package_patches[patch.package] = patch
# Show highest severity of multiple vulnerabilities
if current_package_patch.severity_index > patch.severity_index:
package_patches[patch.package][
"severity_index"
] = current_package_patch.severity_index
package_patches[patch.package]["severity"] = SEVERITIES[
current_package_patch.severity_index
]
alternatives = get_consolidated_alternate_versions(
current_alternates, new_alternates
)
package_patches[patch.package][
"alternative_patched_versions"
] = alternatives
else:
package_patches[patch.package] = patch
# Remove the recommended version from the alternatives list
alternatives = []
for alternate in package_patches[patch.package]["alternative_patched_versions"]:
if get_uniform_version(patch.patch_version) != get_uniform_version(
alternate.version
):
alternatives.append(alternate)
package_patches[patch.package]["alternative_patched_versions"] = alternatives
print(package_patches)
return package_patches
def get_patch_list(repo):
"""
The same package can have multiple alerts or multiple advisories within the same
alert so to get down to one recommended version per package you have to do an
aggregation for the advisories in each alert and then to see if there are
multiple alerts for the same package.
Also the highest patched version may not correspond to the highest severity
of all the advisories.
"""
repo_patches = get_package_patches(repo)
print(repo_patches)
# sort by descending severity
if len(repo_patches.keys()) > 0:
patch_list = [patch for package, patch in repo_patches.items()]
patch_list.sort(key=lambda patch: patch["severity_index"], reverse=True)
print(patch_list)
patches = {patch["package"]: patch for patch in patch_list}
else:
patches = {}
return patches
def get_repository_severity_counts(repo):
"""
Count results by severity in repo.patches
"""
patches = get_patch_list(repo)
severity_counts = {}
for package, patch in patches.items():
severity = patch.severity
if severity not in severity_counts:
severity_counts[severity] = 1
else:
severity_counts[severity] += 1
return severity_counts