MySQL Injection
Pentesting-Web
MYSQL comment
MYSQL Union Based
Detect columns number
First you need to know the number of columns
Using order by
or group by
Keep incrementing the number until you get a False response. Even though GROUP BY and ORDER BY have different funcionality in SQL, they both can be used in the exact same fashion to determine the number of columns in the query.
or
Using order by
or group by
Error Based
Similar to the previous method, we can check the number of columns with 1 request if error showing is enabled.
or
Using UNION SELECT
Error Based
This method works if error showing is enabled
Using LIMIT INTO
Error Based
This method works if error showing is enabled.
It is useful for finding the number of columns when the injection point is after a LIMIT clause.
Using SELECT * FROM SOME_EXISTING_TABLE
Error Based
This works if you know the table name you're after and error showing is enabled.
It will return the amount of columns in the table, not the query.
Extract database with information_schema
Then the following codes will extract the databases'name, tables'name, columns'name.
Extract columns name without information_schema
Method for MySQL >= 4.1
.
First extract the column number with
Then extract the column name.
Method for MySQL 5
Extract data without columns name
Extracting data from the 4th column without knowing its name.
Injection example inside the query select author_id,title from posts where author_id=[INJECT_HERE]
MYSQL Error Based
MYSQL Error Based - Basic
Works with MySQL >= 4.1
MYSQL Error Based - UpdateXML function
Shorter to read:
MYSQL Error Based - Extractvalue function
Works with MySQL >= 5.1
MYSQL Blind
MYSQL Blind with substring equivalent
MySQL Blind SQL Injection in ORDER BY clause using a binary query and REGEXP
This query basically orders by one column or the other, depending on whether the EXISTS() returns a 1 or not. For the EXISTS() function to return a 1, the REGEXP query needs to match up, this means you can bruteforce blind values character by character and leak data from the database without direct output.
MySQL Blind SQL Injection binary query using REGEXP.
Payload:
Would work in the query (where the "where" clause is the injection point):
In said query, it will check to see if an item exists in the "name" column in the "items" database that starts with an "a". If it will sleep for 3 seconds per item.
MYSQL Blind using a conditional statement
TRUE: if @@version starts with a 5
:
False: if @@version starts with a 4
:
MYSQL Blind with MAKE_SET
MYSQL Blind with LIKE
'_' acts like the regex character '.', use it to speed up your blind testing
MYSQL Time Based
The following SQL codes will delay the output from MySQL.
Using SLEEP in a subselect
Using conditional statements
MYSQL DIOS - Dump in One Shot
MYSQL Current queries
This table can list all operations that DB is performing at the moment.
MYSQL Read content of a file
Need the filepriv
, otherwise you will get the error : ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
If you are root
on the database, you can re-enable the LOAD_FILE
using the following query
MYSQL Write a shell
Into outfile method
Into dumpfile method
MYSQL Truncation
In MYSQL "admin
" and "admin
" are the same. If the username column in the database has a character-limit the rest of the characters are truncated. So if the database has a column-limit of 20 characters and we input a string with 21 characters the last 1 character will be removed.
Payload: username = "admin a"
MYSQL Fast Exploitation
Requirement: MySQL >= 5.7.22
Use json_arrayagg()
instead of group_concat()
which allows less symbols to be displayed
- group_concat() = 1024 symbols
- json_arrayagg() > 16,000,000 symbols
MYSQL UDF command execution
First you need to check if the UDF are installed on the server.
Then you can use functions such as sys_exec
and sys_eval
.
MYSQL Out of band
DNS exfiltration
UNC Path - NTLM hash stealing
References
- MySQL Out of Band Hacking - @OsandaMalith
- [Sqli] Extracting data without knowing columns names - Ahmed Sultan @0x4148
- Help по MySql инъекциям - rdot.org
- SQL Truncation Attack - Warlock
- HackerOne @ajxchapman 50m-ctf writeup - Alex Chapman @ajxchapman
- SQL Wiki - netspi
- ekoparty web_100 - 2016/10/26 - p4-team
- Websec - MySQL - Roberto Salgado - May 29, 2013.