MySQL LOAD_FILE() and INTO OUTFILE() Sql Injection

Ismail Tasdelen
InfoSec Write-ups
Published in
3 min readJan 19, 2023

--

Photo by Sara Bakhshi on Unsplash

In this article, we will discuss the MySQL LOAD_FILE() function and the INTO OUTFILE() function and Sql Injection. MySQL provides the LOAD_FILE() and INTO OUTFILE() functions that can be used to read or write files on the file system of the server where the MySQL database is running. These functions can be vulnerable to SQL injection attacks if user input is not properly sanitized.

An attacker could use SQL injection to modify the file path passed to the LOAD_FILE() or INTO OUTFILE() function to access or write to unauthorized files on the server. For example, an attacker could use SQL injection to read sensitive files on the server or to write malicious code to the server, potentially leading to further compromise of the system.

To prevent SQL injection attacks when using these functions, it’s important to properly validate and sanitize any user input used as a parameter in the function. Additionally, it’s also a good practice to use the least privileged MySQL user account when connecting to the database, as this will limit the potential damage that an attacker can do if they are able to successfully inject malicious SQL.

<?php
$file_path = $_GET['file'];
$sql = "SELECT * FROM users WHERE profile_picture = LOAD_FILE('" . $file_path . "')";
$result = mysqli_query($connection, $sql);
?>

This code assumes that the file path passed to the script via the ‘file’ GET parameter is the path to an image file on the server that should be used as the profile picture for a user. However, because the code doesn’t properly validate or sanitize the file path, an attacker could use SQL injection to modify the file path passed in the GET parameter to read any file on the server. For example, an attacker could use a file path of “../../../etc/passwd” to read the password file on a Linux server.

A sample of vulnerable PHP code that uses the MySQL INTO OUTFILE() function:

<?php
$file_path = $_POST['file'];
$content = $_POST['content'];
$sql = "SELECT '".$content."' INTO OUTFILE '" . $file_path . "'";
$result = mysqli_query($connection, $sql);
?>

This code assumes that the file path passed to the script via the ‘file’ POST parameter is the path to the file on the server where the content passed via ‘content’ POST parameter should be written. However, because the code doesn’t properly validate or sanitize the file path, an attacker could use SQL injection to modify the file path passed in the POST parameter to write any file on the server. For example, an attacker could use a file path of “/var/www/html/backdoor.php” to write a file that contains a malicious script in the web root directory of the server.

Kermit

In this post, we have discussed MySQL LOAD_FILE() function and INTO OUTFILE() function and Sql Injection, see you in the next post, take care.

--

--

I'm Ismail Tasdelen. I have been working in the cyber security industry for +7 years. Don't forget to follow and applaud to support my content.