02.03.2018 Views

Sybex CEH Certified Ethical Hacker Version 8 Study Guide

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

338 Chapter 14 ■ SQL Injection<br />

If this code returns any result other than an error, then increment the number after the<br />

order by statement by 1 (or some other amount if desired) until an error is returned. When<br />

an error is encountered, it indicates that the last entry that did not return an error is the<br />

number of columns in the database.<br />

Once the columns have been determined, you can establish whether you can make<br />

queries against the system. Do so by performing what is known as a union select on the<br />

system by appending it to the end of the URL:<br />

http://www.somesite.com/default.php?id=-1 union select 1,2,3,4,5,6,7,8<br />

Take a close look at this statement. This statement assumes that you discovered that<br />

there were eight columns in the database in your previous step. If more or fewer were<br />

encountered, you would adjust the numbers after the select accordingly. Also note that you<br />

add a hyphen after the = sign and before the number 1 (after the id).<br />

Once the results of this query are returned, you will see that column numbers are<br />

returned. The numbers that are returned indicate that queries are accepted against these<br />

columns, and you can now inject further refined SQL statements into each.<br />

You can now start doing some interesting tasks. Let’s begin by identifying the SQL<br />

version that is in use. To do this, you will use the command @@version or version() to<br />

extract the version information from the database. You will target one of the columns that<br />

accept SQL queries. In our example, let’s use column 3:<br />

http://www.somesite.com/default.php?id=-1 union select 1,2,@@version,4,5,6<br />

The version information returned will replace the @@version. Depending on the<br />

database version being returned, you can determine the next stage of the attack. In our<br />

example here, let’s assume the version returned is correct for our next step.<br />

This example assumes that the database in use is MySQL and that the version<br />

is at least version 5. If another version or brand of database is in use,<br />

then be sure to tailor the attack to that environment.<br />

With the version information checking out, you can do something even more interesting.<br />

You can obtain a list of the databases present on the system by executing the following<br />

command:<br />

http://www.somesite.com/default.php?id=-1 union select ~CA<br />

1,2,group_concat(schema_name),4,5,6 from information_schema.schemata--<br />

To determine the current database:<br />

http://www.somesite.com/default.php?id=-1 union select ~CA<br />

1,2,concat(database()),4,5,6--<br />

To get the current user:<br />

http://www.somesite.com/default.php?id=-1 union select ~CA<br />

1,2,concat(user()),4,5,6--

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!