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

Using wfuzz to identify SQL injection


Exploiting SQL Injection
Error-based Payloads
Use errors to extract useful information.


Sample Oracle blind SQL injection payload
Using Offset to query our database name
Identifying our schema
Querying the flag
UNION-based Payloads


Stacked Queries
Executing more than one query at a time.
Example stacked query
SQL syntax for an INSERT statement


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




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


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





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

Placing the quotes in the POST body
Hacking attempt
Verbose erro rmessage from Piwigo

Exploiting Error-based SQL Injection
Using ExtractValue() with group_concat()

Error message containing the database schemas
Updated payload to extract
Error message with table names
Updated payload with LIMIT and OFFSET values

Payload to extract column names for piwigo_users table
Error message identifying the columns of the piwigo_users table
Payload to extract password values

Extracting a partial password hash
Last updated