views:

96

answers:

3

Hello, I have some Regex, it looks like this:

string regexForDrop = @"^((%27)|'|(\-\-))\s*(d|%64|%44)(r|%72|%52)(o|%6F|%4F)(p|%70|%50)$";

It works fine, when i write to the input "--drop", but it does not works, when i write "drop table users" or something like that. I need that it would be working, no matter what comes after "--drop". How i can implement that?

Thanks

+6  A: 

It seems you trying to prevent a sql injection attack. For this use Parameterized Queries this way you don't need to check for injections.

Good Read:
http://weblogs.asp.net/scottgu/archive/2006/09/30/Tip_2F00_Trick_3A00_-Guard-Against-SQL-Injection-Attacks.aspx

JeremySpouken
+1, what other `bad` statements will the regex miss?
KM
Yes, I know about SP, i just do some research for studies about regex to. Thanks for the link, I would read it.
Vytas999
+2  A: 
string regexForDrop = @"^((%27)|'|(\-\-))\s*(d|%64|%44)(r|%72|%52)(o|%6F|%4F)(p|%70|%50).*$";

? or without the $

RC
Great. Thank You !
Vytas999
+1, whatever @Vytas999 reason, you wrote the code to solve it
KM
+1  A: 

Remove the '$' at the end of your regular expression. $ matches the end of the input string.

cleek