前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PHP审计之POP链挖掘

PHP审计之POP链挖掘

作者头像
Imaginary
发布2021-12-13 15:47:16
8240
发布2021-12-13 15:47:16
举报
文章被收录于专栏:ImaginaryArcadia

前言#

续上文中的php反序列化,继续来看,这个POP的挖掘思路。在其中一直构思基于AST去自动化挖掘POP链,迫于开发能力有限。没有进展,随后找到了一个别的师傅已经实现好的项目。

魔术方法#

代码语言:javascript
复制
__wakeup() //使用unserialize时触发
__sleep() //使用serialize时触发
__destruct() //对象被销毁时触发
__call() //在对象上下文中调用不可访问的方法时触发
__callStatic() //在静态上下文中调用不可访问的方法时触发
__get() //用于从不可访问的属性读取数据
__set() //用于将数据写入不可访问的属性
__isset() //在不可访问的属性上调用isset()或empty()触发
__unset() //在不可访问的属性上使用unset()时触发
__toString() //把类当作字符串使用时触发,file_exists()判断也会触发
__invoke() //当脚本尝试将对象调用为函数时触发

__call__callstatic#

现实情况下__call的利用居多,该魔术方法触发的条件是在对象上下文中调用不可访问的方法时触发。

调用流程如下:

代码语言:javascript
复制
$this->a() ==> 当前类a方法 ==> 父类a方法 ==> 当前类__call方法 ==> 父类__call方法

如果触发__call方法,那么a,即方法名,会作为__call的方法的第一个参数,而参数列表会作为__call的方法第二个参数。

来看到代码

代码语言:javascript
复制
function __destruct(){
    $this->a->b();
}

这里有2个利用路径,一个是$this->a中构造一个存在方法的实例化类,另一种方式是找一个不存在b方法并且存在__call方法的类,当b不存在时,即自动调用__call

__callstatic方法只有在调用到静态方法的时候才能触发

__get__set#

不存在该类变量或者不可访问时,则会调用对应的__get方法

代码语言:javascript
复制
$this->a ==> 当前类a变量 ==> 父类a变量 ==> 当前类__get方法 ==> 父类__get方法

__get代码案例

代码语言:javascript
复制
function __destruct(){
    echo $this->a;
}

调用不存在变量a,即会自动触发__get方法,

数据写入不可访问的变量或不存在的变量即调用__set

代码语言:javascript
复制
function __destruct(){
    $this->a = 1;
}

__toString#

把类当作字符串使用时触发

代码语言:javascript
复制
$this->_adapterName = $adapterName;
$adapterName = 'xxx' . $adapterName;

POP链挖掘#

此前构思的自动化挖掘POP链的功能已经被其他师傅实现了,在此就不班门弄斧了,直接拿现成的来用。 按照个人理解反序列化入口点一般为__wakeup__destruct__construct等 思路其实就是寻找__destruct方法,作为入口点,然后寻找一个回调函数作为末端。而中间需要寻找各种中间链,将其串联起来。串联的方法基本上就是一些魔术方法和一些自定义的方法。

项目地址:https://github.com/LoRexxar/Kunlun-M

代码语言:javascript
复制
cp Kunlun_M/settings.py.bak Kunlun_M/settings.py

python kunlun.py init initialize

python kunlun.py config load

python kunlun.py plugin php_unserialize_chain_tools -t C:\kyxscms-1.2.7

结果:

代码语言:javascript
复制
 [20:28:51] [PhpUnSerChain] New Source __destruct() in thinkphp#library#think#Process_php.Class-Process
 [20:28:51] thinkphp#library#think#Process_php.Class-Process
 newMethod                        Method-__destruct()
 [20:28:51] thinkphp#library#think#Process_php.Class-Process.Method-__destruct
 MethodCall                       Variable-$this->stop()
 [20:28:51] thinkphp#library#think#Process_php.Class-Process.Method-stop
 MethodCall                       Variable-$this->updateStatus('Constant-false',)
 [20:28:51] thinkphp#library#think#Process_php.Class-Process.Method-updateStatus
 MethodCall                       Variable-$this->readPipes('Variable-$blocking', '\ === Constant-DIRECTORY_SEPARATOR ? 627')
 [20:28:51] thinkphp#library#think#Process_php.Class-Process.Method-readPipes
 MethodCall                       Variable-$this->processPipes->readAndWrite('Variable-$blocking', 'Variable-$close')
 [20:28:51] thinkphp#library#think#console#Output_php.Class-Output
 newMethod                        Method-__call('$method', '$args')
 [20:28:51] thinkphp#library#think#console#Output_php.Class-Output.Method-__call.If
 FunctionCall                     call_user_func_array("Array-['Variable-$this', 'block']", 'Variable-$args')
 [20:28:51] [PhpUnSerChain] UnSerChain is available.

这其实利用链就清晰了

代码语言:javascript
复制
Process->__destruct ==>Process->stop ==>Process->updateStatus ==> Process->readPipes ==> Output->readAndWrite ==> Output->__call==> call_user_func_array()

内容补充#

查看一下这三个方法的调用

代码语言:javascript
复制
<?php

class User{

    const SITE = 'uusama';



    public $username;

    public $nickname;

    private $password;

    private $id;



    public function __construct($username, $nickname, $password)

    {

        $this->username = $username;

        $this->nickname = $nickname;



    }



    // 定义反序列化后调用的方法

    public function __wakeup()

    {

        $this->password = '1234';

    }
    public function __destruct(){
        $this->id = '123';
    }

}
$user = new User('uusama', 'uu', '12345');
$ser= serialize($user);
var_dump(unserialize($ser));

结果:

代码语言:javascript
复制
object(User)[2]
  public 'username' => string 'uusama' (length=6)
  public 'nickname' => string 'uu' (length=2)
  private 'password' => string '1234' (length=4)
  private 'id' => null

在反序列化中一般开发可能会在__wakeup中做一些过滤。

参考#

浅析 PHP 反序列化漏洞的利用与审计

如何自动化挖掘php反序列化链 - phpunserializechain诞生记

结尾#

但该工具并没有达到我个人的预期,因为该工具中只是使用__destruct这单个方法作为反序列化的入口点。

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言#
  • 魔术方法#
    • __call与__callstatic#
      • __get与__set#
        • __toString#
        • POP链挖掘#
          • 内容补充#
            • 参考#
            • 结尾#
            相关产品与服务
            文件存储
            文件存储(Cloud File Storage,CFS)为您提供安全可靠、可扩展的共享文件存储服务。文件存储可与腾讯云服务器、容器服务、批量计算等服务搭配使用,为多个计算节点提供容量和性能可弹性扩展的高性能共享存储。腾讯云文件存储的管理界面简单、易使用,可实现对现有应用的无缝集成;按实际用量付费,为您节约成本,简化 IT 运维工作。
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档