序言
这道题比我们之前见到的题目长的多
题目
<?php
include("flag.php");
highlight_file(__FILE__);
class FileHandler {
protected $op; //procted变量
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);
}
}
?>
write up
分析:题目执行是这样的 $op
决定我们执行的方法。
$op
=1 执行write() file_put_connect() 写入文件
$op
=2 执行read() file_get_connect() 读取文件
protect属性
此属性介于public private 属性之间,在PHP 7.1 以后,对属性类型不敏感。
public 与 protect属性不严格区分。
服务器PHP7.4:
is_valid()函数
is_valid()函数对传入的字符串进行判断,确保每一个字符ASCII码值都在32-125,即该函数的作用是确保参数字符串的每一个字符都是可打印的,才返回true。
private属性序列化的时候会引入两个\x00,两个\x00就是ascii码为0的字符。可能看不到,甚至导致截断,但是url编码后就可以看得很清楚了。
from https://blog.csdn.net/Oavinci/article/details/106998738
绕过方法:
- 使用public属性代替private
- 序列化内容中用大写S表示字符串,此时这个字符串就支持将后面的字符串用16进制表示。
分析代码
既然有write方法,我们就尝试写个马。
生成payload:
O:11:"FileHandler":3:{s:2:"op";s:1:"1";s:8:"filename";s:9:"shell.php";s:7:"content";s:18:"";}