题目地址:跳转提示
打开题目网址后,发现出现一段php代码:
<?php
// php版本:5.4.44
header("Content-type: text/html; charset=utf-8");
highlight_file(__FILE__);
class evil{
public $hint;
public function __construct($hint){
$this->hint = $hint;
}
public function __destruct(){
if($this->hint==="hint.php")
@$this->hint = base64_encode(file_get_contents($this->hint));
var_dump($this->hint);
}
function __wakeup() {
if ($this->hint != "╭(●`∀´●)╯") {
//There's a hint in ./hint.php
$this->hint = "╰(●’◡’●)╮";
}
}
}
class User
{
public $username;
public $password;
public function __construct($username, $password){
$this->username = $username;
$this->password = $password;
}
}
function write($data){
global $tmp;
$data = str_replace(chr(0).'*'.chr(0), '\0\0\0', $data);
$tmp = $data;
}
function read(){
global $tmp;
$data = $tmp;
$r = str_replace('\0\0\0', chr(0).'*'.chr(0), $data);
return $r;
}
$tmp = "test";
$username = $_POST['username'];
$password = $_POST['password'];
$a = serialize(new User($username, $password));
if(preg_match('/flag/is',$a))
die("NoNoNo!");
unserialize(read(write($a)));
?>
根据分析可知,第一步要先使得evil类中的解析函数里的$this->hint==="hint.php"判断条件成立,从而得到提示。
这就涉及到了对unserialize()函数的理解,该函数配合serialize()函数使用,分别将对象或者数组进行序列化和反序列化,经过serialize()函数序列化后会返回一个字符串,类似:
$username='admin';
$password='123';
$a = serialize(new User($username, $password));
echo unserialize($a)
得到结果:O:4:"User":2{s:8:"username";s:5:"admin";s:8:"password";s:3:"123";}
已知,序列化后的字符串,以;为字段之间的分隔,以}为结束;
于是可以利用unserialize()构造出一个evil类的对象,参考反序列化(Unserialize)漏洞详解_小蓝同学`的博客-CSDN博客_unserialize反序列化漏洞
得知,当在反序列化发现所标的数量与双引号内的字符数量不一样时,会继续往下寻找直至匹配到相应的数量,只要往下寻找之后符合数量并且不影响后面属性的反序列化也是可以正常反序列化的。
想要利用反序列化构造一个evil对象,就需要加入一段:“s:8:"password";O:4:"evil":1:{s:4:"hint";s:8:"hint.php";}”
又因为使用unserialize()时会调用对象的_wakeup函数,而evil的_wakeup函数会将hint的内容改成一个表情符,因此需要绕过该函数,可以标错序列字符串的对象属性个数,这时函数会返回错误,但依然会构造对象,且不会再调用_wakeup函数。
故而可以改成:“s:8:"password";O:4:"evil":2:{s:4:"hint";s:8:"hint.php";}”
联合read和write函数,要构造一个evil类对象,反序列字符串需要类似于:
O:4:"User":2:{s:8:"username";s:?:"?";s:8:"password";s:59:"1";s:8:"password";O:4:"evil":2:{s:4:"hint";s:8:"hint.php";}";}
其中?即需要修改填充的部分
经过read(write($a)),\0\0\0会被修改为chr(0).'*'.chr(0),从长度6变为3,可以利用这点缩短原本username中字符串的长度,而这时序列字符串中的长度属性没有修改,就会自动寻找差额的字符补齐长度,此时图中灰色部分";s:8:"password";s:59:"1即为需要被掩盖的字符串,其长度为:24
因此只需要8个'\0\0\0'组成的字符串就可以掩盖,payload
username=\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0&password=b";s:8:"password";O:4:"evil":2:{s:4:"hint";s:8:"hint.php";}
得到结果:
对得到的字符串base64解码得到:
<?php
$hint = "index.cgi";
// You can't see me~
根据提示访问页面得到:
发现使用get获得name值,因为页面有回显,尝试使用file协议访问本地文件
即访问http://114.67.175.224:13517/index.cgi?name=%20file:///flag得到结果: