SQL Injection for HI-TEC Conference (July, 2013)

There are four parts to this project:
  1. Getting Kali started
  2. Constructing a vulnerable Web application based on SQlol
  3. Stealing the data with a Windows system running the Havij attack tool
  4. Fixing the SQL injection vulnerability with input validation

Warning

This is the tool LulzSec and Anonymous use to earn long prison terms. It's so easy it will make you sick. ONLY SCAN SYSTEMS YOU HAVE PERMISSION TO ATTACK.

Getting Kali Started

Configuring and Starting MySQL

In a Terminal window, enter this command:
dpkg-reconfigure mysql-server-5.5
A box asks you for the "New password".

Type in a password of

password
and press Enter

In the next box, type in a password of

password
and press Enter.
Warning: this is obviously an insecure password, not apropriate for use on a production system.
In the Terminal window, enter this command:
service mysql start
MySQL starts, as shown below:

Restarting Apache

In a Terminal window, enter this command:
service apache2 restart

Downloading and Installing SQLol

The original SQlol software was developed by spiderlabs. I modified it slightly and put a copy on my Web server--that's the version that makes this project easiest.

In your Linux machine, in a Terminal window, enter these commands:

cd /var/www

rm sqlol-sam.tar.gz

rm -R sqlol

wget http://samsclass.info/123/proj10/sqlol-sam-old3.tar.gz

tar xzf sqlol-sam-old3.tar.gz

In your Linux machine, from the menu bar, click Applications, Internet, "IceWeasel Web Browser".

In IceWeasel, go to

localhost/sqlol/simple.php

SQLol opens, as shown below:

Click RESET

A message appears saying "Done!"

If you see the "Could not connect..." message shown below, restart mySQL with this command:

service mysql restart

Using the "Find Users" Page

SQLol is too complex for Havij to exploit in its original state, so I added a simplified "Find Users" page.

In your Linux machine, in IceWeasel, go to

localhost/sqlol/search.htm

A "Find Users" page opens, as shown below:

In the Name field, type

C%
Click the Submit button.

You should see the username "Chunk MacRunfast", as shown below:

In IceWeasel, click the Back button to return to the "Find Users" page.

In the Name field, type

%
Click the Submit button.

You should see all five usernames, as shown below:

Breaking the "Find Users" Page

Apparently the designers of this site don't regard usernames as confidential. But the database also contains social security numbers, and those really are confidential.

The whole point of SQL injection is that a simple form like this can be tricked into revealing more data than the designer intended to reveal.

In IceWeasel, click the Back button to return to the "Find Users" page.

In the Name field, type

O'Neil
Click the Submit button.

You should see an error message, as shown below:

This is the kiss of death--an error like this means that you have lost control of the database, and an attacker can often extract all the data.

We'll steal the data with Havij, which is absurdly easy to use.

Getting the IP Address of the Linux Machine

On your Linux machine, in a Terminal window, enter this command:
ifconfig
Record the IP Address of your Linux machine.

If you are using a virtual machine, it's best to use "Bridged" networking rather than NAT.

Starting the Windows Machine

Start your Windows machine. In a Command prompt, PING the IP address of your Linux machine. You should get replies. If you do not, you need to troubleshoot your networking before proceeding further.

Viewing the "Find Users" Page from the Windows Machine

On your Windows machine, open a Web browser and go to

http://192.168.198.175/sqlol/search.htm

Replace 192.168.198.136 with the correct IP address of your Linux machine.

A "Find Users" page opens, as shown below:

In the Name field, type

x
Click the Submit button.

You should see the results page, as shown below:

No users were found, but that's not a problem. What we really want is the URL of this page, as shown in the address bar:

http://192.168.198.175/sqlol/search.php?q=x
You don't need to use the form to perform a search. You can do it by inserting the search parameter directly into the URL.

On the Windows machine, in the URL bar, add an apostrophe to the end of the URL, so it becomes this:

http://192.168.198.175/sqlol/search.php?q=x'
Press the Enter key.

A SQL error message appears, as shown below:

This URL is the attack point Havij needs: a URL with a vulnerable parameter.

Installing Havij

Havij is a free and powerful SQL Injection attack tool.

On your Windows machine, in a Web browser, go to:

http://itsecteam.com/products/havij-advanced-sql-injection/

Look in the middle of the screen, and in the "Havij Advanced SQL Injection" section, click the "Download" tab.

Click "Havij v1.15 Free"

After the file downloads, extract it with 7-zip. If you don't have 7-zip, get it here

Install Havij with the default options.

If it doesn't launch automatically, click Start, type Havij into the Search box, and run Havij. It does not require Administrator privileges.

Using Havij to Steal Data

In the Havij window, paste this URL into the Target field, using the correct IP address of your Linux machine:

http://192.168.198.175/sqlol/search.php?q=x

In the Havij window, click the Analyze button.

The Log at the bottom of the Havij window should show "Current DB: sqlol", as shown below:

In the upper center of the Havij window, click the Tables button.

In the lower bar that appears, click the "Get Tables" button.

The tables "ssn" and "users" appear, as shown below:

In the left-center pane of the Havij window, check the ssn and users boxes.

In the center of the Havij window, click the "Get Columns" button.

The column names appear, as shown below:

In the left-center pane of the Havij window, in the "ssn" table section, check the name and ssn boxes.

In the center of the Havij window, click the "Get Data" button.

The names and ssns appear, as shown below:

Fixing the Vulnerability with Input Validation

Testing the "Find Users" Page

In your Linux machine, in IceWeasel, go to

localhost/sqlol/search.htm

A "Find Users" page opens, as shown below:

In the Name field, type

O'Neil
Click the Submit button.

You should see a SQL syntax error, as shown below:

This error indicates a SQL injection vulnerability.

Fixing the SQL Injection Vulnerability

In a Terminal window, execute these commands:
cd /var/www/sqlol

cp search.php old-search.php

nano search.php

Find the existing code marked with a comment saying "PATCH VULNERABLE CODE HERE", as shown below:

Add this line under the comment:

$q = mysql_real_escape_string($q);
Your code should now look like the image shown below:

Save the page with Ctrl+X, Y, Enter.

This line adds backslash characters before quotation marks, which will prevent SQL syntax errors.

Testing the Fixed Page

In your Linux machine, in IceWeasel, go to

localhost/sqlol/search.htm

A "Find Users" page opens, as shown below:

In the Name field, type

O'Neil
Click the Submit button.

Now, instead of an error, you see a chart showing that no results were found, as shown below:

You can see how the fix works: the URL shows the original search string of O'Neil but the top of the results Web page shows the escaped version O\'Neil

Using Havij Again

In the Havij window, if necessary, paste this URL into the Target field, using the correct IP address of your Linux machine:

http://192.168.198.175/sqlol/search.php?q=x

In the Havij window, click the Analyze button.

The Log at the bottom of the Havij window shows red error messages, as shown below, because the site is no longer vulnerable.


Source

https://github.com/SpiderLabs/SQLol

Last modified: 7:44 am 7-21-13