-
-
Notifications
You must be signed in to change notification settings - Fork 303
Expand file tree
/
Copy pathcollect_ssvc_trees.py
More file actions
145 lines (118 loc) · 4.86 KB
/
collect_ssvc_trees.py
File metadata and controls
145 lines (118 loc) · 4.86 KB
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
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# VulnerableCode is a trademark of nexB Inc.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/aboutcode-org/vulnerablecode for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#
import logging
from django.db.models import Prefetch
from django.db.models import Q
from vulnerabilities.models import SSVC
from vulnerabilities.models import AdvisorySeverity
from vulnerabilities.models import AdvisoryV2
from vulnerabilities.pipelines import VulnerableCodePipeline
from vulnerabilities.severity_systems import SCORING_SYSTEMS
logger = logging.getLogger(__name__)
class CollectSSVCPipeline(VulnerableCodePipeline):
"""
Collect SSVC Pipeline
This pipeline collects SSVC from Vulnrichment project and associates them with existing advisories.
"""
pipeline_id = "collect_ssvc_trees"
@classmethod
def steps(cls):
return (cls.collect_ssvc_data,)
def collect_ssvc_data(self):
vulnrichment_advisories = (
AdvisoryV2.objects.filter(
severities__scoring_system=SCORING_SYSTEMS["ssvc"],
)
.distinct()
.prefetch_related(
Prefetch(
"severities",
queryset=AdvisorySeverity.objects.filter(
scoring_system=SCORING_SYSTEMS["ssvc"]
),
)
)
)
self.log(
f"Found {vulnrichment_advisories.count()} advisories from Vulnrichment with SSVC severities."
)
for advisory in vulnrichment_advisories:
self.log(f"Processing advisory: {advisory.advisory_id}")
for severity in advisory.severities.all():
ssvc_vector = severity.scoring_elements
self.log(f"SSVC Vector found: {ssvc_vector}")
try:
ssvc_tree, decision = convert_vector_to_tree_and_decision(ssvc_vector)
self.log(
f"Advisory: {advisory.advisory_id}, SSVC Tree: {ssvc_tree}, Decision: {decision}, vector: {ssvc_vector}"
)
ssvc_obj, _ = SSVC.objects.get_or_create(
source_advisory=advisory,
defaults={
"options": ssvc_tree,
"decision": decision,
"vector": ssvc_vector,
},
)
# All advisories that have advisory.advisory_id in their aliases or advisory_id same as advisory.advisory_id
related_advisories = AdvisoryV2.objects.filter(
Q(advisory_id=advisory.advisory_id) | Q(aliases__alias=advisory.advisory_id)
).distinct()
related_advisories = related_advisories.exclude(id=advisory.id)
ssvc_obj.related_advisories.set(related_advisories)
except Exception as e:
logger.error(
f"Failed to parse SSVC vector '{ssvc_vector}' for advisory '{advisory}': {e}"
)
REVERSE_POINTS = {
"E": ("Exploitation", {"N": "none", "P": "poc", "A": "active"}),
"A": ("Automatable", {"N": "no", "Y": "yes"}),
"T": ("Technical Impact", {"P": "partial", "T": "total"}),
"P": ("Mission Prevalence", {"M": "minimal", "S": "support", "E": "essential"}),
"B": ("Public Well-being Impact", {"M": "minimal", "A": "material", "I": "irreversible"}),
"M": ("Mission & Well-being", {"L": "low", "M": "medium", "H": "high"}),
}
REVERSE_DECISION = {
"T": "Track",
"R": "Track*",
"A": "Attend",
"C": "Act",
}
VECTOR_ORDER = ["E", "A", "T", "P", "B", "M"]
def convert_vector_to_tree_and_decision(vector: str):
"""
Convert a given SSVC vector string into a structured tree and decision.
Args:
vector (str): The SSVC vector string.
Returns:
tuple: A tuple containing the SSVC tree (dict) and decision (str).
"""
if not vector.startswith("SSVCv2/"):
raise ValueError("Invalid SSVC vector")
parts = [p for p in vector.replace("SSVCv2/", "").split("/") if p]
options = []
decision = None
for part in parts:
if ":" not in part:
continue
key, value = part.split(":", 1)
if key == "D":
decision = REVERSE_DECISION.get(value)
continue
if key in REVERSE_POINTS:
name, mapping = REVERSE_POINTS[key]
options.append({name: mapping[value]})
options.sort(
key=lambda o: VECTOR_ORDER.index(
next(k for k, _ in REVERSE_POINTS.values() if k == next(iter(o)))
)
if False
else 0
)
return options, decision