MySQL (3306)
Last updated
Was this helpful?
Last updated
Was this helpful?
MySQL stores information about itself in the database, which is a read-only repository of the metadata of the MySQL database server, providing insights into the structure and organization of the database environment. It contains some useful tables, such as:
-> Information about all databases.
-> Information about all tables.
-> Details about columns in the tables.
We can use the following SELECT
-based queries to enumerate the DBMS via the information_schema
database.
We can connect through Linux via mysql
.
For an example of MySQL usage see .
On the below commands, the comment at the end includes a space: --
! The #
symbol can also be used.
MySQL has a secure_file_priv
system variable that restricts which directories can be used to read
or write
files.
Read files from anywhere.
random_directory
Only read from the specified directory.
NULL
Cannot read/write anywhere.
Thus, we first need to enumerate secure_file_priv
's value.
We can use the INTO OUTFILE '<file>'
clause to write a file (the file location must be writable to the OS user the database software is running as). For example, if the secure_file_priv
is set to the /var/lib/mysql-files
directory, we can only write
a file within that folder.
To read the above file, we can use the LOAD_FILE('<file>')
clause.
Some UNION
-based payload can be found below.
To WRITE files the FILE
privilege is required as well as the secure_file_priv
variable must be disabled. In addition, the user must have write
access to the location we want to write to.
To write a webshell, we must know the webroot. We can find it is by using load_file
to read the server configuration:
Apache
/etc/apache2/apache2.conf
Nginx
/etc/nginx/nginx.conf
ISS
$WinDir%\System32\Inetsrv\Config\ApplicationHost.config
WinRM does not support interactive prompts like mysql
shell normally uses. That means we must use the -e
option to execute SQL statements inline:
For an example of a manual SQLi attack on a MySQL database, see .