<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>How do I mitigate a SQL injection vuln?</title><link>http://blogs.msdn.com/ericlippert/archive/2006/11/01/how-do-i-mitigate-a-sql-injection-vuln.aspx</link><description>Joel points out today that SQL injection vulnerabilities are common and bad, bad, bad. He does a good job of describing the attack but doesn't really talk about how to mitigate it. When I advise people on how to close security holes like this I always</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: How do I mitigate a SQL injection vuln?</title><link>http://blogs.msdn.com/ericlippert/archive/2006/11/01/how-do-i-mitigate-a-sql-injection-vuln.aspx#927168</link><pubDate>Thu, 02 Nov 2006 00:17:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:927168</guid><dc:creator>Dave</dc:creator><description>&lt;p&gt;&amp;quot; A great way to mitigate the risk of a SQL injection attack is to use stored procedures. &amp;quot;&lt;/p&gt;
&lt;p&gt;Absolutely! Then call the stored procedures this way:&lt;/p&gt;
&lt;p&gt;query=&amp;quot;exec InsertRecord @value1=&amp;quot; &amp;amp; request.querystring(&amp;quot;value1&amp;quot;) &amp;amp; ...&lt;/p&gt;
&lt;p&gt;:-)&lt;/p&gt;
&lt;p&gt;Seriously though, versioning on stored procedures is a pain. Ad-hoc queries aren't *that* slow and you don't have to deal with the DBAs to get work done. For inserts I tend to stick with SPs though.&lt;/p&gt;
</description></item><item><title>re: How do I mitigate a SQL injection vuln?</title><link>http://blogs.msdn.com/ericlippert/archive/2006/11/01/how-do-i-mitigate-a-sql-injection-vuln.aspx#927192</link><pubDate>Thu, 02 Nov 2006 00:19:39 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:927192</guid><dc:creator>Bob Barrows</dc:creator><description>&lt;p&gt;&amp;lt;quote&amp;gt; use stored procedures &amp;lt;/quote&amp;gt;&lt;/p&gt;
&lt;p&gt;SQL Injection is still possible if stored procedures are used:&lt;/p&gt;
&lt;p&gt;1. If the stored procedure includes code to create and execute a dynamic sql statement using user inputs &lt;/p&gt;
&lt;p&gt;2. If the developer uses dynamic sql to execute the stored procedure, as in&lt;/p&gt;
&lt;p&gt;sql=&amp;quot;exec mysproc '&amp;quot; &amp;amp; userinput &amp;amp; &amp;quot;'&amp;quot;&lt;/p&gt;
&lt;p&gt;SQL Injection depends on the use of dynamic sql. Therefore, the best way to defeat it is to not use dynamic sql: use parameters to pass data to either a stored procedure or to a sql string containing parameter markers.&lt;/p&gt;
&lt;p&gt;This does not relieve one of the requirement to validate all user input, rejecting suspect data rather than forcing it into the database.&lt;/p&gt;
</description></item><item><title>re: How do I mitigate a SQL injection vuln?</title><link>http://blogs.msdn.com/ericlippert/archive/2006/11/01/how-do-i-mitigate-a-sql-injection-vuln.aspx#927279</link><pubDate>Thu, 02 Nov 2006 00:46:05 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:927279</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;Dave: there is always a balance between security and convenience. Deciding where that balance is requires a cost analysis of each, and the cost of low security is often quite high!&lt;/p&gt;
&lt;p&gt;Bob: Yes, I should have been more clear. &amp;nbsp;Call stored procedures with parameters, otherwise you're just trading one injection path for another.&lt;/p&gt;
</description></item><item><title>re: How do I mitigate a SQL injection vuln?</title><link>http://blogs.msdn.com/ericlippert/archive/2006/11/01/how-do-i-mitigate-a-sql-injection-vuln.aspx#927908</link><pubDate>Thu, 02 Nov 2006 04:21:09 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:927908</guid><dc:creator>roberthahn</dc:creator><description>&lt;p&gt;Actually, I'm not sure the advice regarding the string checker is at all that good - as proposed, anyone needing accented characters (or even Kanji) is going to find their content gets stripped out. &amp;nbsp;It's not every day that someone wants to report that a m&amp;#248;&amp;#248;se bit their webpage on your DB-backed web app, but it could happen, y'know?&lt;/p&gt;
&lt;p&gt;It might be smarter to simply evaluate which non-alphanumerics are bad and strip those out. &amp;nbsp;I wonder how Unicode-savvy SQL interpreters are -- perhaps you could replace 'bad' characters with some visual equivalent that's not in the ASCII range of characters.&lt;/p&gt;
</description></item><item><title>re: How do I mitigate a SQL injection vuln?</title><link>http://blogs.msdn.com/ericlippert/archive/2006/11/01/how-do-i-mitigate-a-sql-injection-vuln.aspx#927909</link><pubDate>Thu, 02 Nov 2006 04:22:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:927909</guid><dc:creator>roberthahn</dc:creator><description>&lt;p&gt;whups -- i should mention that dispite that nit, the article overall is very well thought out. I hope that people reading this learn how to put these ideas to work, then actually do it.&lt;/p&gt;
</description></item><item><title>re: How do I mitigate a SQL injection vuln?</title><link>http://blogs.msdn.com/ericlippert/archive/2006/11/01/how-do-i-mitigate-a-sql-injection-vuln.aspx#928026</link><pubDate>Thu, 02 Nov 2006 05:20:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:928026</guid><dc:creator>bleroy</dc:creator><description>&lt;p&gt;Robert: you're basically advocating for a blacklist. That's bad. You're right on the Unicode remark, but that doesn't apply to a numeric field, which was &amp;nbsp;Eric's example. For a text field, you've got to rely on the other layers of protection, because it's a string and basically everything is valid in there, including ' delete * from foo.&lt;/p&gt;
</description></item><item><title>re: How do I mitigate a SQL injection vuln?</title><link>http://blogs.msdn.com/ericlippert/archive/2006/11/01/how-do-i-mitigate-a-sql-injection-vuln.aspx#928176</link><pubDate>Thu, 02 Nov 2006 07:33:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:928176</guid><dc:creator>mike</dc:creator><description>&lt;p&gt;Steve Friedl posted a step-by-step walkthrough of how he cracked a SQL Server once. In that case, the door was apparently wide open, but still, it's interesting reading:&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://www.unixwiz.net/techtips/sql-injection.html"&gt;http://www.unixwiz.net/techtips/sql-injection.html&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: How do I mitigate a SQL injection vuln?</title><link>http://blogs.msdn.com/ericlippert/archive/2006/11/01/how-do-i-mitigate-a-sql-injection-vuln.aspx#928224</link><pubDate>Thu, 02 Nov 2006 08:09:01 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:928224</guid><dc:creator>Binu Purayil</dc:creator><description>&lt;p&gt;my approach to minimize sql injection..&lt;/p&gt;
&lt;p&gt;1. Use stored procedure&lt;/p&gt;
&lt;p&gt;2. Avoid using dynamic sql in stored procedure&lt;/p&gt;
&lt;p&gt;3. Grant &amp;nbsp;only minumum required permission to the account executing the proc&lt;/p&gt;
&lt;p&gt;4. validate all user input. &amp;nbsp;use XMLSchema for input validation&lt;/p&gt;
&lt;p&gt;Please add more...&lt;/p&gt;
</description></item><item><title>re: How do I mitigate a SQL injection vuln?</title><link>http://blogs.msdn.com/ericlippert/archive/2006/11/01/how-do-i-mitigate-a-sql-injection-vuln.aspx#929257</link><pubDate>Thu, 02 Nov 2006 13:58:29 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:929257</guid><dc:creator>Alexey Romanov</dc:creator><description>&lt;p&gt;What about simply using type system to do this (a la &lt;a rel="nofollow" target="_new" href="http://blog.moertel.com/articles/2006/10/18/a-type-based-solution-to-the-strings-problem"&gt;http://blog.moertel.com/articles/2006/10/18/a-type-based-solution-to-the-strings-problem&lt;/a&gt;)?&lt;/p&gt;
</description></item><item><title>re: How do I mitigate a SQL injection vuln?</title><link>http://blogs.msdn.com/ericlippert/archive/2006/11/01/how-do-i-mitigate-a-sql-injection-vuln.aspx#932567</link><pubDate>Thu, 02 Nov 2006 18:38:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:932567</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;Using the type system to track semantic information about string contents is an excellent idea and a step in the right direction. &amp;nbsp;No doubt about that. &amp;nbsp;But based on the word &amp;quot;simply&amp;quot; in your suggestion, I suspect that you have missed the point of my posting. &amp;nbsp;The point is that good defense requires more than one layer of protection. &amp;nbsp;If an attack requires three things to be broken to work, FIX ALL OF THEM. Don't just fix the strings problem and hope for the best. &amp;nbsp;Take a multi-layered approach to security.&lt;/p&gt;
</description></item><item><title>re: How do I mitigate a SQL injection vuln?</title><link>http://blogs.msdn.com/ericlippert/archive/2006/11/01/how-do-i-mitigate-a-sql-injection-vuln.aspx#933545</link><pubDate>Thu, 02 Nov 2006 19:49:20 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:933545</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;Rob: I said to ensure that the string contains only what you _expect_. &amp;nbsp;If you expect kanji, then write a regular expression filter that allows kanji through!&lt;/p&gt;
&lt;p&gt;A few questions do still arise though.&lt;/p&gt;
&lt;p&gt;First, what if you WANT to allow &amp;quot; * - ;, etc? &amp;nbsp;The classic approach is to run them through an escapement algorithm that encodes them into alphanumeric entities. Those entities then do not have semantic import when injected into SQL.&lt;/p&gt;
&lt;p&gt;Second, what do you do when you get a string that doesn't meet your filter? &amp;nbsp;You can 1) escape it, 2) strip the offending characters, or 3) reject it entirely. &amp;nbsp;Which is the right thing to do depends on whether you need to maintain fidelity of the string and how paranoid you are about people attacking you. If you think that there's a high likelihood that unusual characters are characteristic of attacks, then the sensible thing to do is to put your shields up at the first sign of trouble.&lt;/p&gt;
</description></item><item><title>re: How do I mitigate a SQL injection vuln?</title><link>http://blogs.msdn.com/ericlippert/archive/2006/11/01/how-do-i-mitigate-a-sql-injection-vuln.aspx#938161</link><pubDate>Fri, 03 Nov 2006 08:19:51 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:938161</guid><dc:creator>O(1)</dc:creator><description>&lt;p&gt;I first heard of these dangerous SQL injection attacks a few years ago and wondered why they were so important. Well, once I understood the nature of this problem it became obvious to me that such a thing would almost never happen to my code because I almost never incorporate user submitted strings in my code. I always compare user supplied data to an internal list and only if it matches my list do I execute some code that I wrote and the code only executes data that I provide. Usually nothing that the user provides ever gets embedded in a command. In the rare cases when you do have to accept some input from the user then it is of paramount importance to correctly use some form of carefully constructed regex that will only pass the data that you expect it to and no more. If people did this then they would almost never suffer from SQL injection attacks and it would become an obscure issue.&lt;/p&gt;
</description></item><item><title>re: How do I mitigate a SQL injection vuln?</title><link>http://blogs.msdn.com/ericlippert/archive/2006/11/01/how-do-i-mitigate-a-sql-injection-vuln.aspx#943223</link><pubDate>Fri, 03 Nov 2006 19:24:06 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:943223</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;Yes, again, you are correct. And again, people seem to be missing my point. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;The problem could be solved by allowing the injection attack but limiting the database permissions to only allow reading from certain tables. &amp;nbsp;If everyone did that, it would become an &amp;quot;obscure issue&amp;quot;. &amp;nbsp;And if everyone used stored procedures it would be a non-issue.&lt;/p&gt;
&lt;p&gt;My point is that there are any number of ways to fix this problem. &amp;nbsp;DO ALL OF THEM. &amp;nbsp;Don't just do enough to fix the problem and then hope for the best. &amp;nbsp;Defense in depth!&lt;/p&gt;
</description></item><item><title>Common vulnerability: SQL Injection</title><link>http://blogs.msdn.com/ericlippert/archive/2006/11/01/how-do-i-mitigate-a-sql-injection-vuln.aspx#993180</link><pubDate>Mon, 06 Nov 2006 10:22:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:993180</guid><dc:creator>Steven Yong's Weblog</dc:creator><description>&lt;p&gt;Have you ever get any error message telling something's wrong with the SQL statement? If yes, you might consider attack the application in order to, say for example, increase your bank account balance.While SQL Injection is a well-known security vulnerab&lt;/p&gt;
</description></item><item><title>re: How do I mitigate a SQL injection vuln?</title><link>http://blogs.msdn.com/ericlippert/archive/2006/11/01/how-do-i-mitigate-a-sql-injection-vuln.aspx#999965</link><pubDate>Mon, 06 Nov 2006 17:55:54 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:999965</guid><dc:creator>Mike</dc:creator><description>&lt;p&gt;&amp;quot;Anything that gets passed to JScript's eval could be an injection attack&amp;quot;&lt;/p&gt;
&lt;p&gt;Crikey! I'd say that any script using Eval was a problem in itself. I've yet to come up with a scenario where it was _ever_ necessary (apart from an &amp;quot;execute any script&amp;quot; page, which is bad enough in itself).&lt;/p&gt;
</description></item><item><title>re: How do I mitigate a SQL injection vuln?</title><link>http://blogs.msdn.com/ericlippert/archive/2006/11/01/how-do-i-mitigate-a-sql-injection-vuln.aspx#1000028</link><pubDate>Mon, 06 Nov 2006 18:07:15 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1000028</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;Indeed! &amp;nbsp;See&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/ericlippert/archive/2003/11/01/53329.aspx"&gt;http://blogs.msdn.com/ericlippert/archive/2003/11/01/53329.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;and&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/ericlippert/archive/2003/11/04/53335.aspx"&gt;http://blogs.msdn.com/ericlippert/archive/2003/11/04/53335.aspx&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: How do I mitigate a SQL injection vuln?</title><link>http://blogs.msdn.com/ericlippert/archive/2006/11/01/how-do-i-mitigate-a-sql-injection-vuln.aspx#1057410</link><pubDate>Sat, 11 Nov 2006 09:51:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1057410</guid><dc:creator>Gabe</dc:creator><description>&lt;p&gt;I never understood why so many people write code that's so vulnerable. I just always write a function like this:&lt;/p&gt;
&lt;p&gt;function SQLString(val)&lt;/p&gt;
&lt;p&gt;....if val is null then&lt;/p&gt;
&lt;p&gt;........return &amp;quot;NULL&amp;quot;&lt;/p&gt;
&lt;p&gt;....else&lt;/p&gt;
&lt;p&gt;........return &amp;quot;'&amp;quot; + str(val).replace(&amp;quot;'&amp;quot;, &amp;quot;''&amp;quot;) + &amp;quot;'&amp;quot; 'replace single apostrophe with two apostrophes&lt;/p&gt;
&lt;p&gt;end function&lt;/p&gt;
&lt;p&gt;Just so long as all user inputs (even dates and numbers) are passed through this function, no injection attack is possible with SQL Server. Why doesn't everybody do this?&lt;/p&gt;
&lt;p&gt;And I should add that filtering out bad characters is generally a bad idea. There's nothing worse than not being able to use a secure password on a site because some fool that doesn't know how to prevent code injection decided that they need to make all non-alphanumeric characters illegal.&lt;/p&gt;
&lt;p&gt;Figuring out what characters to allow is much trickier than preventing injection attacks every other place (SQLString function, stored procs, prepared statements), so it's probably more harmful to your product than beneficial. For example, a naive coder writing a regex to only accept numbers might write /[0-9]+/, only to realize he forgot about negative numbers &amp;nbsp;and decimals (now he has [.0-9-]). That works fine until somebody tries to enter scientific notation, and now he has [.eE0-9+-]. And even then it would fail for somebody in Europe who uses commas for decimal places. I believe the correct regex is [.,eE0-9+-], and even then I could be forgetting about some valid possibilities I hadn't thought of.&lt;/p&gt;
&lt;p&gt;Cross-site scripting (XSS) attacks are much harder to prevent, though. HTML-encoding every literal string is easy, but user input that can include HTML is much more of a problem. These really do require a very strict HTML white-list for tags and attributes. Otherwise you will have to write an HTML parser that keeps up with all the latest browser features to know what's code and what isn't. Besides, you probably want to be careful about what tags you allow anyway (you likely don't want &amp;lt;meta&amp;gt; or &amp;lt;/body&amp;gt; tags, and unclosed tables can screw up your layout).&lt;/p&gt;
</description></item><item><title>re: How do I mitigate a SQL injection vuln?</title><link>http://blogs.msdn.com/ericlippert/archive/2006/11/01/how-do-i-mitigate-a-sql-injection-vuln.aspx#1143901</link><pubDate>Sat, 25 Nov 2006 02:23:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1143901</guid><dc:creator>Jonas</dc:creator><description>&lt;p&gt;is no one else bothered by the fact that using stored procedures for simple queries is blaringly bad design? You move business logic (what do I want to know) into the storage layer of your application where it doesn't belong and is hard to manage and maintain.&lt;/p&gt;
&lt;p&gt;Use prepared statements, preferably with named parameters! And avoid the problem of having to change a query that is used multiple times in the application by moving it into a constant.&lt;/p&gt;
&lt;p&gt;and @Gabe: I know you only talk about SQL Server, but your supposed fix breaks on MySQL if you input &amp;quot; blah\' &amp;quot;, because MySQL allows different escaping characters. Rule-of-thumb: only vendor-supplied character escape functions are safe (and even those have bugs sometimes, prepared statements are a much better solution).&lt;/p&gt;
</description></item><item><title>re: How do I mitigate a SQL injection vuln?</title><link>http://blogs.msdn.com/ericlippert/archive/2006/11/01/how-do-i-mitigate-a-sql-injection-vuln.aspx#1149288</link><pubDate>Sat, 25 Nov 2006 23:27:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1149288</guid><dc:creator>Pete Doubleday</dc:creator><description>&lt;p&gt;Well, OK, I'm a dinosaur.&lt;/p&gt;
&lt;p&gt;I'm not proud of this, and I'm not going to boast. &amp;nbsp;The only reason that the server-side systems I write (or more accurately co-write) are not vulnerable to SQL injection attacks, or indeed any other sort of insertion attack, is that they tend to have very constrained interfaces; basically yacc'n'lex parsing.&lt;/p&gt;
&lt;p&gt;Now, as Hunter S Thompson once said, &amp;quot;I don't recommend drugs and violence for everybody; they just happen to work for me...&amp;quot;&lt;/p&gt;
&lt;p&gt;I loathe stored procedures, and if you happen to use them across an unprotected interface that's open to dynamic SQL, then you are basically a moron and deserve to be kicked in the kidneys. &amp;nbsp;Like many other database features, they have their place, but they are wildly overused.&lt;/p&gt;
&lt;p&gt;Now, let's attack this problem from the other end of the telescope. &amp;nbsp;Sure, you can layer one, two, three or more defences against SQL injection attacks and the like, but these things tend to be automated nowadays. &amp;nbsp;There are a million idiots out there who will patch together vulnerable sites -- and the percentage vulnerability is quite frankly terrifying.&lt;/p&gt;
&lt;p&gt;You could take the Darwinian approach and claim that these sites deserve to be attacked, and perhaps they do. &amp;nbsp;I don't suppose it does Joe Public many favours to be exposed to identity fraud and the like, and it probably does none of us a favour in the long run, but what they heck? These people deserve it. &amp;nbsp;Their sites should be hunted down and destroyed like the rats' nests they are.&lt;/p&gt;
&lt;p&gt;On the other hand we could take a more aggressive approach to fraud (which is basically what an SQL injection attack is). &amp;nbsp;Why not welcome it? Why not make it easy for an attacker to feed destructive guff into a bare SQL statement?&lt;/p&gt;
&lt;p&gt;Think about it.&lt;/p&gt;
&lt;p&gt;It's easier to parse an attack like this (hell, a simple regular expression could do it -- even PHP manages this) and throw an alert at the proper authority than it is to build in n-million layers of security into what is, fundamentally, an insecure and ill-thought-out mass-distribution network. &amp;nbsp;Yup, that's WWW for you.&lt;/p&gt;
&lt;p&gt;Seriously. &amp;nbsp;Has anyone considered this sort of approach?&lt;/p&gt;
&lt;p&gt;-- Pete&lt;/p&gt;
</description></item><item><title>re: How do I mitigate a SQL injection vuln?</title><link>http://blogs.msdn.com/ericlippert/archive/2006/11/01/how-do-i-mitigate-a-sql-injection-vuln.aspx#1333608</link><pubDate>Thu, 21 Dec 2006 00:41:35 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1333608</guid><dc:creator>Shaun Kruger</dc:creator><description>&lt;p&gt;One of the first things I do is find a function that will escape my untrusted input strings.&lt;/p&gt;
&lt;p&gt;Basicly I look for backslash, quote, and double quote characters.&lt;/p&gt;
&lt;p&gt;For my PostgreSQL database projects I always throw these functions into an appropriate class (usually class DBEscape).&lt;/p&gt;
&lt;p&gt;public static string Escape(string str)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;	return Escape(str,&amp;quot;\\\&amp;quot;\'&amp;quot;);&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;public static string Escape(string str,string chrs)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;	string ret = &amp;quot;&amp;quot;;&lt;/p&gt;
&lt;p&gt;	int i;&lt;/p&gt;
&lt;p&gt;	int o;&lt;/p&gt;
&lt;p&gt;	int oct = chrs.Length;&lt;/p&gt;
&lt;p&gt;	int ct = str.Length;&lt;/p&gt;
&lt;p&gt;	bool found;&lt;/p&gt;
&lt;p&gt;	for(i=0;i&amp;lt;ct;i++)&lt;/p&gt;
&lt;p&gt;	{&lt;/p&gt;
&lt;p&gt;		found = false;&lt;/p&gt;
&lt;p&gt;		for(o=0;o&amp;lt;oct;o++)&lt;/p&gt;
&lt;p&gt;		{&lt;/p&gt;
&lt;p&gt;			if(str[i] == chrs[o])&lt;/p&gt;
&lt;p&gt;			{&lt;/p&gt;
&lt;p&gt;				ret += &amp;quot;\\&amp;quot;+str[i].ToString();&lt;/p&gt;
&lt;p&gt;				found = true;&lt;/p&gt;
&lt;p&gt;			}&lt;/p&gt;
&lt;p&gt;		}&lt;/p&gt;
&lt;p&gt;		if(found == false)&lt;/p&gt;
&lt;p&gt;			ret += str[i].ToString();&lt;/p&gt;
&lt;p&gt;	}&lt;/p&gt;
&lt;p&gt;	return ret;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;For my lisp projects I do this which even has support for adding the encasing single quotes, but not for passing a list of characters to escape.&lt;/p&gt;
&lt;p&gt;(defun db-escape (str &amp;amp;optional addquote)&lt;/p&gt;
&lt;p&gt; &amp;nbsp;(format nil (if addquote&lt;/p&gt;
&lt;p&gt;				&amp;quot;'~a'&amp;quot; &amp;quot;~a&amp;quot;)&lt;/p&gt;
&lt;p&gt;		 &amp;nbsp;(format nil &amp;quot;~{~a~}&amp;quot; &lt;/p&gt;
&lt;p&gt;				 &amp;nbsp;(loop for c across str collect&lt;/p&gt;
&lt;p&gt;						(case c&lt;/p&gt;
&lt;p&gt;						 &amp;nbsp;(#\' &amp;quot;''&amp;quot;)&lt;/p&gt;
&lt;p&gt;						 &amp;nbsp;(#\\ &amp;quot;\\&amp;quot;)&lt;/p&gt;
&lt;p&gt;						 &amp;nbsp;(t c))))))&lt;/p&gt;
&lt;p&gt;If it's an input string escape it. &amp;nbsp;If it's an input number (in string form) make sure it passes a Convert.Int32().&lt;/p&gt;
&lt;p&gt;I got tired of errors from users wanting to type names to things into the database like &amp;quot;Bart's call list&amp;quot;. &amp;nbsp;It wasn't until I wrote my Escape() function that these became a thing of the past.&lt;/p&gt;
&lt;p&gt;I quickly learned that if you escape and validate types you will throw less errors and you'll go a long way to avoiding SQL injection attacks. &amp;nbsp;This is a habit that will do me good the rest of my days.&lt;/p&gt;
</description></item><item><title>re: How do I mitigate a SQL injection vuln?</title><link>http://blogs.msdn.com/ericlippert/archive/2006/11/01/how-do-i-mitigate-a-sql-injection-vuln.aspx#1334756</link><pubDate>Thu, 21 Dec 2006 02:57:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1334756</guid><dc:creator>Matthew</dc:creator><description>&lt;p&gt;&amp;gt;&amp;gt; Just so long as all user inputs (even dates and numbers) are passed through this function, no injection attack is possible with SQL Server. Why doesn't everybody do this?&lt;/p&gt;
&lt;p&gt;Because most people are smart enough to use parameterised queries, which avoid the problem altogether.&lt;/p&gt;
</description></item><item><title>re: How do I mitigate a SQL injection vuln?</title><link>http://blogs.msdn.com/ericlippert/archive/2006/11/01/how-do-i-mitigate-a-sql-injection-vuln.aspx#1388296</link><pubDate>Sun, 31 Dec 2006 09:30:02 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1388296</guid><dc:creator>Chad Okere</dc:creator><description>&lt;p&gt;Stored procedures suck. They make version control a bitch and a half. Used &amp;lt;b&amp;gt;prepared&amp;lt;/b&amp;gt; statements. Defined in your code, so they'll be version controlled, but not mailable. Also you can't use dynamic SQL with them.&lt;/p&gt;
</description></item></channel></rss>