Light
TryHackMe — Light — SQL Injection Writeup
🎉 Nice work closing this one out, Navanath! “Light” looks small on the surface — just a netcat prompt asking for a username — but it packs in a proper, realistic SQL injection chain: a keyword filter to get past, a syntax trap that burns a few attempts, and a payload that finally dumps the whole admin table in one shot. That’s exactly the kind of “easy” box that quietly teaches real technique. Here’s the full writeup — command by command, including the parts that failed, because those are just as instructive as the win.
1. Target & Service
nc 10.66.144.135 1337Welcome to the Light database!
Please enter your username:A raw TCP service that accepts a “username,” prompts for a “password,” and is clearly backing onto a SQL database directly from that input (classic CTF pattern for teaching SQL injection without a web/HTTP layer in between).
2. Baseline check — legitimate login
Please enter your username: smokey
Password: vYQ5ngPpw8AdUmLThis confirms the service does a normal SELECT ... WHERE username = '<input>' style lookup, returns a password field, and that input is going straight into a query unsanitized — a prime candidate for SQL injection.
3. First injection attempt — blocked by keyword filter
Please enter your username: ' UNION SELECT username from admintable'
Ahh there is a word in there I don't like :(Why it failed: The app has a case-sensitive (or exact-string) blocklist filtering specific keywords — almost certainly UNION SELECT typed in standard case. This is a classic but weak defense: a denylist of literal strings rather than real input validation or parameterized queries.
4. Bypassing the filter — case randomization
Please enter your username: ' UnIoN SeLeCt username FrOm admintable'
Password: <Username_flag_in_normal_letters>Why this worked: SQL keywords are case-insensitive to the database engine (UNION, UnIoN, and union all execute identically in SQLite/MySQL/etc.), but the app's keyword filter was almost certainly doing a literal or case-sensitive string match (e.g. checking for the exact substring "UNION SELECT"). Mixing the case defeats a naive filter while the query itself still parses and executes normally. This is one of the oldest, most common WAF/filter bypass techniques in the book.
5. Second attempt — syntax error from quote mismatch
Please enter your username: ' UnIoN SeLeCt password FrOm admintable WhErE username="<Username_flag_in_normal_letters>"'
Error: near "''": syntax errorWhy it failed: Double quotes ("<Username_flag_in_normal_letters>") were used for the string literal. In SQLite (and standard ANSI SQL), string literals must use single quotes — double quotes are reserved for identifiers (column/table names). Combined with the surrounding injected quote from the input field, this broke the query's quoting structure and threw a parser error rather than executing.
Retry with single quotes
Please enter your username: ' UnIoN SeLeCt password FrOm admintable WhErE username='<Username_flag_in_normal_letters''
Username not found.Why this still failed: Close, but the quote balancing was still off — an extra trailing ' unbalanced the statement, so instead of a syntax error the query now parsed but returned no matching row (the app's own "not found" branch), meaning the final WHERE clause didn't actually match anything in the injected context. This is a good reminder that in blind/semi-blind SQLi, "it ran without erroring" doesn't mean "it returned what I wanted" — the quote/paren balance has to be exactly right for the whole final query, not just locally valid.
6. The working payload — string concatenation
Please enter your username: ' UnIoN SeLeCt username || ':' || password FrOm admintable'
Password: <Username_flag_in_normal_letters>Why this worked: Instead of fighting with a WHERE username = '...' clause (which kept breaking on quote balancing), this pulls both columns back in a single field using SQLite's || string concatenation operator, formatted as username:password. Since the app only ever displays one field back as "Password: ...", concatenating two columns into one is a clean way to exfiltrate multiple fields through a single-value output point — a common technique when the response only has one visible "slot" to leak data through.
Get Navanath’s stories in your inbox
Join Medium for free to get updates from this writer.
7. Full extraction — dumping the whole table in one go
Please enter your username: ' UnIoN SeLeCt GrOuP_CoNcAt(username || ':' || password)FrOm admintable'
Password: <Username_flag_in_normal_letters>:<Password_Flag>,flag:THM{<Final_flag}Why this worked, and why it’s the “real” finisher: GROUP_CONCAT() aggregates every row of admintable into a single comma-separated string, still case-scrambled to dodge the keyword filter. This is the technique that turns a one-row leak into a full table dump through a single-value response channel — instead of guessing/iterating row by row, every row comes back in one query. The result reveals a second account, flag, with the challenge flag as its "password": THM{<Final_Flag}
8. Flag
THM{Final_flag}9. Attack chain summary
Netcat service (raw SQL-backed username prompt)
└─ baseline login → confirms unsanitized input reaching the query
└─ UNION SELECT (plain case) → blocked by keyword filter
└─ UnIoN SeLeCt (mixed case) → filter bypass, leaks admin username
└─ double-quote string literal → SQL syntax error (SQLite requires single quotes)
└─ quote-balance mismatch → query ran but matched nothing
└─ UNION + || concatenation → leaks one row as "user:pass"
└─ UNION + GROUP_CONCAT() → leaks entire table → flag10. Why this is vulnerable — root cause
This challenge demonstrates two separate, stacked failures, which is what made it exploitable end-to-end:
- No parameterization (the real vulnerability). User input (the “username”) is concatenated directly into a SQL query string instead of being passed as a bound parameter. This is the entire reason injection is possible at all — everything downstream (the filter, the bypass, the dump) only matters because this root cause exists.
- Denylist-based “security” (the broken mitigation). The app tried to defend itself by blocking the literal string
UNION SELECT, which is trivially bypassed by case variation, inline comments (UN/**/ION), alternate whitespace, or encoding. A denylist can never enumerate every syntactic variant of a dangerous keyword — it's security theater, not a real control.
11. How to secure this — Red Team perspective
If you’re assessing or building offense around this pattern, these are the things to actively test for and report on:
- Confirm root cause, not just impact. Demonstrate the injection is a true parameterization failure (not just a filter gap) by showing the payload works regardless of how the keyword filter is bypassed — case, comments, encoding, whitespace tricks. This proves the fix needs to be architectural, not another denylist entry.
- Test filter bypass systematically. Case randomization, inline SQL comments (
/**/), URL/hex encoding, double encoding, and alternate logical operators (||,OR 1=1,UNION ALL SELECT) should all be tried — a report that only shows one bypass technique understates the real exposure. - Chase full impact, not just a PoC. The difference between “username leaked” and “entire table dumped via GROUP_CONCAT” is the difference between a medium and critical finding — always push to table/schema enumeration (
sqlite_master,information_schema, etc.) to show the true blast radius. - Check for secondary injection points. If the username field is injectable, test whether the password field, or any other inputs that reach the same query layer, are injectable too.
12. How to secure this — Blue Team / Defensive perspective
- Use parameterized queries / prepared statements, always. This is the only real fix — user input should never be concatenated into a SQL string. Every major language/DB driver supports bound parameters (
?placeholders, named parameters, ORMs with proper escaping). - Never rely on keyword denylists as a security boundary. If a WAF or input filter is used, it should be a defense-in-depth layer, not the only control — and it should be paired with parameterization underneath it, not instead of it.
- Enforce least privilege on the DB account. The account used by this service shouldn’t have read access to an
admintableit doesn't need, and definitely shouldn't be able to runUNION SELECTacross arbitrary tables. Scope DB permissions tightly per application function. - Don’t store plaintext passwords. Even with injection fixed,
admintablestoring passwords in plaintext (mamZtAuMlrsEy5bp6q17) is a second failure — passwords should be hashed with a slow, salted algorithm (bcrypt/Argon2), so that even a successful breach doesn't yield usable credentials. - Monitor and alert on anomalous query patterns.
UNION,GROUP_CONCAT, and queries touching system tables (sqlite_master/information_schema) from an application's low-privilege DB account are strong indicators of active exploitation — these should trigger alerts in a properly instrumented environment. - Validate input by allowlist, not denylist. If a username field should only ever contain alphanumerics, enforce that positively (regex match on allowed characters) rather than trying to block “bad” patterns after the fact.
13. Key takeaway
This box is a compact demonstration that SQL injection is still fundamentally a root-cause problem, not a “did we block the right keyword” problem. Every bypass technique in this writeup exists only because the real fix — parameterized queries — was never applied. For red teams, that’s the point worth proving; for blue teams, that’s the point worth fixing first, before anything else.






