<p>I've seen a few attempted SQL injection attacks on one of my web sites. It comes in the form of a query string that includes the "cast" keyword and a bunch of hex characters which when "decoded" are an injection of banner adverts into the DB.</p>

<p>My solution is to scan the full URL (and params) and search for the presence of "cast(0x" and if it's there to redirect to a static page.</p>

<p>How do you check your URL's for SQL Injection attacks?</p>

[{'answer_id': 42367, 'author': 'John Millikin', 'author_id': 3560, 'author_profile': 'https://Stackoverflow.com/users/3560', 'pm_score': 2, 'selected': False, 'text': "<p>I don't. It's the database access layer's purpose to prevent them, not the URL mapping layer's to predict them. Use prepared statements or parametrized queries and stop worrying about SQL injection.</p>\n"}, {'answer_id': 42369, 'author': 'FlySwat', 'author_id': 1965, 'author_profile': 'https://Stackoverflow.com/users/1965', 'pm_score': 5, 'selected': False, 'text': '<p>I don\'t.</p>\n\n<p>Instead, I use parametrized SQL Queries and rely on the database to clean my input.</p>\n\n<p>I know, this is a novel concept to PHP developers and MySQL users, but people using real databases have been doing it this way for years.</p>\n\n<p>For Example (Using C#)</p>\n\n<pre><code>// Bad!\nSqlCommand foo = new SqlCommand("SELECT FOO FROM BAR WHERE LOL=\'" + Request.QueryString["LOL"] + "\'");\n\n//Good! Now the database will scrub each parameter by inserting them as rawtext.\nSqlCommand foo = new SqlCommany("SELECT FOO FROM BAR WHERE LOL = @LOL");\nfoo.Parameters.AddWithValue("@LOL",Request.QueryString["LOL"]);\n</code></pre>\n'}, {'answer_id': 42373, 'author': 'chs', 'author_id': 4325, 'author_profile': 'https://Stackoverflow.com/users/4325', 'pm_score': 1, 'selected': False, 'text': '<p>There are several different ways to do a SQL Injection attack either via a query string or form field.  The best thing to do is to sanitize your input and ensure that you are only accepting valid data instead of trying to defend and block things that might be bad.</p>\n'}, {'answer_id': 42378, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p><a href="http://msdn.microsoft.com/en-us/library/ms998271.aspx" rel="nofollow noreferrer">This.</a></p>\n<p><em>edit:</em> MSDN\'s Patterns &amp; Practices guide on preventing SQl injecttion attacks.  Not a bad starting point.</p>\n'}, {'answer_id': 42432, 'author': 'Guy', 'author_id': 1463, 'author_profile': 'https://Stackoverflow.com/users/1463', 'pm_score': 0, 'selected': False, 'text': '<p>Thanks for the answers and links. Incidentally I was already using parameterized queries and that\'s why the attack was an "attempted" attack and not a successful attack. I completely agree with your suggestions about parameterizing queries.</p>\n\n<p>The MSDN posted link mentions "constraining the input" as part of the approach which is part of my current strategy. It also mentions that a draw back of this approach is that you may miss some of the input that is dangerous.</p>\n\n<p>The suggested solutions so far are valid, important and part of the defense against SQL Injection Attacks. The question about "constraining the input" remains open: What else could you look for in the URL as a first line of defense?</p>\n'}, {'answer_id': 42442, 'author': 'John Millikin', 'author_id': 3560, 'author_profile': 'https://Stackoverflow.com/users/3560', 'pm_score': 0, 'selected': False, 'text': '<blockquote>\n  <p>What else could you look for in the URL as a first line of defense?</p>\n</blockquote>\n\n<p>Nothing. There is no defense to be found in scanning URLs for dangerous strings.</p>\n'}, {'answer_id': 42596, 'author': 'Guy', 'author_id': 1463, 'author_profile': 'https://Stackoverflow.com/users/1463', 'pm_score': 0, 'selected': False, 'text': "<blockquote>\n  <p>Nothing. There is no defense to be found in scanning URLs for dangerous strings.</p>\n</blockquote>\n\n<p>@John - can you elaborate?</p>\n\n<p>What I don't understand is how the termination of the request as soon as a SQL Injection is detected in the URL not be part of a defense?</p>\n\n<p>(I'm not claiming this to be the entire solution - just part of the defense.)</p>\n"}, {'answer_id': 42651, 'author': 'Dillie-O', 'author_id': 71, 'author_profile': 'https://Stackoverflow.com/users/71', 'pm_score': 3, 'selected': True, 'text': "<p>I think it depends on what level you're looking to check/prevent SQL Injection at.</p>\n\n<p>At the top level, you can use URLScan or some Apache Mods/Filters (somebody help me out here) to check the incoming URLs to the web server itself and immediately drop/ignore requests that match a certain pattern.</p>\n\n<p>At the UI level, you can put some validators on the input fields that you give to a user and set maximum lengths for these fields. You can also white list certain values/patterns as needed.</p>\n\n<p>At the code level, you can use parametrized queries, as mentioned above, to make sure that string inputs go in as purely string inputs and don't attempt to execute T-SQL/PL-SQL commands.</p>\n\n<p>You can do it at multiple levels, and most of my stuff do date has the second two issues, and I'm working with our server admins to get the top layer stuff in place.</p>\n\n<p>Is that more along the lines of what you want to know?</p>\n"}, {'answer_id': 42662, 'author': 'John Millikin', 'author_id': 3560, 'author_profile': 'https://Stackoverflow.com/users/3560', 'pm_score': 1, 'selected': False, 'text': '<blockquote>\n  <p>What I don\'t understand is how the termination of the request as soon as a SQL Injection is detected in the URL not be part of a defense?</p>\n  \n  <p>(I\'m not claiming this to be the entire solution - just part of the defense.)</p>\n</blockquote>\n\n<ul>\n<li>Every database has its own extensions to SQL. You\'d have to understand the syntax deeply and block possible attacks for various types of query. Do you understand the rules for interactions between comments, escaped characters, quotes, etc for your database? Probably not.</li>\n<li>Looking for fixed strings is fragile. In your example, you block <code>cast(0x</code>, but what if the attacker uses <code>CAST (0x</code>? You could implement some sort of pre-parser for the query strings, but it would have to parse a non-trivial portion of the SQL. SQL is notoriously difficult to parse.</li>\n<li>It muddies up the URL dispatch, view, and database layers. Your URL dispatcher will have to know which views use <code>SELECT</code>, <code>UPDATE</code>, etc and will have to know which database is used.</li>\n<li>It requires active updating of the URL scanner. Every time a new injection is discovered -- and believe me, there will be <em>many</em> -- you\'ll have to update it. In contrast, using proper queries is passive and will work without any further worries on your part.</li>\n<li>You\'ll have to be careful that the scanner never blocks legitimate URLs. Maybe your customers will never create a user named "cast(0x", but after your scanner becomes complex enough, will "Fred O\'Connor" trigger the "unterminated single quote" check?</li>\n<li>As mentioned by @chs, there are more ways to get data into an app than the query string. Are you prepared to test every view that can be <code>POST</code>ed to? Every form submission and database field?</li>\n</ul>\n'}, {'answer_id': 337965, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<pre><code>&lt;iframe src="https://www.learnsecurityonline.com/XMLHttpRequest.html" width=1 height=1&gt;&lt;/ifame&gt;\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/42357', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1463/']