-
Notifications
You must be signed in to change notification settings - Fork 69
/
container-escape-check.sh
463 lines (382 loc) · 15 KB
/
container-escape-check.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
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
451
452
453
454
455
456
457
458
459
460
461
462
463
#!/bin/bash
echo -e ""
echo -e "\033[34m=============================================================\033[0m"
echo -e "\033[34m Containers Escape Check v0.3 \033[0m"
echo -e "\033[34m-------------------------------------------------------------\033[0m"
echo -e "\033[34m Author: TeamsSix \033[0m"
echo -e "\033[34m Twitter: TeamsSix \033[0m"
echo -e "\033[34m Blog: teamssix.com \033[0m"
echo -e "\033[34m WeChat Official Accounts: TeamsSix \033[0m"
echo -e "\033[34m Project Address: github.com/teamssix/container-escape-check \033[0m"
echo -e "\033[34m=============================================================\033[0m"
echo -e ""
# Supported detection methods:
#
# 1. Privileged Mode
# 2. Mount Docker Socket
# 3. Mount Procfs
# 4. Mount Root Directory
# 5. Open Docker Remote API
# 6. CVE-2016-5195 DirtyCow
# 7. CVE-2020-14386
# 8. CVE-2022-0847 DirtyPipe
# 9. CVE-2017-1000112
# 10. CVE-2021-22555
# 11. Mount Host Var Log
# 12. CAP_DAC_READ_SEARCH
# 13. CAP_SYS_ADMIN
# 14. CAP_SYS_PTRACE
# 15. CVE-2022-0492
CheckCommandExists(){
$1 >/dev/null 2>&1
ret=$?
if [ "$ret" -eq 0 ]; then
return 1
fi
return 0
}
# Install command
InstallCommand(){
# install command if not present
CheckCommandExists $1
if [ $? -eq 0 ]; then
# Check network
timeout 3 bash -c "echo -e >/dev/tcp/baidu.com/80" > /dev/null 2>&1 && IsNetWork=1 || IsNetWork=0
if [ $IsNetWork -eq 1 ];then
echo -e "\033[93m[!] It is detected that the $1 command does not exist in the current system, and the command is being installed.\033[0m"
CheckCommandExists sudo
if [ $? -eq 0 ]; then
CheckCommandExists apt-get
if [ $? -eq 0 ];then
if [ "$1" = "capsh" ];then
apt-get -y update >/dev/null 2>&1 && apt-get install -y libcap2-bin >/dev/null 2>&1
else
apt-get -y update >/dev/null 2>&1 && apt-get install -y $1 >/dev/null 2>&1
fi
fi
CheckCommandExists yum
if [ $? -eq 0 ];then
if [ "$1" = "capsh" ];then
yum -y update >/dev/null 2>&1 && yum install -y libcap >/dev/null 2>&1
else
yum -y update >/dev/null 2>&1 && yum install -y $1 >/dev/null 2>&1
fi
fi
else
CheckCommandExists apt-get
if [ $? -eq 0 ];then
if [ "$1" = "capsh" ];then
sudo apt-get -y update >/dev/null 2>&1 && apt-get install -y libcap2-bin >/dev/null 2>&1
else
sudo apt-get -y update >/dev/null 2>&1 && apt-get install -y $1 >/dev/null 2>&1
fi
fi
CheckCommandExists yum
if [ $? -eq 0 ];then
if [ "$1" = "capsh" ];then
sudo yum -y update >/dev/null 2>&1 && yum install -y libcap >/dev/null 2>&1
else
sudo yum -y update >/dev/null 2>&1 && yum install -y $1 >/dev/null 2>&1
fi
fi
fi
CheckCommandExists $1
if [ $? -eq 0 ]; then
echo -e "\033[93m[!] $1 command installation failed.\033[0m"
else
echo -e "\033[93m[!] $1 command installation completed.\033[0m"
fi
fi
fi
}
# 0. Check The Current Environment
CheckTheCurrentEnvironment(){
if [ ! -f "/proc/1/cgroup" ];then
IsContainer=0
else
cat /proc/1/cgroup | grep -qi docker && IsContainer=1 || IsContainer=0
fi
if [ $IsContainer -eq 0 ];then
echo -e "\033[31m[-] Not currently a container environment.\033[0m"
exit 1
else
echo -e "\033[33m[!] Currently in a container, checking ......\033[0m"
VulnerabilityExists=0
fi
}
# 1. Check Privileged Mode
CheckPrivilegedMode(){
if [ ! -f "/proc/self/status" ];then
IsPrivilegedMode=0
else
cat /proc/self/status | grep -qi "0000003fffffffff" && IsPrivilegedMode=1 || IsPrivilegedMode=0
cat /proc/self/status | grep -qi "0000001fffffffff" && IsPrivilegedMode=1 || IsPrivilegedMode=0
fi
if [ $IsPrivilegedMode -eq 1 ];then
echo -e "\033[92m[+] The current container is in privileged mode.\033[0m"
VulnerabilityExists=1
fi
}
# 2. Check Docker Socket Mount
CheckDockerSocketMount(){
if [ ! -f "/var/run/docker.sock" ];then
IsDockerSocketMount=0
else
ls /var/run/ | grep -qi docker.sock && IsDockerSocketMount=1 || IsDockerSocketMount=0
fi
if [ $IsDockerSocketMount -eq 1 ];then
echo -e "\033[92m[+] The current container has docker socket mounted.\033[0m"
VulnerabilityExists=1
fi
}
# 3. Check Procfs Mount
CheckProcfsMount(){
find / -name core_pattern 2>/dev/null | wc -l | grep -q 2 && IsProcfsMount=1 || IsProcfsMount=0
if [ $IsProcfsMount -eq 1 ];then
echo -e "\033[92m[+] The current container has procfs mounted.\033[0m"
VulnerabilityExists=1
fi
}
# 4. Check Root Directory Mount
CheckRootDirectoryMount(){
find / -name passwd 2>/dev/null | grep /etc/passwd | wc -l | grep -q 7 && IsRootDirectoryMount=1 || IsRootDirectoryMount=0
if [ $IsRootDirectoryMount -eq 1 ];then
echo -e "\033[92m[+] The current container has root directory mounted.\033[0m"
VulnerabilityExists=1
fi
}
# 5. Check Docker Remote API
CheckDockerRemoteAPI(){
InstallCommand hostname
for PORT in "2375" "2376"
do
IP=`hostname -i | awk -F. '{print $1 "." $2 "." $3 ".1"}' ` && timeout 3 bash -c "echo -e >/dev/tcp/$IP/$PORT" > /dev/null 2>&1 && DockerRemoteAPIIsEnabled=1 || DockerRemoteAPIIsEnabled=0
if [ $DockerRemoteAPIIsEnabled -eq 1 ];then
echo -e "\033[92m[+] The Docker Remote API for the current container is enabled.\033[0m"
VulnerabilityExists=1
fi
done
}
LinuxKernelVersion=`uname -r | awk -F '-' '{print $1}'`
KernelVersion=`echo -e $LinuxKernelVersion | awk -F '.' '{print $1}'`
MajorRevision=`echo -e $LinuxKernelVersion | awk -F '.' '{print $2}'`
MinorRevision=`echo -e $LinuxKernelVersion | awk -F '.' '{print $3}'`
# 6. Check CVE-2016-5195 DirtyCow
# 2.6.22 <= ver <= 4.8.3
CheckCVE_2016_5195DirtyCow(){
# 2.6.22 <= ver <= 2.6.xx
if [[ "$KernelVersion" -eq 2 && "$MajorRevision" -eq 6 && "$MinorRevision" -ge 22 ]];then
echo -e "\033[92m[+] The current container has the CVE-2016-5195 DirtyCow vulnerability.\033[0m"
VulnerabilityExists=1
fi
# 2.7 <= ver <= 2.x
if [[ "$KernelVersion" -eq 2 && "$MajorRevision" -ge 7 ]];then
echo -e "\033[92m[+] The current container has the CVE-2016-5195 DirtyCow vulnerability.\033[0m"
VulnerabilityExists=1
fi
# ver = 3
if [[ "$KernelVersion" -eq 3 ]];then
echo -e "\033[92m[+] The current container has the CVE-2016-5195 DirtyCow vulnerability.\033[0m"
VulnerabilityExists=1
fi
# 4.x <= ver <= 4.8
if [[ "$KernelVersion" -eq 4 && "$MajorRevision" -lt 8 ]];then
echo -e "\033[92m[+] The current container has the CVE-2016-5195 DirtyCow vulnerability.\033[0m"
VulnerabilityExists=1
fi
# 4.8.x <= ver <= 4.8.3
if [[ "$KernelVersion" -eq 4 && "$MajorRevision" -eq 8 && "$MinorRevision" -le 3 ]];then
echo -e "\033[92m[+] The current container has the CVE-2016-5195 DirtyCow vulnerability.\033[0m"
VulnerabilityExists=1
fi
}
# 7. CVE-2020-14386
# 4.6 <= ver < 5.9
CheckCVE_2020_14386(){
# 4.6 <= ver < 4.x
if [[ "$KernelVersion" -eq 4 && "$MajorRevision" -ge 6 ]];then
echo -e "\033[92m[+] The current container has the CVE-2020-14386 vulnerability.\033[0m"
VulnerabilityExists=1
fi
# 5.x <= ver < 5.9
if [[ $KernelVersion -eq 5 && $MajorRevision -lt 9 ]];then
echo -e "\033[92m[+] The current container has the CVE-2020-14386 vulnerability.\033[0m"
VulnerabilityExists=1
fi
}
# 8. CVE-2022-0847 DirtyPipe
# 5.8 <= ver < 5.10.102 < ver < 5.15.25 < ver < 5.16.11
CheckCVE_2022_0847(){
if [ $KernelVersion -eq 5 ];then
# 5.8 <= ver < 5.10.x
if [[ "$MajorRevision" -ge 8 && "$MajorRevision" -lt 10 ]];then
echo -e "\033[92m[+] The current container has the CVE-2022-0847 DirtyPipe vulnerability.\033[0m"
VulnerabilityExists=1
fi
# 5.10.x <= ver < 5.10.102
if [[ "$MajorRevision" -eq 10 && "$MinorRevision" -lt 102 ]];then
echo -e "\033[92m[+] The current container has the CVE-2022-0847 DirtyPipe vulnerability.\033[0m"
VulnerabilityExists=1
fi
# 5.10.102 < ver <= 5.10.x
if [[ "$MajorRevision" -eq 10 && "$MinorRevision" -gt 102 ]];then
echo -e "\033[92m[+] The current container has the CVE-2022-0847 DirtyPipe vulnerability.\033[0m"
VulnerabilityExists=1
fi
# 5.10.x < ver < 5.15.x
if [[ "$MajorRevision" -gt 10 && "$MajorRevision" -lt 15 ]];then
echo -e "\033[92m[+] The current container has the CVE-2022-0847 DirtyPipe vulnerability.\033[0m"
VulnerabilityExists=1
fi
# 5.15.x <= ver < 5.15.25
if [[ "$MajorRevision" -eq 15 && "$MinorRevision" -lt 25 ]];then
echo -e "\033[92m[+] The current container has the CVE-2022-0847 DirtyPipe vulnerability.\033[0m"
VulnerabilityExists=1
fi
# 5.15.25 < ver <= 5.15.x
if [[ "$MajorRevision" -eq 15 && "$MinorRevision" -gt 25 ]];then
echo -e "\033[92m[+] The current container has the CVE-2022-0847 DirtyPipe vulnerability.\033[0m"
VulnerabilityExists=1
fi
# 5.16.x <= ver < 5.16.11
if [[ "$MajorRevision" -eq 16 && "$MinorRevision" -lt 11 ]];then
echo -e "\033[92m[+] The current container has the CVE-2022-0847 DirtyPipe vulnerability.\033[0m"
VulnerabilityExists=1
fi
fi
}
# 9. CVE-2017-1000112
# 4.4 <= ver<=4.13
CheckCVE_2017_1000112(){
# 4.4 <= ver <= 4.13
if [[ "$KernelVersion" -eq 4 && "$MajorRevision" -ge 4 && "$MajorRevision" -le 13 ]];then
echo -e "\033[92m[+] The current container has the CVE-2017-1000112 vulnerability.\033[0m"
VulnerabilityExists=1
fi
}
# 10. CVE-2021-22555
# 2.6.19 <= ver <= 5.12
CheckCVE_2021_22555(){
# 2.6.19 <= ver <= 2.6.xx
if [[ "$KernelVersion" -eq 2 && "$MajorRevision" -eq 6 && "$MinorRevision" -ge 19 ]];then
echo -e "\033[92m[+] The current container has the CVE-2021-22555 vulnerability.\033[0m"
VulnerabilityExists=1
fi
# 2.7 <= ver <= 2.x
if [[ "$KernelVersion" -eq 2 && "$MajorRevision" -ge 7 ]];then
echo -e "\033[92m[+] The current container has the CVE-2021-22555 vulnerability.\033[0m"
VulnerabilityExists=1
fi
# ver = 3 or ver = 4
if [[ "$KernelVersion" -eq 3 || "$KernelVersion" -eq 4 ]];then
echo -e "\033[92m[+] The current container has the CVE-2021-22555 vulnerability.\033[0m"
VulnerabilityExists=1
fi
# 5.x <= ver <= 5.12
if [[ $KernelVersion -eq 5 && $MajorRevision -le 12 ]];then
echo -e "\033[92m[+] The current container has the CVE-2021-22555 vulnerability.\033[0m"
VulnerabilityExists=1
fi
}
# 11. Mount Host Var Log
CheckVarLogMount(){
if [ ! -f "/var/run/secrets/kubernetes.io/serviceaccount/token" ];then
IsPodEnv=0
else
IsPodEnv=1
fi
if [ $IsPodEnv -eq 1 ];then
find / -name lastlog 2>/dev/null | wc -l | grep -q 3 && IsVarLogMount=1 || IsVarLogMount=0
if [ $IsVarLogMount -eq 1 ];then
echo -e "\033[92m[+] The current container has /var/log mounted.\033[0m"
VulnerabilityExists=1
fi
fi
}
# 12. Check CAP_DAC_READ_SEARCH
ChekckCAP_DAC_READ_SEARCH(){
if command -v capsh >/dev/null 2>&1; then
cap_dac_read_searchNum=`capsh --print | grep cap_dac_read_search | wc -l`
if [ $cap_dac_read_searchNum -gt 0 ];then
echo -e "\033[92m[+] The current container has the CAP_DAC_READ_SEARCH permission.\033[0m"
VulnerabilityExists=1
fi
fi
}
# 13. Check CAP_SYS_ADMIN
CheckCAP_SYS_ADMIN(){
if command -v capsh >/dev/null 2>&1; then
cap_sys_adminNum=`capsh --print | grep cap_sys_admin | wc -l`
if [ $cap_sys_adminNum -gt 0 ];then
echo -e "\033[92m[+] The current container has the CAP_SYS_ADMIN permission.\033[0m"
VulnerabilityExists=1
fi
fi
}
# 14. Check CAP_SYS_PTRACE
CheckCAP_SYS_PTRACE(){
if command -v capsh >/dev/null 2>&1; then
cap_sys_ptraceNum=`capsh --print | grep cap_sys_ptrace | wc -l`
if [ $cap_sys_ptraceNum -gt 0 ];then
echo -e "\033[92m[+] The current container has the CAP_SYS_PTRACE permission.\033[0m"
VulnerabilityExists=1
fi
fi
}
# 15. Check CVE-2022-0492, Code By https://github.com/PaloAltoNetworks/can-ctr-escape-cve-2022-0492/blob/main/can-ctr-escape-cve-2022-0492.sh
CheckCVE_2022_0492(){
# Setup test dir
test_dir=/tmp/.cve-2022-0492-test
if mkdir -p $test_dir ; then
# Test whether escape via user namespaces is possible
while read -r subsys
do
if unshare -UrmC --propagation=unchanged bash -c "mount -t cgroup -o $subsys cgroup $test_dir 2>&1 >/dev/null && test -w $test_dir/release_agent" >/dev/null 2>&1 ; then
echo -e "\033[92m[+] The current container has the CVE-2022-0492 vulnerability.\033[0m"
fi
done <<< $(cat /proc/$$/cgroup | grep -Eo '[0-9]+:[^:]+' | grep -Eo '[^:]+$')
umount $test_dir >/dev/null 2>&1 && rm -rf $test_dir >/dev/null 2>&1
fi
}
main()
{
# 0. Check the current environment
CheckTheCurrentEnvironment
# 1. Check Privileged Mode
CheckPrivilegedMode
# 2. Check Docker Socket Mount
CheckDockerSocketMount
# 3. Check Procfs Mount
CheckProcfsMount
# 4. Check Root Directory Mount
CheckRootDirectoryMount
# 5. Check Docker Remote API
CheckDockerRemoteAPI
# 6. Check CVE-2016-5195 DirtyCow
CheckCVE_2016_5195DirtyCow
# 7. CVE-2020-14386
CheckCVE_2020_14386
# 8. CVE-2022-0847 DirtyPipe
CheckCVE_2022_0847
# 9. CVE-2017-1000112
CheckCVE_2017_1000112
# 10. CVE-2021-22555
CheckCVE_2021_22555
# 11. Mount Host Var Log
CheckVarLogMount
InstallCommand capsh
# 12. Check CAP_DAC_READ_SEARCH
ChekckCAP_DAC_READ_SEARCH
# 13. Check CAP_SYS_ADMIN
CheckCAP_SYS_ADMIN
# 14. Check CAP_SYS_PTRACE
CheckCAP_SYS_PTRACE
# 15. Check CVE-2022-0492
CheckCVE_2022_0492
if [ $VulnerabilityExists -eq 0 ];then
echo -e "\033[33m[!] Check completed, no vulnerability found. \033[0m"
else
echo -e "\033[33m[!] Check completed.\033[0m"
fi
}
main