SlimXXE漏洞
漏洞详情:
我们看到解析body的代码:
Default
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信息并输出的功能:
Default
require 'vendor/autoload.php';
$app = new \Slim\App();
$app->post("/post", function($request, $response) {
$parsedBody = $request->getParsedBody();
print_r($parsedBody);
});
$app->run();
触发XXE漏洞并读取/etc/passwd:
漏洞修复:
在slimphp2中,官方是对这块进行一定处理了:
Default
/**
* 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库版本有关。官方还没有注意到。