-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcvechecker.sh
executable file
·282 lines (216 loc) · 6.62 KB
/
cvechecker.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
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
#!/bin/bash
versionString="1.2"
version()
{
echo "cvechecker $versionString"
}
usage()
{
cat <<'EOUSAGE'
Description: Check for all/specific CVEs on multiple servers and docker containers.
Usage: cvechecker path/to/hosts/file
cvechecker -l "HOST1 HOST2 USER@HOST3..."
cvechecker [-c "CVE CVE2"] path/to/hosts/file
cvechecker [-d "/path/to/dir /path/to/another/dir"] path/to/hosts/file
cvechecker [-s] path/to/hosts/file
cvechecker [-j NUMBER] path/to/hosts/file
cvechecker [-n] path/to/hosts/file
cvechecker -h | -V | -v | --help | --version
-l give remote hosts as a command line argument instead of a path to a file
-c for which CVEs to search. To search for any CVE just write 'all'. Defaults to all if none are specified
-d which directories of the host to search through. Defaults to '/', if none are specified
-s slow/step-by-step mode in which there are no more than 10 ssh connections simultaneously
-j how many ssh sessions to have simultaneously
-n create the remote script and print it
-h display program usage information and exit
-V display program version information and exit
-v display program version information and exit
EOUSAGE
}
#Dafaults
maxNumberOfSshSessions=10
numberOfRequiredArguments=1
dirToCheck="/"
while getopts 'lc:d:sj:nhVv-:' o; do
case "$o" in
l)
lFlag=1
((numberOfRequiredArguments++))
;;
c)
cveList=$OPTARG
cveList=$(sed 's+ +\\|+g' <<< "${cveList}")
((numberOfRequiredArguments+=2))
;;
d)
dirToCheck=$OPTARG
((numberOfRequiredArguments++))
;;
s)
sFlag=1
((numberOfRequiredArguments++))
;;
j)
maxNumberOfSshSessions=$OPTARG
sFlag=1
((numberOfRequiredArguments+=2))
;;
n)
nFlag=1
((numberOfRequiredArguments++))
;;
h)
hFlag=1
;;
V)
vFlag=1
;;
v)
vFlag=1
;;
-)
if [ "$OPTARG" = 'help' ]; then
hFlag=1
elif [ "$OPTARG" = 'version' ]; then
vFlag=1
else
echo "Invalid long option ""$OPTARG"" specified" 1>&2
usage 1>&2
exit 1
fi
;;
*)
usage 1>&2
exit 1
;;
esac
done
[ -z "$vFlag" ] || version
[ -z "$hFlag" ] || usage
[ -z "$vFlag$hFlag" ] || exit 0
#Check if the needed number of arguments are provided
if [[ $# -ne $numberOfRequiredArguments ]]; then
usage 1>&2
exit 1
fi
#Check if hosts are provided either as file or as an argument
if [ $lFlag ]
then
hosts=${@:$#:1}
else
if ! [ -f "${@:$#:1}" ]
then
echo "There is no ${@:$#:1} file in this directory" 1>&2
exit 2
fi
hosts=$(cat "${@:$#:1}")
fi
#Prepare local directory for storing results
mkdir -p "cvechecker"
#Prepare remote script
remoteScript=$(cat << EOREMOTESCRIPT
#!/bin/bash
#Install all needed dependencies
apt-get install -y jq
yum install -y jq
curl -sSfL https://raw.githubusercontent.com/anchore/grype/main/install.sh | sudo sh -s -- -b /usr/local/bin
#get starting directory
ogDir=\$(pwd)
#get directories to check
dirs=${dirToCheck}
#create directory for docker image save files and go into it
mkdir -p dockerImages
cd dockerImages
#check if there is docker
if command -v docker &> /dev/null
then
#Collect 'docker image save' for all containers
for i in \$(sudo docker ps --format "{{json . }}" | jq -r '"\(.Image) \(.Names)"' | sed 's/ /*/g')
do
image=\$(echo "\${i}" | awk -F* '{print \$1}')
name=\$(echo "\${i}" | awk -F* '{print \$2}')
docker image save "\${image}" > "\${name}.image"
done
#Check all images for packets with CVE
for i in \$(ls)
do
echo "Container name: \${i}"
grype "\${i}" | grep "${cveList}" >> \${ogDir}/cvecheck.results
done
fi
#Check host for packets with CVE
echo "Host:\$(hostname)"
for i in \$(echo "\${dirs}")
do
grype dir:\${i} | grep "${cveList}" >> \${ogDir}/cvecheck.results
done
#Mark the job as finished
echo 'Checks finished. Job done.' >> \${ogDir}/cvecheck.results
EOREMOTESCRIPT
)
if [ $nFlag ]
then
echo "${remoteScript}"
rm -r "cvechecker"
exit 0
fi
#Function to copy, run, disown and check for end of script on remote hosts. Explanation on `>&- 2>&- <&- &` can be found right here: https://serverfault.com/questions/172484/doing-a-long-running-command-on-ssh
function runScript() {
hostToCheck="$1"
echo "Starting check on host \"${hostToCheck}\""
ssh -q "${hostToCheck}" "mkdir -p cvecheck" < /dev/null #Prepare remote directory
echo "${remoteScript}" | ssh -q "${hostToCheck}" "cat > cvecheck/cvecheck.sh" #Copy script
ssh -q "${hostToCheck}" "sudo chmod +x cvecheck/cvecheck.sh" #make script executable
ssh -q "${hostToCheck}" "cd cvecheck ; nohup sudo ./cvecheck.sh >> ./cvecheck.messages 2>&1 &" < /dev/null #Run and disown script
#Check if script has finished
for (( ; ; ))
do
jobDone=$(ssh -q "${hostToCheck}" 'grep "Checks finished. Job done." cvecheck/cvecheck.results' < /dev/null)
if [ -n "${jobDone}" ]
then
break
fi
sleep 5m
echo "Waiting on check to finish on host \"${hostToCheck}\""
done
#Copy results to a local file
result=$(ssh -q "${hostToCheck}" "sudo cat cvecheck/cvecheck.results" < /dev/null)
echo "${result}" >> "cvechecker/${hostToCheck}.results"
#Copy messages to a local file
result=$(ssh -q "${hostToCheck}" "sudo cat cvecheck/cvecheck.messages" < /dev/null)
echo "${result}" >> "cvechecker/${hostToCheck}.messages"
#Cleanup remote directory
ssh -q "${hostToCheck}" "sudo rm -r cvecheck" < /dev/null
echo "Check has finished on host \"${hostToCheck}\""
return 0
}
if [ -z "$sFlag" ]
then
numberOfSshSessions=0
for server in $hosts; do
if [[ ${numberOfSshSessions} -ge ${maxNumberOfSshSessions} ]]
then
wait -n
((numberOfSshSessions--))
fi
runScript "${server}" &
((numberOfSshSessions++))
done
else
for server in $hosts; do
runScript "${server}" &
done
fi
wait
echo "Start of file" > cvechecker-output.txt
for server in $hosts; do
outputFile="cvechecker/${server}.results"
echo "Host ${server}:" >> cvechecker-output.txt
head -n -1 "${outputFile}" >> cvechecker-output.txt
messagesFile="cvechecker/${server}.messages"
echo "Host ${server}:" >> cvechecker-messages.txt
head -n -1 "${outputFile}" >> cvechecker-messages.txt
done
cat cvechecker-output.txt
rm -r "cvechecker"
exit 0