Learning · picoCTF 2026

ORDER ORDER: second-order SQL injection

A web exploitation challenge where the place you type the payload isn't where it runs.

Practice challenge on a platform built for it. Never test a system you don't own or have written permission to test. The flag is hidden here on purpose: picoCTF{…redacted…}.

Overview

The signup username is stored, then reused later inside an ORDER BY clause when the Inbox page renders. That's a second-order injection: the SQL runs on a different page than the one where you entered it. The backend was Flask with an SQLite database.

  1. Inject SQL into the username at signup.
  2. Log in and open the Inbox page.
  3. The server runs a query that sorts by that stored username, so the payload executes.
  4. Use UNION SELECT to read SQLite's schema, then pull out the flag.

What didn't work first

I tried the URL parameters ?i=, ?order= and ?sort=. None changed the SQL. The Expenses page was static and report generation didn't run my input directly. That pointed to a stored injection.

Triggering it

Signing up with a username like a' ORDER BY 1-- made the Inbox show SQL errors. They confirmed three things: the username lands in an ORDER BY, UNION works, and the result has exactly three columns.

Reading the schema

a' UNION SELECT name, sql, 3 FROM sqlite_master--

That listed the tables, including one with an odd base64-looking name. Listing its columns:

a' UNION SELECT 1, name, 3 FROM pragma_table_info("<that table>")--

It had two columns, name and value. Listing the stored keys showed one called flag.

Getting the flag

a' UNION SELECT 1, value, 3 FROM "<that table>" WHERE name='flag'--

The Inbox page then showed the value inside the CSV report.

Why it exists

The app builds something like SELECT subject, report_path, created_at FROM reports WHERE username = '…' ORDER BY … and drops the stored username straight into the ORDER BY. You can't parameterize ORDER BY the way you can a value, so dynamic identifiers are dangerous.

Takeaways

  1. Never put unsanitized input into ORDER BY, column names or table names.
  2. Use a whitelist of sortable fields.
  3. Stored user data becomes dangerous when it's reused in SQL later.
  4. In SQLite, sqlite_master and PRAGMA make schema enumeration easy.
  5. The username field is a common, subtle second-order vector.

Back to my learning log

Business consulting

Don't need Core Response? Meet me on EveryExpert.

If you just need help getting a business set up, growing it, or fixing what's costing you money, book a free 20-minute intro call with me there. It's a straight answer on your next steps, not a pitch.

  • Starting a business, without the costly mistakes most new owners make
  • Fixing the leaks and wasted effort in how you run things
  • Using AI without putting your company at risk

Book a free 20-minute intro call

Booked through my verified EveryExpert profile.

Book my free 15-minute demoFor missed-call text-backMy credentialsDegrees and certificates20-minute callFor consultation