Solved

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

Posted on 2011-09-26
7
243 Views
Last Modified: 2012-05-12
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.

0
Comment
Question by:Alfahane
7 Comments
 
LVL 17

Expert Comment

by:Garry-G
Comment Utility
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
 
LVL 22

Expert Comment

by:dportas
Comment Utility
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
 

Author Comment

by:Alfahane
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 17

Expert Comment

by:Garry-G
Comment Utility
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
 

Author Comment

by:Alfahane
Comment Utility
Ok, so there is not a list with characters and pose a threat?
0
 
LVL 17

Accepted Solution

by:
Garry-G earned 500 total points
Comment Utility
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
 
LVL 15

Expert Comment

by:Russell_Venable
Comment Utility
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

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Getting hacked is no longer a matter or "if you get hacked" — the 2016 cyber threat landscape is now titled "when you get hacked." When it happens — will you be proactive, or reactive?
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now