The php framework slim architecture has the XXE vulnerability (a typical form of XXE). [this is the New Year's Eve. I published a previous inventory article every day for a total of seven articles .]
The emergence of the modern cms Framework (laraval/symfony/slim) has led to some changes in the current php vulnerabilities, principles, and utilization methods, in this series, we hope to summarize the cms vulnerabilities we have discovered.
Slim is a well-known php light framework with advanced design ideas. it works perfectly with psr7, and has more than 100 million users so far:
When I read the source code, I found that there is a vulnerability that only occurs in the framework CMS.
Http://www.slimframework.com/
Vulnerability details
This vulnerability exists in the latest version (3.0. First install it with conposer
Composer require slim/slim "^ 3.0 @ RC"
See its documentation: http://www.slimframework.com/docs/objects/request.html#the-request-body to get POST data, is the use of the getParsedBody method, and this method of POST processing, according to content-type to distinguish and parse:
A typical problem, also mentioned in this post: sometimes the http://zone.wooyun.org/content/19908 framework will help developers some of the "busy" He may not need, such as slimphp here, the content-type of a conventional POST is application/x-www-form-urlencoded, but as long as I change it to application/json, I can pass in POST data in json format, if you change it to application/xml, you can input data in XML format. This feature will cause two problems:
- WAF bypass
- Possible XXE vulnerability
WAF does not need to bypass this. In general, WAF only checks the data of application/x-www-form-urlencoded. Once the data type is modified, it will kill WAF. XXE is the focus of this vulnerability. We can see the code for parsing the 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; }); }
In fact, the parsing code is written in the construction method of the Request class as a callback function. We can see that simplexml_load_string is directly called to parse $ input, resulting in the XML entity injection vulnerability. Therefore, CMS developed with slim framework 3.0 will be affected by this XXE vulnerability as long as POST data is obtained.
Vulnerability proof
Compile a simple demo page with only one function for obtaining POST information and outputting it:
require 'vendor/autoload.php';$app = new \Slim\App();$app->post("/post", function($request, $response) { $parsedBody = $request->getParsedBody(); print_r($parsedBody);});$app->run();
Built in three white hats: http://520fdc0ca2c37864f.jie.sangebaimao.com/
Normal request:
Trigger the XXE vulnerability and read/etc/passwd:
Vulnerability repair
In slimphp2, the official website handles this part:
/** * 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; }
I wonder why the official version of 3.0 ignores this issue. I guess there may be two reasons:
- The official website noticed this question, but thought that the php version required by version 3.0 is later than version 5.5, and the mistake was that php version 5.5 and later does not have XXE risks. However, XML external entity parsing is not related to the php version, but to the libxml Library version during compilation.
- The official team has not noticed this problem.
I feel that the former is more likely. Therefore, the solution is based on the solution in step 2.