← All scrolls
D rank
INFERNO CTF v2 · Web

Recipe: LaTeX injection with a double-caret bypass

A recipe-card PDF generator feeds one field straight into pdflatex. A keyword denylist blocks input, but TeX's ^^ hex notation rebuilds the command at compile time.

05 Sept 20262 min readEasy
#latex-injection#filter-bypass#pdflatex

Category: Web
Difficulty: Easy
Vulnerability: LaTeX Injection with TeX Double-Caret Hex Bypass
Flag: INFERNOCTF{0ops_1_br0k3_th3_p4p3rtr41l_w1th_l4t3x_1nj3ct10n}


Challenge Card & Overview

Recipe Challenge Box

Recipe Overview


Overview & Reconnaissance

The target is a web application that generates printable recipe cards as downloadable PDFs using a backend LaTeX typesetting engine (pdflatex).

When generating a card:

  • Most form fields (title, author, ingredients, method, etc.) are escaped using strict character replacement mapping.
  • The kitchen_note field, however, is filtered through a custom denylist function (validate_kitchen_note) and directly concatenated into the LaTeX document:
\noindent\rule{\linewidth}{0.4pt}\\[-1mm]
{\footnotesize\textit{Kitchen note:} <USER_INPUT>}
\end{document}

The flag is stored on the server and symlinked inside the temporary compilation directory as ./flag.


Filter Analysis

The kitchen_note validation logic in app.py checks:

  1. Length Limit: len(note) <= 118 characters.
  2. Blocked Characters: $, &, #, _, %, ~, |, <, >, ‘', ;, \x00`.
  3. No Paths / URLs: Rejects .., ://, or /.
  4. Keyword Denylist: Checks if any of the following substrings appear in the lowercased note:
    BLOCKED_FRAGMENTS = (
        "input", "include", "read", "write", "openin", "openout",
        "catcode", "csname", "endcsname", "usepackage", "document",
        "newcommand", "renewcommand", "providecommand", "def",
        "loop", "repeat", "immediate", "special", "verbatim",
        "scantokens", "every", "jobname"
    )

Notice that standard file reading commands like \input, \include, and \lstinputlisting are blocked by the substring check "input".


The Vulnerability & Bypass Mechanics

TeX Double-Caret Hex Notation (^^)

In TeX/LaTeX, the scanner supports double-caret encoding (^^XX) for characters using two-digit lowercase hexadecimal ASCII values:

  • ^^69 corresponds to ASCII 0x69, which is the character i.
  • ^^6e corresponds to ASCII 0x6e, which is the character n.

Bypassing the Python Filter

When we submit:

\lst^^69nputlisting{flag}
  1. Python Validation:

    • The string literal is \lst^^69nputlisting{flag}.
    • The substring "input" is not present in the string.
    • The character ^ is not in BLOCKED_CHARS ($&#_%~|<>;\x00`).
    • Length is 26 characters (well within the 118 character limit).
    • Filter passes!
  2. LaTeX Compilation:

    • When pdflatex processes the file, its lexical scanner decodes ^^69 into i.
    • The token becomes \lstinputlisting{flag}.
    • Because \usepackage{listings} is pre-loaded in the template preamble, \lstinputlisting reads ./flag and typesets its content directly into the output PDF.

Exploitation & Payload Examples

Primary Exploit Payload

Set the kitchen_note field in the form to:

\lst^^69nputlisting{flag}

Alternate Variations

  • Encoding other letters in input:
    \lsti^^6eputlisting{flag}
  • Full hex encoding of the keyword:
    \lst^^69^^6e^^70^^75^^74listing{flag}

Capturing the Flag

  1. Fill out the recipe form with any values.
  2. In the Kitchen note field, enter:
    \lst^^69nputlisting{flag}
  3. Click Export Recipe Card (PDF).
  4. Open the generated PDF to read the flag rendered at the bottom:
INFERNOCTF{0ops_1_br0k3_th3_p4p3rtr41l_w1th_l4t3x_1nj3ct10n}

Remediation

  • Do not rely on keyword denylists: TeX syntax provides multiple character expansion mechanisms (such as ^^ notation) that easily evade string pattern matching.
  • Escape all user-controlled inputs: Apply character sanitization (LATEX_REPLACEMENTS) uniformly across all form fields.
  • Harden the LaTeX environment: Use sandboxed compilation environments and disable file inclusion directives.