search
HomeBackend DevelopmentPHP Tutorialphp框架slim架构上存在XXE漏洞(XXE的典型存在形式)

【过年了,每天发一篇以前的存货,一共七篇。】

现代cms框架(laraval/symfony/slim)的出现,导致现今的php漏洞出现点、原理、利用方法,发生了一些变化,这个系列希望可以总结一下自己挖掘的此类cms漏洞。

slim是一个设计思路超前的 知名的php轻框架 ,完美结合了psr7来设计, 至今用户已超过100w:

在阅读其源码的过程中,我发现其存在一个只有在框架式CMS中才会出现的漏洞。

官方网站: http://www.slimframework.com/

漏洞详情

这个漏洞存在于最新版(3.0)中。 首先用conposer安装之

composer require slim/slim "^3.0@RC"

看其文档: http://www.slimframework.com/docs/objects/request.html#the-request-body 获取POST数据,是利用getParsedBody方法,而这个方法对POST的处理,是按照content-type来区分和解析的:

很典型的问题,在这篇帖子里也提到过: http://zone.wooyun.org/content/19908 有时候框架会帮开发者一些他可能并不需要的『忙』,比如slimphp这里,常规的POST的content-type为application/x-www-form-urlencoded,但只要我将其修改为application/json,我就可以传入json格式的POST数据,修改为application/xml,我就可以传入XML格式的数据。 这个特性将会导致两个问题:

  1. WAF绕过
  2. 可能存在的XXE漏洞

WAF绕过这个肯定不用说了,常规的WAF一般只检测application/x-www-form-urlencoded的数据,一旦修改数据类型则将通杀各大WAF。 XXE是本漏洞的重点。 我们看到解析body的代码:

public function __construct($method, UriInterface $uri, HeadersInterface $headers, array $cookies, array $serverParams, StreamInterface $body, array $uploadedFiles = [])    {        $this->originalMethod = $this->filterMethod($method);        $this->uri = $uri;        $this->headers = $headers;        $this->cookies = $cookies;        $this->serverParams = $serverParams;        $this->attributes = new Collection();        $this->body = $body;        $this->uploadedFiles = $uploadedFiles;        if (!$this->headers->has('Host') || $this->uri->getHost() !== '') {            $this->headers->set('Host', $this->uri->getHost());        }        $this->registerMediaTypeParser('application/json', function ($input) {            return json_decode($input, true);        });        $this->registerMediaTypeParser('application/xml', function ($input) {            return simplexml_load_string($input);        });        $this->registerMediaTypeParser('text/xml', function ($input) {            return simplexml_load_string($input);        });        $this->registerMediaTypeParser('application/x-www-form-urlencoded', function ($input) {            parse_str($input, $data);            return $data;        });    }

实际上解析代码是作为回调函数写在Request类的构造方法里了。 可见这里直接调用了simplexml_load_string解析$input,造成XML实体注入漏洞。 所以,用slim framework 3.0开发的CMS,只要获取了POST数据,都将受到此XXE漏洞的影响。

漏洞证明

编写一个最简单的demo页面,只有一个获取POST信息并输出的功能:

require 'vendor/autoload.php';$app = new \Slim\App();$app->post("/post", function($request, $response) {    $parsedBody = $request->getParsedBody();    print_r($parsedBody);});$app->run();

搭建在三个白帽里: http://520fdc0ca2c37864f.jie.sangebaimao.com/

正常请求:

触发XXE漏洞并读取/etc/passwd:

漏洞修复

在slimphp2中,官方是对这块进行一定处理了:

/**     * Parse XML     *     * This method creates a SimpleXMLElement     * based upon the XML input. If the SimpleXML     * extension is not available, the raw input     * will be returned unchanged.     *     * @param  string                  $input     * @return \SimpleXMLElement|string     */    protected function parseXml($input)    {        if (class_exists('SimpleXMLElement')) {            try {                $backup = libxml_disable_entity_loader(true);                $result = new \SimpleXMLElement($input);                libxml_disable_entity_loader($backup);                return $result;            } catch (\Exception $e) {                // Do nothing            }        }        return $input;    }

不知为何在3.0版本中官方就无视这个问题了。 我猜可能有两个原因:

  1. 官方注意到了这个问 题,但认为3.0版本需求的php版本在5.5以上,而错以为5.5以上的php就已经不存在XXE的隐患了。但实际上XML外部实体的解析,和php版本并无关系,而是和编译时的libxml库版本有关。
  2. 官方尚未 注意到这个问题。

感觉前者的可能性较大。 所以解决方案也还是按照2中的方案进行。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Working with Flash Session Data in LaravelWorking with Flash Session Data in LaravelMar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIscURL in PHP: How to Use the PHP cURL Extension in REST APIsMar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Simplified HTTP Response Mocking in Laravel TestsSimplified HTTP Response Mocking in Laravel TestsMar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

12 Best PHP Chat Scripts on CodeCanyon12 Best PHP Chat Scripts on CodeCanyonMar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Explain the concept of late static binding in PHP.Explain the concept of late static binding in PHP.Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

PHP Logging: Best Practices for PHP Log AnalysisPHP Logging: Best Practices for PHP Log AnalysisMar 10, 2025 pm 02:32 PM

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

How to Register and Use Laravel Service ProvidersHow to Register and Use Laravel Service ProvidersMar 07, 2025 am 01:18 AM

Laravel's service container and service providers are fundamental to its architecture. This article explores service containers, details service provider creation, registration, and demonstrates practical usage with examples. We'll begin with an ove

Customizing/Extending Frameworks: How to add custom functionality.Customizing/Extending Frameworks: How to add custom functionality.Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft