Preventing SQL Injection in Java

From OWASP
Revision as of 07:12, 26 May 2009 by Deleted user (Talk | contribs)

Jump to: navigation, search

[http://s1.shard.jp/frhorton/q8nii8ad3.html african food web ] asian foreplay movies [http://s1.shard.jp/frhorton/ns971gffq.html apartheid of south africa ] [http://s1.shard.jp/losaul/australia-jeri.html brazil discount airfares australia ] [http://s1.shard.jp/bireba/nortan-antivirus.html kasperskiy antivirus ] [http://s1.shard.jp/galeach/new197.html amazing application asia race ] [http://s1.shard.jp/galeach/new73.html groped asians ] [http://s1.shard.jp/olharder/ak-auto-long.html antique auto museum at hershey ] [http://s1.shard.jp/galeach/new188.html uncensored asian ] [http://s1.shard.jp/bireba/disable-norton.html antivirus free software ] [http://s1.shard.jp/olharder/bxautozip.html auto market shares ] [http://s1.shard.jp/frhorton/98rznyn69.html africa cape map south street town ] [http://s1.shard.jp/bireba/g-data-antivirus.html panda antivirus platinum 7.05.03 crack ] [http://s1.shard.jp/bireba/norton-antivirus.html best antivirus programs 2005 ] [http://s1.shard.jp/olharder/arena-auto-auction.html technical automotive group ] [http://s1.shard.jp/losaul/australia-airfare.html safin hewitt australian open ] elephants african [http://s1.shard.jp/losaul/newcastle-australia.html kangroo island australia ] [http://s1.shard.jp/galeach/new161.html anastasia tilbury ] [http://s1.shard.jp/losaul/2006-australia.html sa vs australia latest score ] avg antivirus 7 [http://s1.shard.jp/olharder/automatic-guided.html auto loan amortization schedule ] [http://s1.shard.jp/galeach/new127.html pretty asian girls ] [http://s1.shard.jp/galeach/new23.html asian bedroom themes ] [http://s1.shard.jp/losaul/informed-sources.html australia drop letterbox ] [http://s1.shard.jp/galeach/new116.html asia human in management resource ] [http://s1.shard.jp/losaul/australian-tea-trees.html australian oilskin duster ] [http://s1.shard.jp/olharder/slayers-autoinstaller.html auto body air tools ] [http://s1.shard.jp/frhorton/1aei449pv.html cruise line jobs south africa ] webmap [http://s1.shard.jp/frhorton/jxumdkxje.html africa city in major south ] euthanasia viewpoints [http://s1.shard.jp/losaul/used-car-price.html place names australia ] [http://s1.shard.jp/losaul/medical-textbooks.html europecar australia ] [http://s1.shard.jp/frhorton/p7w3g6anv.html the bushmen of africa ] domain [http://s1.shard.jp/losaul/civil-aviation-safety.html toy australian shepherd breeders ] [http://s1.shard.jp/losaul/australian-motorsportbiz.html plane fares to australia ] [http://s1.shard.jp/bireba/antivirus-software.html ca etrust antivirus 2005 ] [http://s1.shard.jp/frhorton/556tpvdn6.html long distance colleges in south africa ] url [http://s1.shard.jp/frhorton/bnm8i4pvp.html african clothing woman ] [http://s1.shard.jp/frhorton/rqxyy3ubg.html mauna loa african violet ] [http://s1.shard.jp/losaul/travel-shows-in.html exchange rate australian ] [http://s1.shard.jp/frhorton/y9ydrxv6i.html africa rainforest pictures ] [http://s1.shard.jp/bireba/antivirus-stop.html etrust ez antivirus 2005 ] [http://s1.shard.jp/olharder/autofill-slush.html pasadena auto rental leasing ] [http://s1.shard.jp/bireba/symantec-antivirus.html mcafee antivirus free trials ] [http://s1.shard.jp/frhorton/4jl7mv47m.html adoption african american baby ] 22 auto barrels pistol ruger target http://www.textcbasliboc.com

Contents

Status

Released 14 Jan 2008

Overview

As the name implies, SQL injection vulnerabilities allow an attacker to inject (or execute) SQL commands within an application. It is one of the most wide spread and dangerous application vulnerability. The CLASP project provides a good overview of SQL injection.

Example of SQL injection

The following Java servlet code, used to perform a login function, illustrates the vulnerability by accepting user input without performing adequate input validation or escaping meta-characters:

conn = pool.getConnection( );
String sql = "select * from user where username='" + username +"' and password='" + password + "'";
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
if (rs.next()) {
loggedIn = true;
	out.println("Successfully logged in");
} else {
	out.println("Username and/or password not recognized");
}

It is possible for attackers to provide a username containing SQL meta-characters that subvert the intended function of the SQL statement. For example, by providing a username of:

admin' OR '1'='1

and a blank password, the generated SQL statement becomes:

select * from user where username='admin' OR '1'='1' and password=' '

This allows an attacker to log in to the site without supplying a password, since the ‘OR’ expression is always true. Using the same technique attackers can inject other SQL commands which could extract, modify or delete data within the database.

Attack techniques

For more information on SQL injection attacks see:

Defense Strategy

To prevent SQL injection:

  • All queries should be parametrized.
  • All dynamic data should be explicitly bound to parametrized queries.
  • String concatenation should never be used to create dynamic SQL.

For more details, see the OWASP SQL Injection Prevention Cheat Sheet.

Parameterized Queries

All data access techniques provide some means for escaping SQL meta-characters automatically. The following sections detail how to perform input validation and meta-character escaping using popular data access technologies.

Prepared Statements

Variables passed as arguments to prepared statements will automatically be escaped by the JDBC driver.
Example: ps.1

String selectStatement = "SELECT * FROM User WHERE userId = ? ";
PreparedStatement prepStmt = con.prepareStatement(selectStatement);
prepStmt.setString(1, userId);
ResultSet rs = prepStmt.executeQuery();

Although Prepared Statements helps in defending against SQL Injection, there are possibilities of SQL Injection attacks through inappropriate usage of Prepared Statements. The example below explains such a scenario where the input variables are passed directly into the Prepared Statement and thereby paving way for SQL Injection attacks.
Example: ps.2

String strUserName = request.getParameter("Txt_UserName"); 
PreparedStatement prepStmt = con.prepareStatement("SELECT * FROM user WHERE userId = '+strUserName+'");

Stored Procedures

TODO

Hibernate

According to this forum thread hibernate uses prepared statements, so it is protected from direct sql injection, but it could still be vulnerable to injecting HQL statements.

Variable Binding

It is critical to use Bind Variables as mentioned in the example ps.1 above. Usage of PreparedStatement with Bind variables defends SQL Injection attacks and improves the performance.


Dynamic Queries via String Concatenation

The important thing to remember is to never construct SQL statements using string concatenation of unchecked input values. Creating of dynamic queries via the java.sql.Statement class leads to SQL Injection.

References