XML External Entity (XXE) Injection

πŸ’€ Server-Side Attack: Exploiting XML parsers to access local files, execute code, and perform SSRF attacks

Overview

XML External Entity (XXE) injection is a web security vulnerability that allows an attacker to interfere with an application's processing of XML data. XXE attacks occur when XML input containing a reference to an external entity is processed by a weakly configured XML parser.

XXE Attack Capabilities:

  • Local File Disclosure - Read sensitive server files

  • Remote Code Execution - Execute system commands

  • Server-Side Request Forgery (SSRF) - Access internal networks

  • Denial of Service (DoS) - Crash server with entity bombs

  • Source Code Disclosure - Extract application source code


1. Local File Disclosure

Identifying XXE Vulnerabilities

XML Input Detection

Common XXE Targets:

  • Contact forms submitting XML data

  • API endpoints accepting XML content

  • File upload functionality processing XML/SVG

  • SOAP web services

  • RSS feeds and XML sitemaps

Testing Methodology

Step 1: Identify XML Processing

Step 2: Test Entity Processing

Vulnerability Indicators:

  • Entity value (Inlane Freight) appears in response

  • Non-vulnerable apps show &company; as raw text

  • XML parsing errors reveal parser type/version

Basic File Disclosure Attacks

Reading System Files

Target /etc/passwd:

Common Target Files

Reading Source Code

PHP Source Code Disclosure

Problem: Direct file inclusion breaks XML format

Solution: PHP Filter Wrapper

Decoding Base64 Output:

Remote Code Execution

PHP Expect Wrapper (Rare)

Requirements: expect module installed and enabled

Web Shell Deployment

Method 1: Download and Execute

Step 3: XXE payload to download shell:

Note: Replace spaces with $IFS to avoid breaking XML syntax

Other XXE Attack Vectors

Server-Side Request Forgery (SSRF)

Denial of Service (Billion Laughs)


2. Advanced File Disclosure

Advanced Exfiltration with CDATA

Problem with Special Characters

Issue: Files containing XML special characters break entity parsing

CDATA Solution

Theory: Wrap content in CDATA to treat as raw data

Parameter Entity Bypass

Problem: Cannot join internal and external entities directly

Solution: Use Parameter Entities (%)

Complete CDATA Attack

Step 1: Create External DTD

Step 2: XXE Payload

Benefits:

  • Works with any file type

  • No base64 encoding required

  • Preserves original formatting

  • Bypasses character restrictions

Error-Based XXE

Scenario: Blind XXE Exploitation

Problem: Application doesn't display XML entity values Solution: Force errors to leak file content

Error-Based Technique

Step 1: Create Error-Inducing DTD

Step 2: Trigger Error with File Content

Result: Error message contains file content


3. Blind Data Exfiltration

Out-of-band (OOB) Data Exfiltration

Scenario: Completely Blind XXE

Problem: No entity output displayed AND no error messages shown Solution: Out-of-band data exfiltration via HTTP requests

OOB Attack Methodology

Theory: Instead of displaying file content in response, make application send HTTP request to attacker server with file content as URL parameter

Manual OOB Technique

Step 1: Create Exfiltration DTD

Step 2: Setup Decoding Server

Step 3: Start PHP Server

Step 4: OOB XXE Payload

Step 5: Create External DTD

Result: Server receives HTTP request with base64-encoded file content

Alternative OOB Methods

DNS OOB Exfiltration

Advanced Technique: Use DNS subdomain queries to exfiltrate data

Capture with tcpdump:

Automated OOB Exfiltration

XXEinjector Tool

Installation:

Tool Usage

Step 1: Prepare HTTP Request Template

Step 2: Execute XXEinjector

XXEinjector Advanced Options

Complete OOB Attack Workflow

Step-by-Step Implementation


HTB Academy Lab Solutions

Lab 1: Connection.php API Key Extraction

Target: http://10.129.234.170 (ACADEMY-WEBATTACKS-XXE)

Objective: Read connection.php and find api_key value

Solution:

Decode Base64 Response:

🎯 Flag: UTM1NjM0MmRzJ2dmcTIzND0wMXJnZXdmc2RmCg

Lab 2: Advanced Flag.php Extraction

Target: http://10.129.234.170 (ACADEMY-WEBATTACKS-XXE)

Objective: Read /flag.php using CDATA or Error-based methods

Method 1: CDATA Approach (at /index.php)

Step 1: Create External DTD

Step 2: XXE Payload

Method 2: Error-Based Approach (at /error)

Step 1: Create Error DTD

Step 2: Error XXE Payload

🎯 Flag: HTB{...}

Lab 3: Blind OOB Data Exfiltration

Target: http://10.129.234.170 (ACADEMY-WEBATTACKS-XXE)

Objective: Use OOB exfiltration on /blind page to read /327a6c4304ad5938eaf0efb6cc3e53dc.php

Manual OOB Method

Step 1: Setup Decoding Server

Step 2: Create External DTD

Step 3: OOB XXE Payload

Step 4: Send to /blind/submitDetails.php

Step 5: Check Server Logs

  • Server receives HTTP request with base64-encoded PHP file

  • Example base64 response: PD9waHAgJGZsYWcgPSAiSFRCezFfZDBuN19uMzNkXzB1N3B1N183MF8zeGYxbDdyNDczX2Q0NzR9IjsgPz4K

  • PHP auto-decodes and displays flag in error log

Decode Base64 to Get Flag:

Automated XXEinjector Method

Step 1: Prepare Request File

Step 2: Execute XXEinjector

🎯 Flag: HTB{...}


Automated XXE Testing

XXE Detection Script

Burp Suite XXE Testing

Intruder Payloads

Content-Type Bypass


Vulnerable Code Examples

PHP - Insecure XML Processing

Secure XML Processing


Prevention & Hardening

XML Parser Configuration

PHP Security Settings

Java Security Settings

Application-Level Controls

Input Validation

Content-Type Validation


Detection & Monitoring

Log Analysis

Web Application Firewall Rules

Security Testing Checklist


XXE injection vulnerabilities highlight the importance of secure XML parser configuration and input validation in web applications.

Last updated