一些 PHP 反序列化笔记

First Post:

Last Update:

Word Count:
512

Read Time:
2 min

__wakeup()绕过

CVE-2016-7124

适用于 PHP5 < 5.6.25 | PHP7 < 7.0.10

具体表现为当序列化后字符串中表示对象中属性个数的数值大于实际属性数量时会跳过__wakeup()的执行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
class Demo {
public $file = 'index.php';
public function __construct($file) {
echo "construct\n";
}
function __destruct() {
echo "destruct\n";
}
function __wakeup() {
echo "wakeup\n";
}
}
unserialize('O:4:"Demo":1:{s:4:"file";s:9:"index.php";}');
?>
1
2
3
E:\>php unserialize.php
wakeup
destruct

修改为
unserialize(‘O:4:”Demo”:2:{s:4:”file”;s:9:”index.php”;}’);

1
2
E:\>php unserialize.php
destruct

正则绕过

反斜杠分割命令

1
/(cat|flag|tac|php|ls)/

该正则用于匹配某些特定命令

在命令中添加反斜杠,命令仍然能够被正确识别

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
┌──(Malpl3naInk㉿WIN-U10PUEP8DSD)-[~/CTF/temp]
└─$ cat 01
001

┌──(Malpl3naInk㉿WIN-U10PUEP8DSD)-[~/CTF/temp]
└─$ c\at 01 # 在命令中添加反斜杠
001

┌──(Malpl3naInk㉿WIN-U10PUEP8DSD)-[~/CTF/temp]
└─$ c/at 01 # 在命令中添加正斜杠(无法识别)
-bash: c/at: No such file or directory

┌──(Malpl3naInk㉿WIN-U10PUEP8DSD)-[~/CTF/temp]
└─$ c\at 0\1 # 在命令与参数中都添加反斜杠
001

内敛执行输出

`<command>`${<command>} 中命令的输出作为输入

1
2
3
4
5
6
7
8
9
10
11
┌──(Malpl3naInk㉿WIN-U10PUEP8DSD)-[~/CTF/temp]
└─$ ls
01

┌──(Malpl3naInk㉿WIN-U10PUEP8DSD)-[~/CTF/temp]
└─$ cat 01
001

┌──(Malpl3naInk㉿WIN-U10PUEP8DSD)-[~/CTF/temp]
└─$ cat `ls` # 将 ls 命令获取的文件名 01 作为参数传入 cat 命令
001

由示例可看到,命令 `ls` 的输出文件名 01 作为输入传入 cat 命令,结合为命令 cat 01

此外,内敛执行还可以使用 printf 八进制命令的方式执行命令

1
2
3
4
5
6
7
┌──(Malpl3naInk㉿WIN-U10PUEP8DSD)-[~/CTF/temp]
└─$ `printf "\154\163"` # ls 的八进制
01

┌──(Malpl3naInk㉿WIN-U10PUEP8DSD)-[~/CTF/temp]
└─$ `printf "\143\141\164\40\60\61"` # cat 01 的八进制
001

特征正则绕过

1
/^O:\d+/

该正则会匹配序列化字符串开头是否为对象字符串 O:<数字> 开头

将序列化字符串开头改为 O:+<数字> 可以在不影响识别的情况下绕过正则匹配