学习php的序列化和反序列化

学习php的序列化和反序列化

通过学长给的两个包含相关知识点的链接学习了相关的知识点

https://www.cnblogs.com/youyoui/p/8610068.html

https://xz.aliyun.com/t/7570

自己在B站搜索相关内容也找到了相关的视频学习

BV1Z54y1U7up

BV1Jh411d7Ta

BV1tv411y7DA

先来讲讲学到了哪几个知识点和一些要注意的点

序列化

<?php
class test{
    private $a;
    protected $b;
    public $c;
    var $d;
    static $f;
    function __construct()
    {
        $this->a=$this->b=$this->c=$this->d=$this->f=$this->e=1;
    }
$t=new test();
$p=serialize($t);
print($p);

输出的结果是:

O:4:"test":6:{s:7:" test a";i:1;s:4:" * b";i:1;s:1:"c";i:1;s:1:"d";i:1;s:1:"e";i:1;s:1:"f";i:1;}

详细解释即为

O:strlen(object name):object name:object size:{s:strlen(property name):property name:property definition;(repeated per property)}

这里有两个值得注意的点,为什么testa的长度为7,以及为什么*b的长度为4?

在原来输出界面上看testa和*b里面是没有空格的,但是将输出的字符串复制出来会发现test前面有一个空格,test和a之间有一个空格,以及*前与*和b中间有空格,而原来的格式中空格那个位置是一个%00的ascii码,会占据一个位置,所以它们的长度分别是7和4。

从上面的输出也得知private变量的序列化输出是“%00类名%00变量名”,protected变量的输出是“%00*%00变量名。”

在“深度剖析PHP序列化和反序列化”那篇博客里还有一个值得注意的知识点“序列化对象时,不会保存常量的值。对于父类中的变量,则会保留。”

魔术方法__sleep

function __sleep()
{
    return ["a","b"];
}

可以制定想要返回的函数值,输出结果为

O:4:"test":2:{s:7:" test a";i:1;s:4:" * b";i:1;}

反序列化

$n=unserialize('O:4:"test":6:{s:7:" test a";i:1;s:4:" * b";i:1;s:1:"c";i:1;s:1:"d";i:1;s:1:"e";i:1;s:1:"f";i:1;}');
var_dump($n);

输出结果

object(test)#2 (8) {
  ["a":"test":private]=>
  NULL
  ["b":protected]=>
  NULL
  ["c"]=>
  int(1)
  ["d"]=>
  int(1)
  [" test a"]=>
  int(1)
  [" * b"]=>
  int(1)
  ["e"]=>
  int(1)
  ["f"]=>
  int(1)
}

魔术方法__wakeup

在执行反序列化过程中会先执行__wakeup这个函数

function __wakeup()
{
    $this->c=22;
}

用上述同样方法输出得到只有如下数据改变,其余数据不变

 

以上就是基础部分,在我的理解中序列化与反序列化就是将对象变为字符串和将字符串变为对象。

 

绕过__wakeup()函数

这个部分挺基础的

当对象的属性(变量)数大于实际的个数时,__wakeup()魔法函数被绕过,以下payload为例子

file=O:1:"A":1:{s:4:"file";s:8:"flag.php";}  //不能绕过file=O:1:"A":2:{s:4:"file";s:8:"flag.php";}  //能绕过

如果对象是private或者是protected,payload则变为

file=O:1:"A":2:{s:7:"%00A%00file";s:8:"flag.php";}  //privatefile=O:1:"A":2:{s:7:"%00*%00file";s:8:"flag.php";}  //protected

session 反序列化注入漏洞

首先先了解Session序列化机制

图源

 当序列化的引擎和反序列化的引擎不一致时,就可以利用引擎之间的差异产生序列化注入漏洞

 

phar://

相关知识点已看完,正在与POP链一同总结中,日后填坑

字符串逃逸

已阅读完相关知识点,仍在总结中,日后填坑

 

学习中参考的文章

https://blog.csdn.net/qq_41107295/article/details/102943476

https://blog.csdn.net/nzjdsds/article/details/82703639?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-4.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-4.control

https://blog.csdn.net/qq_41107295/article/details/102943476

https://www.cnblogs.com/hello-py/articles/13501786.html

https://xz.aliyun.com/t/7570#toc-4

https://w.cnblogs.com/youyoui/p/8610068.html

posted @ 2021-02-08 21:50  Fr3Nky  阅读(72)  评论(0编辑  收藏  举报