Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

[Tutorial] Using LOAD_FILE with Time Based MySQL Injection [HQ!]

Today I'm going to be going over using LOAD_FILE with Time Based Blind SQL Injection. This method may look advanced, but if you take a look at my tutorials bit by bit the pieces will come together.

First off, Time Based Blind MySQL injection is a painful method, and very time consuming. However, if you're running out of options, this may be a chance to further exploit your vulnerable website.
Now first off, time based should only be used when there is NO other option available. Blind MySQL injection is slow enough, but TIME BASED...even worse.

Now skipping all the other options using MySQL Injection (digging for login info, into outfile, etc), we're going to jump into LOAD_FILE to get the tutorial started.

LOAD_FILE does what it sounds like, loads a file. Ex: on linux. Here's an example of a UNION based attack using LOAD_FILE.

Spoiler (Click to View)

If magic_quotes_gpc = On, then convert your file name to HEX.

Spoiler (Click to View)

Using TIME BASED attack:

Spoiler (Click to View)

Now my page takes 3 seconds to load, this confirms I can use a time based attack.

Now in order to use LOAD_FILE, you need file permissions. Here's an example of how to check if the MySQL user you're running has proper privileges.

Spoiler (Click to View)

If magic_quotes_gpc = On, convert to HEX.

Spoiler (Click to View)

Now my site takes an extra 3 seconds to load, which means the query returned a true value, meaning I have file privileges.

Now that we know I can use LOAD_FILE, let's get some FPD (Full Path Disclosure) to get the full website path (which we will use to guess configuration files).
Let's try converting our parameter to an array, in order the force a PHP error.

Spoiler (Click to View)

Now that we got the full site path, we're going to be using a function in SQL known as ISNULL(), to check whether the file we're looking for exists.
As most programmers know, ! is a sign for NOT, so !isnull is the same is NOT null. First, let's try using a known file. Let's take a look at our HTML source.

Spoiler (Click to View)

Take a look at the HREF attribute, which specifies a destination link. It's referring to a file called "index.css". Now let's combine this with our full path, and check if the file exists using !isnull.

Spoiler (Click to View)

If magic_quotes_gpc = On

Spoiler (Click to View)

Success! Our page took an extra 3 seconds to send the request, meaning our file we're attempting to load is NOT NULL (meaning it exists).
Now that we know how to check if files exist, let's try digging up some MySQL configuration files 😏

Here's a few common configuration file names.

Code
connect.php
mysql.php
config.inc.php
config.php

Now this is my local server, I know the configuration file name. So to save time, let's try guessing one that DOES NOT exist, then one that EXISTS.

Spoiler (Click to View)

If magic_quotes_gpc = On

Spoiler (Click to View)

The page loads instantly, meaning our query returned false, and the file we're trying to load is NULL (does not exist).

Now, let's try guessing the config file name (in this case, config.php).

Spoiler (Click to View)

If magic_quotes_gpc = On

Spoiler (Click to View)

Our page takes 3 seconds to send the request, meaning the file we're loading (config.php) EXISTS!!

Now let's try pulling some data from it. As you may know, most PHP files start with "<?php". Let's try seeing if this is true using a combination of a TIME BASED attack, SUBSTRING, and LOAD_FILE.

Spoiler (Click to View)

If magic_quotes_gpc = On

Spoiler (Click to View)

The page takes 3 seconds to send the request, meaning this query returns true. This says that the first 5 characters in config.php are '<?php'

Now we can dig up some of that login info using Blind MySQL injection. I hope you guys learned something new, enjoy!

Resources:

Time Based Blind SQLi using SLEEP()
[Detailed] Boolean Based Blind Injection [Tutorial]
[Behind The Scenes] Boolean Based Blind Injection (Blind SQLi)
Mega Webhacking Tutorials By Me
✌
good job this is a very HQ tutorial as always from you thank you so much :D
Thanks for the reply. Any other feedback is appreciated.
IFNULL works too! here is an example
Thank you OP! I am sure I will use this at one point or another.
Great tuts as always DownFall, thanks for sharing.
(Jun 7, 2014 04:59 AM)Tea. Wrote:  I'm not trying to be disrespectful or anything, I just started sqli yesterday and barely did any research, but I think that IFNULL is a better function. The syntax is much easier for this type of situation.

IFNULL(expression_1,expression_2)
The IFNULL function returns expression_1 if expression_1 is not NULL; otherwise it returns expression_2. The IFNULL function returns a string or a numeric based on the context where it is used.

so you would just do SELECT IFNULL(load_file('../configs.txt',sleep(3)))
here is an example I think

Either way, thank you for the tutorial it is really cool and you have been very helpfull my first few days!

Thanks for the input mate, however ~ they both essentially do the job, so I see neither as a "better" option. I'd label it as a personal preference, use whichever one you find more convenient.

Here's an example using IFNULL().

PHP Code

<?php 
http
://127.0.0.1/PHP/index.php
?id=1' AND (SELECT IFNULL(LOAD_FILE('C:/xampp1/htdocs/PHP/config.php'),SLEEP(5)))-- x 
This is probably one of the most important threads on HF and it got thrown away to the third page...
man how we can read all the file config ?!! its imossible how we can guess all the file ?
(Jun 9, 2014 02:14 AM)dotcppfile Wrote:  This is probably one of the most important threads on HF and it got thrown away to the third page...

It's always the best of threads, that usually do :3

(Jun 9, 2014 08:20 AM)JACK.HAMMER Wrote:  man how we can read all the file config ?!! its imossible how we can guess all the file ?

You guess character by character using SUBSTRING. Look at the tutorials I linked at the bottom where it says resources, study Blind MySQL injection, and you'll understand.
(Jun 9, 2014 02:14 AM)dotcppfile Wrote:  This is probably one of the most important threads on HF and it got thrown away to the third page...

How often have you had the need to use load_file in conjunction with a time based MySQL injection vulnerability?

Just wondering.
(Jun 9, 2014 12:40 PM)wowrude Wrote:  How often have you had the need to use load_file in conjunction with a time based MySQL injection vulnerability?

Just wondering.

I know you're job in this forum is to act smart and rude Lol but I would use Load_File whenever I can since it's one of the most important functions, don't you think?
(Jun 9, 2014 12:42 PM)dotcppfile Wrote:  I know you're job in this forum is to act smart and rude Lol but I would use Load_File whenever I can since it's one of the most important functions, don't you think?

Well if you totally disregard the fact that it's only available while the mysql user is root or dba (aka virtually never), I guess you could call it important. In that case I would argue that it would be more imperative to be able to correctly use INTO OUTFILE with various types of SQLi rather than load_file.
Nice. Been looking for a nice tutorial on both load_file and time based.
(Jun 9, 2014 12:40 PM)wowrude Wrote:  How often have you had the need to use load_file in conjunction with a time based MySQL injection vulnerability?

Just wondering.

I'll admit, it's not very common given the circumstances but it's something new here.

(Jun 9, 2014 12:54 PM)Ρain Wrote:  Nice. Been looking for a nice tutorial on both load_file and time based.

Thanks for the feedback mate, I'm glad you'll be using this.
(Jun 9, 2014 12:54 PM)wowrude Wrote:  Well if you totally disregard the fact that it's only available while the mysql user is root or dba (aka virtually never), I guess you could call it important. In that case I would argue that it would be more imperative to be able to correctly use INTO OUTFILE with various types of SQLi rather than load_file.

I never did that, I did say that I will use it whenever I can and as long as anyone around here have the privileges to use LOAD_FILE then they should because they can do whatever they want with it. I donno what's your point, everyone knows that we can't always use it so please try to say something useful...


Possibly Related Threads...
Thread / Author Replies Views Last Post
by cyberdocno1Apr 29, 2025 08:50 PM 7 380 Apr 29, 2025 08:50 PM
Last Post: franticll
by BeggFoMercyOct 13, 2020 08:12 AM 6 286 Oct 13, 2020 08:12 AM
Last Post: satoshikilla
by Error667Aug 10, 2020 10:13 PM 16 1,551 Aug 10, 2020 10:13 PM
Last Post: johnperter