SQL Injection = Extracting data using SQL flaws
1. Types of SQL Injection
1.1 UNION-based SQLi
-
Used when query results are displayed on the screen.
-
Attacker injects a UNION SELECT
to retrieve additional data.
Used when query results are displayed on the screen.
Attacker injects a UNION SELECT
to retrieve additional data.
1.2 Error-Based SQLi
-
Used when SQL errors are shown on the page.
-
Errors are triggered intentionally to extract database information.
Used when SQL errors are shown on the page.
Errors are triggered intentionally to extract database information.
1.3 Blind SQLi
-
Works when the application does not display query results or errors.
-
The attacker relies on behavioral differences (like page content or response time).
Works when the application does not display query results or errors.
The attacker relies on behavioral differences (like page content or response time).
Depending on the situation, one of the three methods is chosen.
2. The Goal of SQL Injection
To execute your own SELECT queries on the server and extract sensitive data.
3. How to Identify SQL Injection Points
Step-by-step:
-
Find inputs that are sent to the server (text fields, cookies, headers, etc.)
-
Test with a payload like:
-
If the results differ: the input is vulnerable.
-
If results are identical: test further using variations.
Find inputs that are sent to the server (text fields, cookies, headers, etc.)
Test with a payload like:
-
If the results differ: the input is vulnerable.
-
If results are identical: test further using variations.
Check parameters in:
-
Form fields
-
URL query strings
-
HTTP headers (e.g. User-Agent)
-
Cookies
Always consider how the server constructs its SQL queries.
Don't inject blindly — think about the query logic first.
4. Real-world Injection Examples
Cookie-based Injection Example
A forum post listing shows:
Test with:
Result differs → SQLi confirmed.
Column Name Injection
-
Use this to guess how many columns exist.
Use this to guess how many columns exist.
You can also inject:
Or even:
To trigger an error or test visibility of injected data.
5. SQLi via ORDER BY clause
Some parameters may be used in ORDER BY
clauses, allowing injection if not sanitized properly.
6. SQL Injection Prevention Methods
6.1 Prepared Statements
-
Use placeholders (e.g., ?
) to pre-compile SQL queries.
-
Protects against injection by separating data from code.
Use placeholders (e.g., ?
) to pre-compile SQL queries.
Protects against injection by separating data from code.
Note:
-
Can't always be used with
ORDER BY
, table names, or column names.
6.2 Whitelisting (Allow-List Filtering)
-
Only allow approved input values.
-
Safer than blacklisting forbidden characters or words.
Only allow approved input values.
Safer than blacklisting forbidden characters or words.
7. SQL Injection Cheat Sheet Summary
1. UNION-based SQLi
-
Combine queries using UNION
.
-
Determine number of columns using ORDER BY
.
-
Match data types before injecting.
Combine queries using UNION
.
Determine number of columns using ORDER BY
.
Match data types before injecting.
Example:
2. Error-Based SQLi
-
Trigger type or logic errors to reveal system details.
-
Works only if DB error messages are shown to the user.
Trigger type or logic errors to reveal system details.
Works only if DB error messages are shown to the user.
Example:
3. Blind SQLi
A. Boolean-based Blind SQLi
-
Observe content changes on the page.
Observe content changes on the page.
Example:
B. Time-based Blind SQLi
-
Inject SLEEP()
or WAITFOR DELAY
to observe response time.
Inject SLEEP()
or WAITFOR DELAY
to observe response time.
Example:
댓글 없음:
댓글 쓰기