突破 Docker 容器到主机文件系统的 root

use*_*ser 4 docker

我正在关注文章中链接的以下 youtube 视频,该视频允许 docker 容器在主机上获得 root 访问权限。

有几个步骤不清楚,有人可以解释一下它们是如何进一步工作的吗?

https://www.lvh.io/posts/dont-expose-the-docker-socket-not-even-to-a-container.html

    Step 1> Bind mount /var/run/docker.sock from host to container
    Step 2> Install docker in container   <<< at this stage I see that docker ps 
    -a shows all the containers which are present on the host.
    **QUESTION:** How can the container see the containers present on the host? Is it because dockerd on the new container is using /var/run/docker.sock on the host? netstat/ss in the new container doesn't show anything..  

    Step 3> Run another container from the 1st container. Pass the following parameters to it:
        docker run -dit -v /:/host ubuntu

Intention of this is to mount / from host filesystem to /host in the 2nd container being created
         **QUESTION:** How does the 1st container have access to / (being filesystem of the host?)
Run Code Online (Sandbox Code Playgroud)

谢谢。

sma*_*sey 6

Dockerservice在主机上作为 a 运行。这service通过套接字与客户端通信,默认情况下,该套接字是 unix 套接字:unix:/var/run/docker.sock

当您与任何容器共享此套接字时,该容器将获得对 docker 守护程序的完全访问权限。从那里,容器可以启动其他容器,删除容器/卷/等,甚至可以随意将卷从主机映射到新容器,例如,如您的问题中所述-v /:/host。这样做将使容器 root 访问/host/.

简而言之:你应该小心地与任何你不信任的容器共享这个宝贵的套接字。在某些情况下,共享套接字是有意义的(例如portainer:用作 docker 管理 GUI 的容器)。

  • 两个推论:不要设计你的应用程序来调用 Docker API 来响应网络调用(如果你没有做到这一点,你就有 root 主机的风险);永远不要使用 Docker 守护进程“-H”选项来打开对 Docker 守护进程的远程网络访问(任何可以访问该端口的人都可以执行此答案所描述的操作)。 (2认同)
  • @DavidMaze 谢谢:)你会对上次我在端口 2376 上的托管提供商上运行“masscan”时弹出的数量感到惊讶/害怕。我确信有人在一些视频博客上提出了这一点码头工人少年。 (2认同)