Introduction

A developer never wants to get hacked his own web application .But intruder , malicious persons are more than developers, and I used to be one of them and then turned into a developer .As i have walk in both the shoes , so i have decided to write a series of articles which will definitely help to hack proofing a web application .

A developer should always concerned about hack attempts in their applications ,and its a developer duty as well. Lots of online tools, spoofing tools, sniffers tool, etc., are available on the internet .so even a normal internet user can turned into a hacker . i hope everybody knows the consequences of being hacked , so by not describing them , i better do write my article.

lets gets start understanding of some hacks and how a developer can prevent them .in this first article i will start by sql injections.

SQL Injection

SQL injection is an attack in which one or more commands are inserted into a query to form a danger query which may retrieve , damage , manipulate your existed data channel. This almost always occurs when dynamic SQL is being used and when you’re concatenating strings in your code (C#,VB,J#,F#) to form SQL statements. SQL injection can occur in your Microsoft .NET Framework code if you’re forming a query or procedure call, and it can occur in your server-side T-SQL code as well, such as in the case of dynamic SQL in stored procedures.

SQL injection was number one attack in 2010 .And legacy coded applications are still vulnerable to sql injections. let me describe it more clearly or we can say in simple language , it happens when commands(or other sql queries) are inserted where we were supposed to send the DATA into sql .We can divide a whole query into two channel control channel (query) and data channel (user inputs). A attacker usually do not care about your control channel (query) , he just do care ,how he can insert malicious query in your data channel.

There are other ways too to inject sql injection : -

String truncations from SQL functions

Automated tools

Except being hijacked of any individual or group accounts , sql injection can help to do virtually anything on the system that permissions allow : -

How is it Exploited -

In the above code we are concatenating the string with user input data and forming a sql statement .and we supposed to work our query like in the following image -

But a hacker thinks differently , he will manipulate the query as in the following image -

SQL Injection Used in the above example -

' union select username , password ,'1' from [User] --'

Above is just a example, there are many sql injections which a hacker can try on a application.Hacker can get the passwords , install the backdoor . hacker can manipulate the query to get the data from sysobject for all the database detail .

How to prevent SQL Injections -

Validate user input (Using Regex or anything else)

Using Parameterized query

Use Stored procedure

Use an ORM

1.Validate user input

The simplest way is that we can validate the user input using Regular Expression and replace the danger character by blank

But but this is not the best way to prevent application from sql injection .Because SQL Injection does not only contains "-" or " ' " . There are many sql injections which consists legal code too.

2.Using Parameterized Query -

if we want to use inline SQL ,then to stop SQL injection we can pass the parameter in sql query and can add SQL Parameter in the query .this way we can prevent sql injection .there is nothing wrong in using sql statement inline.

parameterized query tell to the SQL server that data passed into any parameter will remain in the data channel thats how sql server prevent the SQL injection. But still its not the best way as we are writing business logic inline .every time i have to write again this query .

3.Using SQL Procedure -

The best way to prevent from SQL injection is use stored procedure . As business logic are hidden , Its provide better performance , reusability . Now you will have to just protect the table and stored procedure by using permissions .

Share

About the Author

I do believe life is to help others ... So here i am .. in my spare time i learn new things of programming and try to help people with my knowledge .
I'm an energetic, self-motivated and hard-working Developer and Information Technology Professional with experience in projects, website design and development.

I'm being hacked. And am very frustrated and have exhausted all avenues. Pls help me to know how to protect my network. I do believe this is mu nieighbour. I no u think I'm crazy, I'm not... Pls help me to layer my computer
And if I can with a real IP tracer or rules for my roughter. What ever software I need ur help. You are a developer
And I believe u can help me. My neighbourr I s a soft ware developer and programmer. Is there any way I can protect my network??? I would appreciate it if u call Ed me or e mailed me.

If you use a wireless network:
change the default SSID and administration username and password
turn off your SSID broadcast
turn encryption on and use the strongest encryption option available
restrict access so that only specific computers or devices can access the network
turn off remote access.http://lifehacker.com/5576927/how-to-stay-safe-on-public-wi-fi-networks[^]

I still cant believe people are still writing Sql inside their application code to connect to a Sql Server database. Not only is this fraught with danger (as you are trying to rectify with this post), but it also tightly couples the data base logic with the application logic. Creating a stored procedure helps mitigate a number of the Sql Injection problems, unless of course you are using dynamic Sql and executing that directly.