Making a website ,using SQL server and never heard of SQL injection attacks .....
Then please read this post before you go ahead with launching your website.
A Small example
Suppose this is your sql statement for your Login control
select * from profiletable where username='Admin' and password ='P@ssword123'Now when a user enters
Admin'-- at the textbox
Then the sql statement becomes
select * from profiletable where username='Admin'--' and password ='P@ssword123'In Sql
-- is used as comment,Hence the SQL statement which is executed is ....
select * from profiletable where username ='Admin'-- comments the sql query after 'Admin' and hence the user gets the access to the details of the user without the need of the password .
This was a very simple example of SQL injection attacks ,Lets see how a user finds out how the details of program .....
Obtaining Information about database using Error MessagesA User can get information about your database using Error messages .
The Attacker will purposefully enter such entries in the Textbox which might give an error .
For Example:
Line 26: com.Parameters.Add("@user",SqlDbType.DateTime).Value=TextBox1.Text;
Line 27: //com.Parameters.AddWithValue("user", TextBox1.Text);
Line 28: SqlDataReader dr = com.ExecuteReader();
Line 29: if (dr.Read())
This Error message will give a lot of information about your program and database.....
Advanced SQL injection attacks include use of Extended,Custom Stored Procedures and many new innovative methods .....
Hence follow these set of guidelines when making a website :*Use of Parametric SQL Queries and if possible use of stored procedures - (Parametric queries validate the input entered and stored procedures donot show the query details on errors )
*Use of Linq if you are building application on .Net 3.5 framework .Linq is safer and faster than SQL .
*Use of TRY,CATCH blocks with sql query and defining error pages .
*Making your application an N-layer application having clearly defined rules in buisness layer to know who all can access the Data Layer.
*Rejecting Bad input ,Accepting Good input : Validation
function escape( input )
input = replace(input, " ' ", " ' ")
escape = input
end function
function validate_string( input )
known_bad = array( "select", "insert", "update", "delete", "drop", "--", "'" )
validate_string = true
for i = lbound( known_bad ) to ubound( known_bad )
if ( instr( 1, input, known_bad(i), vbtextcompare ) <> 0 ) then
validate_string = false
exit function
end if
next
end function
function validatepassword( input )
good_password_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
validatepassword = true
for i = 1 to len( input )
c = mid( input, i, 1 )
if ( InStr( good_password_chars, c ) = 0 ) then
validatepassword = false
exit function
end if
next
end function
*SQL Lockdown
- Determine methods of connection to the server-Verify that only the network libraries you're using are enabled, using the 'Network utility'
- Verify which accounts exist-Remove unneccessary accounts,Ensure that all accounts have strong password ,Run a password auditing script to against the server on regular basis .
- Verify which objects exist-Remove the extended stored procedures which are not needed .
There are Tools to determine this which you can find at www.Sqlsecurity.com.
So now rethink about your application and design it using these guidelines .....