On this page 27 sections
- What MySQL is
- Default port
- Initial enumeration
- Useful Nmap scripts
- Connect with the MySQL client
- Basic database enumeration
- Select a database
- Enumerate tables
- Inspect table structure
- Query table data
- INFORMATION_SCHEMA
- Current privileges
- Enumerate MySQL users
- Users and allowed hosts
- secure_file_priv
- FILE privilege
- Read files with LOAD_FILE
- LOAD DATA INFILE
- MySQL authentication testing
- Metasploit MySQL version
- Metasploit MySQL login
- Metasploit MySQL enumeration
- Execute SQL with Metasploit
- Schema dump with Metasploit
- MySQL service vs SQL injection
- Enumeration workflow
- Quick reference
What MySQL is
MySQL is a relational database management system that uses SQL to store, retrieve and manage structured data. Applications commonly use MySQL to store users, configuration, application data and authentication information.
Default port
MySQL normally listens on TCP port 3306. A remotely reachable MySQL service should be enumerated independently from any web application using the database.
Initial enumeration
Start by identifying whether MySQL is reachable and determining the database server version.
nmap -sV -p 3306 <TARGET>nmap -sC -sV -p 3306 <TARGET>Useful Nmap scripts
Nmap includes NSE scripts that can collect MySQL server information and capabilities.
nmap -p 3306 --script mysql-info <TARGET>ls /usr/share/nmap/scripts/mysql*Connect with the MySQL client
The mysql command-line client can connect to remote MySQL servers when valid credentials and network access are available.
mysql -h <TARGET> -u <USERNAME> -pmysql -h <TARGET> -P <PORT> -u <USERNAME> -pBasic database enumeration
Once authenticated, begin by identifying the current user, server version and available databases.
SELECT USER();SELECT VERSION();SHOW DATABASES;Select a database
USE changes the active database for subsequent queries.
USE <DATABASE>;Enumerate tables
Once a database is selected, SHOW TABLES lists the tables available to the current account.
SHOW TABLES;Inspect table structure
DESCRIBE shows the fields and column types of a table and helps determine what information it stores.
DESCRIBE <TABLE>;Query table data
SELECT retrieves data from tables. During authorised enumeration, focus on information relevant to understanding application configuration and account structure.
SELECT * FROM <TABLE>;SELECT <COLUMN1>, <COLUMN2> FROM <TABLE>;INFORMATION_SCHEMA
INFORMATION_SCHEMA is a system database containing metadata about databases, tables, columns and privileges. It is useful for structured enumeration when direct SHOW commands are insufficient.
SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA;SELECT TABLE_SCHEMA, TABLE_NAME FROM INFORMATION_SCHEMA.TABLES;SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS;Current privileges
SHOW GRANTS displays the privileges assigned to the current authenticated account.
SHOW GRANTS;Enumerate MySQL users
Accounts with sufficient privileges may be able to inspect MySQL user information. Access to the mysql system database varies by version and account permissions.
SELECT user, host FROM mysql.user;Users and allowed hosts
A MySQL account consists of both a username and a host component. The same username may have different privileges depending on the source host from which it connects.
SELECT user, host FROM mysql.user;secure_file_priv
secure_file_priv controls where MySQL is allowed to read or write files using certain SQL file operations. Its value is important when evaluating database file-access capabilities.
SHOW VARIABLES LIKE 'secure_file_priv';FILE privilege
The FILE privilege allows certain server-side file operations. It should be treated as a sensitive database privilege because file operations occur from the MySQL server context.
SHOW GRANTS;Read files with LOAD_FILE
When permissions and MySQL configuration allow it, LOAD_FILE can read a file from the filesystem visible to the MySQL server process.
SELECT LOAD_FILE('/path/to/file');LOAD DATA INFILE
LOAD DATA INFILE imports file content into a table. Its availability depends on MySQL privileges, configuration and filesystem access.
LOAD DATA INFILE '/path/to/file' INTO TABLE <TABLE>;MySQL authentication testing
When credential testing is authorised, discovered usernames and passwords can be validated against the MySQL service. Account lockout behaviour depends on the environment and surrounding controls.
hydra -L <USERLIST> -P <PASSWORDLIST> mysql://<TARGET>Metasploit MySQL version
Metasploit contains several auxiliary modules for MySQL enumeration and authentication testing.
use auxiliary/scanner/mysql/mysql_versionset RHOSTS <TARGET>runMetasploit MySQL login
mysql_login can validate username and password combinations against the database service.
use auxiliary/scanner/mysql/mysql_loginshow optionsMetasploit MySQL enumeration
Once valid credentials are available, Metasploit can perform additional MySQL enumeration.
use auxiliary/admin/mysql/mysql_enumExecute SQL with Metasploit
The mysql_sql module executes an SQL statement using supplied MySQL credentials.
use auxiliary/admin/mysql/mysql_sqlshow optionsSchema dump with Metasploit
The schema-dump module can collect database schema information when the authenticated account has sufficient access.
use auxiliary/scanner/mysql/mysql_schemadumpMySQL service vs SQL injection
An exposed MySQL service and SQL injection are different attack surfaces. MySQL enumeration targets the database service directly, normally on port 3306. SQL injection is an application vulnerability where user-controlled input modifies a backend SQL query.
Enumeration workflow
Identify the MySQL service and version, obtain or validate credentials when authorised, enumerate databases and tables, inspect INFORMATION_SCHEMA, identify users and privileges, and then review sensitive capabilities such as FILE and secure_file_priv.
nmap -sC -sV -p 3306 <TARGET>mysql -h <TARGET> -u <USERNAME> -pSHOW DATABASES;SHOW GRANTS;Quick reference
These are the MySQL enumeration commands worth remembering.
nmap -sC -sV -p 3306 <TARGET>mysql -h <TARGET> -u <USERNAME> -pSHOW DATABASES;SHOW TABLES;SELECT USER();SELECT VERSION();SHOW GRANTS;SHOW VARIABLES LIKE 'secure_file_priv';use auxiliary/scanner/mysql/mysql_versionuse auxiliary/scanner/mysql/mysql_login