SqlShare.com Logo
 
Skip Navigation Links
Home
Video List
Events
Classes
About Us
Login / Register
Subscribe RSS Feed 

Confio Ignite

Preventing SQL Injection With String Filtering

SQL injection is an attack that is done by entering data that changes the execution of certain SQL queries. Plamen demonstrates how to reduce the chance of an injection attack by using string filtering.

Duration:
4 mins 48 secs
Skill Level:
100
Rating:
4.11 out of 5
Publish Date:
November 06, 2008
Preventing SQL Injection With String Filtering You must be logged in to view this video.  
Bookmark and Share
 
1=Poor, 3=Good, 5=Excellent

About the Author

Image of Plamen Ratchev
Plamen Ratchev has over two decades experience in the software architecture and development field. He has enjoyed building solutions for UNIX, DOS and Windows platforms, with main focus on SQL Server since version 4.21. Plamen is founder of Tangra, specializing in relational database applications analysis, implementation, and tuning. His particular interest is in design patterns, performance a...

References

There are no downloads or recommended reading links for this video

Comments
Satish on 1/20/2009
Say if one of my product name Dropper, then that product will not be processed as it will raise error... so how do u think we can handle such situations?

chapas on 1/20/2009
I guess it's a very rare situation, altough we could serach for '%DROP %' instead of '%DROP%' that would reduce the odds. Right?

Claudio Pracilio on 1/20/2009
Extremely poor implementation of dynamic filtering. This exposes SQL syntax in the filters, as opposed to just the data/columns to be filtered on.

Rob Griffis on 1/20/2009
This is really not a good idea. There are almost infinite syntaxes that cause an SQL injection. The use of parameters should be used.

Harvey Schwartz on 1/20/2009
Is there a more generic approach that does not require a detail filtering against all possible dangerous T-sql commands?

Plamen Ratchev on 1/20/2009
Satish, as noted by Chapas you can add an extra space after DROP and make the filtering more precise: '%DROP %'. Note the space between DROP and %. A more advanced approach would be to query the system catalog views/tables and get the names of your objects/tables, then exclude those from the filter.

Plamen Ratchev on 1/20/2009
Claudio, if you allow end users to freely type text that you concatenate to a dynamic query and execute, then you are left with little options but to filter the user input. It can be done on the client side or server side, but it drills down to a form of filtering for keywords that they should not be using.

Plamen Ratchev on 1/20/2009
Rob, this is correct. However, the point here is not to illustrate best practices on how to handle dynamic queries, but to show how to protect the code when no parameterization is used.

Plamen Ratchev on 1/20/2009
Harvey, the best way is to avoid dynamic SQL! But it cannot always be the case as dynamic SQL has good uses for implementing dynamic search, some admin functions, dynamic pivoting, etc. The best approach is: use parameters instead of freely concatenating user entered text; use the system stored procedure sp_executesql instead of EXEC; add additional protection like the QUOTENAME function to concatenate identifiers, etc. If you parameterize your queries then there is no need for detail filtering at all.

Stu Bailey on 1/28/2009
This was a very useful recommendation...I intend to implement this recommendation asap.

Need more complete list of key words to prohibit.

Plamen Ratchev on 2/10/2009
Satyabodhi, the list can vary based on your requirements. Here is just an example of what it may be: "DROP, ALTER, CREATE, TRUNCATE, DELETE, UPDATE, INSERT, SELECT, EXEC".

82796FB536 on 4/30/2009
Hi There I still believe that the best way is to alway parameterize the commands send to the sql server.

Plamen Ratchev on 5/1/2009
82796FB536, parameterization is the best way to solve SQL injection. This is just an additional method. What if your client API does not support parameterization?

01EFFB45CF on 5/30/2009
Very nice. Will use this example in productin.

afshin on 2/20/2011
Very good exmaple. Liked it. Thanks

mastanrao on 6/18/2011
Excellent!!!

Jason Vollaire on 6/27/2011
Very useful training tip.

Binod on 6/27/2011
I really like this Video, act I was searching this kind of Anti SQL injection materials. Thnaks Good Job.... Al the best.

Cosmin Tornea on 6/27/2011
cool

Scott Taylor on 6/27/2011
This is a half-baked solution for a non-problem, especially if best practices are being used. Direct SQL should never be allowed from the presentation layer, particularly not in production. Parameterized SQL or the equivalent (created using your favorite technology) is the only way to avoid the chances for SQL injection attacks (and is also a lot more efficient).

Joe DeMarco on 6/27/2011
I've seen this technique before,but it's hard to assure that all possible conditions for an SQL injection attack can be covered unless the if statement gets pretty complicated, and maybe not then.

Calvin Johnson on 6/27/2011
Very good!

245BE142DD on 6/27/2011
extremely simplistic

Faisal Lodhi on 6/27/2011
Thanks, Great topic!

Victor Maldonado on 6/27/2011
was hoping that the intructor would cover parameterizing in sp_executesql

Team FAB on 6/27/2011
There are probably better ways of doing this, but I guess this would work.

bc on 6/27/2011
Would have been better to show normal execution of procedure with valid parameter, then injection attempt, then demoing the prevention piece of the script.

DavidB on 6/27/2011
It might be good to have these scripts written before and then just talk through the code that you have in place. It would probably save some time in the presentation. Good job.

Devrim on 6/27/2011
Poor programmings skills with dynamic queries, poor technics like 'concatanatings strings"....

Daniel Frankel on 6/27/2011
not a great mechanism to detect SQL injection as very often some of your search criteria may contain SQL keywords.

Daniel Frankel on 6/27/2011
I agree that this is not a good method of preventing SQL injection. The key is to escape any single quotes from the string, For example if you have a paramter where sku='@param' and your @param = xxx';Drop table products, you should escape the quote as xxx'';Drop table products so the final query looks like where sku='xxx'';Drop table products' This will prevent SQL Injection

Alejandro on 6/28/2011
Excellent video, but too short...

Munna Bhai on 6/28/2011
Excellent video.Thank you for sharing and keep the great work.

aaron williams on 6/28/2011
What if "drop" or "update" are legitimate words to have as part of the search criteria? It seems like there must be a better way to prevent sql injection than this.

Plamen Ratchev on 6/30/2011
Aaron, yes there is a better method. That is to use correct parameter queries. See more details here: http://pratchev.blogspot.com/2009/03/sql-injection.html. The method in this video is only supplemental in case parameterization cannot be used. There are some APIs that do not allow parameter queries.

ravi on 7/2/2011
goog one

Kat on 7/12/2011
Good topic

lojze kavsek on 10/4/2011
Very simple and clear sample!

Maurice Ivory on 11/16/2011
I liked this, I learned a few things

A Alagu Ganesh on 3/30/2012
Good



Must Be Logged In
 

How Do I Become a Video Author? |  Newsletter History

Copyright © Fourdeuce, Inc., 2005-2009. All Rights Reserved | Privacy Policy | Terms & Conditions