I am trying to simply prove here that this simple function is not good enough to prevent every sql injection in the world:
Function CleanForSQL(ByVal input As String) As String Return input.Replace("'", "''") End Function
Here is a typical insert statement from one of our applications:
Database.DBUpdate("UPDATE tblFilledForms SET Text1 = '" + CleanForSQL(txtNote.Text) + "' WHERE FilledFormID = " + DGVNotes.SelectedRows(0).Cells("FilledFormID").Value.ToString)
I know this is not safe, due to a google search and other questions on StackOverflow.com. There is one question that I found in which all functions, such as the one that I presented above, are irrelevant and meaningless.
So, based on the message I'm connected to, simply by typing
'Chr (8); update tblMaint SET Value1 = 2 WHERE ValueID = 2 -
in txtNote should be enough to clear every value in text1 throughout the tblFilledForms table and then update the second row of the tblmaint table to be correct?
What SHOULD happen here is that VB will interpret this as
UPDATE tblFilledForms SET Text1 = '' 'Chr (8); update tblMaint SET Value1 = 2 WHERE ValueID = 2-- 'WHERE FilledFormID = 5120327
and send it to SQL, which will execute chr (8), to erase the third, which will create
UPDATE tblFilledForms SET Text1 = ''; update tblMaint SET Value1 = 2 WHERE ValueID = 2-- 'WHERE FilledFormID = 5120327
for correct execution in the database?
Then I copied Chr (8) from the clipboard and replaced Chr (8) in the text box with the contents of the clipboard and still failed. It puts the entire line directly in the field without any problems.
So what am I doing wrong here? or what else can i do to break it?
Technologies and background: I use MS SQL Server 2005 and VB.NET 2005. The Text1 field in the database is the Varchar field (600) (don't ask me why its not MAX, its meaningless, I know) The table has certain triggers that would prevent a massive update such as this, and throw some errors if the injection really worked correctly.
PS. I know that parameterized queries are a way to go here, and I'm not looking for answers, for example, "well, I donβt know why this does not work, but parameterized queries are the way." I am looking for an opportunity to prove that our software is broken and that we need to rewrite it using the best principles.
For anyone reading this question, to find out how best to filter your text fields, the answer DOES NOT! Use the options! they are much better, safer and easier!