php框架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格式的数据。 这个特性将会导致两个问题:
- WAF绕过
- 可能存在的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版本中官方就无视这个问题了。 我猜可能有两个原因:
- 官方注意到了这个问 题,但认为3.0版本需求的php版本在5.5以上,而错以为5.5以上的php就已经不存在XXE的隐患了。但实际上XML外部实体的解析,和php版本并无关系,而是和编译时的libxml库版本有关。
- 官方尚未 注意到这个问题。
感觉前者的可能性较大。 所以解决方案也还是按照2中的方案进行。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.
