I've written a tiny string scrubber for my ASP.NET application. I thought I'd post it here just to get input on it - a community built scrubber would probably give us all a better answer to work with so I'll throw this one out there as a place to start.
I imagine there are more efficient ways to do this - I went with the 'long hand' approach so that it would be clear what was going on and why.
Public Shared Function ScrubString(ByVal str As String) As String 'Purpose : To scrub strings of characters that might be used ' : in SQL injection attacks. 'Trim spaces
str = str.Trim
'Scrub potentially dangerous commandsstr = Regex.Replace(str,
"OPENROWSET", "", RegexOptions.IgnoreCase) 'Kills the first part of some powerful SQL commands such as xp_cmdshellstr = Regex.Replace(str,
"xp_", "", RegexOptions.IgnoreCase) 'Scrub potentially dangerous non-alphanumeric charactersstr = str.Replace(
"-", "") ' - :used to comment out portions of a SQL query.str = str.Replace(
"*", "") ' * :used in hacked SQL to fish for information.str = str.Replace(
"&", "and") ' & :used to create special characters on web pages.str = str.Replace(
"=", "equals") ' = :used in hacked SQL to fish for information.str = str.Replace(
">", ")") ' > :used to create unwanted HTML elements.str = str.Replace(
"<", "(") ' < :used to create unwanted HTML elements.str = str.Replace(
"%", "") ' % :used in like statements to fish for data.str = str.Replace(
"'", "") ' ' :used in string manipulation of SQL statementsstr = str.Replace(
"/", "") ' / :used in SQL injection attacksstr = str.Replace(
"\", "") ' / :used in SQL injection attacksstr = str.Replace(
";", "") ' / :used in SQL injection attacks Return str End FunctionWhile I appreciate your effort and intentions, you will find that there are many, many times that some of the characters you have being filtered are legitimate characters in a data entry field. This is a pretty generic and pessimistic approach.
With web-apps, the best solution is to provide validation for all data entry fields. At that point, you can apply a regular expression filter (for instance) to identify unwanted characters. You can certainly create a helper class, such as StringScrubber, that exposes methods to handle the different scenerios you run across and assists with the validation so that the process is shared by all of your objects - which is what I think your intention is.
An example what I mean is to provide a validation check to determine if the entered value is actually an e-mail address, a date, numeric, social security number, url, etc. By restricting the field during validation, you can accomplish the same thing in an even MORE restrictive way than you have shown. Afterall, if it is a valid e-mail address, then it doesn't have most of what you are filtering already.
Not sure if that's the kind of feedback you were looking for. Just my 2 cents.
I used stored procedures for everything, However, I'd heard at one point that parameterized procedures weren't entirely safe. I'm not finding information on that right now besides the following:
While stored procedures seem to be a wonderful panacea against injection attacks, this is not necessarily the case. As mentioned above, it is important to validate data to check that it is correct and it is a definite benefit of stored procedures that they can do this; however, it is doubly important to validate data if the stored procedure is going to use EXEC(some_string)
where some_string
is built up from data and string literals to form a new command. Source: http://www.codeproject.com/cs/database/SqlInjectionAttacks.asp
So it may very well be that my string scrubbing is unnecessary if I stick with the stored procedures since I wouldn't EXEC on a string in a stored procedure anyways.
MadGerbil:I used stored procedures for everything, However, I'd heard at one point that parameterized procedures weren't entirely safe.
Thank you for the information. If I understand you correctly it is when a parameter is used to build a string instead of being used as place holder in a complete statement that a problem arises. That is, use a parameter as a parameter and not as a variable for string concatenation.
Copyright (c) Marimer LLC