-
Notifications
You must be signed in to change notification settings - Fork 0
/
cvssv3.go
450 lines (405 loc) · 9.07 KB
/
cvssv3.go
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
// Package cvssv3 provides parsing and scoring with Common Vulunerability
// Scoring System version 3.0 (CVSSv3).
// Author: Bunji2
// Inspired by "go-cvss" ( https://github.com/umisama/go-cvss ),
// but implementated in different way.
package cvssv3
import (
"fmt"
"math"
"regexp"
)
// Vector reprecents a CVSS vector.
type Vector map[string] string
// Val(x) returns float value of m's metrics x
func (m Vector) Val(x string) float64 {
val, ok := m[x]
if ! ok {
return math.NaN()
}
switch x {
case "AV":
return val_AV(val)
case "AC":
return val_AC(val)
case "PR":
return val_PR(val, m.IsScopeChanged())
case "UI":
return val_UI(val)
case "S":
if val == "C" {
return 1.0 // scope is changed
}
return 0.0 // scope is unchanged
case "C", "I", "A":
return val_Impact(val)
case "E":
return val_E(val)
case "RL":
return val_RL(val)
case "RC":
return val_RC(val)
case "CR", "IR", "AR":
return val_Requirements(val)
case "MAV":
if val == "X" {
return m.Val("AV")
}
return val_AV(val)
case "MAC":
if val == "X" {
return m.Val("AC")
}
return val_AC(val)
case "MPR":
if val == "X" {
return m.Val("PR")
}
return val_PR(val, m.IsModifiedScopeChanged())
case "MUI":
if val == "X" {
return m.Val("UI")
}
return val_UI(val)
case "MS":
if val == "X" {
return m.Val("S")
}
if val == "C" {
return 1.0 // scope is changed
}
return 0.0 // scope is unchanged
case "MC":
if val == "X" {
return m.Val("C")
}
return val_Impact(val)
case "MI":
if val == "X" {
return m.Val("I")
}
return val_Impact(val)
case "MA":
if val == "X" {
return m.Val("A")
}
return val_Impact(val)
}
return math.NaN()
}
// Str(x) returns string value of m's metrics x
func (m Vector) Str(x string) string {
val, ok := m[x]
if ok {
return val
}
return "?"
}
// IsScopeChanged returns that whether m's Scope is changed
func (m Vector) IsScopeChanged() bool {
if m.Val("S") == 1.0 {
return true
}
return false
}
// IsScopeChanged returns that whether m's Modified Scope is changed
func (m Vector) IsModifiedScopeChanged() bool {
if m.Val("MS") == 1.0 {
return true
}
return false
}
// ParseVector create new Vector object with str.
// str must valid as CVSS:3.0/base/temporal/environment Vector.
func ParseVector(str string) (Vector, error) {
submatches := regexp.MustCompile(
`CVSS:3.0\/` +
`AV:([NALP])\/AC:([LH])\/PR:([NLH])\/UI:([NR])\/S:([UC])\/` +
`C:([HLN])\/I:([HLN])\/A:([HLN])`+
`(?:\/E:([XUPFH])\/RL:([XOTWU])\/RC:([XURC])` +
`(?:\/CR:([XHML])\/IR:([XHML])\/AR:([XHML])\/MAV:([XNALP])\/` +
`MAC:([XLH])\/MPR:([XNLH])\/MUI:([XNR])\/MS:([XUC])\/` +
`MC:([XHLN])\/MI:([XHLN])\/MA:([XHLN])` +
`)?` +
`)?`).FindStringSubmatch(str)
// fmt.Printf("%s(%d)", str, len(submatches))
if len(submatches) < 9 || submatches[0] != str {
return Vector{},
fmt.Errorf("invalid Vector string: %s(%d)", str, len(submatches))
}
metrics := map[string]string {
// mandatory metrics
"AV": submatches[1],
"AC": submatches[2],
"PR": submatches[3],
"UI": submatches[4],
"S": submatches[5],
"C": submatches[6],
"I": submatches[7],
"A": submatches[8],
// optional metrics
"E": "X",
"RL": "X",
"RC": "X",
"CR": "X",
"IR": "X",
"AR": "X",
"MAV": "X",
"MAC": "X",
"MPR": "X",
"MUI": "X",
"MS": "X",
"MC": "X",
"MI": "X",
"MA": "X",
}
if len(submatches) > 11 {
if submatches[9] != "" {
metrics["E"] = submatches[9]
}
if submatches[10] != "" {
metrics["RL"] = submatches[10]
}
if submatches[11] != "" {
metrics["RC"] = submatches[11]
}
}
if len(submatches) > 22 {
if submatches[12] != "" {
metrics["CR"] = submatches[12]
}
if submatches[13] != "" {
metrics["IR"] = submatches[13]
}
if submatches[14] != "" {
metrics["AR"] = submatches[14]
}
if submatches[15] != "" {
metrics["MAV"] = submatches[15]
}
if submatches[16] != "" {
metrics["MAC"] = submatches[16]
}
if submatches[17] != "" {
metrics["MPR"] = submatches[17]
}
if submatches[18] != "" {
metrics["MUI"] = submatches[18]
}
if submatches[19] != "" {
metrics["MS"] = submatches[19]
}
if submatches[20] != "" {
metrics["MC"] = submatches[20]
}
if submatches[21] != "" {
metrics["MI"] = submatches[21]
}
if submatches[22] != "" {
metrics["MA"] = submatches[22]
}
}
m := Vector(metrics)
//fmt.Println(metrics)
return m, nil
}
// String returns formatted m.
func (m Vector) String() string {
prefix := "CVSS:3.0"
base := "AV:" + m.Str("AV") + "/AC:" + m.Str("AC") +
"/PR:" + m.Str("PR") + "/UI:" + m.Str("UI") +
"/S:" + m.Str("S") + "/C:" + m.Str("C") +
"/I:" + m.Str("I") + "/A:" + m.Str("A")
temp := "E:" + m.Str("E") + "/RL:" + m.Str("RL") +
"/RC:" + m.Str("RC")
env := "CR:" + m.Str("CR") + "/IR:" + m.Str("IR") +
"/AR:" + m.Str("AR") + "/MAV:" + m.Str("MAV") +
"/MAC:" + m.Str("MAC") + "/MPR:" + m.Str("MPR") +
"/MUI:" + m.Str("MUI") + "/MS:" + m.Str("MS") +
"/MC:" + m.Str("MC") + "/MI:" + m.Str("MI") +
"/MA:" + m.Str("MA")
return prefix + "/" + base + "/" + temp + "/" + env
}
// BaseScore returns m's base score.
func (m Vector) BaseScore() float64 {
scope_changed := m.IsModifiedScopeChanged()
c := m.Val("C")
i := m.Val("I")
a := m.Val("A")
isc := calc_isc(c, i, a)
impact := calc_impact(scope_changed, isc)
if impact <= 0.0 {
return 0.0
}
av := m.Val("AV")
ac := m.Val("AC")
pr := m.Val("PR")
ui := m.Val("UI")
exploitability := calc_exploitability(av, ac, pr, ui)
base := calc_base(scope_changed, impact, exploitability)
return roundUp1(base)
}
// TemporalScore returns m's temporal score.
func (m Vector) TemporalScore() float64 {
base := m.BaseScore()
e := m.Val("E")
rl := m.Val("RL")
rc := m.Val("RC")
temp := calc_temporal(base, e, rl, rc)
return roundUp1(temp)
}
// EnvironmentalScore returns m's environmental score.
func (m Vector) EnvironmentalScore() float64 {
m_scope_changed := m.IsModifiedScopeChanged()
cr := m.Val("CR")
ir := m.Val("IR")
ar := m.Val("AR")
mc := m.Val("MC")
mi := m.Val("MI")
ma := m.Val("MA")
m_isc := calc_modified_isc(cr, ir, ar, mc, mi, ma)
m_impact := calc_impact(m_scope_changed, m_isc)
if m_impact <= 0.0 {
return 0.0
}
mav := m.Val("MAV")
mac := m.Val("MAC")
mpr := m.Val("MPR")
mui := m.Val("MUI")
m_exploitability := calc_exploitability(mav, mac, mpr, mui)
m_base := calc_base(m_scope_changed, m_impact, m_exploitability)
me := m.Val("E")
mrl := m.Val("RL")
mrc := m.Val("RC")
m_temp := calc_temporal(m_base, me, mrl, mrc)
return roundUp1(m_temp)
}
func calc_base(scope_changed bool, impact, exploitability float64) float64 {
a := float64(1.0)
if scope_changed {
a = float64(1.08)
}
return math.Min(a*(impact+exploitability), 10.0)
}
func calc_impact(scope_changed bool, isc float64) float64 {
if !scope_changed {
return 6.42 * isc
}
return 7.52 * (isc - 0.029) - 3.25 * math.Pow((isc - 0.02), 15.0)
}
func calc_isc(c, i, a float64) float64 {
return float64(1.0 - (1.0 - c) * (1.0 - i) * (1.0 - a))
}
func calc_exploitability(av, ac, pr, ui float64) float64 {
return 8.22 * av * ac * pr * ui
}
func calc_temporal(base, e, rl, rc float64) float64 {
return base * e * rl * rc
}
func calc_modified_isc(cr, ir, ar, c, i, a float64) float64 {
return math.Min(
float64(1.0 - (1.0 - c * cr) * (1.0 - i * ir) * (1.0 - a * ar)),
0.915)
}
func roundUp1(val float64) float64 {
return math.Ceil(val*10)/10
}
func val_AV(v string) float64 {
switch v {
case "N":
return 0.85
case "A":
return 0.62
case "L":
return 0.55
}
// case "P":
return 0.2
}
func val_AC(v string) float64 {
switch v {
case "L":
return 0.77
}
// case "H":
return 0.44
}
func val_PR(v string, scope_changed bool) float64 {
switch v {
case "L":
if scope_changed {
return 0.68
}
return 0.62
case "H":
if scope_changed {
return 0.50
}
return 0.27
}
// case "N":
return 0.85
}
func val_UI(v string) float64 {
switch v {
case "R":
return 0.62
}
// case "N":
return 0.85
}
func val_Impact(v string) float64 {
switch v {
case "H":
return 0.56
case "L":
return 0.22
}
// case "N":
return 0.0
}
func val_E(v string) float64 {
switch v {
case "F":
return 0.97
case "P":
return 0.94
case "U":
return 0.91
}
// case "X", "H":
return 1.0
}
func val_RL(v string) float64 {
switch v {
case "W":
return 0.97
case "T":
return 0.96
case "O":
return 0.95
}
// case "X", "U":
return 1.0
}
func val_RC(v string) float64 {
switch v {
case "R":
return 0.96
case "U":
return 0.92
}
// case "X", "C":
return 1.0
}
func val_Requirements(v string) float64 {
switch v {
case "H":
return 1.5
case "L":
return 0.5
}
// case "X", "M":
return 1.0
}
// End of package cvssv3