<?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>code-diesel &#187; security</title>
	<atom:link href="http://www.codediesel.com/category/security/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codediesel.com</link>
	<description>/* PHP &#38; MySQL Journal */</description>
	<lastBuildDate>Thu, 02 Feb 2012 13:19:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Changing WordPress admin login error message</title>
		<link>http://www.codediesel.com/security/changing-wordpress-admin-login-error-message/</link>
		<comments>http://www.codediesel.com/security/changing-wordpress-admin-login-error-message/#comments</comments>
		<pubDate>Mon, 12 Dec 2011 05:13:15 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[security]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2965</guid>
		<description><![CDATA[It can be surprising how easy it is to overlook security issues in software design. WordPress for example, after all the versions, still displays login error information that can be informative for a potential hacker. Rather than displaying a generic login error message, WordPress admin specifically displays whether a &#8216;username&#8217; was entered wrong or a [...]]]></description>
			<content:encoded><![CDATA[<p>It can be surprising how easy it is to overlook security issues in software design. WordPress for example, after all the versions, still displays login error information that can be informative for a potential hacker. Rather than displaying a generic login error message, WordPress admin specifically displays whether a &#8216;username&#8217; was entered wrong or a &#8216;password&#8217; as the following screenshot shows.<br />
<span id="more-2965"></span><br />
<a href="http://www.codediesel.com/wp-content/uploads/2011/12/wp-login1.gif"><img src="http://www.codediesel.com/wp-content/uploads/2011/12/wp-login1.gif" alt="" title="wp-login1" width="330" height="293" class="aligncenter size-full wp-image-2966" /></a></p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2011/12/wp-login3.gif"><img src="http://www.codediesel.com/wp-content/uploads/2011/12/wp-login3.gif" alt="" title="wp-login3" width="333" height="307" class="aligncenter size-full wp-image-2969" /></a></p>
<p>This can be useful for a hacker to get  the username right. A good idea is to display a generic message as given below.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2011/12/wp-login2.gif"><img src="http://www.codediesel.com/wp-content/uploads/2011/12/wp-login2.gif" alt="" title="wp-login2" width="331" height="291" class="aligncenter size-full wp-image-2967" /></a></p>
<p>You can easily achieve that by adding the following line to your themes &#8216;functions.php&#8217; file.</p>

<div class="wp_codebox"><table><tr id="p29653"><td class="code" id="p2965code3"><pre class="php" style="font-family:monospace;">add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'login_errors'</span><span style="color: #339933;">,</span>
            <span style="color: #990000;">create_function</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'$no_login_error'</span><span style="color: #339933;">,</span> 
                            <span style="color: #0000ff;">&quot;return 'Oops! Wrong Credentials.';&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The second argument to the &#8216;add_filter&#8217; creates a anonymous callback function. This is equivalent to the following.</p>

<div class="wp_codebox"><table><tr id="p29654"><td class="code" id="p2965code4"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> no_login_error<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">'Oops! Wrong Credentials.'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'login_errors'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'no_login_error'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/security/changing-wordpress-admin-login-error-message/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WordPress plugins to check for security threats</title>
		<link>http://www.codediesel.com/security/wordpress-plugins-to-check-for-security-threats/</link>
		<comments>http://www.codediesel.com/security/wordpress-plugins-to-check-for-security-threats/#comments</comments>
		<pubDate>Sat, 10 Dec 2011 05:47:53 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[security]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2952</guid>
		<description><![CDATA[With WordPress installations on the rise, security threats are a common concern for users. Below is a small collection of WordPress plugins that I found adequate for use on most WordPress installations to keep security in check. As always security is not 100% achievable, but the following plugins will help you find any loopholes in [...]]]></description>
			<content:encoded><![CDATA[<p>With WordPress installations on the rise, security threats are a common concern for users. Below is a small collection of WordPress plugins that I found adequate for use on most WordPress installations to keep security in check. As always security is not 100% achievable, but the following plugins will help you find any loopholes in your site and help you monitor for any security breaches.<br />
<span id="more-2952"></span><br />
<strong>BulletProof Security</strong><br />
<a href="http://wordpress.org/extend/plugins/bulletproof-security/" rel="nofollow"  title="BulletProof Security" target="_blank">BulletProof Security</a> protects your website from XSS, RFI, CSRF, Base64, Code Injection and SQL Injection hacking attempts. The plugin also Protects wp-config.php, bb-config.php, php.ini, php5.ini, install.php and readme.html with .htaccess security protection. BPS has built-in Backup and Restore, a File Editor, a File Uploader and a File Downloader to make managing the BPS master .htaccess files and your website security fast and simple.</p>
<p><strong>WordPress File Monitor</strong><br />
<a href="http://wordpress.org/extend/plugins/wordpress-file-monitor/" rel="nofollow"  title="WordPress File Monitor" target="_blank">WFM</a> monitors your WordPress installation for any added, deleted or changed files. When a change is detected an email is sent to a specified address. Currently does not support multi-site installations.</p>
<p><strong>Ultimate Security Checker</strong><br />
<a href="http://wordpress.org/extend/plugins/ultimate-security-checker/" rel="nofollow"  title="Ultimate Security Checker" target="_blank">Ultimate Security Checker</a> helps you identify security problems with your WordPress installation. It scans your WordPress blog for hundreds of known threats, then gives you a security &#8220;grade&#8221; based on how well you have protected yourself.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/security/wordpress-plugins-to-check-for-security-threats/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The HTML5 keygen element</title>
		<link>http://www.codediesel.com/security/the-html5-keygen-element/</link>
		<comments>http://www.codediesel.com/security/the-html5-keygen-element/#comments</comments>
		<pubDate>Tue, 16 Aug 2011 04:50:51 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[html]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[html5]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2889</guid>
		<description><![CDATA[There is some confusion among users regarding the new HTML5 keygen element. The keygen element generates a public/private key pair and then creates a certificate request. Many users want to know the exact use of the element when SSL is available. This is my take on the subject. Correct me if I&#8217;m wrong. &#60;form action=&#34;process.cgi&#34; [...]]]></description>
			<content:encoded><![CDATA[<p>There is some confusion among users regarding the new HTML5 <strong>keygen</strong> element. The <strong>keygen</strong> element generates a public/private key pair and then creates a certificate request. Many users want to know the exact use of the element when SSL is available. This is my take on the subject. Correct me if I&#8217;m wrong.</p>

<div class="wp_codebox"><table><tr id="p28896"><td class="code" id="p2889code6"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;process.cgi&quot;</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;post&quot;</span> <span style="color: #000066;">enctype</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;multipart/form-data&quot;</span>&gt;</span>
 <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;&lt;keygen <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;key&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
 <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span>submit <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Submit key...&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span></pre></td></tr></table></div>

<p><span id="more-2889"></span><br />
SSL is about &#8220;server identification&#8221; , &#8220;server AND client authentication&#8221; and &#8220;security&#8221;. During the normal SSL handshake the server presents its server-certificate so the browser can be sure that this is the correct server it is connecting to. But what should the server use to identify that it is the same user (browser) it is connecting to once the initial handshake has been done. For this purpose you need a client-certificate. The <strong>keygen</strong> element is used for creating a key for authentication of the user while SSL is concerned about privacy of communication and the authentication of the server.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/security/the-html5-keygen-element/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Checking your site for malicious changes</title>
		<link>http://www.codediesel.com/security/checking-your-site-for-malicious-changes/</link>
		<comments>http://www.codediesel.com/security/checking-your-site-for-malicious-changes/#comments</comments>
		<pubDate>Mon, 08 Aug 2011 13:36:12 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2881</guid>
		<description><![CDATA[Today a couple of hours back my site got compromised. Not much changes to the code, but the .htacces was changed and some code like the below was added to the .htaccess file, which redirected the traffic coming from search engines to a malware site. It has now been removed and to prevent any such [...]]]></description>
			<content:encoded><![CDATA[<p>Today a couple of hours back my site got compromised. Not much changes to the code, but the .htacces was changed and some code like the below was added to the .htaccess file, which redirected the traffic coming from search engines to a malware site. </p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2011/08/htaccess_redirect.png"><img src="http://www.codediesel.com/wp-content/uploads/2011/08/htaccess_redirect.png" alt="" title="htaccess redirect" width="351" height="126" class="aligncenter size-full wp-image-2882" /></a><br />
<span id="more-2881"></span><br />
It has now been removed and to prevent any such changes to the .htaccess file in the future, I&#8217;ve written a small php script that compares the hash (SHA1) of the two major files that usually get compromised and compare them to the one originally stored. The script will run as a cron job and notify me by email if any changes are seen. This is a quick workaround which needs some more work done.</p>

<div class="wp_codebox"><table><tr id="p28818"><td class="code" id="p2881code8"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$hash1</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sha1_file</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'.htaccess'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$hash2</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sha1_file</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'index.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$hash1</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'fa7cdb22b81b0b713bfed609acc984591f9bed2f'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> 
   <span style="color: #009900;">&#40;</span><span style="color: #000088;">$hash2</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'b4bb6070800a340566d7d6872516d248d4a7aff3'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">mail</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;EMAIL&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Status Ok!&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Status Ok!&quot;</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;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">mail</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;EMAIL&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Alert!&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Files have changed!&quot;</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></td></tr></table></div>

<p>Of-course there are other ways the site can get hacked, but the last couple of times my site got compromised was for these reasons. So at-least I&#8217;ve one area covered.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/security/checking-your-site-for-malicious-changes/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Data filtering and validation using Inspekt</title>
		<link>http://www.codediesel.com/php/data-filtering-and-validation-using-inspekt/</link>
		<comments>http://www.codediesel.com/php/data-filtering-and-validation-using-inspekt/#comments</comments>
		<pubDate>Sat, 26 Mar 2011 04:56:07 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[libraries]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2810</guid>
		<description><![CDATA[Most web security glitches are caused by insufficient input filtering and validation. Despite a large number of validation libraries being available, input sanitization is usually bypassed due to sheer laziness or the idea that your site is somehow immune from bad input data. Inspekt is one such library that has a small footprint and is [...]]]></description>
			<content:encoded><![CDATA[<p>Most web security glitches are caused by insufficient input filtering and validation. Despite a large number of validation libraries being available, input sanitization is usually bypassed due to sheer laziness or the idea that your site is somehow immune from bad input data. <a href="https://github.com/funkatron/inspekt" rel="nofollow" target="_blank" >Inspekt</a> is one such library that has a small footprint and is very easy to integrate into your application.<br />
<span id="more-2810"></span></p>
<h4>The basic principle behind the library</h4>
<p>Every data filtering/validation library hinges on three basic principles.</p>
<p>The first principle is of preventing users from accessing user input via the PHP superglobals, because the &#8220;default&#8221; action is to retrieve the raw, potentially dangerous data.</p>
<p>The second principle is preventing ad-hoc filtering/validation code at various places in the application which can be error-prone and difficult to modify.</p>
<p>The final principle is that of simplicity. A validation library should make a programmer&#8217;s job easier. Complex solutions should be avoided unless they are the only solution, as programmers tend to bypass intricate solutions citing various reasons for the same.</p>
<h4>Installing Inspekt</h4>
<p>Inspekt is easy to install. Just download the library and include the &#8216;Inspekt.php&#8217; in your application with the correct path.</p>

<div class="wp_codebox"><table><tr id="p281017"><td class="code" id="p2810code17"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Inspekt.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h4>Caging data</h4>
<p>Take the following simple code using Inspekt. Here all the POST data is only made available through the &#8216;post_cage&#8217; object, once it is initialized. The original POST data is immediately deleted preventing you from accidentally using data from the $_POST array. In short all the POST data is now safety &#8216;caged&#8217; in the &#8216;post_cage&#8217; object, which can only be accessed by using the various objects functions.</p>

<div class="wp_codebox"><table><tr id="p281018"><td class="code" id="p2810code18"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Inspekt.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">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;">'submit'</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: #009933; font-style: italic;">/* This will print the $_POST data */</span>
    <span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* Store all the POST data in a 'cage', 
     * this will also automatically delete 
     * all the original POST data.
    */</span>
    <span style="color: #000088;">$post_cage</span> <span style="color: #339933;">=</span> Inspekt<span style="color: #339933;">::</span><span style="color: #004000;">makePostCage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* This will now return an empty array */</span>
    <span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>html<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>form id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;myform&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;myform&quot;</span> method<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;POST&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;hidden&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;fname&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;testString&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;hidden&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;id&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;500&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;submit&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;submit&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;submit&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;/</span>form<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p><strong>Example 1.</strong></p>
<h4>Accessing &#8216;caged&#8217; data</h4>
<p>Now that we have seen how the POST data is safely stored in a &#8216;cage&#8217;, we will look at how to access and validate the data.</p>
<p>In the above example we have a &#8216;fname&#8217; html field which we can access from the cage as following, which will print the string &#8220;testString&#8221;.</p>

<div class="wp_codebox"><table><tr id="p281019"><td class="code" id="p2810code19"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">.</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$post_cage</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRaw</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'fname'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">.</span>
<span style="color: #339933;">.</span></pre></td></tr></table></div>

<p>But wait, what does this buy us, we are just using another object to access the same POST data in a different way. Well this is just one method, which just returns the raw data, the real value comes when we use validation and filtering methods.</p>
<p>Suppose we wanted to make sure that only alpha-numeric characters are allowed in the &#8220;fname&#8221; variable, we can use the following code which will return the value of the variable if the value is alpha-numeric or &#8216;false&#8217; otherwise.</p>

<div class="wp_codebox"><table><tr id="p281020"><td class="code" id="p2810code20"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">.</span>
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fname</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$post_cage</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">testAlnum</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'fname'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #009933; font-style: italic;">// Do something here if 'fname' is alphanumeric</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">.</span>
<span style="color: #339933;">.</span></pre></td></tr></table></div>

<p>Again let us say we have to check if the value of the &#8216;id&#8217; field lies between 100 &#8211; 600, we can use the following:</p>

<div class="wp_codebox"><table><tr id="p281021"><td class="code" id="p2810code21"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">.</span>
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$post_cage</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">testBetween</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">600</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
     <span style="color: #009933; font-style: italic;">// value lies between 100 - 600, do something</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">.</span>
<span style="color: #339933;">.</span></pre></td></tr></table></div>

<p>Beside the above two, the following are some other useful methods:</p>
<p>    * testAlnum (mixed $key)<br />
    * testAlpha (mixed $key)<br />
    * testBetween (mixed $key, mixed $min, mixed $max, [boolean $inc = TRUE])<br />
    * testCcnum (mixed $key, [mixed $type = NULL])<br />
    * testDate (mixed $key)<br />
    * testDigits (mixed $key)<br />
    * testEmail (mixed $key)<br />
    * testFloat (mixed $key)<br />
    * testGreaterThan (mixed $key, [mixed $min = NULL])<br />
    * testHex (mixed $key)<br />
    * testHostname (mixed $key, [integer $allow = ISPK_HOST_ALLOW_ALL])<br />
    * testInt (mixed $key)<br />
    * testIp (mixed $key)<br />
    * testLessThan (mixed $key, [mixed $max = NULL])<br />
    * testOneOf (mixed $key, [ $allowed = NULL])<br />
    * testPhone (mixed $key, [ $country = 'US'])<br />
    * testRegex (mixed $key, [mixed $pattern = NULL])<br />
    * testUri (unknown_type $key)<br />
    * testZip (mixed $key)</p>
<h4>Caging other input data</h4>
<p>In the above examples we have seen how to add the POST data to a &#8216;cage&#8217; using the &#8216;Inspekt::makePostCage&#8217; method. Likewise, we can also use other methods for other input types:</p>
<p>•	Inspekt::makeGetCage()<br />
Returns an Inspekt_Cage for the $_GET array</p>
<p>•	Inspekt::makePostCage()<br />
Returns an Inspekt_Cage for the $_POST array</p>
<p>•	Inspekt::makeCookieCage()<br />
Returns an Inspekt_Cage for the $_COOKIE array</p>
<p>•	Inspekt::makeServerCage()<br />
Returns an Inspekt_Cage for the $_SERVER array</p>
<p>•	Inspekt::makeFilesCage()<br />
Returns an Inspekt_Cage for the $_FILES array</p>
<p>•	Inspekt::makeEnvCage()<br />
Returns an Inspekt_Cage for the $_ENV array</p>
<p>So to &#8216;cage&#8217; the $_GET data we can use the following:</p>

<div class="wp_codebox"><table><tr id="p281022"><td class="code" id="p2810code22"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$get_cage</span> <span style="color: #339933;">=</span> Inspekt<span style="color: #339933;">::</span><span style="color: #004000;">makeGetCage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h4>Using the Super Cage</h4>
<p>Using a different cage for each different input type can be cumbersome, for this we can use the  &#8216;SuperCage&#8217; method, which encapsulates all the above input data in a single object.</p>

<div class="wp_codebox"><table><tr id="p281023"><td class="code" id="p2810code23"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$super_cage</span> <span style="color: #339933;">=</span> Inspekt<span style="color: #339933;">::</span><span style="color: #004000;">makeSuperCage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Example 1. can be now be coded as below:</p>

<div class="wp_codebox"><table><tr id="p281024"><td class="code" id="p2810code24"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Inspekt.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">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;">'submit'</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;">$super_cage</span> <span style="color: #339933;">=</span> Inspekt<span style="color: #339933;">::</span><span style="color: #004000;">makeSuperCage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$super_cage</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">testAlnum</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'fname'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$super_cage</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">testBetween</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">220</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">600</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>html<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>form id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;myform&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;myform&quot;</span> method<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;POST&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;hidden&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;fname&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;testString&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;hidden&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;id&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;500&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;submit&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;submit&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;submit&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;/</span>form<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<h4>In conclusion</h4>
<p>I find Inspekt a quite interesting validation/filtering library. Although the development seems to have stalled at Version 0.4.1, the code is complete and simple to use. This post has only covered a small portion of the library, for more information you can visit the <a href="http://funkatron.com/inspekt/user_docs/" rel="nofollow" target="_blank" >original source</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/data-filtering-and-validation-using-inspekt/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Choosing strong passwords and user motivation</title>
		<link>http://www.codediesel.com/security/choosing-strong-passwords-and-user-motivation/</link>
		<comments>http://www.codediesel.com/security/choosing-strong-passwords-and-user-motivation/#comments</comments>
		<pubDate>Fri, 04 Mar 2011 04:25:37 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[security]]></category>
		<category><![CDATA[passwords]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2797</guid>
		<description><![CDATA[One of the main reasons for passwords being hacked is because of users choosing weak passwords. Motivation is one of the key driving forces when doing something, which also applies when choosing good passwords. Unless your account login has been hacked before, users have little motivation whatsoever for creating good passwords. (The average user overestimates [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.codediesel.com/wp-content/uploads/2011/03/nkd_password1.gif"><img src="http://www.codediesel.com/wp-content/uploads/2011/03/nkd_password1.gif" alt="" title="password authentication" width="157" height="86" class="alignleft size-full wp-image-2800" /></a> One of the main reasons for passwords being hacked is because of users choosing weak passwords. Motivation is one of the key driving forces when doing something, which also applies when choosing good passwords. Unless your account login has been hacked before, users have little motivation whatsoever for creating good passwords. (The average user overestimates the strength of his password and underestimates the ingenuity of the hacker). Of-course the user is not to blame. Creating a strong password is hard work; you need to balance the complexity of the password with memorability. Choose a strong password and the chances are high that you will tend to forget it if you do not use it on a regular basis.<br />
<span id="more-2797"></span><br />
Many security conscious sites now provide a password strength indicator (Fig 1.); preventing users from registering unless they select a strong enough password. I recently used this method when creating a secure site for a client with good results.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2011/03/nkd_password3.gif"><img src="http://www.codediesel.com/wp-content/uploads/2011/03/nkd_password3.gif" alt="" title="password strength" width="229" height="102" class="aligncenter size-full wp-image-2801" /></a><br />
<strong>Figure 1.</strong></p>
<p>One other method I recently found is that of &#8216;<a href="http://www.nakedpassword.com/" rel="nofollow" target="_blank" >naked passwords</a>&#8216;. While creating passwords, the beautiful model in the input box tastefully removes items of clothing as the password grows stronger (Fig 2). Of-course you can alter the images if they are too sensitive for your users tastes, but then you lose the motivational factor.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2011/03/nkd_password2.gif"><img src="http://www.codediesel.com/wp-content/uploads/2011/03/nkd_password2.gif" alt="" title="password authentication" width="270" height="55" class="aligncentre size-full wp-image-2798" /></a><br />
<strong>Figure 2.</strong></p>
<p>With all the different methods I have seen around the web, this particular one takes the cake, at-least for its motivational (and fun) factor. But all said and done, I&#8217;d prefer to use the password strength meter (Fig 1.) than the above method. Using this (Fig 2) on a public site will be a sure recipe for disaster as it borders on the obscene and gender discrimination; but now we are entering into philosophical territory, which however interesting to peruse, is not the point of this post. If I plan to use this I&#8217;ll surely change the images to something interesting which can be used publicly.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/security/choosing-strong-passwords-and-user-motivation/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Security &#8216;Challenge Questions&#8217; and the social web</title>
		<link>http://www.codediesel.com/security/security-challenge-questions-and-the-social-web/</link>
		<comments>http://www.codediesel.com/security/security-challenge-questions-and-the-social-web/#comments</comments>
		<pubDate>Thu, 24 Feb 2011 05:39:08 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[security]]></category>
		<category><![CDATA[social web]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2792</guid>
		<description><![CDATA[Lately I’ve been a little paranoid about posting details about myself on the social web, especially Facebook. In fact I’ve completely gone off it (never been a fan of it from the first). I’m surprised by the amount of personal information people post on the social web. One of my concerns is related to the [...]]]></description>
			<content:encoded><![CDATA[<p>Lately I’ve been a little paranoid about posting details about myself on the social web, especially Facebook. In fact I’ve completely gone off it (never been a fan of it from the first). I’m surprised by the amount of personal information people post on the social web.</p>
<p>One of my concerns is related to the ‘Challenge Questions’ provided by major websites such as Gmail, Hotmail for authentication, mostly during account recovery. Take for example the Challenge Question offered by Gmail:</p>
<p>What is the name of your manager at your first job?<br />
What is the name of your best friend from childhood?<br />
What was the name of your first teacher?</p>
<p>Answers to most of the above questions can be garnered by a little digging around your social stream, making it easier for the malicious person to hack your account. Aggregators such as <a href="http://identengine.com/" rel="nofollow" target="_blank" >IdentEngine</a>, a Javascript library, can make it even easier to locate your profiles around the social web. In short, the social web &#8220;leaks&#8221; security information.<br />
<span id="more-2792"></span><br />
When Scott McNealy remarked in 1999, &#8220;You have zero privacy anyway,Get over it.&#8221;, he was right on the technical front but wrong otherwise. You still have control over of how much personal information about you floats around the web; it may be a little harder but not impossible. The important point is to be alert and proactive when divulging important information around the web.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/security/security-challenge-questions-and-the-social-web/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Encode your email links to prevent spam</title>
		<link>http://www.codediesel.com/security/encode-your-email-links-to-prevent-spam/</link>
		<comments>http://www.codediesel.com/security/encode-your-email-links-to-prevent-spam/#comments</comments>
		<pubDate>Wed, 15 Dec 2010 10:29:25 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[html]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[spam]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2771</guid>
		<description><![CDATA[One of the popular ways to hide your email on a web page from spam bots is to display the email as an image or to use the &#8216;[at]&#8216; word instead of the &#8216;@&#8217; sign. The code given here is yet another way to fight spam. The below function will let you to encode email [...]]]></description>
			<content:encoded><![CDATA[<p>One of the popular ways to hide your email on a web page from spam bots is to display the email as an image or to use the &#8216;[at]&#8216; word instead of the &#8216;@&#8217; sign. The code given here is yet another way to fight spam. The below function will let you to encode email or other links to their equivalent HTML entity encoded syntax. This will enable you to hide your web-page emails from spam bots. As the browser converts and displays the appropriate string from the encoding the user will be able to correctly see the email id, but a spam bot will have a difficult time to decode the encoded string. Of course we now have quite sophisticated crawlers that can work around this types of encoding, but for other crawlers that rely on regular expressions or other such simple methods, they will find it difficult to grab the email links from the page.<br />
<span id="more-2771"></span></p>
<h4>Encoding HTML characters</h4>
<p>The function is quite simple. It encodes email, links or other string characters to their &#8216;Numeric character references&#8217;. For example &#8216;<strong>code</strong>&#8216; will be encoded to <strong>&amp;#99;&amp;#111;&amp;#100;&amp;#101;</strong> &#8211; a decimal encoding or <strong>&amp;#X63;&amp;#X6f;&amp;#X64;&amp;#X65;</strong>  &#8211; a hexadecimal encoding.<br />
The function randomly encodes a character to a <em>hex</em> or <em>decimal</em> representation, so each page refresh will encode the same string to a different format.</p>

<div class="wp_codebox"><table><tr id="p277127"><td class="code" id="p2771code27"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> encodeString<span style="color: #009900;">&#40;</span><span style="color: #000088;">$email</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$enc_email</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">srand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;"># Convert each character to decimal or hex representation
</span>    <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$email</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$enc_email</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;&amp;#&quot;</span> <span style="color: #339933;">.</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">ord</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$email</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;;&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$enc_email</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;&amp;#X&quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">dechex</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">ord</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$email</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;;&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000088;">$enc_email</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">echo</span> encodeString<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;designhouse@host.com&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>This will convert the email &#8216;designhouse@host.com&#8217; to</p>

<div class="wp_codebox"><table><tr id="p277128"><td class="code" id="p2771code28"><pre class="text" style="font-family:monospace;">&amp;#100;&amp;#101;&amp;#X73;&amp;#X69;&amp;#X67;&amp;#X6e;&amp;#104;
&amp;#X6f;&amp;#X75;&amp;#115;&amp;#101;&amp;#64;&amp;#X68;&amp;#111;
&amp;#X73;&amp;#116;&amp;#X2e;&amp;#99;&amp;#111;&amp;#109;</pre></td></tr></table></div>

<p>As it uses a random base (decimal or hex), the actual results may differ from yours.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/security/encode-your-email-links-to-prevent-spam/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Encrypting uploaded files in PHP</title>
		<link>http://www.codediesel.com/php/encrypting-uploaded-files-in-php/</link>
		<comments>http://www.codediesel.com/php/encrypting-uploaded-files-in-php/#comments</comments>
		<pubDate>Sun, 07 Nov 2010 13:19:07 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[encryption]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2740</guid>
		<description><![CDATA[During a recent project, the client requested that uploaded files be encrypted for security reasons. As I already had the uploaded code ready and tested I just needed to add some extra encryption capability to the code. As earlier I&#8217;d encountered Zends wonderful Zend_Filter class, I decided to go with it and use the Zend_Filter_Encrypt [...]]]></description>
			<content:encoded><![CDATA[<p>During a recent project, the client requested that uploaded files be encrypted for security reasons. As I already had the uploaded code ready and tested I just needed to add some extra encryption capability to the code. As earlier I&#8217;d encountered Zends wonderful <a href="http://framework.zend.com/manual/en/zend.filter.set.html" rel="nofollow" target="_blank" >Zend_Filter</a> class, I decided to go with it and use the <em>Zend_Filter_Encrypt</em> and <em>Zend_Filter_Decrypt</em> to accomplish the work. The Zend_Filter component provides a set of common useful data filters, among which are the encryption filters. Although my project was not developed in Zend, I could easily integrate the required classes in the code. Note that Zend has a great upload library, <a href="http://framework.zend.com/manual/en/zend.file.transfer.introduction.html" rel="nofollow" target="_blank" >Zend_File_Transfer</a>, that lets you easily manage file uploading and also encryption, but as I already had the upload code tested, I decided to just add the encryption part.<br />
<span id="more-2740"></span></p>
<h4>Downloading the Zend framework</h4>
<p>As the following code requires the Zend framework make sure you <a href="http://www.zend.com/community/downloads" rel="nofollow" target="_blank" >download</a> it first. For this code I used Zend 1.11.0 Full version. You can also download the required files at the end of this post.</p>
<h4>Which Zend framework files do I need</h4>
<p>You only need some selected files to make the below code work. The following is a list of files and directories that I used from the &#8216;ZendFramework-1.11.0\library\Zend&#8217; directory. The top three in the list are directories. There are also many files in the &#8216;Filter&#8217; directory that are not required, but for keeping it simple we will use the whole thing.</p>
<p>File (dir)<br />
Filter (dir)<br />
Loader (dir)<br />
Loader.php<br />
Filter.php<br />
Exception.php</p>
<h4>Encrypting uploaded files</h4>
<p>For the following example I&#8217;ve not included the file uploading code. I assume you already have the upload code ready. You can also use the following code by itself to encrypt/decrypt files. The program for file encryption is shown below.</p>

<div class="wp_codebox"><table><tr id="p274038"><td class="code" id="p2740code38"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Load the Zend file encrypting filter */</span>
<span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'./Zend/Filter/File/Encrypt.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/*  Set various encryption options. */</span>
<span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                <span style="color: #009933; font-style: italic;">// Encryption type - Openssl or Mcrypt</span>
                <span style="color: #0000ff;">'adapter'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'mcrypt'</span><span style="color: #339933;">,</span> 
                <span style="color: #009933; font-style: italic;">// Initialization vector</span>
                <span style="color: #0000ff;">'vector'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'236587hgtyujkirtfgty5678'</span><span style="color: #339933;">,</span> 
                <span style="color: #009933; font-style: italic;">// Encryption algorithm</span>
                <span style="color: #0000ff;">'algorithm'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'rijndael-192'</span><span style="color: #339933;">,</span> 
                <span style="color: #009933; font-style: italic;">// Encryption key</span>
                <span style="color: #0000ff;">'key'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'KFJGKDK$$##^FFS345678FG2'</span> 
                <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Initialize the library and pass the options */</span>
<span style="color: #000088;">$encrypt</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Filter_File_Encrypt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* 
   Set output filename, where the encrypted file will be stored.
   If we omit this, the encrypted file will overwrite the original file.
*/</span>
<span style="color: #000088;">$filter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFilename</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'test.enc.pdf'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Now encrypt a file */</span>
<span style="color: #000088;">$encrypt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">filter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'test.pdf'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Decrypting the encrypted files is as simple as above. Note that you need to keep the &#8216;vector&#8217; and &#8216;key&#8217; values the same as you used for encryption, or you will not be able to correctly decrypt the file.</p>

<div class="wp_codebox"><table><tr id="p274039"><td class="code" id="p2740code39"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Load the Zend file decrypting filter */</span>
<span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'./Zend/Filter/File/Decrypt.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/*  Set various decryption options. */</span>
<span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                <span style="color: #009933; font-style: italic;">// Encryption type - Openssl or Mcrypt</span>
                <span style="color: #0000ff;">'adapter'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'mcrypt'</span><span style="color: #339933;">,</span> 
                <span style="color: #009933; font-style: italic;">// Initialization vector</span>
                <span style="color: #0000ff;">'vector'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'236587hgtyujkirtfgty5678'</span><span style="color: #339933;">,</span> 
                <span style="color: #009933; font-style: italic;">// Decryption algorithm</span>
                <span style="color: #0000ff;">'algorithm'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'rijndael-192'</span><span style="color: #339933;">,</span> 
                <span style="color: #009933; font-style: italic;">// Decryption key</span>
                <span style="color: #0000ff;">'key'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'KFJGKDK$$##^FFS345678FG2'</span> 
                <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Initialize the library and pass the options */</span>
<span style="color: #000088;">$decrypt</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Filter_File_Decrypt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* 
   Set output filename, where the decrypted file will be stored.
*/</span>
<span style="color: #000088;">$filter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFilename</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'test.pdf'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Now decrypt the previously encrypted file */</span>
<span style="color: #000088;">$decrypt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">filter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'test.enc.pdf'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<h4>Selecting a encryption algorithm</h4>
<p>In the example given I&#8217;ve used the <a href="http://en.wikipedia.org/wiki/Advanced_Encryption_Standard" rel="nofollow" target="_blank" >rijndael-192 (AES)</a> algorithm but you can choose some other according to the availability. &#8216;rijndael&#8217; is fine for most requirements. First you will need to know what algorithms are supported by your installation. A quick way to find out is to use the following code.</p>

<div class="wp_codebox"><table><tr id="p274040"><td class="code" id="p2740code40"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
    <span style="color: #000088;">$algorithms</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mcrypt_list_algorithms</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$algorithms</span> <span style="color: #000000; font-weight: bold;">as</span> <span style="color: #000088;">$cipher</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$cipher</span>&lt;br /&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>On my system it lists the following algorithms.</p>
<p>cast-128 , gost , rijndael-128 , twofish , arcfour , cast-256 , loki97 , rijndael-192 , saferplus , wake , blowfish-compat , des , rijndael-256 , serpent , xtea , blowfish , enigma , rc2 , tripledes.</p>
<h4>Selecting a random Initialization vector</h4>
<p>Before we continue our discussion, a brief overview of Initialization vector (IV). An IV is a random string that can be used along with a key for data encryption. IV is used to prevent a sequence of text that is identical to a previous sequence from producing the same exact cipher-text when encrypted. </p>
<p>As you can see from the example code we have used a fixed IV. The disadvantage of this is that there is a possibility that a committed hacker would be able to guess the IV by studying the pattern in the encrypted files and thus break the encryption. The best way then is to let the class generate a random IV for each new encryption. This requires a little change of code, as shown below. The only extra step we now require is to store the generated random IV in a database which will then be used for decryption.</p>
<p>Encryption:</p>

<div class="wp_codebox"><table><tr id="p274041"><td class="code" id="p2740code41"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Load the Zend file encrypting filter */</span>
<span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'./Zend/Filter/File/Encrypt.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/*  Set various encryption options. */</span>
<span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                <span style="color: #009933; font-style: italic;">// Encryption type - Openssl or Mcrypt</span>
                <span style="color: #0000ff;">'adapter'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'mcrypt'</span><span style="color: #339933;">,</span> 
                <span style="color: #009933; font-style: italic;">// Encryption algorithm</span>
                <span style="color: #0000ff;">'algorithm'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'rijndael-192'</span><span style="color: #339933;">,</span> 
                <span style="color: #009933; font-style: italic;">// Encryption key</span>
                <span style="color: #0000ff;">'key'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'KFJGKDK$$##^FFS345678F54'</span> 
                <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Initialize the library and pass the options */</span>
<span style="color: #000088;">$filter</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Filter_File_Encrypt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Generate a random vector */</span>
<span style="color: #000088;">$filter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setVector</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Set output filename, where the encrypted file will be stored. */</span>
<span style="color: #000088;">$filter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFilename</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'test.enc.pdf'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Now encrypt a file */</span>
<span style="color: #000088;">$filter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">filter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'test.pdf'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* 
    Save the vector in a DB or somewhere else,
    we will need this during decryption
*/</span>
<span style="color: #000088;">$vector</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$filter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getVector</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Decryption:</p>

<div class="wp_codebox"><table><tr id="p274042"><td class="code" id="p2740code42"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Load the Zend file decrypting filter */</span>
<span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'./Zend/Filter/File/Decrypt.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/*  Set various encryption options. */</span>
<span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                <span style="color: #009933; font-style: italic;">// Encryption type - Openssl or Mcrypt</span>
                <span style="color: #0000ff;">'adapter'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'mcrypt'</span><span style="color: #339933;">,</span> 
                <span style="color: #009933; font-style: italic;">// Encryption algorithm</span>
                <span style="color: #0000ff;">'algorithm'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'rijndael-192'</span><span style="color: #339933;">,</span> 
                <span style="color: #009933; font-style: italic;">// Encryption key</span>
                <span style="color: #0000ff;">'key'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'KFJGKDK$$##^FFS345678F54'</span> 
                <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Initialize the library and pass the options */</span>
<span style="color: #000088;">$filter</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Filter_File_Decrypt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* 
   Use the saved vector for decryption.
   Note that using a wrong vector will result in a incorrect decryption.
*/</span>
<span style="color: #000088;">$filter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setVector</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'your-saved-vector'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Set output filename, where the decrypted file will be stored. */</span>
<span style="color: #000088;">$filter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFilename</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'test.dec.pdf'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Now decrypt the previously encrypted file */</span>
<span style="color: #000088;">$filter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">filter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'test.enc.pdf'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<h4>Selecting the correct &#8216;vector&#8217; and &#8216;key&#8217; size</h4>
<p>Before you run the code make sure that you set your PHP error reporting to &#8216;E_ALL&#8217;. Mcrypt requires that you use a correct IV and key length, which depends on which algorithm is used. Selecting a wrong IV (if you are using a fixed IV) or key length can generate the following error, which if the errors are disabled will be hidden and you will keep wondering as to why the files are not getting encrypted.:</p>
<p>- in case of a wrong vector length:</p>
<blockquote><p>
Fatal error: Uncaught exception &#8216;Zend_Filter_Exception&#8217; with message &#8216;The given vector has a wrong size for the set algorithm&#8217;
</p></blockquote>
<p>- in case of a wrong key length:</p>
<blockquote><p>
Fatal error: Uncaught exception &#8216;Zend_Filter_Exception&#8217; with message &#8216;The given key has a wrong size for the set algorithm&#8217;
</p></blockquote>
<p>To get the correct sizes use the following code. I&#8217;ve used &#8216;rijndael-192&#8242; algorithm here, but you need to substitute whatever algorithm you have selected instead.</p>

<div class="wp_codebox"><table><tr id="p274043"><td class="code" id="p2740code43"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$cipher</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mcrypt_module_open</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'rijndael-192'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ofb'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$vector_size</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mcrypt_enc_get_iv_size</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cipher</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$key_size</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mcrypt_enc_get_supported_key_sizes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cipher</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$vector_size</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$key_size</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Which on my machine returns the following.</p>

<div class="wp_codebox"><table><tr id="p274044"><td class="code" id="p2740code44"><pre class="text" style="font-family:monospace;">24
&nbsp;
Array
(
    [0] =&gt; 16
    [1] =&gt; 24
    [2] =&gt; 32
)</pre></td></tr></table></div>

<p>So if the IV size returns &#8217;24&#8242; then you need to use a random string of 24 character for the Initialization Vector (IV). The key length can be any from the returned values, 16, 24 or 32.</p>
<h4>Downloading encrypted file</h4>
<p>To round-off the post, I&#8217;ve included code to download a encrypted file via a link, which will be decrypted and passed to the user. For example you can call the download link as below:</p>

<div class="wp_codebox"><table><tr id="p274045"><td class="code" id="p2740code45"><pre class="php" style="font-family:monospace;">http<span style="color: #339933;">:</span><span style="color: #009933; font-style: italic;">//www.site.com/download-file.php?docname=test.enc.pdf</span></pre></td></tr></table></div>

<p>The code for &#8216;download-file.php&#8217; is shown below. The code is a bit simplified, for e.g no security validation is done on the $_GET variable.</p>

<div class="wp_codebox"><table><tr id="p274046"><td class="code" id="p2740code46"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* download-file.php */</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* 
   The constants UPLOAD_PATH, TEMP_PATH, INIT_VECTOR, ENCRYPTION_KEY
   have to be changed to your particular setup.
*/</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Zend/Filter/File/Decrypt.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$docname</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'docname'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$filename</span> <span style="color: #339933;">=</span> UPLOAD_PATH <span style="color: #339933;">.</span> <span style="color: #000088;">$docname</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$destination</span> <span style="color: #339933;">=</span> TEMP_PATH <span style="color: #339933;">.</span> <span style="color: #000088;">$docname</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">file_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;Error accessing the file.&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Copy encrypted file to a temporary folder */</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">copy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #339933;">,</span> <span style="color: #000088;">$destination</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error accessing file&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
 <span style="color: #009933; font-style: italic;">/* Zend file decryption */</span>
<span style="color: #000088;">$filter</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Filter_File_Decrypt<span style="color: #009900;">&#40;</span>
                <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'adapter'</span>     <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'mcrypt'</span><span style="color: #339933;">,</span>
                      <span style="color: #0000ff;">'vector'</span>      <span style="color: #339933;">=&gt;</span> INIT_VECTOR<span style="color: #339933;">,</span>
                      <span style="color: #0000ff;">'algorithm'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'rijndael-192'</span><span style="color: #339933;">,</span>
                      <span style="color: #0000ff;">'key'</span>         <span style="color: #339933;">=&gt;</span> ENCRYPTION_KEY
                      <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$filter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">filter</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$destination</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$destination</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'rb'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strstr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'HTTP_USER_AGENT'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;MSIE&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Type: &quot;application/octet-stream&quot;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Disposition: attachment; filename=&quot;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$docname</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Expires: 0'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Cache-Control: must-revalidate, post-check=0, pre-check=0'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-Transfer-Encoding: binary&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Pragma: public'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-Length: &quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">filesize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$destination</span><span style="color: #009900;">&#41;</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;">else</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Type: &quot;application/octet-stream&quot;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Disposition: attachment; filename=&quot;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$docname</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-Transfer-Encoding: binary&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Expires: 0'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Pragma: no-cache'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-Length: &quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">filesize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$destination</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #990000;">fpassthru</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Delete the copied decrypted from the temp folder */</span>
<span style="color: #990000;">unlink</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$destination</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/encrypting-uploaded-files-in-php/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Prevent hotlinking of your site images</title>
		<link>http://www.codediesel.com/security/prevent-hotlinking-of-your-site-images/</link>
		<comments>http://www.codediesel.com/security/prevent-hotlinking-of-your-site-images/#comments</comments>
		<pubDate>Mon, 18 Oct 2010 11:13:24 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[security]]></category>
		<category><![CDATA[hotlinking]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2720</guid>
		<description><![CDATA[A common problem with image intensive sites is &#8220;Image-Theft&#8221; also known as hotlinking. If your site hosts images that other sites are linking to, it can cause a substantial increase in your bandwidth costs. For small sites who pay by the gigabyte&#8217;s this can be quite a problem. For example, if a hotlinked image of [...]]]></description>
			<content:encoded><![CDATA[<p>A common problem with image intensive sites is &#8220;Image-Theft&#8221; also known as hotlinking. If your site hosts images that other sites are linking to, it can cause a substantial increase in your bandwidth costs. For small sites who pay by the gigabyte&#8217;s this can be quite a problem. For example, if a hotlinked image of 150K from your site is being accessed 1000 times a day, then that&#8217;s around 145MB of wasted data transferred from your site daily. Add a few more images and the bandwidth costs quickly add-up. A simple way is to use .htaccess to disallow other sites from hotlinking to your images.<br />
<span id="more-2720"></span></p>
<h4>The Rewrite rule</h4>
<p>Below is shown the complete htaccess rule to prevent image hotlinking. The details of each line are given next.</p>

<div class="wp_codebox"><table><tr id="p272053"><td class="code" id="p2720code53"><pre class="php" style="font-family:monospace;">RewriteEngine On
RewriteCond <span style="color: #339933;">%</span><span style="color: #009900;">&#123;</span>HTTP_REFERER<span style="color: #009900;">&#125;</span> <span style="color: #339933;">!</span>^http<span style="color: #339933;">:</span><span style="color: #009933; font-style: italic;">//(.+\.)?codediesel\.com/ [NC]</span>
RewriteCond <span style="color: #339933;">%</span><span style="color: #009900;">&#123;</span>HTTP_REFERER<span style="color: #009900;">&#125;</span> <span style="color: #339933;">!</span>^$
RewriteRule <span style="color: #339933;">.*</span>\<span style="color: #339933;">.</span><span style="color: #009900;">&#40;</span>gif<span style="color: #339933;">|</span>bmp<span style="color: #339933;">|</span>png<span style="color: #339933;">|</span>jpe?g<span style="color: #009900;">&#41;</span>$ <span style="color: #339933;">/</span><span style="color: #000000; font-weight: bold;">var</span><span style="color: #339933;">/</span>www<span style="color: #339933;">/</span>images<span style="color: #339933;">/</span>hotlink<span style="color: #339933;">.</span>gif <span style="color: #009900;">&#91;</span>L<span style="color: #009900;">&#93;</span></pre></td></tr></table></div>

<h4>RewriteRule breakup</h4>
<p>The first line kicks the htaccess Rewrite engine into action. The second condition checks to see if the image is requested from our own site. If you have another site that you want to be able to link to your images, then add it to your .htaccess. The [NC] at the end is a no-case flag, a rule with this flag will not care whether the text that we are considering is uppercase or lowercase. The default behavior of the RewriteRule directive is to be case sensitive.</p>

<div class="wp_codebox"><table><tr id="p272054"><td class="code" id="p2720code54"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;"># Only allow image linking for 'codediesel.com' and 'friendly-site.com'
</span>
RewriteCond <span style="color: #339933;">%</span><span style="color: #009900;">&#123;</span>HTTP_REFERER<span style="color: #009900;">&#125;</span> <span style="color: #339933;">!</span>^http<span style="color: #339933;">:</span><span style="color: #009933; font-style: italic;">//(.+\.)?codediesel\.com/ [NC]</span>
RewriteCond <span style="color: #339933;">%</span><span style="color: #009900;">&#123;</span>HTTP_REFERER<span style="color: #009900;">&#125;</span> <span style="color: #339933;">!</span>^http<span style="color: #339933;">:</span><span style="color: #009933; font-style: italic;">//(.+\.)?friendly-site\.com/ [NC]</span></pre></td></tr></table></div>

<p>The third condition ensures that we accept requests made directly for the image without a referer. This makes sure that we display the images even if for some reason a user-agent sends a empty referrer.This can happen for a variety of reasons. Some browsers, for reasons of privacy can be configured never to return referer information.</p>

<div class="wp_codebox"><table><tr id="p272055"><td class="code" id="p2720code55"><pre class="php" style="font-family:monospace;">RewriteCond <span style="color: #339933;">%</span><span style="color: #009900;">&#123;</span>HTTP_REFERER<span style="color: #009900;">&#125;</span> <span style="color: #339933;">!</span>^$</pre></td></tr></table></div>

<p>The last line specifies that the file names end in .jp(e)g, .gif, .bmp, or .png. This ensures that the Rewrite rule is only triggered for images and not for other files. When an attempt is made to hotlink a image from a domain other than the ones specified, the image given below will be returned, &#8216;myhotlink.gif&#8217; here.</p>

<div class="wp_codebox"><table><tr id="p272056"><td class="code" id="p2720code56"><pre class="php" style="font-family:monospace;">RewriteRule <span style="color: #339933;">.*</span>\<span style="color: #339933;">.</span><span style="color: #009900;">&#40;</span>gif<span style="color: #339933;">|</span>bmp<span style="color: #339933;">|</span>png<span style="color: #339933;">|</span>jpe?g<span style="color: #009900;">&#41;</span>$ <span style="color: #339933;">/</span><span style="color: #000000; font-weight: bold;">var</span><span style="color: #339933;">/</span>www<span style="color: #339933;">/</span>images<span style="color: #339933;">/</span>myhotlink<span style="color: #339933;">.</span>gif <span style="color: #009900;">&#91;</span>L<span style="color: #009900;">&#93;</span></pre></td></tr></table></div>

<p>A variation of this is to use the forbidden flag.</p>

<div class="wp_codebox"><table><tr id="p272057"><td class="code" id="p2720code57"><pre class="php" style="font-family:monospace;">RewriteRule <span style="color: #339933;">.*</span>\<span style="color: #339933;">.</span><span style="color: #009900;">&#40;</span>gif<span style="color: #339933;">|</span>bmp<span style="color: #339933;">|</span>png<span style="color: #339933;">|</span>jpe?g<span style="color: #009900;">&#41;</span>$ <span style="color: #339933;">-</span> <span style="color: #009900;">&#91;</span>F<span style="color: #009900;">&#93;</span></pre></td></tr></table></div>

<h4>Testing if your htaccess rule is working</h4>
<p>A quick way to test if your hotlinking blocking rules are working is use a free online <a href="http://www.free-webhosts.com/hotlinking-checker.php" rel="nofollow" target="_blank" >hotlink checker</a>. Or create a simple HTML on your localhost and add a link to your image. Before checking make sure that your browser cache has been cleared.</p>

<div class="wp_codebox"><table><tr id="p272058"><td class="code" id="p2720code58"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>html<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>img src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://www.your-site.com/images/sample-image.png&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Although there are some other ways to prevent image hotlinking, this is the quickest that I know of. As with any htaccess rewrites, you may block some users behind proxies or firewalls using these techniques.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/security/prevent-hotlinking-of-your-site-images/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic page generated in 5.759 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2012-02-08 10:55:17 -->

