In a CTF, a pdf compilation script [1] cn be use to compile latex document. The directory contains a setuid wrapper [2]. Craft a Latex document that allow us to inject a command to read the content of directory FALG. The setuid wrappers is : int main(int arc, char** arv) {
char *argv[] = { "/bin/bash", "-p", "/challenge/app-script/ch24/ch24.sh", arv[1] , NULL };
setreuid(geteuid(), geteuid());
execve(argv[0], argv, NULL);
return 0;
}
ANd the Latex compilation script [2] is : #!/usr/bin/env bash
You have a LaTeX compilation script (ch24.sh) that runs pdflatex with --shell-escape.
The script is invoked via a setuid wrapper (binary) that calls:
c
execve("/bin/bash", argv, NULL);
with arguments: bash -p /challenge/app-script/ch24/ch24.sh <your_file>
Crucially: bash -p enables privileged mode, meaning the shell keeps its effective UID (i.e., it remains setuid-root), so any commands executed will run as root.
Your goal: Read the contents of a directory called FLAG (likely containing a flag file).
🔍 Key Observations
✅ --shell-escape is enabled → allows LaTeX to execute system commands.
✅ bash -p is used → Bash runs in privileged mode (retains effective UID = root).
✅ The script uses mktemp -d → creates a temporary directory for the .tex file.
✅ The script copies your input .tex file into the temp dir → you control the content of main.tex.
✅ You can write arbitrary LaTeX code, including \write18{...} or \immediate\write18{...} to execute shell commands.
❗ But: The pdflatex process runs inside the temp directory (TMP), which is not writable by non-root users after chmod 750 -R.
However, since the wrapper is setuid root, and the shell runs with -p, any command executed from within the LaTeX file will be executed as root — even if the temp dir is restricted.
So we can exploit this via LaTeX's \write18 to run a command as root.
🎯 Goal: Read /challenge/flag or /challenge/app-script/flag?
Assuming the FLAG directory is located at:
code
/challenge/flag
or possibly:
code
/challenge/app-script/flag
We need to craft a .tex file that executes:
bash
cat /challenge/flag
as root.
But note: the pdflatex process is sandboxed in a temp directory, but the shell command runs under the same privileges as the wrapper — root.
✅ Solution: Use \write18 to execute a command as root
✨ Final Payload (.tex file)
latex
\documentclass{article}
\begin{document}
% Execute a shell command as root using --shell-escape
\immediate\write18{cat /challenge/flag > /tmp/flag.txt; echo "Flag written to /tmp/flag.txt"}
% Optional: Output something to PDF to avoid error
Hello, world!
\end{document}