<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Explore Security &#187; SQL injection</title>
	<atom:link href="http://www.exploresecurity.com/tag/sql-injection/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.exploresecurity.com</link>
	<description>IT security tools, techniques and commentary</description>
	<lastBuildDate>Wed, 15 Jun 2022 09:21:02 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.6.1</generator>
		<item>
		<title>SQL Injection in Search Fields</title>
		<link>http://www.exploresecurity.com/sql-injection-in-search-fields/</link>
		<comments>http://www.exploresecurity.com/sql-injection-in-search-fields/#comments</comments>
		<pubDate>Thu, 25 Sep 2014 22:07:40 +0000</pubDate>
		<dc:creator>Jerome</dc:creator>
				<category><![CDATA[Penetration Testing]]></category>
		<category><![CDATA[Web Applications]]></category>
		<category><![CDATA[penetration testing]]></category>
		<category><![CDATA[pentesting]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL injection]]></category>
		<category><![CDATA[web apps]]></category>

		<guid isPermaLink="false">http://www.exploresecurity.com/?p=234</guid>
		<description><![CDATA[A quick posting about a fun SQL injection I cracked last week (of course, it&#8217;s only when you&#8217;ve cracked them that they&#8217;re fun!). A colleague had found the classic sign of a problem &#8211; add a single quote and you get an error &#8211; but was having no luck doing anything more. I was getting [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>A quick posting about a fun SQL injection I cracked last week (of course, it&#8217;s only when you&#8217;ve cracked them that they&#8217;re fun!). A colleague had found the classic sign of a problem &#8211; add a single quote and you get an error &#8211; but was having no luck doing anything more. I was getting nowhere with my test so I thought I&#8217;d take a look for a change of scene. The input field was in a search box so, for example, <code>search=keyword'</code> returned an error but <code>search=keyword''</code> was fine. Anything more exciting than that, however, such as <code>search=keyword' and '1'='1</code>, didn&#8217;t seem to work as expected: in this case, an error was returned instead of the same set of results that the normal <code>search=keyword</code> produced.<span id="more-234"></span></p>
<p>The first thing I did was to try to terminate the query as simply as possible with no funny business. So in went <code>search=keyword'--</code> but back came an error. It turned out that the injection point was inside a doubly nested query as <code>search=keyword'))--</code> worked, producing the same results as <code>search=keyword</code>. After a bit of faffing about it occurred to me that spaces might be the issue. So I tried <code>search=keyword'and'1'='1</code> (no spaces in there) and it worked! No error was returned &#8211; but it didn&#8217;t produce the same results as <code>search=keyword</code>, it returned no results at all. What produced the same results as <code>search=keyword</code> was <code>search=keyword'or'1'='1</code>. Okay, park that for now. I had found the main problem &#8211; and it was immediately clear what was going on.</p>
<p>With a developer&#8217;s hat on, what would you do if a user ran a search with multiple keywords? The obvious answer would be to split up the search terms with space as a delimiter, run a query on each one and then return all the results together. If that was true then <code>search=keyword' and '1'='1</code> was running a database query against three terms: <code>keyword'</code>, <code>and</code>, <code>'1'='1</code>. The first of these would fail (just like <code>search=keyword'</code> did), as would the last if it got that far. So next I tried <code>search=keyword'/**/and/**/'1'='1</code> using the inline SQL comment characters and got the same result. Again, using AND returned no results but using OR was like a normal query with <code>search=keyword</code>. I had seen this kind of behaviour once before but I couldn&#8217;t remember what the context was, which is why I&#8217;ve written it down this time!</p>
<h3>AND vs OR</h3>
<p>In general, AND within a SQL statement (and thus in SQL injection too) is restrictive, narrowing the result set, whereas OR is inclusive, widening the result set. But, as with all SQL injection, it all depends on the underlying query. So what could be happening here?</p>
<p>Again, with the developer hat on, what else might you do with a user&#8217;s search terms? Well, it would be nice if you searched a little more widely, using them as stubs. In fact some of the SQL errors were giving this away (thanks, guys): <em>Incorrect syntax near &#8216;%&#8217;</em>. The % character is, of course, a wildcard used with LIKE. So when I searched for <code>keyword</code>, somewhere in the resulting query was <code>LIKE '%keyword%'</code>. This perfectly explains the AND vs OR behaviour&#8230;</p>
<p>When I injected <code>search=keyword'and'1'='1</code> the resulting query included <code>LIKE '%keyword'and'1'='1%'</code>. So the AND clause I&#8217;d added was always evaluating to FALSE and hence no results were returned. Whereas injecting <code>search=keyword'or'1'='1</code> produced <code>LIKE '%keyword'or'1'='1%'</code>. Even though one half of the OR clause was evaluating to FALSE, overall it returned TRUE when I got a positive hit on the keyword.</p>
<p>Since the injection point was inside a doubly nested query and this was a black box test, I had no idea what the real query was, but this certainly made sense. I tried a few more injections to test the theory just for the hell of it:</p>
<ol>
<li>When I terminated the statement, AND and OR did their &#8220;usual&#8221; thing. Which is to say that <code>search=keyword'/**/and/**/1=1))--</code> produced the same result as <code>search=keyword</code> whereas <code>keyword'/**/or/**/1=1))--</code> produced lots of results. This is because I was now commenting out the final % along with the rest of the statement.</li>
<li>When I injected <code>search=keyword'and'1%'='1</code> I got the same results as if there had been no injection. This was the real proof. Now the resulting query would have included <code>LIKE '%keyword'and'1%'='1%'</code> so my AND clause evaluated to TRUE when I got a positive hit on the keyword.</li>
<li>Finally, for what it was worth, <code>search=word'and'1%'='1</code> produced the same result, showing that a % preceded the injection point.</li>
</ol>
<h3>sqlmap</h3>
<p>One of the things that makes a great tool is the ability to customise it for a particular attack scenario. And sqlmap offers that in abundance. In this case a &#8220;tamper&#8221; script, which transforms the payloads in some way, worked a treat. One of the built-in tamper scripts is &#8220;space2comment&#8221; &#8211; bingo! In fact running sqlmap with this script allowed it to find the injection point. Without the script, though, sqlmap would have been stuck because, to quote the <a href='https://github.com/sqlmapproject/sqlmap/wiki/Usage#tamper-injection-data'>wiki page</a>, &#8220;sqlmap itself does no obfuscation of the payload sent, except for strings between single quotes replaced by their CHAR()-alike representation&#8221;.</p>
<p>All this was a good reminder that, when things are getting tough, thinking like a developer can help to turn near-misses into exploitable flaws. Having said that, I&#8217;ve seen code in the past that I could never have guessed, when it was clear the developer wasn&#8217;t thinking at all!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.exploresecurity.com/sql-injection-in-search-fields/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
