PHP对象注入 PHP Object Injection
From:PHP Object Injection
Last revision (mm/dd/yy): 01/7/2015
譯者:李秋豪 Wednesday, 24. May 2017 05:48PM
描述
PHP對象注入是一個允許駭客展開各種惡意攻擊的應用級漏洞,比如說代碼注入 ,SQL注入 ,目錄遍歷 和應用程序拒絕服務 (取決于具體的上下文/環境)。當用戶的請求在傳給反序列化函數unserialize()之前沒有被正確的過濾時就會產生該漏洞。因為PHP允許對象序列化,攻擊者就可以提交特定的序列化的字符串給一個具有該漏洞的unserialize函數,最終導致一個在該應用范圍內的任意PHP對象注入。
為了正確的利用一個PHP對象注入漏洞,兩個條件必須滿足:
例子
例一
下面的例子顯示了一個具有PHP對象注入漏洞的類(__destruct方法):
class Example1 {public $cache_file;function __construct(){// some PHP code...}function __destruct(){$file = "/var/www/cache/tmp/{$this->cache_file}";if (file_exists($file)) @unlink($file);} }// some PHP code...$user_data = unserialize($_GET['data']);// some PHP code...在這個例子中,攻擊者可以通過目錄遍歷攻擊刪掉任意一個文件,例如提交以下一個URL:
http://testsite.com/vuln.php?data=O:8:"Example1":1:{s:10:"cache_file";s:15:"../../index.php";}例二
下面的例子顯示了一個具有PHP對象注入漏洞的類(__wakeup方法):
class Example2 {private $hook;function __construct(){// some PHP code...}function __wakeup(){if (isset($this->hook)) eval($this->hook);} }// some PHP code...$user_data = unserialize($_COOKIE['data']);// some PHP code...在這個例子中,攻擊者可以通過發送如下的HTTP請求來進行代碼注入攻擊:
GET /vuln.php HTTP/1.0 Host: testsite.com Cookie: data=O%3A8%3A%22Example2%22%3A1%3A%7Bs%3A14%3A%22%00Example2%00hook%22%3Bs%3A10%3A%22phpinfo%28%29%3B%22%3B%7D Connection: close這里的cookie變量“data”是通過下面的腳本產生的:
class Example2 {private $hook = "phpinfo();"; }print urlencode(serialize(new Example2));例三
最后一個例子展示了利用“POP chain”進行SQL注入的可能性,例如利用一個__toString方法:
class Example3 {protected $obj;function __construct(){// some PHP code...}function __toString(){if (isset($this->obj)) return $this->obj->getValue();} }// some PHP code...$user_data = unserialize($_POST['data']);// some PHP code...如果$user_data是一個“Example3”類的對象并且它在代碼中會被當做一個字符串,那么它的__toString方法就會被調用。因為__toString會調用對象中的getValue獲取對象中的屬性,所以有可能將設置這個屬性于任意對象并運行getValue方法——如果可以的話,否則__call方法就會被調用。假設以下類型在應用中是可獲得的或者是自動加載的:
class SQL_Row_Value {private $_table;// some PHP code...function getValue($id){$sql = "SELECT * FROM {$this->_table} WHERE id = " . (int)$id;$result = mysql_query($sql, $DBFactory::getConnection());$row = mysql_fetch_assoc($result);return $row['value'];} }在這個例子中攻擊者可能有機會通過提交POST請求進行SQL注入攻擊,請求中的“DATA”參量按如下腳本生成:
class SQL_Row_Value {private $_table = "SQL Injection"; }class Example3 {protected $obj;function __construct(){$this->obj = new SQL_Row_Value;} }print urlencode(serialize(new Example3));相關的控制方法
- 輸入合法性檢查
- 靜態代碼分析
防范方法
請不要對用戶的輸入使用unserialize()進行反序列化,應該使用JSON的方法。
參考
PHP:反序列化
魔術方法
PHP RFC: 安全的反序列化unserialize()
PHP漏洞利用 Stefan Esser, POC 2009
PHP漏洞利用之代碼重用/返回導向編程
轉載于:https://www.cnblogs.com/liqiuhao/p/6900872.html
總結
以上是生活随笔為你收集整理的PHP对象注入 PHP Object Injection的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 复习计划
- 下一篇: 动态规划算法php,php算法学习之动态