<?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=Suei8423</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=Suei8423"/>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php/Special:Contributions/Suei8423"/>
		<updated>2026-04-20T21:37:06Z</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=60573</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=60573"/>
				<updated>2009-05-12T04:49:50Z</updated>
		
		<summary type="html">&lt;p&gt;Suei8423: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:OWASP Testing Guide v3}}&lt;br /&gt;
&lt;br /&gt;
== Brief Summary ==&lt;br /&gt;
&lt;br /&gt;
An [[SQL injection]] attack consists of insertion or &amp;quot;injection&amp;quot; of a SQL query via the input data from the client to the application. A successful SQL injection exploit can read sensitive data from the database, modify database data (Insert/Update/Delete), execute administration operations on the database (such as shutdown the DBMS), recover the content of a given file existing on the DBMS file system and, in some cases, issue commands to the operating system. &lt;br /&gt;
SQL injection attacks are a type of  [[Top 10 2007-Injection Flaws | injection attack]], in which SQL commands are injected into data-plane input in order to affect the execution of predefined SQL commands.&lt;br /&gt;
&lt;br /&gt;
==Related Security Activities==&lt;br /&gt;
&lt;br /&gt;
===Description of SQL Injection Vulnerabilities===&lt;br /&gt;
&lt;br /&gt;
See the OWASP article on [[SQL Injection]] Vulnerabilities.&lt;br /&gt;
&lt;br /&gt;
See the OWASP article on [[Blind_SQL_Injection]] Vulnerabilities.&lt;br /&gt;
&lt;br /&gt;
===How to Avoid SQL Injection Vulnerabilities===&lt;br /&gt;
&lt;br /&gt;
See the [[:Category:OWASP Guide Project|OWASP Development Guide]] article on how to [[Guide to SQL Injection | Avoid SQL Injection]] Vulnerabilities.&lt;br /&gt;
&lt;br /&gt;
See the [[:Category:OWASP Code Review Project|OWASP Code Review Guide]] article on how to [[Reviewing Code for SQL Injection|Review Code for SQL Injection]] Vulnerabilities.&lt;br /&gt;
&lt;br /&gt;
See the OWASP Prevention Cheat Sheet Series article on [[SQL Injection Prevention Cheat Sheet | Preventing SQL Injection]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Security Focus Area]]&lt;br /&gt;
__NOTOC__&lt;br /&gt;
&lt;br /&gt;
==  Description of the Issue ==&lt;br /&gt;
SQL Injection attacks can be divided into the following three classes:&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 behaviour of the DB Server.&lt;br /&gt;
&lt;br /&gt;
Independent of the attack class, a successful SQL Injection attack requires the attacker to craft a syntactically correct SQL Query. If the application returns an error message generated by an incorrect query, then it is easy to reconstruct the logic of the original query and, therefore, understand how to perform the injection correctly. However, if the application hides the error details, then the tester must be able to reverse engineer the logic of the original query. The latter case is known as &amp;quot;[[Blind SQL Injection]]&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==  Black Box testing and example == &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 our application connects to a DB Server in order to access some data. Typical examples of cases when an application needs to talk to a DB include:&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, ...) are very likely to be stored in a relational database.&lt;br /&gt;
The tester has to make a list of all input fields whose values could be used in crafting a SQL query, including the hidden fields of POST requests and then test them separately, trying to interfere with the query and to generate an error.&lt;br /&gt;
The very first test usually consists of adding a single quote (') or a semicolon (;) to the field under test. The first is used in SQL as a string terminator and, if not filtered by the application, would lead to an incorrect query. The second is used to end a SQL statement and, if it is not filtered, it is also likely to generate an error. &lt;br /&gt;
The output of a vulnerable field might resemble the following (on a Microsoft SQL Server, in this case):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&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&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Also comments (--) and other SQL keywords like 'AND' and 'OR' can be used to try to modify the query. A very simple but sometimes still effective technique is simply to insert a string where a number is expected, as an error like the following might be generated:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&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;
A full error message, like those in the examples, provides a wealth of information to the tester in order to mount a successful injection. However, applications often do not provide so much detail: a simple '500 Server Error' or a custom error page might be issued, meaning that we need to use blind injection techniques.&lt;br /&gt;
In any case, it is very important to test *each field separately*: only one variable must vary while all the other remain constant, in order to precisely understand which parameters are vulnerable and which are not.&lt;br /&gt;
&lt;br /&gt;
=== Standard SQL Injection Testing ===&lt;br /&gt;
&lt;br /&gt;
Consider the following SQL query:&lt;br /&gt;
&lt;br /&gt;
 SELECT * FROM Users WHERE Username='$username' AND Password='$password' &lt;br /&gt;
&lt;br /&gt;
A similar query is generally used from the web application in order to authenticate a user. If the query returns a value it means that inside the database a user with that credentials exists, then the user is allowed to login to the system, otherwise the access is denied.&lt;br /&gt;
The values of the input fields are generally obtained from the user through a web form. &lt;br /&gt;
Suppose we insert the following Username and Password values: &lt;br /&gt;
&lt;br /&gt;
 $username = 1' or '1' = '1&lt;br /&gt;
 $password = 1' or '1' = '1&lt;br /&gt;
&lt;br /&gt;
The query will be: &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;SELECT * FROM Users WHERE Username='1' OR '1' = '1' AND Password='1' OR '1' = '1'&amp;lt;/nowiki&amp;gt; &lt;br /&gt;
If we suppose that the values of the parameters are sent to the server through the GET method, and if the domain of the vulnerable web site is www.example.com, the request that we'll carry out will be:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;http://www.example.com/index.php?username=1'%20or%20'1'%20=%20'1&amp;amp;password=1'%20or%20'1'%20=%20'1 &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After a short analysis we notice that the query returns a value (or a set of values) because the  condition is always true (OR 1=1). In this way the system has authenticated the user without knowing the username and password.&amp;lt;BR&amp;gt; ''In some systems the first row of a user table would be an administrator user. This may be the profile returned in some cases.''&lt;br /&gt;
Another example of query is the following: &lt;br /&gt;
&lt;br /&gt;
 SELECT * FROM Users WHERE ((Username='$username') AND (Password=MD5('$password'))) &lt;br /&gt;
&lt;br /&gt;
In this case, there are two problems, one due to the use of the parentheses and one due to the use of MD5 hash function. &lt;br /&gt;
First of all, we resolve the problem of the parentheses. &lt;br /&gt;
That simply consists of adding a number of closing parentheses until we obtain a corrected query. To resolve the second problem, we try to invalidate the second condition.&lt;br /&gt;
We add to our query a final symbol that means that a comment is beginning. In this way, everything that follows such symbol is considered a comment.&lt;br /&gt;
Every DBMS has its own symbols of comment, however, a common symbol to the greater part of the database is /*. In Oracle the symbol is &amp;quot;--&amp;quot;.&lt;br /&gt;
This said, the values that we'll use as Username and Password are: &lt;br /&gt;
&lt;br /&gt;
 $username = 1' or '1' = '1'))/*&lt;br /&gt;
 $password = foo&lt;br /&gt;
&lt;br /&gt;
In this way, we'll get the following query: &lt;br /&gt;
&lt;br /&gt;
 SELECT * FROM Users WHERE ((Username='1' or '1' = '1'))/*') AND (Password=MD5('$password'))) &lt;br /&gt;
&lt;br /&gt;
The URL request will be:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;http://www.example.com/index.php?username=1'%20or%20'1'%20=%20'1'))/*&amp;amp;password=foo &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Which returns a number of values. Sometimes, the authentication code verifies that the number of returned tuple is exactly equal to 1. In the previous examples, this situation would be difficult (in the database there is only one value per user). &lt;br /&gt;
In order to go around this problem, it is enough to insert a SQL command that imposes the condition that the number of the returned tuple must be one. (One record returned)&lt;br /&gt;
In order to reach this goal, we use the operator &amp;quot;LIMIT &amp;lt;num&amp;gt;&amp;quot;, where &amp;lt;num&amp;gt; is the number of the tuples that we expect to be returned. With respect to the previous example, the value of the fields Username and Password will be modified as follows:&lt;br /&gt;
&lt;br /&gt;
 $username = 1' or '1' = '1')) LIMIT 1/* &lt;br /&gt;
 $password = foo &lt;br /&gt;
&lt;br /&gt;
In this way, we create a request like the follow:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;http://www.example.com/index.php?username=1'%20or%20'1'%20=%20'1'))%20LIMIT%201/*&amp;amp;password=foo &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Union Query SQL Injection Testing ===&lt;br /&gt;
Another test involves the use of the UNION operator. This operator is used in SQL injections to join a query, purposely forged by the tester, to the original query. The result of the forged query will be joined to the result of the original query, allowing the tester to obtain the values of fields of other tables.&lt;br /&gt;
We suppose for our examples that the query executed from the server is the following: &lt;br /&gt;
&lt;br /&gt;
 SELECT Name, Phone, Address FROM Users WHERE Id=$id &lt;br /&gt;
&lt;br /&gt;
We will set the following Id value: &lt;br /&gt;
&lt;br /&gt;
 $id=1 UNION ALL SELECT creditCardNumber,1,1 FROM CreditCarTable&lt;br /&gt;
&lt;br /&gt;
We will have the following query: &lt;br /&gt;
&lt;br /&gt;
 SELECT Name, Phone, Address FROM Users WHERE Id=1 UNION ALL SELECT creditCardNumber,1,1 FROM CreditCarTable &lt;br /&gt;
&lt;br /&gt;
which will join the result of the original query with all the credit card users. &lt;br /&gt;
The keyword '''ALL''' is necessary to get around queries that use the keyword DISTINCT. &lt;br /&gt;
Moreover, we notice that beyond the credit card numbers, we have selected other two values. These two values are necessary, because the two query must have an equal number of parameters, in order to avoid a syntax error.&lt;br /&gt;
&lt;br /&gt;
=== Blind SQL Injection Testing ===&lt;br /&gt;
We have pointed out that there is another category of SQL injection, called [[Blind SQL Injection]], in which nothing is known on the outcome of an operation. For example, this behavior happens in cases where the programmer has created a custom error page that does not reveal anything on the structure of the query or on the database. (The page does not return a SQL error, it may just return a HTTP 500).&lt;br /&gt;
&amp;lt;BR&amp;gt;&lt;br /&gt;
By using the inference methods, it is possible to avoid this obstacle and thus to succeed to recover the values of some desired fields. This method consists of carrying out a series of boolean queries to the server, observing the answers and finally deducing the meaning of such answers.&lt;br /&gt;
We consider, as always, the www.example.com domain and we suppose that it contains a parameter named id vulnerable to SQL injection.&lt;br /&gt;
This means that carrying out the following request: &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;http://www.example.com/index.php?id=1' &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
we will get one page with a custom message error which is due to a syntactic error in the query. We suppose that the query executed on the server is: &lt;br /&gt;
&lt;br /&gt;
 SELECT field1, field2, field3 FROM Users WHERE Id='$Id' &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 will execute will allow us to obtain the value of the username field, extracting such value character by character. This is possible through the use of some standard functions, present practically in every database. For our examples, we will use the following pseudo-functions: &lt;br /&gt;
&lt;br /&gt;
'''SUBSTRING (text, start, length)''': it returns a substring starting from the position &amp;quot;start&amp;quot; of text and of length &amp;quot;length&amp;quot;. If &amp;quot;start&amp;quot; is greater than the length of text, the function returns a null value. &lt;br /&gt;
&lt;br /&gt;
'''ASCII (char)''': it gives back ASCII value of the input character. A null value is returned if char is 0.&lt;br /&gt;
&lt;br /&gt;
'''LENGTH (text)''': it gives back the length in characters of the input text.&lt;br /&gt;
&lt;br /&gt;
Through such functions, we will execute our tests on the first character and, when we have discovered the value, we will pass to the second and so on, until we will have discovered the entire value. &lt;br /&gt;
The tests will take advantage of the function SUBSTRING, in order to select only one character at a time (selecting a single character means to impose the length parameter to 1), and the function ASCII, in order to obtain the ASCII value, so that we can do numerical comparison. The results of the comparison will be done with all the values of the ASCII table, until the right value is found.&lt;br /&gt;
As an example, we will use the following value for ''Id'': &lt;br /&gt;
&lt;br /&gt;
 $Id=1' AND ASCII(SUBSTRING(username,1,1))=97 AND '1'='1 &lt;br /&gt;
&lt;br /&gt;
that creates the following query (from now on, we will call it &amp;quot;inferential query&amp;quot;): &lt;br /&gt;
&lt;br /&gt;
 SELECT field1, field2, field3 FROM Users WHERE Id='1' AND ASCII(SUBSTRING(username,1,1))=97 AND '1'='1'&lt;br /&gt;
&lt;br /&gt;
The previous example returns a result if and only if the first character of the field username is equal to the ASCII value 97. If we get a false value, then we increase the index of the ASCII table from 97 to 98 and we repeat the request. If instead we obtain a true value, we set to zero the index of the ASCII table and we analyze the next character, modifying the parameters of the SUBSTRING function.&lt;br /&gt;
The problem is to understand in which way 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. &lt;br /&gt;
This is possible by using the following value for ''Id'': &lt;br /&gt;
&lt;br /&gt;
 $Id=1' AND '1' = '2 &lt;br /&gt;
&lt;br /&gt;
by which will create the following query: &lt;br /&gt;
&lt;br /&gt;
 SELECT field1, field2, field3 FROM Users WHERE Id='1' AND '1' = '2' &lt;br /&gt;
&lt;br /&gt;
The obtained response from the server (that is HTML code) will be the false value for our tests. &lt;br /&gt;
This is enough to verify whether the value obtained from the execution of the inferential query is equal to the value obtained with the test executed before. &lt;br /&gt;
Sometimes, this method does not work. If the server returns two different pages as a result of two identical consecutive web requests, we will not be able to discriminate the true value from the false value. In these particular cases, it is necessary to use particular filters that allow us to eliminate the code that changes between the two requests and to obtain a template. Later on, for every inferential request executed, we will extract the relative template from the response using the same function, and we will perform a control between the two templates in order to decide the result of the test.&lt;br /&gt;
&lt;br /&gt;
In the previous discussion, we haven't dealt with the problem of determining the termination condition for out tests, i.e., when we should end the inference procedure. &lt;br /&gt;
A techniques to do 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 value null) and the test returns the value true, then either we are done with the inference procedue (we have scanned the whole string), or the value we have analyzed contains the null character.&lt;br /&gt;
&lt;br /&gt;
We will insert the following value for the field ''Id'': &lt;br /&gt;
&lt;br /&gt;
 $Id=1' AND LENGTH(username)=N AND '1' = '1 &lt;br /&gt;
&lt;br /&gt;
Where N is the number of characters that we have analyzed up to now (not counting the null value). &lt;br /&gt;
The query will be: &lt;br /&gt;
&lt;br /&gt;
 SELECT field1, field2, field3 FROM Users WHERE Id='1' AND LENGTH(username)=N AND '1' = '1' &lt;br /&gt;
&lt;br /&gt;
The query returns either true or false. If we obtain true, then we have completed inference and, therefore, we know the value of the parameter. If we obtain false, this means that the null character is present in the value of the parameter, and we must continue to analyze the next parameter until we find another null value.&lt;br /&gt;
&lt;br /&gt;
The blind SQL injection attack needs a high volume of queries. The tester may need an automatic tool to exploit the vulnerability.&lt;br /&gt;
A simple tool which performs this task, via GET requests on the MySql DB, is SqlDumper, which is shown below.&lt;br /&gt;
&lt;br /&gt;
[[Image:sqldumper.jpg]]&lt;br /&gt;
&lt;br /&gt;
=== Stored Procedure Injection ===&lt;br /&gt;
Question: How can the risk of SQL injection be eliminated? &amp;lt;br&amp;gt;&lt;br /&gt;
Answer: Stored procedures.&amp;lt;br&amp;gt;&lt;br /&gt;
I have seen this answer too many times without qualifications.  Merely the use of stored procedures does not assist in the mitigation of SQL injection.  If not handled properly, dynamic SQL within stored procedures can be just as vulnerable to SQL injection as dynamic SQL within a web page.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
When using dynamic SQL within a stored procedure, the application must properly sanitize the user input to eliminate the risk of code injection.  If not sanitized, the user could enter malicious SQL that will be executed within the stored procedure.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Black box testing uses SQL injection to compromise the system. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the following &amp;lt;b&amp;gt;SQL Server Stored Procedure:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
 Create procedure user_login @username varchar(20), @passwd varchar(20) As&lt;br /&gt;
 Declare @sqlstring varchar(250) &lt;br /&gt;
 Set @sqlstring  = ‘&lt;br /&gt;
 Select 1 from users &lt;br /&gt;
 Where username = ‘ + @username + ‘ and passwd = ‘ + @passwd&lt;br /&gt;
 exec(@sqlstring)&lt;br /&gt;
 Go&lt;br /&gt;
User input: &amp;lt;br&amp;gt;&lt;br /&gt;
 anyusername or 1=1'&lt;br /&gt;
 anypassword&lt;br /&gt;
This procedure does not sanitize the input, therefore allowing the return value to show an existing record with these 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 user, but consider a dynamic reporting query where the user selects the columns to view. The user could insert malicious code into this scenario and compromise the data.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider the following &amp;lt;b&amp;gt;SQL Server Stored Procedure:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
 Create procedure get_report @columnamelist varchar(7900) 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;
User input: &amp;lt;br&amp;gt;&lt;br /&gt;
 1 from users; update users set password = 'password'; select *&lt;br /&gt;
&lt;br /&gt;
This will result in the report running and all users’ passwords being updated.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;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.nextgenss.com/papers/advanced_sql_injection.pdf&lt;br /&gt;
* Chris Anley: &amp;quot;More Advanced SQL Injection&amp;quot; - http://www.nextgenss.com/papers/more_advanced_sql_injection.pdf&lt;br /&gt;
* David Litchfield: &amp;quot;Data-mining with SQL Injection and Inference&amp;quot; - http://www.nextgenss.com/research/papers/sqlinference.pdf&lt;br /&gt;
* Imperva: &amp;quot;Blind SQL Injection&amp;quot; - http://www.imperva.com/application_defense_center/white_papers/blind_sql_server_injection.html&lt;br /&gt;
* Ferruh Mavituna: &amp;quot;SQL Injection Cheat Sheet&amp;quot; - http://ferruh.mavituna.com/makale/sql-injection-cheatsheet/&lt;br /&gt;
&lt;br /&gt;
'''Tools'''&amp;lt;br&amp;gt;&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--:  MySql Blind Injection Bruteforcing, Reversing.org - [http://www.reversing.org/node/view/11 sqlbftools]&amp;lt;br&amp;gt;&lt;br /&gt;
* Bernardo Damele and Daniele Bellucci: sqlmap, a blind SQL injection tool - http://sqlmap.sourceforge.net&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;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:FIXME|broken links&lt;br /&gt;
&lt;br /&gt;
* Kevin Spett: &amp;quot;SQL Injection&amp;quot; - http://www.spidynamics.com/papers/SQLInjectionWhitePaper.pdf&lt;br /&gt;
* Kevin Spett: &amp;quot;Blind SQL Injection&amp;quot; - http://www.spidynamics.com/whitepapers/Blind_SQLInjection.pdf&lt;br /&gt;
&lt;br /&gt;
* Antonio Parata: Dump Files by SQL inference on Mysql - [http://www.ictsc.it/site/IT/projects/sqlDumper/sqldumper.src.tar.gz SqlDumper]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
]]&lt;/div&gt;</summary>
		<author><name>Suei8423</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=SQL_Injection&amp;diff=60558</id>
		<title>SQL Injection</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=SQL_Injection&amp;diff=60558"/>
				<updated>2009-05-11T17:42:23Z</updated>
		
		<summary type="html">&lt;p&gt;Suei8423: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:Attack}}&lt;br /&gt;
[[Category:OWASP ASDR Project]]&lt;br /&gt;
[[Category:Security Focus Area]]&lt;br /&gt;
__NOTOC__&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Last revision (mm/dd/yy): '''{{REVISIONMONTH}}/{{REVISIONDAY}}/{{REVISIONYEAR}}'''&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
A [[SQL injection]] attack consists of insertion or &amp;quot;injection&amp;quot; of a SQL query via the input data from the client to the application. A successful SQL injection exploit can read sensitive data from the database, modify database data (Insert/Update/Delete), execute administration operations on the database (such as shutdown the DBMS), recover the content of a given file present on the DBMS file system and in some cases issue commands to the operating system. &lt;br /&gt;
SQL injection attacks are a type of  [[Top 10 2007-Injection Flaws | injection attack]], in which SQL commands are injected into data-plane input in order to effect the execution of predefined SQL commands.&lt;br /&gt;
&lt;br /&gt;
==Threat Modeling==&lt;br /&gt;
* SQL injection attacks allow attackers to spoof identity, tamper with existing data, cause repudiation issues such as voiding transactions or changing balances, allow the complete disclosure of all data on the system, destroy the data or make it otherwise unavailable, and become administrators of the database server. &lt;br /&gt;
* SQL Injection is very common with PHP and ASP applications due to the prevalence of older functional interfaces. Due to the nature of programmatic interfaces available, J2EE and ASP.NET applications are less likely to have easily exploited SQL injections. &lt;br /&gt;
* The severity of SQL Injection attacks is limited by the attacker’s skill and imagination, and to a lesser extent, defense in depth countermeasures, such as low privilege connections to the database server and so on. In general, consider SQL Injection a high impact severity.&lt;br /&gt;
&lt;br /&gt;
==Related Security Activities==&lt;br /&gt;
&lt;br /&gt;
===How to Avoid SQL Injection Vulnerabilities===&lt;br /&gt;
&lt;br /&gt;
See the [[:Category:OWASP Guide Project|OWASP Guide]] article on how to [[Guide to SQL Injection | Avoid SQL Injection]] Vulnerabilities.&amp;lt;br&amp;gt;&lt;br /&gt;
See the OWASP [[SQL Injection Prevention Cheat Sheet]].&lt;br /&gt;
&lt;br /&gt;
===How to Review Code for SQL Injection Vulnerabilities===&lt;br /&gt;
&lt;br /&gt;
See the [[:Category:OWASP Code Review Project|OWASP Code Review Guide]] article on how to [[Reviewing Code for SQL Injection|Review Code for SQL Injection]] Vulnerabilities.&lt;br /&gt;
&lt;br /&gt;
===How to Test for SQL Injection Vulnerabilities===&lt;br /&gt;
&lt;br /&gt;
See the [[:Category:OWASP Testing Project|OWASP Testing Guide]] article on how to [[Testing for SQL Injection    (OWASP-DV-005)|Test for SQL Injection]] Vulnerabilities.&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
SQL injection errors occur when:&lt;br /&gt;
&lt;br /&gt;
# Data enters a program from an untrusted source. &lt;br /&gt;
# The data used to dynamically construct a SQL query &lt;br /&gt;
&lt;br /&gt;
The main consequences are:&lt;br /&gt;
&lt;br /&gt;
* '''Confidentiality''': Since SQL databases generally hold sensitive data, loss of confidentiality is a frequent problem with [[Glossary#SQL Injection|SQL Injection]] vulnerabilities.&lt;br /&gt;
&lt;br /&gt;
* '''Authentication''': If poor SQL commands are used to check user names and passwords, it may be possible to connect to a system as another user with no previous knowledge of the password.&lt;br /&gt;
&lt;br /&gt;
* '''Authorization''': If authorization information is held in a SQL database, it may be possible to change this information through the successful exploitation of a [[Glossary#SQL Injection|SQL Injection]] vulnerability.&lt;br /&gt;
&lt;br /&gt;
* '''Integrity''': Just as it may be possible to read sensitive information, it is also possible to make changes or even delete this information with a [[Glossary#SQL Injection|SQL Injection]] attack.&lt;br /&gt;
&lt;br /&gt;
== Risk Factors==&lt;br /&gt;
The platform affected can be:&lt;br /&gt;
* Language: SQL&lt;br /&gt;
* Platform: Any (requires interaction with a SQL database)&lt;br /&gt;
&lt;br /&gt;
[[Glossary#SQL Injection|SQL Injection]] has become a common issue with database-driven web sites. The flaw is easily detected, and easily exploited, and as such, any site or software package with even a minimal user base is likely to be subject to an attempted attack of this kind. &lt;br /&gt;
&lt;br /&gt;
Essentially, the attack is accomplished by placing a meta character into data input to then place SQL commands in the control plane, which did not exist there before. This flaw depends on the fact that SQL makes no real distinction between the control and data planes.&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
===Example 1===&lt;br /&gt;
&lt;br /&gt;
In SQL:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
select id, firstname, lastname from authors&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If one provided:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Firstname: evil'ex&lt;br /&gt;
Lastname: Newman&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the query string becomes:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
select id, firstname, lastname from authors where forename = 'evil'ex' and surname ='newman'&lt;br /&gt;
which the database attempts to run as &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Incorrect syntax near al' as the database tried to execute evil. &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A safe version of the above SQL statement could be coded in Java as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String firstname = req.getParameter(&amp;quot;firstname&amp;quot;);&lt;br /&gt;
String lastname = req.getParameter(&amp;quot;lastname&amp;quot;);&lt;br /&gt;
// FIXME: do your own validation to detect attacks&lt;br /&gt;
String query = &amp;quot;SELECT id, firstname, lastname FROM authors WHERE forename = ? and surname = ?&amp;quot;;&lt;br /&gt;
PreparedStatement pstmt = connection.prepareStatement( query );&lt;br /&gt;
pstmt.setString( 1, firstname );&lt;br /&gt;
pstmt.setString( 2, lastname );&lt;br /&gt;
try&lt;br /&gt;
{&lt;br /&gt;
	ResultSet results = pstmt.execute( );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Example 2===&lt;br /&gt;
&lt;br /&gt;
The following C# code dynamically constructs and executes a SQL query that searches for items matching a specified name. The query restricts the items displayed to those where owner matches the user name of the currently-authenticated user.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
	...&lt;br /&gt;
	string userName = ctx.getAuthenticatedUserName();&lt;br /&gt;
	string query = &amp;quot;SELECT * FROM items WHERE owner = &amp;quot;'&amp;quot; &lt;br /&gt;
					+ userName + &amp;quot;' AND itemname = '&amp;quot;  &lt;br /&gt;
					+ ItemName.Text + &amp;quot;'&amp;quot;;&lt;br /&gt;
	sda = new SqlDataAdapter(query, conn);&lt;br /&gt;
	DataTable dt = new DataTable();&lt;br /&gt;
	sda.Fill(dt);&lt;br /&gt;
	...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The query that this code intends to execute follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
	SELECT * FROM items&lt;br /&gt;
	WHERE owner = &lt;br /&gt;
	AND itemname = ;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
However, because the query is constructed dynamically by concatenating a constant base query string and a user input string, the query only behaves correctly if itemName does not contain a single-quote character. If an attacker with the user name wiley enters the string &amp;quot;name' OR 'a'='a&amp;quot; for itemName, then the query becomes the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
	SELECT * FROM items&lt;br /&gt;
	WHERE owner = 'wiley'&lt;br /&gt;
	AND itemname = 'name' OR 'a'='a';&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The addition of the OR 'a'='a' condition causes the where clause to always evaluate to true, so the query becomes logically equivalent to the much simpler query:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
	SELECT * FROM items;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This simplification of the query allows the attacker to bypass the requirement that the query only return items owned by the authenticated user; the query now returns all entries stored in the items table, regardless of their specified owner.&lt;br /&gt;
&lt;br /&gt;
===Example 3===&lt;br /&gt;
&lt;br /&gt;
This example examines the effects of a different malicious value passed to the query constructed and executed in Example 1. If an attacker with the user name hacker enters the string &amp;quot;hacker'); DELETE FROM items; --&amp;quot; for itemName, then the query becomes the following two queries:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
	SELECT * FROM items &lt;br /&gt;
	WHERE owner = 'hacker'&lt;br /&gt;
	AND itemname = 'name';&lt;br /&gt;
&lt;br /&gt;
	DELETE FROM items;&lt;br /&gt;
&lt;br /&gt;
	--'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Many database servers, including Microsoft® SQL Server 2000, allow multiple SQL statements separated by semicolons to be executed at once. While this attack string results in an error in Oracle and other database servers that do not allow the batch-execution of statements separated by semicolons, in databases that do allow batch execution, this type of attack allows the attacker to execute arbitrary commands against the database.&lt;br /&gt;
&lt;br /&gt;
Notice the trailing pair of hyphens (--), which specifies to most database servers that the remainder of the statement is to be treated as a comment and not executed. In this case the comment character serves to remove the trailing single-quote left over from the modified query. In a database where comments are not allowed to be used in this way, the general attack could still be made effective using a trick similar to the one shown in Example 1. If an attacker enters the string &amp;quot;name'); DELETE FROM items; SELECT * FROM items WHERE 'a'='a&amp;quot;, the following three valid statements will be created:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
	SELECT * FROM items &lt;br /&gt;
	WHERE owner = 'hacker'&lt;br /&gt;
	AND itemname = 'name';&lt;br /&gt;
&lt;br /&gt;
	DELETE FROM items;&lt;br /&gt;
&lt;br /&gt;
	SELECT * FROM items WHERE 'a'='a';&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One traditional approach to preventing SQL injection attacks is to handle them as an input validation problem and either accept only characters from a whitelist of safe values or identify and escape a blacklist of potentially malicious values. Whitelisting can be a very effective means of enforcing strict input validation rules, but parameterized SQL statements require less maintenance and can offer more guarantees with respect to security. As is almost always the case, blacklisting is riddled with loopholes that make it ineffective at preventing SQL injection attacks. For example, attackers can:&lt;br /&gt;
&lt;br /&gt;
* Target fields that are not quoted &lt;br /&gt;
* Find ways to bypass the need for certain escaped meta-characters &lt;br /&gt;
* Use stored procedures to hide the injected meta-characters &lt;br /&gt;
&lt;br /&gt;
Manually escaping characters in input to SQL queries can help, but it will not make your application secure from SQL injection attacks.&lt;br /&gt;
&lt;br /&gt;
Another solution commonly proposed for dealing with SQL injection attacks is to use stored procedures. Although stored procedures prevent some types of SQL injection attacks, they fail to protect against many others. For example, the following PL/SQL procedure is vulnerable to the same SQL injection attack shown in the first example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
	procedure get_item (&lt;br /&gt;
		itm_cv IN OUT ItmCurTyp,&lt;br /&gt;
		usr in varchar2,&lt;br /&gt;
		itm in varchar2)&lt;br /&gt;
	is&lt;br /&gt;
		open itm_cv for ' SELECT * FROM items WHERE ' ||&lt;br /&gt;
				'owner = '''|| usr || &lt;br /&gt;
				' AND itemname = ''' || itm || '''';&lt;br /&gt;
	end get_item;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Stored procedures typically help prevent SQL injection attacks by limiting the types of statements that can be passed to their parameters. However, there are many ways around the limitations and many interesting statements that can still be passed to stored procedures. Again, stored procedures can prevent some exploits, but they will not make your application secure against SQL injection attacks.&lt;br /&gt;
&lt;br /&gt;
==Related [[Threat Agents]]==&lt;br /&gt;
* [[:Category:Command Execution]] &lt;br /&gt;
* [[Injection problem]]&lt;br /&gt;
&lt;br /&gt;
==Related [[Attacks]]==&lt;br /&gt;
* [[Top 10 2007-Injection Flaws | injection attack]]&lt;br /&gt;
* [[Blind SQL Injection]]&lt;br /&gt;
* [[Code Injection]]&lt;br /&gt;
* [[Double Encoding]]&lt;br /&gt;
* [[Interpreter_Injection#ORM_Injection]]&lt;br /&gt;
&lt;br /&gt;
==Related [[Vulnerabilities]]==&lt;br /&gt;
* [[:Category:Input Validation Vulnerability]]&lt;br /&gt;
&lt;br /&gt;
==Related [[Controls]]==&lt;br /&gt;
* [[Input Validation]]&lt;br /&gt;
* [[Output Validation]]&lt;br /&gt;
* [[Static Code Analysis]]&lt;br /&gt;
[[Category:FIXME|this was the text that was here before we added the links. Can it be deleted?&lt;br /&gt;
Avoidance and mitigation &lt;br /&gt;
&lt;br /&gt;
* Requirements specification: A non-SQL style database which is not subject to this flaw may be chosen.&lt;br /&gt;
&lt;br /&gt;
* Implementation: Use vigorous white-list style checking on any user input that may be used in an SQL command. Rather than escape meta-characters, it is safest to disallow them entirely. Reason: Later use of data that has been entered in the database may neglect to escape meta-characters before use.&lt;br /&gt;
&lt;br /&gt;
* Image:Advanced Topics on SQL Injection Protection.ppt&lt;br /&gt;
]]&lt;br /&gt;
&lt;br /&gt;
==References ==&lt;br /&gt;
* [http://www.greensql.net/ GreenSQL Open Source SQL Injection Filter] - An Open Source database firewall used to protect databases from SQL injection attacks.&lt;br /&gt;
* [http://www.net-security.org/dl/articles/IntegrigyIntrotoSQLInjectionAttacks.pdf An Introduction to SQL Injection Attacks for Oracle Developers] - This also includes recommended defenses.&lt;br /&gt;
* OWASP [[:Category:OWASP_SQLiX_Project | SQLiX Project]] - An SQL Injection Scanner.&lt;br /&gt;
* [http://www.nosec.org/en/pangolin.html Pangolin Amazing SQL INJECTION World] - An Amazing SQL Injection Scanner.&lt;br /&gt;
[[Category:Injection Attack]]&lt;br /&gt;
[[category:Attack]]&lt;/div&gt;</summary>
		<author><name>Suei8423</name></author>	</entry>

	</feed>