<?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>Saiweb &#187; injection</title>
	<atom:link href="http://www.saiweb.co.uk/tag/injection/feed" rel="self" type="application/rss+xml" />
	<link>http://www.saiweb.co.uk</link>
	<description>Ramblings of a Sys admin</description>
	<lastBuildDate>Mon, 06 Feb 2012 14:57:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>PHP Security considerations, a quick reference for the newbies.</title>
		<link>http://www.saiweb.co.uk/security/php-security-considerations</link>
		<comments>http://www.saiweb.co.uk/security/php-security-considerations#comments</comments>
		<pubDate>Thu, 20 Mar 2008 09:57:17 +0000</pubDate>
		<dc:creator>Buzz</dc:creator>
				<category><![CDATA[mySQL]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[injection]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://www.saiweb.co.uk/php/php-security-considerations</guid>
		<description><![CDATA[To often I get passed code to review that quite frankly is so full of holes it wouldn&#8217;t make an adequate sieve&#8230; So in this quick blog I outline a few simple and easily implemented steps to ensure as you start out in the world of PHP, your first site isn&#8217;t hacked within 5 minutes, [...]]]></description>
			<content:encoded><![CDATA[<p>To often I get passed code to review that quite frankly is so full of holes it wouldn&#8217;t make an adequate sieve&#8230;</p>
<p>So in this quick blog I outline a few simple and easily implemented steps to ensure as you start out in the world of PHP, your first site isn&#8217;t hacked within 5 minutes, leaving you a whimpering wrek &#8230;</p>
<p><strong>PHP DON&#8217;T EXAMPLE 1:</strong></p>
<p>Passing RAW globals to mysql!</p>
<p>i.e.</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$sql = &quot;SELECT * FROM users WHERE email='.$_GET['email'].&quot;' and password='&quot;.$_GET['password']&quot;';&quot;;<br />
$result = mysql_query($sql);</div></td></tr></tbody></table></div>
<p>So what is wrong with the above? <strong><a title="SQL INject Wikipedia Entry" href="http://en.wikipedia.org/wiki/SQL_injection" target="_blank">SQL INJECTION</a> </strong>welcome to a world where people want to break your website, simply because they can &#8230;</p>
<p>I am not going to add more description, just click through to the wiki pedia entry linked above &#8230;</p>
<p>To avoid this PHP comes with two functions <a title="PHP mysql_escape_string()" href="http://www.php.net/mysql_escape_string" target="_blank">mysql_escape_string()</a> and <a title="PHP mysql_real_escape_string()" href="http://www.php.net/mysql_real_escape_string" target="_blank">mysql_real_escape_string()</a></p>
<p>An example taken from the <a title="PHP mysql_real_escape_string()" href="http://www.php.net/mysql_real_escape_string" target="_blank">mysql_real_escape_string()</a> page:</p>
<p class="example"><strong>Example#2 An example SQL Injection Attack</strong></p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;span style=&quot;color: #000000;&quot;&gt;&lt;span style=&quot;color: #0000bb;&quot;&gt;&amp;lt;?php<br />
&lt;/span&gt;&lt;span style=&quot;color: #ff8000;&quot;&gt;// Query database to check if there are any matching users<br />
&lt;/span&gt;&lt;span style=&quot;color: #0000bb;&quot;&gt;$query &lt;/span&gt;&lt;span style=&quot;color: #007700;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: #dd0000;&quot;&gt;&quot;SELECT * FROM users WHERE user='{$_POST['username']}' AND password='{$_POST['password']}'&quot;&lt;/span&gt;&lt;span style=&quot;color: #007700;&quot;&gt;;<br />
&lt;/span&gt;&lt;span style=&quot;color: #0000bb;&quot;&gt;mysql_query&lt;/span&gt;&lt;span style=&quot;color: #007700;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #0000bb;&quot;&gt;$query&lt;/span&gt;&lt;span style=&quot;color: #007700;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;</div></td></tr></tbody></table></div>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;span style=&quot;color: #000000;&quot;&gt;&lt;span style=&quot;color: #007700;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff8000;&quot;&gt;// We didn't check $_POST['password'], it could be anything the user wanted! For example:<br />
&lt;/span&gt;&lt;span style=&quot;color: #0000bb;&quot;&gt;$_POST&lt;/span&gt;&lt;span style=&quot;color: #007700;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #dd0000;&quot;&gt;'username'&lt;/span&gt;&lt;span style=&quot;color: #007700;&quot;&gt;] = &lt;/span&gt;&lt;span style=&quot;color: #dd0000;&quot;&gt;'aidan'&lt;/span&gt;&lt;span style=&quot;color: #007700;&quot;&gt;;<br />
&lt;/span&gt;&lt;span style=&quot;color: #0000bb;&quot;&gt;$_POST&lt;/span&gt;&lt;span style=&quot;color: #007700;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #dd0000;&quot;&gt;'password'&lt;/span&gt;&lt;span style=&quot;color: #007700;&quot;&gt;] = &lt;/span&gt;&lt;span style=&quot;color: #dd0000;&quot;&gt;&quot;' OR ''='&quot;&lt;/span&gt;&lt;span style=&quot;color: #007700;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #007700;&quot;&gt;&lt;span style=&quot;color: #ff8000;&quot;&gt;// This means the query sent to MySQL would be:<br />
&lt;/span&gt;&lt;span style=&quot;color: #007700;&quot;&gt;echo &lt;/span&gt;&lt;span style=&quot;color: #0000bb;&quot;&gt;$query&lt;/span&gt;&lt;span style=&quot;color: #007700;&quot;&gt;;<br />
&lt;/span&gt;&lt;span style=&quot;color: #0000bb;&quot;&gt;?&amp;gt;&lt;/span&gt;&lt;/span&gt;</div></td></tr></tbody></table></div>
<p class="example-contents">The query sent to MySQL:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;code&gt;SELECT * FROM users WHERE user='aidan' AND password='' OR ''=''</div></td></tr></tbody></table></div>
<p></code></p>
<p class="example-contents">This would allow anyone to log in without a valid password.</p>
<p class="example-contents">So in summary READ the <a title="PHP mysql_real_escape_string()" href="http://www.php.net/mysql_real_escape_string" target="_blank">mysql_real_escape_string()</a> page, and even if you don't implement the "best practice" example on that page PLEASE make sure you at least escape $_SESSION $_GET $_POST inputs with a mysql escape function!</p>
<p class="example-contents">
<p class="example-contents">
<p><span style="float: left;" ><a class="twitter-share-button"  data-via="Saiweb" data-count="horizontal" data-related="Saiweb:David Busby" data-lang="en" data-url="http://www.saiweb.co.uk/security/php-security-considerations" data-text="PHP Security considerations, a quick reference for the newbies." href="http://twitter.com/share?via=Saiweb&#038;count=horizontal&#038;related=Saiweb%3ADavid%20Busby&#038;lang=en&#038;url=http%3A%2F%2Fwww.saiweb.co.uk%2Fsecurity%2Fphp-security-considerations&#038;text=PHP%20Security%20considerations%2C%20a%20quick%20reference%20for%20the%20newbies." >Tweet</a></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.saiweb.co.uk/security/php-security-considerations/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using apc
Database Caching 3/11 queries in 0.062 seconds using apc
Object Caching 590/601 objects using apc
Content Delivery Network via Rackspace Cloud Files: cdn.saiweb.co.uk

Served from: www.saiweb.co.uk @ 2012-02-08 15:46:07 -->
