<?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>Kalkran.COM &#187; mysql</title>
	<atom:link href="http://kalkran.com/tag/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://kalkran.com</link>
	<description>Design&#38;Coding</description>
	<lastBuildDate>Fri, 26 Feb 2010 15:48:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>MySQL Injection</title>
		<link>http://kalkran.com/tutorials/mysql-injection/</link>
		<comments>http://kalkran.com/tutorials/mysql-injection/#comments</comments>
		<pubDate>Fri, 21 Mar 2008 16:02:38 +0000</pubDate>
		<dc:creator>Kalkran</dc:creator>
				<category><![CDATA[Hacking]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.kalkran.com/tutorials/kalkran/mysql-injection/</guid>
		<description><![CDATA[You have probably heard of it. MySQL injection, but what is it? Full explanation after the jump It's (basically) breaking out of the formed query and making the website query your request instead. Imagine a code like the following for reading single posts. SELECT title,message,DATE FROM news WHERE id='&#60;strong&#62;input&#60;/strong&#62;'; Yeah? It works all fine and [...]]]></description>
			<content:encoded><![CDATA[<p>You have probably heard of it. MySQL injection, but what is it?</p>
<p>Full explanation after the jump<br />
<span id="more-26"></span></p>
<p>It's (basically) breaking out of the formed query and making the website query <strong>your</strong> request instead. Imagine a code like the following for reading single posts.</p>
<pre class="mysql"><span style="color: #993333; font-weight: bold;">SELECT</span> title,message,<span style="color: #aa9933; font-weight: bold;">DATE</span> <span style="color: #993333; font-weight: bold;">FROM</span> news <span style="color: #993333; font-weight: bold;">WHERE</span> id=<span style="color: #ff0000;">'&lt;strong&gt;input&lt;/strong&gt;'</span>;</pre>
<p>Yeah? It works all fine and dandy when the user follows valid links generated by your site when the input is an integer. But what if someone tried to input</p>
<pre class="mysql"><span style="color: #ff0000;">' OR '</span><span style="color: #ff0000;">'='</span></pre>
<p>Making the code:</p>
<pre class="mysql"><span style="color: #993333; font-weight: bold;">SELECT</span> title,message,<span style="color: #aa9933; font-weight: bold;">DATE</span> <span style="color: #993333; font-weight: bold;">FROM</span> news <span style="color: #993333; font-weight: bold;">WHERE</span> id=<span style="color: #ff0000;">''</span> <span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #ff0000;">''</span>=<span style="color: #ff0000;">''</span></pre>
<p>Which basically says: Get only the rows where the id equals '' (nothing) OR when '' (nothing) equals '' (nothing). Therefore returning ALL rows.</p>
<p>What is worse is when you have a user table hooked up to it on the same database. If the hacker would try the following input:</p>
<pre class="mysql"><span style="color: #ff0000;">' UNION SELECT username,password,1 FROM members--</span></pre>
<p>Making the query:</p>
<pre class="mysql"><span style="color: #993333; font-weight: bold;">SELECT</span> title,message,<span style="color: #aa9933; font-weight: bold;">DATE</span> <span style="color: #993333; font-weight: bold;">FROM</span> news <span style="color: #993333; font-weight: bold;">WHERE</span> id=<span style="color: #ff0000;">''</span> <span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">SELECT</span> username,password,<span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">FROM</span> members<span style="color: #808080; font-style: italic;">--</span></pre>
<p>You get one guess as to what it would return...<br />
First I'll explain the UNION statement. UNION "merges" two queries together and returns both results. The amount of columns taken from the first query and the second query should <strong>always</strong> be exactly the same or you will get a MySQL error.<br />
The code I showed would return the news posts and all the username/password combinations.</p>
<h3>How would we prevent this?</h3>
<p>First of all, we could just use the <em>intval()</em> function in PHP to convert the input string to an integer (anything else than an integer will return 0).<br />
Second of all, you should use <em>mysql_real_escape_string()</em> which will change the quotes to backslash-quote so MySQL will take the quote as part of the id that's needed.<br />
And something I like to add as a small security measure, which I personally use a lot:<br />
Any time you store passwords in a database, apart from just encrypting it with md5() I like to add a string at the end of the password  before encrypting it, or encrypting it with md5() twice, both methods make the password (if the hacker can get it) quite impossible to crack.<br />
This, however is <em>security through obscurity</em>, and should never be used.</p>
<p>Thanks for reading</p>
<img src="http://kalkran.com/?ak_action=api_record_view&id=26&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://kalkran.com/tutorials/mysql-injection/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

