-
Notifications
You must be signed in to change notification settings - Fork 0
/
report-tool.sh
executable file
·146 lines (130 loc) · 4.58 KB
/
report-tool.sh
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
#!/usr/bin/env bash
function substituteWithCVEIfAvailable() {
vulndbURL=$GOVULNDB_URL
if [[ -z "$vulndbURL" ]]; then
cat -
return
fi
vulnIdx=$(curl -fsSL "$vulndbURL/index/vulns.json")
while read -r vuln; do
cve=$(
echo "$vulnIdx" |
jq -r ".[] | select(.id == \"$vuln\") | .aliases.[]" |
grep CVE || true
)
if [[ -n "$cve" ]]; then
echo "$cve"
else
echo "$vuln"
fi
done
}
cmd=${1:-}
case $cmd in
"stats")
discovered=$($0 discovered | wc -l)
failed=$($0 failed | wc -l)
scanned=$($0 scanned | wc -l)
vulnerable=$($0 vulnerable | wc -l)
nonvulnerable=$($0 non-vulnerable | wc -l)
# ensure failed + scanned = discovered
if [[ $discovered -ne $((failed + scanned)) ]]; then
echo -e "Error: $discovered (discovered) != $failed (failed) + $scanned (scanned)\n"
fi
# ensure scanned = vulnerable + non-vulnerable
if [[ $scanned -ne $((vulnerable + nonvulnerable)) ]]; then
echo -e "Error: $scanned (scanned) != $vulnerable (vulnerable) + $nonvulnerable (non-vulnerable)\n"
fi
vulns=$(rg -c 'Vulnerability #' report.txt)
echo "Packages discovered: $discovered"
echo "Packages failed: $failed ($(( 100 * failed / discovered ))%)"
echo "Packages scanned: $scanned ($(( 100 * scanned / discovered ))%)"
echo "Packages vulnerable: $vulnerable ($(( 100 * vulnerable / scanned ))% of scanned)"
echo "Total vulnerabilities: $vulns"
;;
"discovered")
rg -NI 'Checking nixpkg ([^\s]+)' -or '$1' report.txt
;;
"failed")
rg -NI 'Checking nixpkg|govulncheck: (loading packages|no go.mod file)' report.txt |
rg --multiline 'Checking nixpkg ([^\s]+)\ngovulncheck' -or '$1'
;;
"scanned")
rg -NI 'Checking nixpkg|Vulnerability #|govulncheck:' report.txt |
rg --multiline -v 'Checking nixpkg ([^\s]+)\ngovulncheck:' |
rg 'Checking nixpkg ([^\s]+)' -or '$1'
;;
"vulnerable")
rg -NI 'Checking nixpkg|Vulnerability #' report.txt |
rg --multiline 'Checking nixpkg ([^\s]+)\n\s*Vulnerability #' -or '$1'
;;
"non-vulnerable")
rg -NI 'Checking nixpkg|Vulnerability #|govulncheck:' report.txt |
rg --multiline -v 'Checking nixpkg [^\s]+\n(Vulnerability #|govulncheck:)' |
rg 'Checking nixpkg ([^\s]+)' -or '$1'
;;
"report")
pkgName=$2
awk "/Checking nixpkg ${pkgName}/,/for more details/" report.txt
;;
"findings")
pkgName=$2
awk "/Checking nixpkg ${pkgName}/,/for more details/" report.txt |
grep 'More info: ' |
sed 's/\s*More info: //'
;;
"mark")
pkgName=$2
awk "/Checking nixpkg ${pkgName}/,/for more details/" report.txt |
grep 'Vulnerability #' |
sed 's/\s*Vulnerability #[[:digit:]]*: //' |
substituteWithCVEIfAvailable |
sort -ur |
sed 's/^/"/' |
sed 's/$/"/'
;;
"fix")
pkgName=$2
while read -r fix; do
modName=$(echo "$fix" | cut -d' ' -f1)
modVersion=$(echo "$fix" | cut -d' ' -f2)
echo "go get -u $modName@$modVersion"
echo "go mod tidy"
echo "git diff -q --exit-code || git commit -am \"update $modName to $modVersion\""
done < <(
awk "/Checking nixpkg ${pkgName}/,/for more details/" report.txt |
grep 'Fixed in:' |
sed 's/\s*Fixed in: //' |
awk -F@ '{print $1 " " $2}' |
sort -k1,1 -k2Vr |
awk '!seen[$1]++'
)
;;
*)
cat <<EOF
Usage: $0 <command> [args]
Commands:
stats
Show statistics about the report.
discovered
List packages which were tried to be checked.
failed
List packages for which the check failed.
scanned
List packages that were successfully scanned.
vulnerable
List packages that have vulnerabilities.
non-vulnerable
List packages that do not have vulnerabilities.
report <pkgName>
Show the report for a specific package.
findings <pkgName>
List the found vulnerabilities (URL) for a specific package.
mark <pkgName>
Show the vulnerabilities for a specific package in a format that can be
used to mark the package as vulnerable in the nixpkgs repository.
fix <pkgName>
Show the commands to fix the vulnerabilities upstream.
EOF
;;
esac