国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
Preventing SQL Injection in Java

Contents

 [hide]

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.

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
ProFTPD Character Encoding SQL Injection Vuln...
網(wǎng)絡(luò)攻擊技術(shù)
用jdbc.properties文件配置連接數(shù)據(jù)庫 心得
淺談 JDBC 中 CreateStatement 和 PrepareStatement 的區(qū)別與優(yōu)劣。
Java開發(fā)之JDBC學(xué)習(xí)分享
如何提高JDBC性能
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服