---
title: "5. XXE - Local file disclosure"
date: 2024-12-30
draft: false
---

When webapps trust unfiltered XML data from user input we can reference external XML DTD docs and define new custom XML entities. 
- Suppose define new ext. ent. + make them ref local file

First step in identifying a potential XXE vuln is finding webpages that accept XML user input. 
![](/img/Pasted image 20241230133232.png)

Assume outdated XML libraries used to parse our xml data. 
Assume no filters or sanitization on our XML input. 
Potentially exploitable. 

Sending request we get: 

![](/img/Pasted image 20241230133336.png)

The value `email` is reflected back to us from user input. 
To print content of ext. file to page - note which elements get displayed back to us, and use that element to inject. 

So, let's define a new entity and use it as a variable in the `email` element. 

**Note:** In our example, the XML input in the HTTP request had no DTD being declared within the XML data itself, or being referenced externally, so we added a new DTD before defining our entity. If the `DOCTYPE` was already declared in the XML request, we would just add the `ENTITY` element to it.

![](/img/Pasted image 20241230133627.png)

Now we should have an xml entity named `company` reference-able with `&company;` 

![](/img/Pasted image 20241230133715.png)

**This confirms we are dealing with a webapp that is vulnerable to XXE**
- Some webapps may default to json format in http req but still accept other formats, including XML. 
- Even if webapp sends req in JSON, we can try changing `Content-Type` header to `application/xml` and convert JSON to XML with an online tool. 
- If webapp DOES accept req with XML data, we may also test XXE 

## Leveraging XXE to read sensitive files 

![](/img/Pasted image 20241230133916.png)

Same steps as above. XML variable referring to a local file, and referencing that xml variable. 

We can read content of sensitive files using this, like config files that may contain passwords or other sensitive files like an SSH private key of a specific user. 
NOTE: 
- **In certain java webapps we may also be able to specify a directory instead of a file and get a directory listing instead, which can be very useful for locating sensitive files**

Same concept applies for reading the source code of the webapplication, enabling a Whitebox pentest to unveil more vulnerabilities in the webapplication or at the very least reveal secret configs or api keys. 

![](/img/Pasted image 20241230134128.png)

On first attempt this doesn't work: Why? 
- File we are referencing is not in a proper XML format, so it fails to be referenced as external xml entities. 
- If file contains XML special characters (<, >, &) it breaks external entity reference and not be used for the reference
- Furthermore - unable to read binary data as it also does not conform to XML format. 
- PHP provides wrapper filters that allow us to b64 encode resources 'including files' like we've seen in previous sections. 
	- In our `file://` reference use `php://filter/` wrapper and specify `convert.base64-encode` 
	- `"php://filter/convert.base64-encode/resource=index.php"`

![](/img/Pasted image 20241230134528.png)

we can decode this right from burp 

![](/img/Pasted image 20241230134625.png)

**This trick only works for PHP.**

Module exercise: Read `connection.php` 

![](/img/Pasted image 20241230141056.png)
