Avoid SQL Injection

SQL Injection attack

What is SQL Injection attack and how do i address it during my application development?

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

1. 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.

2. 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;

How to Protect ?

Securing applications against SQL injection

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() )
{

}}}

 

Posted in Uncategorized