SQL Injection

SQLi is a common web application vulnerability that is caused by unsanitized user input being inserted into SQL queries.

Automatization with sqlmap

# Post
sqlmap -r request.txt -p username

# Get
sqlmap -u "http://example.com/index.php?id=1" -p id

# Crawl
sqlmap -u http://example.com --dbms=mysql --crawl=3

Note: request.txt is a request saved in BurpSuite.

Dumping a Table

sqlmap -r request.txt -p username -D database_name -T table_name --dump

Union Attack

When an application is vulnerable to SQL injection and the results of the query are returned within the application's responses, the UNION keyword can be used to retrieve data from other tables within the database.

MySQL syntax for the example:

$sql = "SELECT id, name, text FROM example WHERE id=" . $_GET['id'];

Column Number Enumeration

After detect that the application is vulnerable to SQLi we need to know how many columns are queried. To do that task we are going to use order by to guess the number of columns retrieved. The idea is to increment the number until get an error.

Output Layout

Now that we know how many columns are in the table, we can use this information to retrieve information. But we need to before understand where this information will be displayed, so we are going to set parameteres to that fields.

Extracting Data from Database

Now knowing that the third column is for descriptions, we can put there all information.

Read files

Some databases allows us to read or write files in the filesystem.

From SQLi to RCE

Since we are allowed to upload files, we can upload a webshell to the web root.

In case of exploiting a Microsoft SQL Server check this:

PORT 1433/tcp - Microsoft SQL Server

Login Bypass

The most classic ones:

Then others:

Knowing the username

When we are aware of some username we can impersonate him with SQLi by introducing the username and commenting the rest of the SQL Query.

Error Based SQLi

Use CONVERT or CAST to force an ERROR and see the output of the query on errors logs.

Example of Microsoft SQL Server:

Blind SQLi

A SQLi is blind because we don't have access to the error log or any type of output which difficult a lot the process of exploitation.

Time Based

Since we are not aware about any type of error or output we can use sleeps.

If it loads for four seconds extra we know that the database is processing our sleep() command.

Dump tables

It can also be done with sqlmap or manually with a custom script. In that case the script is dumping MD5 hashes from password field.

Note: MD5 hash are hexadecimal with 33 character length.

References

Last updated

Was this helpful?