SQL Injection

 If we use the Statement object to send the query then SQL Injection will happen 


SQLInjection

==========

users

username    upwd

sachin        tendulkar

virat             kohli


eg:

select count(*) from users where username ='"+uname+"'" and upwd =' "+upwd+"'";

username = 'sachin'

password = 'tendulkar'

Query nature

select count(*) from users where username ='sachin' and upwd =' tendulkar' ";

validation is succesful and given the authentication


eg:

select count(*) from users where username ='"+uname+"'" and upwd =' "+upwd+"'";

username = 'sachin'--

password = 'tendulkar'

Query nature

select count(*) from users where username ='sachin'-- and upwd ='tendulkar'

";

validation is succesfull and given the authentication


Note:

1. -- Single line sql comment

2. /*

Multiline sql comment

*/

If we use Statement Object to send the Query, then the problem of SQLInjection will

happen.

eg: Statement stmt = con.createStatement();

String query = "select count(*) from users where username

='"+uname+"'" and upwd =' "+upwd+"'";

ResultSet resultSet =stmt.executeQuery(query);                                                                                                        |                                                                                                                                                                    |DB: select count(*) from users where username ='"+sachin'-- ";                                                                |

count(*) = 1 (validation is successful give authentication)


if we use PreparedStatement Object to send the Query, then the problem of

SQLInjection will not happen.

eg: String query = "select count(*) from users where username =? and upwd

=?";


PreparedStatement pstmt = con.prepareStatement(query);

pstmt.setString(1,"sachin'--");

pstmt.setString(2,"tendulkar");

ResultSet resultSet =pstmt.executeQuery()                                                                                                                |                                                                                                                                                                    | for compilation using PreparedStatement                                                                                                    |                                                                                                                                                            DB: select count(*) from users where username =? and upwd =?;                                                                    |                                                                                                                                                                    |                                                                                                                                                            select count(*) from users where username ='sachin'--' and upwd='tendulkar';                                                    |                                                                                                                                                            count(*) => 0 (validation not succesfull so no authentication)

Note: In real time database used in production envrionment is "Oracle", only during

development phase we

use "MySQL" database.

In MySQLDatabase, we can't perform "SQLInjection" through comments,it

happens only in "OracleDatabase".

Comments

Popular posts from this blog

Advantages and Disadvantages of Monolithic Architecture

Monolithic Architecture

Eclipse debugging