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
|
package cvss
import (
"math"
"regexp"
"strings"
)
// CVSS v3.1 prefix
var v31Prefix = "CVSS:3.1/"
// CVSS 3.1 vector.
type v31Vector []v3Metric
// Convert vector to string
func (v v31Vector) String() string {
// convert to slice of metrics
metrics := []v3Metric(v)
// build vector
r := make([]string, len(metrics))
for i, m := range(metrics) {
r[i] = m.String()
}
// build and return string
return v31Prefix + strings.Join(r, "/")
}
// Return CVSS version.
func (v31Vector) Version() Version {
return V31
}
// Return metrics in this vector.
func (v v31Vector) Metrics() []Metric {
// build slice of metrics
r := make([]Metric, len(v))
for i, m := range(v) {
r[i] = m
}
// return result
return r
}
// create CVSS 3.1 vector from string.
func newV31Vector(s string) (v31Vector, error) {
// strip version prefix, split into metric strings
strs := strings.Split(s[len(v31Prefix):], "/")
r := make([]v3Metric, len(strs))
// build results
for i, ms := range(strs) {
// get metric from string
m, err := getV3Metric(V31, ms)
if err != nil {
return nil, err
}
// add to results
r[i] = m
}
// return result
return v31Vector(r), nil
}
// // Unmarshal CVSS 3.1 vector from JSON string.
// func (me *v31Vector) UnmarshalJSON(b []byte) error {
// // decode string, check for error
// var s string
// if err := json.Unmarshal(b, &s); err != nil {
// return err
// }
//
// // parse vector, check for error
// r, err := newV31Vector(s)
// if err != nil {
// return err
// }
//
// // save result, return success
// *me = r
// return nil
// }
var v31VecRe = regexp.MustCompile(
"\\ACVSS:3\\.1(?:/(?:" + strings.Join([]string {
"(?:AV:[NALP])",
"(?:AC:[LH])",
"(?:PR:[NLH])",
"(?:UI:[NR])",
"(?:S:[UC])",
"(?:C:[HLN])",
"(?:I:[HLN])",
"(?:A:[HLN])",
"(?:E:[XHFPU])",
"(?:RL:[XUWTO])",
"(?:RC:[XCRU])",
"(?:CR:[XHML])",
"(?:IR:[XHML])",
"(?:AR:[XHML])",
"(?:MAV:[XNALP])",
"(?:MAC:[XLH])",
"(?:MPR:[XNLH])",
"(?:MUI:[XNR])",
"(?:MS:[XUC])",
"(?:MC:[XNLH])",
"(?:MI:[XNLH])",
"(?:MA:[XNLH])",
}, "|") + "))+\\z",
)
// Is the given string a CVSSv3.1 vector string?
func isV31VectorString(s string) bool {
return (len(s) > len(v31Prefix)) &&
(s[:len(v31Prefix)] == v31Prefix) &&
v31VecRe.MatchString(s)
}
// Return numerical scores for this vector.
//
// Reference implementation: https://www.first.org/cvss/calculator/cvsscalc31.js
func (v v31Vector) Scores() (Scores, error) {
scopeChanged := false
modScopeChanged := false
// default metrics map
keys := map[Key]v3Metric {
v3ExploitCodeMaturity: v3ENotDefined,
v3RemediationLevel: v3RLNotDefined,
v3ReportConfidence: v3RCNotDefined,
v3ConfidentialityRequirement: v3CRNotDefined,
v3IntegrityRequirement: v3IRNotDefined,
v3AvailabilityRequirement: v3ARNotDefined,
v3ModifiedAttackVector: v3MAVNotDefined,
v3ModifiedAttackComplexity: v3MACNotDefined,
v3ModifiedPrivilegesRequired: v3MPRNotDefined,
v3ModifiedUserInteraction: v3MUINotDefined,
v3ModifiedConfidentiality: v3MCNotDefined,
v3ModifiedIntegrity: v3MINotDefined,
v3ModifiedAvailability: v3MANotDefined,
v3ModifiedScope: v3MSNotDefined,
}
// populate metrics map
for _, m := range([]v3Metric(v)) {
keys[m.Key()] = m
switch m {
case v3SUnchanged: // S:U
scopeChanged = false
case v3SChanged: // S:C
scopeChanged = true
case v3MSUnchanged: // MS:U
modScopeChanged = false
case v3MSChanged: // MS:U
modScopeChanged = true
}
}
attackVector := v3MetricCoefs[keys[v3AttackVector]]
attackComplexity := v3MetricCoefs[keys[v3AttackComplexity]]
userInteraction := v3MetricCoefs[keys[v3UserInteraction]]
conf := v3MetricCoefs[keys[v3Confidentiality]]
integ := v3MetricCoefs[keys[v3Integrity]]
avail := v3MetricCoefs[keys[v3Availability]]
ecm := v3MetricCoefs[keys[v3ExploitCodeMaturity]]
remediationLevel := v3MetricCoefs[keys[v3RemediationLevel]]
reportConfidence := v3MetricCoefs[keys[v3ReportConfidence]]
confReq := v3MetricCoefs[keys[v3ConfidentialityRequirement]]
availReq := v3MetricCoefs[keys[v3AvailabilityRequirement]]
integReq := v3MetricCoefs[keys[v3IntegrityRequirement]]
// adjust privsRequired based on scopeChanged
// (CVSS v3.1 spec, section 7.4, table 16)
privsRequired := v3PrivReqCoefs[keys[v3PrivilegesRequired]][scopeChanged]
modAttackVector := getV3ModCoef(keys, v3ModifiedAttackVector)
modAttackComplexity := getV3ModCoef(keys, v3ModifiedAttackComplexity)
modUserInteraction := getV3ModCoef(keys, v3ModifiedUserInteraction)
modConf := getV3ModCoef(keys, v3ModifiedConfidentiality)
modInteg := getV3ModCoef(keys, v3ModifiedIntegrity)
modAvail := getV3ModCoef(keys, v3ModifiedAvailability)
if v, _ := keys[v3ModifiedScope]; v == v3MSNotDefined {
// default to base scopeChanged
modScopeChanged = scopeChanged
}
// adjust modPrivsRequired based on scopeChanged
// (CVSS v3.1 spec, section 7.4, table 16)
modPrivsRequired := 0.0
{
mpr, _ := keys[v3ModifiedPrivilegesRequired]
pr, _ := keys[v3PrivilegesRequired]
ms, _ := keys[v3ModifiedScope]
if mpr != v3MPRNotDefined && ms != v3MSNotDefined {
modPrivsRequired = v3PrivReqCoefs[mpr][ms == v3MSChanged]
} else if mpr != v3MPRNotDefined && ms == v3MSNotDefined {
modPrivsRequired = v3PrivReqCoefs[mpr][scopeChanged]
} else if mpr == v3MPRNotDefined && ms != v3MSNotDefined {
modPrivsRequired = v3PrivReqCoefs[pr][ms == v3MSChanged]
} else {
// default to base privsRequired
// modPrivsRequired = privsRequired
modPrivsRequired = v3PrivReqCoefs[pr][scopeChanged]
}
}
// calculate base score (CVSS v3.1 spec, section 7.1)
baseScore := 0.0
{
// calculate impact sub-score (cvss v3.1 spec, section 7.1)
iss := 1.0 - ((1.0 - conf) * (1.0 - integ) * (1.0 - avail))
// calculate impact
impact := 0.0
if scopeChanged {
impact = 7.52 * (iss - 0.029) - 3.25 * math.Pow(iss - 0.02, 15)
} else {
impact = 6.42 * iss
}
// exploitability
expl := 8.22 * attackVector * attackComplexity * privsRequired * userInteraction
if impact <= 0.0 {
baseScore = 0
} else if scopeChanged {
baseScore = roundup(math.Min(1.08 * (impact + expl), 10.0))
} else {
baseScore = roundup(math.Min(impact + expl, 10.0))
}
}
// temporal score (CVSS v3.1 spec, section 7.2)
tempScore := 0.0
if hasV3TemporalScoreKeys(keys) {
tempScore = roundup(baseScore * ecm * remediationLevel * reportConfidence)
}
// environmental score (CVSS v3.1 spec, section 7.3)
envScore := 0.0
if hasV3EnvScoreKeys(keys) {
// modified impact sub score (ISC_m)
miss := math.Min(
1 - (1 - confReq * modConf) * (1 - integReq * modInteg) * (1 - availReq * modAvail),
0.915,
)
// modified impact
// NOTE: exponent of 13 differs for CVSS v3.0 and CVSS v3.1
impact := 0.0
if modScopeChanged {
impact = 7.52 * (miss - 0.029) - 3.25 * math.Pow(miss * 0.9731 - 0.02, 13)
} else {
impact = 6.42 * miss
}
// modified exploitability sub score
expl := 8.22 * modAttackVector * modAttackComplexity * modPrivsRequired * modUserInteraction
// calculate env score
if impact <= 0.0 {
envScore = 0.0
} else if modScopeChanged {
// Roundup(Roundup[Minimum(1.08 × [ModifiedImpact + ModifiedExploitability], 10)] × ExploitCodeMaturity × RemediationLevel × ReportConfidence)
envScore = roundup(roundup(math.Min(1.08 * (impact + expl), 10.0)) * ecm * remediationLevel * reportConfidence)
} else {
// Roundup(Roundup[Minimum([ModifiedImpact + ModifiedExploitability], 10) ] × ExploitCodeMaturity × RemediationLevel × ReportConfidence)
envScore = roundup(roundup(math.Min((impact + expl), 10.0)) * ecm * remediationLevel * reportConfidence)
}
}
// build and return new scores
return NewScores(baseScore, tempScore, envScore)
}
|