3306 mariaDB - mySQL
Description
MySQL: MySQL is an open-source relational database management system(RDBMS) based on Structured Query Language (SQL). It is developed and managed by oracle corporation and initially released on 23 may, 1995. It is widely being used in many small and large scale industrial applications and capable of handling a large volume of data. After the acquisition of MySQL by Oracle, some issues happened with the usage of the database and hence MariaDB was developed.
MariaDB: MariaDB is an open source relational database management system (RDBMS) that is a compatible drop-in replacement for the widely used MySQL database technology. It is developed by MariaDB Foundation and initially released on 29 October 2009. MariaDB has a significantly high number of new features, which makes it better in terms of performance and user-orientation than MySQL.
Connect to database: mariadb
Connect to database: mysql
From Linux
sqsh
mssqlclient.py from impacket
If we can guess or gain access to credentials, this allows us to remotely connect to the MSSQL server and start interacting with databases using T-SQL (Transact-SQL
). Authenticating with MSSQL will enable us to interact directly with databases through the SQL Database Engine. From Pwnbox or a personal attack host, we can use Impacket's mssqlclient.py to connect as seen in the output below. Once connected to the server, it may be good to get a lay of the land and list the databases present on the system.
From windows
mysql.exe
sqlcmd
Mariadb basic commands
mysql basic commands
See mysql.
Enumeration queries
Command execution
Upload a shell
Take a wordpress installation that uses a mysql database. If you manage to login into the mysql panel (/phpmyadmin) as root then you could upload a php shell to the /wp-content/uploads/ folder.
Another example:
To be able to write files to the back-end server using a MySQL database, we require three things:
1. User with FILE
privilege enabled. If our user is root:
2. MySQL global secure_file_priv
variable not enabled
The secure_file_priv variable is used to determine where to read/write files from. MariaDB has this variable set to empty by default, which lets us read/write to any file if the user has the FILE
privilege. However, MySQL
uses /var/lib/mysql-files
as the default folder. This means that reading files through a MySQL
injection isn't possible with default settings.
3. Write access to the location we want to write to on the back-end server. The SELECT INTO OUTFILE statement can be used to write data from select queries into files. This is usually used for exporting data from tables.
Tip: Advanced file exports utilize the 'FROM_BASE64("base64_data")' function in order to be able to write long/advanced files, including binary data.
Now, uploading a shell. This is a PHP shell:
Let's replicate the UNION injection attack:
This can be verified by browsing to the /shell.php
file and executing commands via the 0
parameter, with ?0=id
in our URL:
Writing files
MySQL
supports User Defined Functions which allows us to execute C/C++ code as a function within SQL, there's one User Defined Function for command execution in this GitHub repository.
MySQL
does not have a stored procedure like xp_cmdshell
, but we can achieve command execution if we write to a location in the file system that can execute our commands. So basically, we need to check if we have enough privileges to do so.
In MySQL, a global system variable secure_file_priv limits the effect of data import and export operations, such as those performed by the LOAD DATA and SELECT … INTO OUTFILE statements and the LOAD_FILE() function.
If secure_file_priv
is set as:
- set to NUL: the server disables import and export operations. We can't do anything.
- set to the name of a directory: the server limits import and export operations to work only with files in that directory. The directory must exist; the server does not create it.
- is empty: the variable has no effect, which is not a secure setting.
Example:
Now, as for demo purposes, let's imagine that MySQL
operates on a PHP-based web server or other programming languages like ASP.NET, having the appropriate privileges, we will attempt to write a file using SELECT INTO OUTFILE in the webserver directory.
Reading files
MySQL - Read Local Files in MySQL
If permissions allows it: