title: Defending against a 'WAITFOR DELAY' sql injection attack?
body: <p><strong>The problem</strong></p>
  
  <p>We need to defend against a 'WAITFOR DELAY' sql injection attack in our java application.</p>
  
  <p><strong>Background</strong></p>
  
  <p>[This is long. Skip to 'Solution?' section below if you're in a rush ]</p>
  
  <p>Our application mostly uses prepared statements and callable statements (stored procedures) in accessing the database. </p>
  
  <p>In a few places we dynamically build-and-execute queries for selection. In this paradigm we use a criteria object to build the query depending on the user-input criteria. For example, if the user specified values for first_name and last_name, the result querying always looks something like this:   </p>
  
  <pre><code>SELECT first_name,last_name FROM MEMBER WHERE first_name ='joe' AND last_name='frazier'
  </code></pre>
  
  <p>(In this example the user would have specified "joe" and "frazier" as his/her input values. If the user had more or less critieria we would have longer or shorter queries. We have found that this approach is easier than using prepared statements and quicker/more performant than stored procedures).</p>
  
  <p><strong>The attack</strong></p>
  
  <p>A vulnerability audit reported an sql injection failure. The attacker injected the value 'frazier WAITFOR DELAY '00:00:20' for the 'last_name' parameter, resulting in this sql:</p>
  
  <pre><code>   SELECT first_name,last_name FROM MEMBER WHERE first_name ='joe' AND last_name='frazier' WAITFOR DELAY '00:00:20'
  </code></pre>
  
  <p>The result: the query executes successfully, but takes 20 seconds to execute.  An attacker could tie up all your database connections in the db pool and effectively shut down your site.</p>
  
  <p><strong>Some observations about this 'WAITFOR DELAY' attack</strong></p>
  
  <ul>
  <li><p>I had thought that because we used Statement executeQuery(String) we would be safe from sql injection. executeQuery(String) will not execute DML or DDL (deletes or drops). And executeQuery(String) chokes on semi-colons, thus the 'Bobby Tables' paradigm will fail (i.e. user enters 'frazier; DROP TABLE member' for a parameter. See. <a href="http://xkcd.com/327/" rel="nofollow noreferrer">http://xkcd.com/327/</a>)</p></li>
  <li><p>The 'WAITFOR' attack differs in one important respect:  WAITFOR modifies the existing 'SELECT' command, and is not a separate command.</p></li>
  <li><p>The attack only works on the 'last parameter' in the resulting query. i.e. 'WAITFOR' must occur at the very end of the sql statement</p></li>
  </ul>
  
  <p><strong>Solution, Cheap Hack, or Both?</strong></p>
  
  <p>The most obvious solution entails simply tacking "AND 1=1" onto the where clause. </p>
  
  <p>The resulting sql fails immediately and foils the attacker:</p>
  
  <pre><code>   SELECT first_name,last_name FROM MEMBER WHERE first_name ='joe' AND last_name='frazier' WAITFOR DELAY '00:00:20' AND 1=1
  </code></pre>
  
  <p><strong>The Questions</strong></p>
  
  <ul>
  <li>Is this a viable solution for the WAITFOR attack?</li>
  <li>Does it defend against other similar vulnerabilities?</li>
  <li>I think the best option would entail using prepared statements. More work, but less vulnerable.</li>
  </ul>