This repository has been archived by the owner on Mar 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadjust_cvss.py
108 lines (89 loc) · 2.61 KB
/
adjust_cvss.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
import argparse
import json
import re
from globber import match
def parse_pattern(line):
sepchar = ':'
escchar = '\\'
rule_pattern = ''
score_pattern = ''
seen_separator = False
i = 0
while i < len(line):
c = line[i]
i = i + 1
if c == sepchar:
if seen_separator:
raise Exception('Invalid pattern: "' + line + '" Contains more than one separator!')
seen_separator = True
continue
elif c == escchar:
nextc = line[i] if (i < len(line)) else None
if nextc in ['+' , '-', escchar, sepchar]:
i = i + 1
c = nextc
if seen_separator:
score_pattern = score_pattern + c
else:
rule_pattern = rule_pattern + c
return rule_pattern, str(float(score_pattern))
def adjust_cvss(args):
if args.split_lines:
tmp = []
for p in args.patterns:
tmp = tmp + re.split('\r?\n', p)
args.patterns = tmp
args.patterns = [parse_pattern(p) for p in args.patterns if p]
print('Given patterns:')
for idp, sp in args.patterns:
print(
'IDs: {id_pattern} scores: {score_pattern}'.format(
id_pattern=idp,
score_pattern=sp
)
)
with open(args.input, 'r') as f:
s = json.load(f)
for run in s.get('runs', []):
for ext in run.get('tool', {}).get('extensions', []):
for rule in ext.get('rules', []):
props = rule.get('properties', [])
qip = props.get('id', '')
cvss = props.get('security-severity', None)
if cvss:
for idp, sp in args.patterns:
if match(idp, qip):
print('adjusted')
props['security-severity'] = sp
with open(args.output, 'w') as f:
json.dump(s, f, indent=2)
def main():
parser = argparse.ArgumentParser(
prog='adjust-cvss'
)
parser.add_argument(
'--input',
help='Input SARIF file',
required=True
)
parser.add_argument(
'--output',
help='Output SARIF file',
required=True
)
parser.add_argument(
'--split-lines',
default=False,
action='store_true',
help='Split given patterns on newlines.'
)
parser.add_argument(
'patterns',
help='score patterns.',
nargs='+'
)
def print_usage(args):
print(parser.format_usage())
args = parser.parse_args()
adjust_cvss(args)
main()