Too XXE For My Shirt

Until tonight, I'd never gotten a chance to try an xml external entity (XXE) attack. Earlier, I was updating XMLmao and XSSmh with the same interface improvements and custom blacklist features already present in SQLol. The idea, eventually, is to combine them to form a training environment whose working title is the "Magical Code Injection Rainbow" and which would continue to be free (both as in liberty and as in beer) as the individual projects are.

XMLmao has had support for XML injection attacks since its inception, but only allowed for XXE attacks since the latest version.

What most people know of XML is the basic document structure, which is just hierarchical tags like its popular cousin HTML. Lesser known are the document type definition (DTD) directives, which can be specified in-line or externally. One such directive is the ENTITY directive, which looks like this:

<!ENTITY copyright "(c) FooBar LLC 2012" >

This entity can be placed in an XML document like so:


And the XML parser will dynamically replace the content, resulting in something like this:

<foo>(c) FooBar LLC 2012</foo>

Now here's where it gets interesting: Entities can be defined using external sources. These sources are specified as URIs. If we want to include the contents of a file on the system, we can do so as follows:

<!ENTITY xxe SYSTEM "file:///var/www/mydata.xml" >

This will replace any instance of "&xxe;" with the contents of /var/www/mydata.xml. If we had control over this entity or could define our own, we could define this entity as being the contents of /etc/passwd or some other such file. We can also use HTTP URIs, which allows us to use the XML parser as a sort of proxy, retrieving data from any HTTP resource to which the system running the XML parse can reach. Internal resources which may be otherwise inaccessible may now be within reach.

It's possible to use this functionality as a jury-rigged port scanner, to discover internal content. It may even be possible to exploit internal resources, given that an exploit can be delivered through a URL. For instance:

<!ENTITY uhoh SYSTEM ";drop+table+important_things;--" >


If you'd like to try XXE attacks, go get the latest version of XMLmao.

Trustwave reserves the right to review all comments in the discussion below. Please note that for security and other reasons, we may not approve comments containing links.