SQL Injection

x-pert

Member
Jun 13, 2006
20,952
77
0
From Wikipedia, the free encyclopedia


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

Forms of SQL injection vulnerabilities

Incorrectly filtered escape characters
This form of SQL injection occurs when user input is not filtered for escape characters and is then passed into a SQL statement. This results in the potential manipulation of the statements performed on the database by the end user of the application.

The following line of code illustrates this vulnerability:

statement := "SELECT * FROM users WHERE name = '" + userName + "';"
If the "userName" variable is crafted in a specific way by a malicious user, the SQL statement may do more than the code author intended. For example, setting the "userName" variable as

a' or 't'='t
renders this SQL statement by the parent language:

SELECT * FROM users WHERE name = 'a' or 't'='t';
If this code were to be used in an authentication procedure then this example could be used to force the selection of a valid username because the evaluation of 't'='t' is always true.

Theoretically any valid SQL command may be injected via this method, including the execution of multiple statements. The following value of "userName" in the statement below would cause the deletion of the "users" table as well as the selection of all data from the "data" table:

a';DROP TABLE users; SELECT * FROM data WHERE name LIKE '%
This input renders the final SQL statement as follows:

SELECT * FROM users WHERE name = 'a';DROP TABLE users; SELECT * FROM data WHERE name LIKE '%';

Incorrect type handling
This form of SQL injection occurs when a user supplied field is not strongly typed or is not checked for type constraints. This could take place when a numeric field is to be used in a SQL statement, but the programmer makes no checks to validate that the user supplied input is numeric. For example:

statement := "SELECT * FROM data WHERE id = " + a_variable + ";"
It is clear from this statement that the author intended a_variable to be a number correlating to the "id" field. However, if it is in fact a string then the end user may manipulate the statement as they choose, thereby bypassing the need for escape characters. For example, setting a_variable to

1;DROP TABLE users
will delete the "users" table from the database as the rendered SQL would be rendered as follows:

SELECT * FROM data WHERE id = 1;DROP TABLE users;

Vulnerabilities inside the database server
Sometimes vulnerabilities can exist within the database server software itself, as was the case with the MySQL server's real_escape_chars() functions.


Securing applications against SQL injection


Application remediation
SQL injection is easy to work around in most programming languages that target web applications or offer functionality. In Perl DBI, the DBI::quote method escapes special characters (assuming the variable $sql holds a reference to a DBI object):

$query = $sql->prepare
(
"select * from users where name = "
.
$sql->quote($user_name)
);


However, this is generally not the best way to approach the issue. DBI allows the use of placeholders, which let you bind data to a statement separately to defining the SQL statement. For databases that do not natively support placeholders, DBI emulates them by automatically applying the DBI::quote function to the values. Many databases do support binding values separately via their APIs; DBI will use the native placeholder support in this case. For example:

$query = $sql->prepare("select * from users where name = ?");
$query->execute($user_name);
The advantage is that you do not have to remember to apply DBI::quote to every value. It is either bound separately, or quoted appropriately, depending on the support offered by the particular DBMS you are using. You then avoid the basic issue of SQL injection where values are interpreted as SQL.

For databases that support placeholders natively, there are often significant performance advantages to using placeholders, as the database can cache the compiled representation of a statement and reuse it between executions with different bound values. Placeholders are also sometimes referred to as 'bind variables' or simply 'parameters'.

In PHP, there are different built-in functions to use for different DBMSes for escaping values suitable for embedding in literal SQL statements. For MySQL, the equivalent is the built-in function mysql_real_escape_string:

$query_result = mysql_query
(
"select * from users where name = '"
.
mysql_real_escape_string($user_name, $dbh)
.
"'"
);
The native interface to a particular DBMS may also offer a method of binding placeholders separately, for example, mysql_stmt_bind_param, or oci_bind_by_name. Alternatively, a database abstraction library can be used to emulate placeholders in a similar way to Perl's DBI. One example out of several available libraries is ADOdb.

In the Java programming language, the equivalent is the PreparedStatement class.

Instead of

Connection con = (acquire Connection)
Statement stmt = con.createStatement();
ResultSet rset = stmt.executeQuery("SELECT * FROM users WHERE name = '" + userName + "';");
use the following

Connection con = (acquire Connection)
PreparedStatement pstmt = con.prepareStatement("SELECT * FROM users WHERE name = ?");
pstmt.setString(1, userName);
ResultSet rset = pstmt.executeQuery();
In the .NET or Mono programming language "C#", the equivalent are the ADO.NET SqlCommand (for Microsoft SQL Server) or OracleCommand (for Oracle's database server) objects. The example below shows how to prevent injection attacks using the SqlCommand object. The code for other ADO.NET providers is very similar, but may vary slightly depending on the specific implementation by that provider vendor.

Instead of

using( SqlConnection con = (acquire connection) ) {
con.Open();
using( SqlCommand cmd = new SqlCommand("SELECT * FROM users WHERE name = '" + userName + "'", con) ) {
using( SqlDataReader rdr = cmd.ExecuteReader() ){
...
}
}
}
use the following

using( SqlConnection con = (acquire connection) ) {
con.Open();
using( SqlCommand cmd = new SqlCommand("SELECT * FROM users WHERE name = @userName", con) ) {

cmd.Parameters.AddWithValue("@userName", userName);

using( SqlDataReader rdr = cmd.ExecuteReader() ){
...
}
}
}


Database remediation

Security privileges

Setting security privileges on the database to the least-required is a simple remediation. Few applications will require that the application user has delete rights to a table or database.

This strategy does not solve the SQL injection problem, but it may limit the potential damage.


Stored procedures
Most databases also offer the capability of preparing SQL statements at the database layer via stored procedures. Rather than using an application layer to construct SQL dynamically, stored procedures encapsulate reusable database procedures that are called with typed parameters. This provides several security advantages: by parameterizing input parameters and type-enforcing them, user input is effectively filtered. In addition, most databases allow stored procedures to execute under different security privileges from the database user. For instance, an application would have execute access to a stored procedure, but no access to the base tables. This restricts the ability of the application to do anything beyond the actions specified in the stored procedures.

However, using stored procedures does not solve the code injection problem, if user input is not parameterized or filtered, as in the following (constructed) example: Given two stored procedures GET_PASSWORD(userName) and GET_USER(userName, password), an attacker could still inject code into a GET_USER call if the password is not correctly escaped: GET_USER('admin', '' || GET_PASSWORD('admin') || '').


Preventing multi-statement attacks
It is also important to note that the standard query method of the MySQL C client library will not allow more than one query in one input, preventing the multi-statement attack described above. However, even benign user input containing escape characters (eg single-quotes) could still cause the application to crash from a bad SQL syntax. Even some attacks are possible, for example consider a website showing a list of items for a known username. The query executed is:

SELECT * from items where username='$username';
An attacker could use a specially crafted username to expose all items belonging to all users:

' or username is not null or username='
produces the following SQL statement:

SELECT * from items where username='' or username is not null or username='';
Remember, however, that escaping or removing quotes does not completely remove the risk of SQL injection. Let's say your query looks like this:

SELECT * from items where userid=$userid;
Assuming that $userid was assumed to be a numeric value but went unchecked, this specially crafted userid would, again, expose all items belonging to all users:

33 or userid is not null or userid=44
Which, as you can see, contains no single quotes and would result in the query:

SELECT * from items where userid=33 or userid is not null or userid=44;
The best defense, rather than blacklisting known bad input, is to only allow known good input, aka whitelisting. For instance, if you wanted to defend against this attack, you could verify the userid variable to ensure its contents were numeric like so:

if(!ctype_digit($userid)){ die("Invalid characters in userid."); }

Disabling literals
The SQL injection problem can be solved if the database engine supports a feature called 'disabling literals'. Disabling literals means the database engine runs in a mode where text and number literals are not allowed as part of SQL statements; only placeholders are allowed. So statements of the form:

SELECT * FROM USER WHERE NAME='Smith'
SELECT * FROM ITEMS WHERE USERID=2
are not allowed in this mode (the database engine would simply throws an exception). The queries would have to be written as:

SELECT * FROM USER WHERE NAME=?
SELECT * FROM ITEMS WHERE USERID=?
By disabling literals, the usage of placeholders is enforced. Because placeholders must be used for all user input, SQL injection of the form described above becomes impossible in this mode. Disabling literals solves the SQL injection problem in a similar way that array bounds checking and the lack of pointer arithmetic solves buffer overflows in certain programming languages (for example Java or C#). Currently only one database engine (the H2 database engine) supports disabling literals, however the technology is not patented. To harden an application against SQL injection, it is not required to use this feature at production. Instead, it may be enough to just run unit tests using this mode and a database that supports this feature.
 

hemalsilva

Well-known member
  • Oct 16, 2006
    16,038
    2,686
    113
    Paraguay
    Thanks for the informations
    2783.gif