<?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>Will Fitch&#039;s Blog &#187; Security</title>
	<atom:link href="http://www.willfitch.com/tag/security/feed" rel="self" type="application/rss+xml" />
	<link>http://www.willfitch.com</link>
	<description></description>
	<lastBuildDate>Sun, 11 Dec 2011 23:39:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>PHP Security: SQL Injection Overview</title>
		<link>http://www.willfitch.com/php-security-sql-injection-overview.html</link>
		<comments>http://www.willfitch.com/php-security-sql-injection-overview.html#comments</comments>
		<pubDate>Wed, 30 Nov -0001 00:00:00 +0000</pubDate>
		<dc:creator>Will Fitch</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[sql injection]]></category>

		<guid isPermaLink="false">http://358357084</guid>
		<description><![CDATA[SQL injection is increasingly becoming a problem for developers, especially PHP. This tutorial will give some insight into what SQL injection is, and how simple it is to protect your code from it. SQL injection is a security vulnerability that occurs in the database layer of an application. Its source is the incorrect escaping of [...]]]></description>
			<content:encoded><![CDATA[<p>SQL injection is increasingly becoming a problem for developers, especially PHP. This tutorial will give some insight into what SQL injection is, and how simple it is to protect your code from it.</p>
<p><strong>SQL injection</strong> <a title="SQL Injection Definition" href="http://www.google.com/search?hl=en&#038;lr=&#038;defl=en&#038;q=define:SQL+Injection&#038;sa=X&#038;oi=glossary_definition&#038;ct=title" target="_blank">is a security vulnerability that occurs in the database layer of an application. Its source is the incorrect escaping of variables embedded in SQL statements. It is in fact an instance of a more general class of vulnerabilities that can occur whenever one programming or scripting language is embedded inside another. </a></p>
<p><span id="more-102"></span></p>
<p>SQL injection seems to be on the minds of all developers recently. Although very dangerous, this security risk is relatively easy to prevent. Some languages have built in protection against SQL injections by simply auto filtering. PHP has made an attempt at this with magic_quotes_gpc, but as we all know, ended up being a complete nuisance to developers and frankly just didn&#8217;t work. There are a few generic rules to remember. If you abide by these rules, you will be okay.</p>
<ol>
<li><strong>Never trust foreign data!</strong> &#8211; Don&#8217;t depend on the end user inputting valid data!</li>
<li><strong>Filter all input</strong> &#8211; Use built in functions, as well as REGEX to validate input!</li>
<li><strong>Forget about <a title="addslashes definition" href="http://us2.php.net/addslashes" target="_blank">addslashes</a>() !!!</strong> &#8211; If using GBK character set, addslashes() will change 0xbf27 to 0xbf5c27, which is a valid multi-byte character followed by a single quote. Keep in mind that GBK multi-byte characters aren&#8217;t enabled by default.</li>
</ol>
<p>Want to try it out for yourself? Click <a title="SQL Injection Test Script" href="http://www.phpfever.com/uploads/sql_inject1.php" target="_blank">here</a> to try a sample SQL injection script. It will let you know if you&#8217;ve successfully injected or broke a query, or if you didn&#8217;t do anything.</p>
<p>Here is what the code would you are attempting to break looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$mysqli</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> mysqli<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'host'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'user'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'pass'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'db'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$username</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM admins WHERE username='<span style="color: #006699; font-weight: bold;">$username</span>' AND password='<span style="color: #006699; font-weight: bold;">$password</span>'&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$mysqli</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">num_rows</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$found</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$found</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$found</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'
&lt;div style=&quot;color: red&quot;&gt;YOU BROKE MY SCRIPT!!!&lt;/div&gt;
'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;strong&gt;'</span><span style="color: #339933;">.</span><span style="color: #990000;">highlight_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/strong&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;strong&gt;NOPE! Still not in!&lt;/strong&gt;
'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">highlight_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;strong&gt;NOPE! Still not in, but you did break my query!&lt;/strong&gt;
'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">highlight_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$mysqli</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>After looking at this script, there should be alarms going off in your head. Are you getting that hormone rush in your head? You know, the one that says, &#8220;Oh boy! I&#8217;m gonna break this code.&#8221;? Go ahead and play with the test script. If you need help, one answer is in the next paragraph.</p>
<p>How did you do? The easiest way to break that script, which was obviously not escaped, is this:<br />
In the username box, type the following: <strong>&#8216; OR 1=1#</strong></p>
<p>After inputting this data, you will see that you successfully injected my login page. How does it work? The first step was to add a single quote, followed by an OR 1=1 condition, which always returns true, and a hash mark (#), which represents an SQL comment making the rest of the statement irrelevant. How can you stop this?</p>
<p>Simply using the built in MySQL or MySQLi functions mysql_escape_string() and mysqli_escape_string() or escape_string() in OOP will prevent those nasty injections from ruining your day, or job.</p>
<p>This short tutorial should help you understand the dangers of SQL injection, and just how easy it is to prevent it. SQL injection isn&#8217;t limited to login scripts and forms, there are many other ways to do it. Just remember those simple rules, and check back for more SQL injection tutorials in the near future!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.willfitch.com/php-security-sql-injection-overview.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

