Overview
XML External Entity (XXE) injection happens when an application parses XML that an attacker controls, and the underlying parser is allowed to resolve external entities. XML has a feature that lets a document define an entity whose value is loaded from a URI, including file:// URIs. If the parser honours that, an attacker can define an entity pointing at a file on the server, reference it in the document, and get the file’s contents reflected back in the response.
This post uses the “XML parser” challenge from the DD CTF (a concluded, educational CTF run by team bi0s) as the worked example, but the technique is the point: XXE is OWASP-classic and still shows up in real applications that accept XML uploads, SOAP requests, SAML, SVG, or office-document formats.
Scope and ethics. This was done against a deliberately vulnerable target provided by a CTF that has since ended. Only ever test XXE against systems you own or are explicitly authorised to assess.
Why XML parsers are dangerous by default
Two XML features combine to make this possible.
Document Type Definitions (DTDs) let a document declare its own structure inline, in a <!DOCTYPE ...> block.
Entities are essentially variables. A general entity declared as <!ENTITY name "value"> can then be referenced as &name; and the parser substitutes the value. That is harmless on its own. The danger is the external form:
<!ENTITY xxe SYSTEM "file:///etc/passwd">
Here the entity’s value is not a literal string. SYSTEM tells the parser to fetch the content from the given URI and use that as the value. Many XML libraries historically resolved these external entities automatically, so if the application echoes the parsed document back, &xxe; expands to the contents of /etc/passwd. The attacker never needed access to the file system; the server reads the file on their behalf.
The challenge spells out the setup in its description: an endpoint that parses whatever XML you send and never validates it. The flag lives at /tmp/flag.txt, so the goal is to make the parser read that path and return it.
The attack
The payload defines a DTD with a single external entity pointing at the target file, then references it in the document body so the parsed output contains the file’s contents:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE foo [
<!ELEMENT foo ANY >
<!ENTITY xxe SYSTEM "file:///tmp/flag.txt" >
]>
<foo>&xxe;</foo>
Reading it line by line:
<?xml ... ?>is the standard XML declaration.<!DOCTYPE foo [ ... ]>opens an inline DTD for a root element namedfoo.<!ELEMENT foo ANY >says thefooelement may contain any content, so the parser won’t reject our text node.<!ENTITY xxe SYSTEM "file:///tmp/flag.txt" >is the payload: it declares an external entityxxewhose value is loaded from the local file/tmp/flag.txt.<foo>&xxe;</foo>references the entity. When the parser expands&xxe;, the element’s content becomes the file’s contents.
Submitting this to the parsing endpoint returns the contents of /tmp/flag.txt in the response, the flag bi0s{...} included. The same payload reads any file the server process can access by changing the path in the file:// URI (/etc/passwd, application source, configuration with credentials, and so on).
A few practical notes:
- If the response doesn’t reflect the parsed value back, this becomes blind XXE. You then exfiltrate data out-of-band, typically by hosting an external DTD on a server you control and using a parameter entity to send the file contents to you over HTTP. That’s a larger topic, but the root cause is identical.
file://is only one scheme. Depending on the parser and platform,http://can turn XXE into server-side request forgery (SSRF), and on PHP thephp://filterwrapper is often used to base64-encode files that would otherwise break XML parsing.
Defending against XXE
The fix is not input filtering; it is telling the parser not to resolve external entities and DTDs in the first place. The exact call depends on the library, but the principle is universal:
- Disable DTDs /
DOCTYPEentirely where possible. If the parser never processes a DOCTYPE, external entities can’t be declared. In Java,setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); in Python’slxml, use a parser created withresolve_entities=False(and preferdefusedxml); in .NET, setXmlResolver = null. - Disable external general and parameter entities if you must allow DTDs.
- Use a hardened library. For Python,
defusedxmlexists precisely to patch these defaults. - Prefer a less dangerous format. If the endpoint only needs key/value data, JSON avoids this entire class of bug.
Takeaways
- XXE is a parser-configuration bug, not a user-input bug. You don’t prevent it by blocking the word “DOCTYPE”; you prevent it by disabling external entity resolution.
- The server does the reading for you. That’s what makes XXE powerful: it turns an XML endpoint into a file-disclosure and sometimes SSRF primitive, with no file-system access required.
- Any XML entry point is in scope. File uploads (SVG, DOCX), SOAP/XML APIs, SAML, and RSS all parse XML. If any of them use a default-configured parser, they may be vulnerable.
Worked example: “XML parser”, DD CTF by team bi0s. Flag redacted as bi0s{...}.