Is it possible to prevent SQL-injections solely by filtering out specific characters

Is rigourous filtering of characters a possible way to prevent SQL-injections? I understand that filtering means that you lose functionality as special characters can be a valid part of a valid string.

I also understand that parametrisized is the (generally accepted) best way to go.

AlfahaneAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Garry GlendownConnect With a Mentor Consulting and Network/Security SpecialistCommented:
Well, anything that might terminate a string (` ยด ' "), backslash (\), semicolon (;) come to mind ... anyway, they may be perfectly legitimate at times, so filtering all occurrences may be as bad as not filtering them ... e.g. somebody may have the name "O'Hara" - do you want to forbid him to type his name? Or a description of some item might have a ";" in it ...
Of course you can narrow it down a bit if you know the code - e.g., if you're certain all strings for SQL queries always are made with double quotes ("), you could limit filtering to just that character, allowing the other single quotes. I can't imagine many uses of the backslash, so not allowing that shouldn't hurt too much. The semicolon will mostly hurt in combination with the quotes, so if you've taken care of the quotes-problem, the semicolon shouldn't be much of a problem anymore ...
0
 
Garry GlendownConsulting and Network/Security SpecialistCommented:
Special characters should always be allowed, but you need to ensure that they are encoded/decoded before you use them for building a query.

e.g, imagine somebody entering the following input into a string field:

john doe"; drop database customer;

Open in new window


without escaping, this might seriously harm your database ;)

You need to make sure to sanitize any input to a web form (or GET variable value) before handing it to your database. In PHP, you could use functions like "mysql_real_escape_string" to "fix" the input so that any legitimate information can be searched for; in this example, it would ensure that plain concatenation of the string to the query would be safe for execution ..
0
 
dportasCommented:
You could filter out special characters but you generally shouldn't. Passing your inputs as parameters is the best way to avoid SQL injection.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
AlfahaneAuthor Commented:
Yes, passing as parameters is the best. However, still need to investigate character filtering for an old system that will be shut down but need higher security without changing all queries to parametrized.
0
 
Garry GlendownConsulting and Network/Security SpecialistCommented:
Is it available in source? How many pages are there that get input from forms? I doubt there's some "silver bullet" that will be able to fix the problem for you ... at least not for little or no money and without work ...
Theoretically, an IDS or application firewall could be set up to look for and block certain sequences, but will require some in-depth understanding of what is permitted and safe, and how to set up the system ...
0
 
AlfahaneAuthor Commented:
Ok, so there is not a list with characters and pose a threat?
0
 
Russell_VenableCommented:
I think its important for me to add info here. Its not safe by any means to leave single quotes in. If you absolutely need too. Make sure you filter for those and add a (\) to each single quote so as to not execute a command for your SQL database, this can be achieved by using "addslashes()". Thought if you dont need those quotes at all. I would disable the option "magic_quotes_gpc" that is on by default. You should use "mysql_real_escape_string" or "mysqli_real_escape_string" to escape variables or query's before executing it before anything that has a user definable variable can reach the SQL query. Also note that you need to filter for XSS attacks as well. You can read more about a few other attacks here and fixes for those attacks Foiling cross site forgery. Good luck!
0
All Courses

From novice to tech pro — start learning today.