Solved

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

Posted on 2011-09-26
7
278 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 18

Expert Comment

by:Garry Glendown
ID: 36598514
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
ID: 36713078
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
ID: 37106665
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
What, When and Where - Security Threats from Q1

Join Corey Nachreiner, CTO, and Marc Laliberte, Information Security Threat Analyst, on July 26th as they explore their key findings from the first quarter of 2017.

 
LVL 18

Expert Comment

by:Garry Glendown
ID: 37106716
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
ID: 37106742
Ok, so there is not a list with characters and pose a threat?
0
 
LVL 18

Accepted Solution

by:
Garry Glendown earned 500 total points
ID: 37107045
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
ID: 37260640
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

Are You Headed to Black Hat USA 2017?

Getting ready for Black Hat next week? Kick things off with the WatchGuard Badge Challenge and test your puzzle and cipher skills. Do you have what it takes to earn our limited edition Firebox Badge? Get started today - https://crimsonthorn.net

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

623 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