<?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>JamieRF &#187; open-source</title>
	<atom:link href="http://www.jamierf.co.uk/tag/open-source/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jamierf.co.uk</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Sun, 25 Sep 2011 21:37:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Securely storing passwords using PHP</title>
		<link>http://www.jamierf.co.uk/2011/05/09/securely-storing-passwords-using-php/</link>
		<comments>http://www.jamierf.co.uk/2011/05/09/securely-storing-passwords-using-php/#comments</comments>
		<pubDate>Mon, 09 May 2011 18:46:54 +0000</pubDate>
		<dc:creator>Jamie</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[fluxbb]]></category>
		<category><![CDATA[open-source]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://www.jamierf.co.uk/?p=2550</guid>
		<description><![CDATA[Password hashing is an interesting subject, which I have been working on enhancing in FluxBB v2. Here I&#8217;m going to provide an introduction to storing passwords securely, and share some thoughts as well as our solution. Note: I am talking about hashing opposed to encryption. Many people use the terms interchangeably, however they are very different [...]]]></description>
			<content:encoded><![CDATA[<p>Password hashing is an interesting subject, which I have been working on enhancing in FluxBB v2. Here I&#8217;m going to provide an introduction to storing passwords securely, and share some thoughts as well as our solution.</p>
<p><em><strong>Note:</strong> I am talking about hashing opposed to encryption. Many people use the terms interchangeably, however they are very different operations and should not be confused.</em></p>
<ul>
<li><em><a href="http://en.wikipedia.org/wiki/Hash_function">Hash functions</a> convert variable sized data into a fixed sized result. They are deterministic and one-way &#8211; the same input always hashes to the same output, which cannot be reversed. Hashes have many more useful properties, but for storing passwords the important point is they cannot be reversed.</em></li>
<li><em><a href="http://en.wikipedia.org/wiki/Encryption">Encryption</a> is the process of transforming information to make it unreadable to anyone without the decryption key. The important difference is the ability to recover the original data given the correct decryption key.</em></li>
</ul>
<p><em>In the situation where the database in compromised and users password hashes are exposed, it is a good bet that any decryption key we have stored could also be compromised. Since we have no need to know the users actual password (we can check for a valid login by hashing their input and comparing to the stored hash) we should <strong>never</strong> use encryption for storing passwords.</em></p>
<p><em><span id="more-2550"></span></em></p>
<h1>Hashing the password</h1>
<p>At first glance password hashing seems like an incredibly simple problem &#8211; just run the password through a well known hash function and job done. Most advice these days seems to suggest the use of <a href="http://en.wikipedia.org/wiki/SHA-1">SHA-1</a> or <a href="http://en.wikipedia.org/wiki/SHA-2">SHA-2</a>.</p>
<h2>Hashing</h2>
<p>SHA-1/2 are hash functions, designed by the NSA. Like all hash functions, they are one-way, so given a database of password hashes an attacker cannot reverse them. However passwords hashed using a simple hash function are still vulnerability to reversing using <a href="http://en.wikipedia.org/wiki/Rainbow_table">rainbow tables</a>. Rainbow tables are mappings of resulting hash -&gt; original password for dictionaries of common passwords, etc. To combat rainbow table attacks many people suggest the use of per-user salts.</p>
<h2>Hashing + salt</h2>
<p>A salt is a randomly generated string which is appended to a users password before hashing. This randomly generated string prevents the use of pre-generated rainbow tables as it increases the complexity and length of the final password, and hence the size of rainbow tables required are infeasibly large &#8211; the only solution is for an attacker to attempt a brute force attack using the correct salt.</p>
<p>Thanks to advances in parallel computing and graphics card technology, this is now a viable solution. Various tools such as <a href="http://golubev.com/hashgpu.htm">IGHASHGPU</a> and <a href="http://whitepixel.zorinaq.com">whitepixel</a> now exist &#8211; with whitepixel boasting as many as <a href="http://blog.zorinaq.com/?e=43">33.1 billion</a> MD5 hashed password attempts per second using just 4 AMD Radeon 5970s.</p>
<h2><a href="http://en.wikipedia.org/wiki/Key_stretching">Key stretching</a></h2>
<p>Key stretching refers to techniques used to increase the time it takes to test a password &#8211; and hence decreasing the number of brute force password attempts that can be made per second. Using plain hashing algorithms we are talking one millionth of a second to generate a single hash on a regular CPU, or in the order of one billionth of a second using the above mentioned GPU software. If we can increase the time taken to generate a single attempt to 10s of milliseconds then we have effectively cut the brute force attempts by 4 or 5 orders of magnitude. In the situation where a legitimate user is attempting a login, an extra 10ms is in most cases unnoticeable.</p>
<h3>BCrypt (<a href="http://en.wikipedia.org/wiki/Blowfish_(cipher)">Blowfish</a> encryption)</h3>
<p>I started this post by emphasising the difference between hashing and encryption, and stating that encryption should never be used for storing passwords &#8211; so you might be asking yourself why I am now talking about encryption?</p>
<p>BCrypt is an algorithm for irreversibly obscuring passwords using the blowfish encryption algorithm. It is important to understand that BCrypt does not simply encrypt passwords &#8211; the process is still a one-way process, just like hashing. BCrypt works by encryption a magic string, using a key derived from the provided password. The encrypted magic string is stored in the database, but the derived key is never stored.</p>
<p>The blowfish algorithm is important, as the amount of work done to encrypt a string, and hence time taken, is tuned using a cost parameter. By increasing this cost factor we can increase the time taken to test a password.</p>
<p>BCrypt is available as of PHP 5.3+, using the <a href="http://www.php.net/manual/en/function.crypt.php">crypt</a> function.</p>
<div id="layout_2">
<div id="content">
<div id="function.crypt">
<div>
<blockquote><p><strong>crypt()</strong> will return a hashed string using the standard Unix <abbr>DES</abbr>-based algorithm or alternative algorithms that may be available on the system.</p>
<p><strong><tt>CRYPT_BLOWFISH</tt></strong> &#8211; Blowfish hashing with a salt as follows: &#8220;$2a$&#8221;, a two digit cost parameter, &#8220;$&#8221;, and 22 base64 digits from the alphabet &#8220;./0-9A-Za-z&#8221;. Using characters outside of this range in the salt will cause crypt() to return a zero-length string. The two digit cost parameter is the base-2 logarithm of the iteration count for the underlying Blowfish-based hashing algorithmeter and must be in range 04-31, values outside this range will cause crypt() to fail.</p></blockquote>
</div>
</div>
</div>
</div>
<h3><a href="http://en.wikipedia.org/wiki/PBKDF2">PBKDF2</a></h3>
<p>PBKDF2 (Password-Based Key Derived Function) is a function to produce derived keys by repeatedly hashing the input many times. I&#8217;m not going to attempt to explain PBKDF2 in detail, however by tuning the number of times to repeatedly hash the input the cost can be increased, in the same way as for BCrypt. PBKF2 is used by some well known protocols/systems, such as WPA/WPA2, <a href="http://www.gnu.org/software/grub/">Grub 2</a>, and <a href="https://lastpass.com">LastPass</a>.</p>
<h1>Generating random data with PHP</h1>
<p>Generating random data doesn&#8217;t sound like it should be challenging, and indeed it is not &#8211; however generating cryptographically strong random data is a bit harder.</p>
<p>The default random number source used by PHP is not considered to be cryptographically strong &#8211; however not to worry, there are various sources of cryptographically strong random data on most systems.</p>
<h3>PHP 5.3+, OpenSSL extension</h3>
<p>The OpenSSL extension, included by default in PHP 5.3+, provides a source of cryptographically strong random data.</p>
<div id="layout_2">
<div id="content">
<div id="function.openssl-random-pseudo-bytes">
<div>
<blockquote><p><strong><a href="http://www.php.net/manual/en/function.openssl-random-pseudo-bytes.php">openssl_random_pseudo_bytes</a></strong> — Generate a pseudo-random string of bytes</p>
<p>Generates a <a href="http://www.php.net/manual/en/language.types.string.php">string</a> of pseudo-random bytes, with the number of bytes determined by the <em><tt>length</tt></em> parameter.</p>
<p>It also indicates if a cryptographically strong algorithm was used to produce the pseudo-random bytes, and does this via the optional <em><tt>crypto_strong</tt></em> parameter. It&#8217;s rare for this to be <strong><tt>FALSE</tt></strong>, but some systems may be broken or old.</p></blockquote>
</div>
</div>
</div>
</div>
<h3>/dev/urandom</h3>
<p>For Linux/UNIX systems, cryptographically strong random data is produced from the device unlocked random, /dev/urandom. This can be read using the regular file access functions in PHP.</p>
<h3>Microsoft CSP (Cryptographic Service Provider)</h3>
<p>The Microsoft Cryptographic Service Provider API provides a source of cryptographically strong random data on Microsoft Windows systems. It can be accessed using the <a href="http://www.php.net/manual/en/class.com.php">COM</a> interface.</p>
<h1>Solution used in FluxBB v2</h1>
<p>For FluxBB v2 I have produced a <a href="https://github.com/fluxbb/password">library</a> which makes use of BCrypt when available on the system (PHP 5.3+), and falls back to PBKDF2 otherwise. Cryptographically strong random data is obtained using the OpenSSL extension if available, will fall back to /dev/urandom, or in the worse case PHPs built in non-cryptographically-strong functions.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jamierf.co.uk/2011/05/09/securely-storing-passwords-using-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Extensible PHP software: Hooks &amp; patches</title>
		<link>http://www.jamierf.co.uk/2010/02/08/extensible-php-software-hooks-patches/</link>
		<comments>http://www.jamierf.co.uk/2010/02/08/extensible-php-software-hooks-patches/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 18:32:41 +0000</pubDate>
		<dc:creator>Jamie</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[fluxbb]]></category>
		<category><![CDATA[open-source]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.jamierf.co.uk/?p=321</guid>
		<description><![CDATA[In thinking about FluxBB 2.0, one of the important goals is to come up with a well designed and easy to use extension system. The days of manually digging into the code are past, people now expect to be able to install an extension with one click. In PunBB 1.3 (which we forked into FluxBB [...]]]></description>
			<content:encoded><![CDATA[<p>In thinking about FluxBB 2.0, one of the important goals is to come up with a well designed and easy to use extension system. The days of manually digging into the code are past, people now expect to be able to install an extension with one click.</p>
<p>In PunBB 1.3 (which we forked into FluxBB 1.3 Legacy), Rickard introduced the use of hooks and eval, to allow extensions to insert code and extend the core software. Originally this system was designed to allow small changes, but by the time the software was finished it had mutated into a full blown extension system.</p>
<p>Along with the disadvantages inherent from a hook system (see below), FluxBB 1.3 Legacy used a rather horrible system of PHP code within XML code, which was parsed and inserted into the database on install. The aim was that then the install files could be deleted or overwritten and the extension wouldn&#8217;t be affected until it was uninstalled/updated. However due to the fact that most extensions referenced external files (be it included php files, or css and images) this theory did not work very well.</p>
<p>So, going back to the beginning, what are the different solutions available for writing extensible PHP software?</p>
<p><span id="more-321"></span></p>
<h3>Hooks</h3>
<p>Hooks are probably the most common way used to make PHP software extensible. The idea is fairly simple; throughout the code there are &#8220;hooks&#8221;, where extensions can insert code. Below is an example hook taken from FluxBB 1.3 Legacy:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;color: #FCFFBA;color: black;"><span style="color: #b1b100;color: #B83A24;">if</span> <span style="color: #009900;color: #000;">&#40;</span><span style="color: #990000;color: #B83A24;">isset</span><span style="color: #009900;color: #000;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;color: #000;">&#91;</span><span style="color: #0000ff;">'form_sent'</span><span style="color: #009900;color: #000;">&#93;</span><span style="color: #009900;color: #000;">&#41;</span><span style="color: #009900;color: #000;">&#41;</span>
<span style="color: #009900;color: #000;">&#123;</span>
	<span style="color: #009900;color: #000;">&#40;</span><span style="color: #000088;">$hook</span> <span style="color: #339933;color: #000;">=</span> get_hook<span style="color: #009900;color: #000;">&#40;</span><span style="color: #0000ff;">'po_form_submitted'</span><span style="color: #009900;color: #000;">&#41;</span><span style="color: #009900;color: #000;">&#41;</span> ? <span style="color: #990000;color: #B83A24;">eval</span><span style="color: #009900;color: #000;">&#40;</span><span style="color: #000088;">$hook</span><span style="color: #009900;color: #000;">&#41;</span> <span style="color: #339933;color: #000;">:</span> <span style="color: #009900; font-weight: bold;color: #343832;">null</span><span style="color: #339933;color: #000;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;color: green;">// Make sure form_user is correct</span></pre></div></div>

<p>In the above example the get_hook method will look for all the code which has been registered with the hook &#8220;po_form_submitted&#8221;, combine it and return it. The code will then be executed using the <a href="http://uk2.php.net/manual/en/function.eval.php">eval</a> function.</p>
<p>The disadvantages of the hook system should be fairly obvious&#8230;</p>
<ul>
<li><span style="background-color: #ffffff;">Blocks of code can be inserted, but only at pre defined locations within the software. This means extra thought has to be given during development as to where hooks may be required. If someone writing an extension needs a hook in a place where there isn&#8217;t one, then they are out of luck.</span></li>
<li><span style="background-color: #ffffff;">Insertion of code is all very well, but what if we want to change or remove some existing code? There is no way to do this using hooks. This leads to extensions with rather &#8220;interesting&#8221; algorithms to attempt to undo or block code in the core, which can be both messy and inefficient.</span></li>
</ul>
<p>Despite these rather major sounding disadvantages, I would argue hooks are good! At least much better than requiring users to change code by hand&#8230;</p>
<p>In the example code given above the eval function was used to execute the inserted code, but where is the code actually stored, and is this the best way to execute it? There are a few options:</p>
<ul>
<li>Store the code in a database or XML etc. then load and eval it when required. This probably isn&#8217;t the best solution as it involves storing code in the database or other storage that isn&#8217;t designed for code. Caching would probably also be required, loading and parsing a large XML file on every page load would not be efficient.</li>
<li>Keep the code for each hook in an individual PHP file and include it when required. This option seems reasonable, but could result in having a huge amount of tiny files being included, which could be hard to organize and may not be ideal if the server isn&#8217;t running any form of opcode cache.</li>
<li>Keep the code all in one PHP file and register specific functions with each hook, then call these when required. This seems like the best solution, but introducing functions introduces problems of variable scope. The underlying code would need to be written carefully in a way that appropriate variables can be passed to each hook.</li>
</ul>
<h3>Patches</h3>
<p>A totally different approach to extending PHP software is patching it. In a similar way to how a <a href="http://en.wikipedia.org/wiki/Patch_(Unix)">patch file</a> is used, the software could read instructions from an extension and physically modify the appropriate code.</p>

<div class="wp_syntax"><div class="code"><pre class="diff" style="font-family:monospace;color: #FCFFBA;color: black;"><span style="color: #440088;">@@ -58,7 +58,9 @@</span>
 // Did someone just hit &quot;Submit&quot; or &quot;Preview&quot;?
 if <span style="color: #000;">&#40;</span>isset<span style="color: #000;">&#40;</span>$_POST<span style="color: #000;">&#91;</span>'form_sent'<span style="color: #000;">&#93;</span><span style="color: #000;">&#41;</span><span style="color: #000;">&#41;</span>
 <span style="color: #000;">&#123;</span>
<span style="color: #991111;">-	// Make sure form_user is correct</span>
<span style="color: #00b000;">+	// This is some new code</span>
<span style="color: #00b000;">+	$username = isset<span style="color: #000;">&#40;</span>$_POST<span style="color: #000;">&#91;</span>'username'<span style="color: #000;">&#93;</span><span style="color: #000;">&#41;</span> ? $_POST<span style="color: #000;">&#91;</span>'username'<span style="color: #000;">&#93;</span> : null;</span>
<span style="color: #00b000;">+</span></pre></div></div>

<p>This means that once the extension has been installed, there is no extension code left to worry about; the physical files have been changed and the extension installation files can be removed.</p>
<p>At first this may seem like a much better solution than hooks; code can be inserted/changed/deleted anywhere in any files, there is no need to litter hooks all over the place. However it has three major disadvantages:</p>
<ul>
<li>Often in shared hosting environments PHP will be configured to run as a certain unix user (usually &#8220;www-data&#8221; or &#8220;nobody&#8221;). For the software to be able to patch itself, all files would require to be writeable by that user. This could prove to be a major security flaw as it would allow other users on the server to modify your files. A work around for this could be to modify the files over (S)FTP instead, but then the user would need to provide their hosting login details.</li>
<li>Updating the core software becomes harder. Since the files have been physically modified it is no longer a simple task to replace them with updated files. In theory core updates could be applied the same way extensions are installed (i.e. as patches), but with a modified board conflicts are fairly likely, and code that been removed by extensions is likely to just be replaced when the core is updated.</li>
<li>An extension with a bug, or even a malicious extension, could render the whole software unusable by deleting sections of code or inserting invalid code. Since the actual files would have physically been overwritten there would be no way to recover from this, other than the user manually uploading a backup (if they were organized enough to have one!).</li>
</ul>
<p>To me this seems like a rather show stopping problem, what&#8217;s the point in a one-click install system if uninstallation can require manually removing the changes. The idea of a buggy extension being able to totally destroy a working install is not acceptable!</p>
<h3>Conclusions</h3>
<p>What will be use in FluxBB 2.0? Only time will tell, but I&#8217;m currently leaning towards making use of hooks again, but attaching functions loaded from native PHP scripts, rather than chunks of code to be evaled.</p>
<p>Have any suggestions or better ideas? Please leave a comment!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jamierf.co.uk/2010/02/08/extensible-php-software-hooks-patches/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>

