githubEdit

Testing for SQL


How SQL Injection Works

When user input is inserted directly into SQL queries without sanitization, attackers can break out of the intended query and execute arbitrary SQL.

Vulnerable PHP Code Example

$username = $_POST['username'];
$query = "SELECT * FROM logins WHERE username='$username'";

If user inputs: admin'-- -

Query becomes:

SELECT * FROM logins WHERE username='admin'-- -'

The -- - comments out the rest, bypassing any password check.


SQLi Discovery

Escape Context Characters

Try these to break out of the current query context:

[Nothing]
'
"
`
')
")
`)
'))
"))
`))

Test Payloads

Payload
URL Encoded

'

%27

"

%22

`

%60

#

%23

;

%3B

)

%29

')

%27%29

"))

%22%29%29

If you get a SQL error or different behavior, injection may be possible.


SQL Comments (End Query Early)

DBMS
Comment Syntax

MySQL

#, -- - (space required), /*comment*/

PostgreSQL

--, /*comment*/

MSSQL

--, /*comment*/

Oracle

--

SQLite

--, /*comment*/

Note: -- requires a space after. Use -- - or URL encode as --+


Types of SQL Injection

Type
Description

Union-based

Results visible on page, use UNION to extract data

Error-based

Database errors reveal query output

Boolean Blind

True/false responses based on conditions

Time Blind

Use SLEEP() to infer data based on response time

Out-of-band

Exfiltrate data via DNS or HTTP requests


Authentication Bypass

Common Payloads

How OR Injection Works

Original query:

With input admin' OR '1'='1'-- -:

Since '1'='1' is always true, authentication is bypassed.


UNION Injection

UNION combines results from multiple SELECT statements. Both queries must return the same number of columns.

Step 1: Detect Number of Columns

Method A: ORDER BY

Method B: UNION SELECT

Step 2: Find Visible Columns

If page displays 2 and 3, those columns are visible for data extraction.

Step 3: Extract Data


Database Enumeration

MySQL Fingerprinting

Payload
Expected Output

SELECT @@version

MySQL/MariaDB version string

SELECT POW(1,1)

1 (numeric test)

SELECT SLEEP(5)

5 second delay

Enumerate Databases

Current Database

Enumerate Tables

Enumerate Columns

Dump Data


MySQL Useful Functions & Variables


File Read (MySQL)

Check FILE Privilege

Read Files with LOAD_FILE()


File Write (MySQL)

Check secure_file_priv

  • Empty = can write anywhere

  • /path/ = can only write to that directory

  • NULL = cannot write files

Write Files with INTO OUTFILE

Write Web Shell

Then access: http://target/shell.php?0=id


Web Root Paths

Server
Common Paths

Apache (Linux)

/var/www/html/, /var/www/, /srv/http/

Nginx (Linux)

/var/www/html/, /usr/share/nginx/html/

IIS (Windows)

C:\inetpub\wwwroot\

XAMPP

/xampp/htdocs/, C:\xampp\htdocs\


Blind SQL Injection

Boolean-Based

Time-Based


Second-Order SQL Injection

Payload stored in database, executed later in different query.

Example: Register with username admin'-- -, later displayed/used in vulnerable query.


WAF Bypass Techniques

Case Manipulation

Comment Injection

URL Encoding

Double URL Encoding

Whitespace Alternatives

Operator Alternatives

UNION SELECT Bypass Strings

Hex Spacing for Gaps


MSSQL Specific

Version

Current User

Databases

Tables

Enable xp_cmdshell (RCE)

MSSQL Blind Exploitation


PostgreSQL Specific

Version

Current User

Databases

Tables

File Read

Command Execution


Oracle Specific

Version

Current User

Tables

Columns


Remote MySQL Connection

After Connection

Change WordPress Password


Output Format Fix

When SQL output is messy in terminal:


SQLMap

See dedicated page: SQLMap Guide

Quick commands:


Resources

Last updated