Friday, July 30, 2010

The day when affirmative Sentences turnd into Interrogative....


You must be surprized by this childish title. but its about my child only; so i chose this one. It seems my daughter is growing too fast and getting crazy and stubborn at twice speed. She is going to be Two Year in this September.

This is the age when kid try to copy each and every action of your so is Ishwin. She seems to learn some thing very fast and some thing which is in our favour she never even bother to learn. Now a Parent can understand what are the things which we parent really matters. :(

Now about title , this is after two three days we relized that our asking her questions has changed her new found linguistic understanding a little. and again this is of NOT in our favour again.
Whenever she wanted some thing earlier she used to say
"Mumma Khana khana hai",
"Mumma Doodh peena hai",
"Papa ghunni chalna hai",
"Papa choket khana hai"
"Mumma Gali(Gadi Any 2Wheeler) pe bethna hai"

And I used to be Proud of her as she was mearly One and Half year old and can say many things as compare to other children of her age.
Now she is about to be Two in Sep/10. Now she still says all these sentences but end always have a new Word "KYA".
Yes now all her sentences end with question mark and i bluntly say Yes or No to all her questions.
Thanks to her Mumma who said me She is not asking question she wants you to do that thing.

So Now when she wants me make her sit in Gadi She says "Papa Gali me bethna hai kya?" and when she wants to eat she will say "Mumma khana khana hai kya?" or any of above sentences which still have the same meaning for her and still she says even more clearly but Sentences are turnd Interrogative......

Thursday, February 4, 2010

SQL Injection

In my last article I discussed about the performance issue with SQL Cursors. In this article I would like to discuss about the Database Security.

What is SQL Injection?

As name suggests, it is special kind of Sql attack on your database. And your Application is the interface for this Attack. Here attacker enters some invalid string sequence in the user input Form or web Page and submit it. These strings may perform some destruction activity in your database.

While developing Web Site we always think about security, user Privilege, Authentication, Authorization, SSL and all… But despite Sql Injection fatality it is not given due attention this is what my perception.

I directly start with an Example.

Example:

This is what my simple Login Method does. Here I am not validating the user inputs. This is a hypothetical example of user login. Here I am using normal SQL to check whether User is Exist or Not.

try

{

using (SqlConnection cn = new SqlConnection())

{

cn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();

cn.Open();

SqlCommand comLogin = new SqlCommand();

comLogin.Connection = cn;

comLogin.CommandType = CommandType.Text;

comLogin.CommandText = "Select * from Users where UserName= '" + txtUserName.Text + "' and " +

"Password = '" + Password.Text + "'";

SqlDataReader dr = comLogin.ExecuteReader();

if (dr.Read())

{

// Succsess

Response.Redirect("LoginSuccess.aspx");

}

else

{

// Failure

Response.Redirect("LoginFail.aspx");

}

}

}

catch(Exception ex)

{

throw ex;

}

In above example I am directly taking the user input from controls.

Now suppose I do enter User Name as Nitin and Password as ‘; Drop Table Users -- ‘ and press Login button. You just guess what damage will be caused by this.

If your Sql profiler is on you will find some thing like below is executed.

Select * from Users where UserName= 'nitin' and Password = ''; drop table Users --''

Above command actually executes two SQL queries.

  1. Select * from Users where UserName= 'nitin' and Password = '';
  2. drop table Users --''

End result, I do not get any record but Users table is dropped. Instead of dropping the Table, you can write some other command which can update existing records or even insert new record.

Or you can just trace pass this login validation by entering password as ' or 1=1 ---'

It makes end sql statement like this.

Select * from Users where UserName= 'nitin' and Password = '' or 1=1 ---''

It always returns records because 1 = 1 is always true.

By now we understood this attack can be performed by skilled attackers like you and me. J

There are many more ways or variety of Inputs an attacker can perform and cause following damages.

  • Delete, update, and insert the data in tables.
  • Change existing important data like Email Id for particular user and can get other information.
  • Get the information about the existing tables and Columns of database.
  • Drop the entire table.
  • Shut down the Sql Server itself by executing command for this.
  • Insert some commands or executable statement in table which may later on cause some damage.
  • Get the information about the Machine which is hosting the Sql Server by executing some system procedures.

And many more ………..

All depends on Attackers expertise with SQL

What can we do to avoid these attacks and make the system more robust?

  1. Replacing Single Quote with Double Quote

I think everybody has used this line of code.

serName.Text.Replace("'","''")

Generally here we are replacing any single quote with Double one. Actually to save Single Quote we have to replace single Quote with Double quotes, that is what we did here. But knowingly or unknowingly we avoided many Sql Injections too.

  1. Validate the input for its type and length.

We may have user input where we expect the numeric value. Just replacing the single Quote with double will not work in this case. Suppose your SQL query is expecting a numeric value and with out validation something like “1; Drop table Users” is passed to your Query. Check this SQL

Select * from Users where UserID = 1; Drop table Users

In above example user write a numeric value “1” which application is expected and then put a semicolon “;” which ends first SQL query and then wrote “Drop table Users”. This will do all the damage. To avoid this we should always validate the Data at first place. We should validate our data for type, Length, escape sequences, etc.. Never build your query directly from User Input, just like what I did in my example. ;)

  1. Use Parameterized Dynamic SQL or Parameterized Stored Procedures.

Using parameter collection has two advantages.

    • Whole Input will be treated as Literal value not an executable code. So even you enter password ‘; Drop Table Users -- ‘. It will do no harm at all like it did in case of direct SQL.
    • You can validate the input for its type and length like below.

comLogin.Parameters.Add("@UserName", SqlDbType.VarChar, 5);

  1. Do not show end user actual database error.

Error message can have information about your database schema, which can be used by attacker for Sql Injection. It is good practice to show the User a general user friendly error message and log the actual Error in Database.

  1. Always store the confidential data in Encrypted format

It is a good practice to store the confidential data in encrypted format even you are taking all the precaution to avoid any SQL Injection.

  1. Do not use Admin user account

If not necessary do not use Admin user or user with Owner rights for connection the database.Better use an account with less privileged, which can perform only the necessary SQL Operations.

References:

Friday, January 29, 2010

Sql Cursors

Hello Coders,

As we all know Sql Cursors, We used them for performing a certain operation in each row of record set, but why should we avoid using them?

Yes, Cursor hampers the performance.
They can lock the “Tables” which are used for populating it, so other users can not be able to update those tables while the Cursor is open.
So considering a long running cursor operation, situation can be very worst.

Some times we may get better execution time in cursor as compare to complicated T-Sql commands which we used to avoid cursor, but still cursor will lock the tables for other users so it may increase the execution time for other users.

We should considering all scenarios before opting for Cursor.

Here are some tips on avoiding Cursors:

• Do not write cursor for performing the same task on each row. Use T-SQL queries, even we may have to write many. It still gives far better performance.

• We can use "Case" statements for conditional updating in records.

• Use Temporary table "#TableName" for some subset specific changes in a table. We can also use "TABLE" variable type which available in Sql Server 2000 and upper version.

• Make use of "Derived Tables", which are also temporary tables.

• Use the "While" loop, because unlike cursors "while" loop doesn't lock the tables while looping through.

Here in this post I am not using any code sample or Syntax being a lazy bum. ;)
But these leads are more then enough for a smart coder like you.
Happy Coding
Cheers,
Nitin