Module 9: SQL Injection

Introduction to SQL Injection

What is SQL Injection?

Unsanitized user input inserrted into queries and passed to the database for execution.

Testing for SQL Injection

String Delimiters

Example payload escaping our closing out a string delimiter

' or 1=1 -- //

Closing Out Strings and Functions

Example payload escaping or closing out a function call

foo') or 1=1 -- //

Sorting

Use something like Burp Suite to modify the request parameters, ex. sort by ID being change to a column number.

Boundary Testing

Submit values that may fall outside the scope of a limited data set. Previous example of submitting an invalid column number to receive an error could be considered as a form of Boundary Testing.

Fuzzing

Capturing POST request to /api/intro in Burp Suite HTTP history

Using wfuzz to identify SQL injection

Using Repeater to test a payload
Using Repeater to exploit SQL injection

Exploiting SQL Injection

Error-based Payloads

Use errors to extract useful information.

Error-based SQL injection in Microsoft SQL Server
Error-based SQL injection using ExtractValue() in MySQL

Sample Oracle blind SQL injection payload

Using Offset to query our database name

Identifying our schema

Querying the flag

UNION-based Payloads

Using a UNION to combine two SELECT statements
Executing a UNION-based SQL injection payload

Stacked Queries

Executing more than one query at a time.

Example stacked query

SQL syntax for an INSERT statement

Results of submitting a stacked query to insert data
Querying the users table to verify our payload worked

Reading and Writing Files

SQL payload to create a new table, copy /etc/passwd into the table, and return the table's content

The table can then be deleted with drop table tmp;

Using the pg_read_file() function to access /etc/passwd
Verifying the secure_file_priv variable is set in our sandbox application
Writing a file using MySQL INTO OUTFILE
Reading a file using MySQL LOAD_FILE()

Remote Code Execution

Commands to enable xp_cmdshell()

Using xp_cmdshell to run a command

Extra Miles

Extra lab.

Database dumping with Automated Tools

SQLMap

Search results on /sqlmap
Baseline request and response that we will provide to sqlmap

Sample sqlmap usage

sqlmap identified a vulnerable parameter

sqlmap results

Excerpt of using sqlmap to dump a database

Case Study: Error-based SQLi in Piwigo

Accessing Piwigo

Start the VPN, the VM, and add its IP to your hosts file.

Discovering the Vulnerable Parameter

Piwigo index page
Piwigo admin section
HTTP request and response for /admin/user_list_backend.php
Sending the POST body to Decoder
Using Decoder to URL-decode the POST body

Excerpt of the decoded POST body to /admin/user_list_backend.php

Sending the POST request to Repeater

Placing the quotes in the POST body

Hacking attempt

Verbose erro rmessage from Piwigo

Updated payload and response in Burp Suite Repeater

Exploiting Error-based SQL Injection

The group_concat() function is unique to MySQL. Current versions of Microsoft SQL Server and PostgreSQL have a very similar STRING_AGG() function. Additionally, current versions of Oracle DB have a LISTAGG() function that is similar to the STRING_AGG() functions.

Using ExtractValue() with group_concat()

Updating our payload in Repeater

Error message containing the database schemas

Updated payload to extract

Error message with table names

Updated payload with LIMIT and OFFSET values

Request and Response identifyin the piwigo_users table

Payload to extract column names for piwigo_users table

Error message identifying the columns of the piwigo_users table

Microsoft SQL Server has a nearly identical SUBSTRING() function and Oracle DB has a SUBSTR() function that takes the same parameters. PostgreSQL has two different functions for substrings. The MySQL SUBSTRING() function follows the same parameter format as the SUBSTR() function. The SUBSTRING() function must include a from or for keyword in the function call.

Payload to extract password values

Repeater updated with new payload

Extracting a partial password hash

Last updated