1

Examining some logs in my web site, I discovered that a bunch of querystring variables contains sql injection snippets:

'myvalue AND CHAR(124) USER CHAR(124)=0 AND ='
'myvalue AND 1=2'

How do you sanitize the querystring variables?


The platform is ASP.NET, fm 4.0, SQL Server 2008. So go with the parametrized query. But there is a part of the application (an old one) running classic ASP. There is no parametrized query in classic ASP...

2
  • 3
    Don't sanitize - don't even start to try. Use parametrized queries instead - always, no excuses. See: codinghorror.com/blog/2005/04/… Commented Feb 28, 2012 at 14:44
  • Good news: there are many bots that try this stuff on every web page they find on google. This could be just a bot. Commented Feb 28, 2012 at 14:46

2 Answers 2

4

You shouldn't worry about sanitizing query string values.

You should worry about writing code that doesn't have SQL Injection vectors to begin with. Parameterized Queries are the way to go. Depending on your platform, the exact SQL can vary a bit.

As it seems you already know how to work with parameterized queries in .NET, I'll skip that.

As for the Classic ASP page, you should take a look at How To Call a Parameterized Query with ADO (paying special attention to the change at the bottom to use a SQL Statement rather than a Stored Procedure).

Sign up to request clarification or add additional context in comments.

Comments

0

I think I had two websites of mine hacked by such a hack! The way to stop the hack is to check the data being read before it hits the database.

If the value is a number check the string has only chars 0-9 in it, if not send them to the page not found!

If you are using strings like 'yes', 'no', 'true', 'false', values that you define don't add them to the database string directly, check for them with an if such as, If(request("dog").equals("true")){...

finally if the value is a string that the client has entered then watch for ' char. An insertion hack works by first closing the string with ' then the expression with ; and then adding and update, insert or delete statement. so if ' ; and for good luck " is escaped the string cannot be closed.

Note: the same thing is true for text boxes, form sends and Ajax.

Always do these checks on the server as a hacker can always fake a page.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.