Login   Search
Skip Navigation Links
Home
Application Security Tips
Oracle , PL/SQL
IT Product Reviews
Project Management
Forum
Contact Us
Links & References
Avoid SQL Injection attack
Threats and Countermeasures: S.T.R.I.D.E
Input Validation
Session Management
Authentication Mechanism
Cross Site Scripting Vulnerabilities
Configuration Management
Scroll up
Scroll down
Oracle 9i - Programming basics PL/SQL
PL/SQL - Conditional Statements – IF
PL/SQL -Nested Block
LOOPS in PL/SQL
PL/SQL Records
Cursors in PL/SQL
PL/SQL Tables
PL/SQL Exceptions
PL/SQL Procedures
PL/SQL Functions
Oracle supplied packages
Packages
PL/SQL Ref Cursors
Types in Oracle PL/SQL
Varrays
Nested Table
Bfile and LOBs
Bulk Binding
Know Depandencies
PL/SQL Wrapper
Triggers
Scroll up
Scroll down
DBMS_SQL package
DBMS_DDL Package
DBMS_JOB Package
UTL_FILE Package
DBMS_METADATA Package
DBMS_PIPE Package
DBMS_SESSION Package
Scroll up
Scroll down

 

Blog

  • Following coding best practices and style guides
  • Imperativeness of agile methodology in software development
  • Get list of installed softwares on machines in your network
  • VMWare - Error - the vmware authorization service is not running
  • Add chart / graphs in ASP.net application / website

Blog

  • Review: uCertify.com: PrepKit for: 70-529 (C#)
  • Bird eye Review: uCertify.com: PrepKit for: 70-529 (C#)
Skip Navigation Links>Application Security Tips>Avoid SQL Injection attack

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

 


SubjectLast EntryRepliesHits
Discussion & Feedback
admin
7/10/2009 11:08 PM
Rahul
0262


Designed & Developed by Rahul Bagal