File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes SQL injection. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "SQL injection." Watch "SQL injection." New topic

SQL injection.

chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Hi all, I was reading about sql injection attack which is explained very clearly here. Then I tried on coderanch itself on my own account.

I did this:

1. Entered my user id
2. Entered my password like this mypassword' or 'x'='x

The system reported invalid userid or password. So i came to know that SQL injection will only succeed if there are not validations done at the server side. I notice that allowing the user to enter apostrophe in the input fields cause this serious attack.

As far as I understood when ever a user is posting data to the server, before it is hitting the database we have to validate the input data.

Am I right?

Love all, trust a few, do wrong to none.
Marco Ehrentreich
best scout

Joined: Mar 07, 2007
Posts: 1294

Correct! SQL injection will only work if a server application is vulnerable for it which usually means the programmer of the application didn't know about SQL injection or wasn't careful enough!

I guess these "hacks" like yours won't work today for the more popular web sites (hopefully). This kind of security problem is well known for years and there are often ways to easily prevent such exploits. With Java/JDBC for example you should use PreparedStatements which can help to avoid that any SQL expressions given for parameter values (like in your example with the password) will lead to SQL injection exploits because all SQL statements are precompiled and therefore parameter values like x=x won't get interpreted. In the worst case an application using PreparedStatements properly will tell you that the value for a parameter is not valid, but it usually won't allow you to inject SQL snippets that easily.

Of course there is still room for plenty of other security problems. So it won't be enough to just use PreparedStatements and think that you're done and your application is highly secure ;-) Like you already said, validation is a must for publicly accessible applications like a typical web app and validation can and probably should take place in multiple places (frontend, business logic, data access layer etc.) in case of multi-tiered applications.

chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Thank you Mr. Marco for explaining in detail. For year I have been using Statement instead PreparedStatement when it comes to coding applications using pure jdbc instead of ORM frameworks. From now on I will use PreparedStatement-s. I thought PreparedStatement-s are used for pl/sql or while using blob or clob data types.

Thanks again.
I agree. Here's the link:
subject: SQL injection.
It's not a secret anymore!