<?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>Web Cash &#187; Security</title>
	<atom:link href="http://www.earn-web-cash.com/category/security/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.earn-web-cash.com</link>
	<description>Writing, Designing, and Making Money Online</description>
	<lastBuildDate>Sun, 04 Dec 2011 22:52:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>How to Spoof a Form, or Why Your Form Isn&#8217;t Safe</title>
		<link>http://www.earn-web-cash.com/2008/02/25/spoof-forms-security/</link>
		<comments>http://www.earn-web-cash.com/2008/02/25/spoof-forms-security/#comments</comments>
		<pubDate>Tue, 26 Feb 2008 00:54:03 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[HTML Tutorials]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[spoof]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/25/spoof-forms-security/</guid>
		<description><![CDATA[Forms can create all kinds of security holes in PHP applications. The biggest reason for this is that you can never be sure just what kind of input you&#8217;ll be getting. There are some steps you can take later on to protect your applications from this unknown input. But first, let&#8217;s take a look at [...]]]></description>
			<content:encoded><![CDATA[<p>Forms can create all kinds of security holes in PHP applications.  The biggest reason for this is that you can never be sure just what kind of input you&#8217;ll be getting.</p>
<p>There are some steps you can take later on to protect your applications from this unknown input.  But first, let&#8217;s take a look at <strong>why</strong> you can never trust a user&#8217;s input and just how easy it is for someone to spoof a form.<br />
<span id="more-160"></span></p>
<h4>What Does It Mean to Spoof a Form?</h4>
<p>It could mean a number of related things, but generally speaking it means submitting form information in a manner other than the original form was intended.  The form processing script, completely unaware of the situation, processes the input as if it came from the original form.</p>
<p>For example, if a form generates a URL query string, one way to spoof that form would be to modify the query string and re-visit the URL.  The new page would think that my query came from the original form &#8211; even though I really crafted the query myself.</p>
<p>Why would someone want to do this?  It may be curiosity.  &#8220;I wonder what happens if I change this variable&#8230;&#8221;  Or it may be malicious.  A hacker can spoof a form processing script to send dangerous input to your PHP script.</p>
<h4>The Problem With the &#8220;Get&#8221; Method</h4>
<p>It is easiest to spoof a form if the information is transmitted through the &#8220;Get&#8221; method.  This means that the variables are placed into a query string and appended to the form&#8217;s action URL.</p>
<p>Let&#8217;s say you have a form with three elements &#8211; a textarea element, a hidden input, and a select element.  Logically, you might think that the hidden input is restricted to whatever value you set it to and the select element is restricted to one of the options you provided.</p>
<p>To make this a little more concrete, let&#8217;s assume the textarea contains a post to a multi-user blog, the hidden input contains the author&#8217;s name, and the select element includes the category number.</p>
<p>When the user enters the values and clicks &#8220;submit,&#8221; he&#8217;s sent to a URL that looks something like this.</p>
<pre>processing.php?text=Bla+bla+bla&#038;author=Walkere&#038;catid=2</pre>
<p>What if I wanted to pretend to be someone else &#8211; like Bob?  I could simply change the &#8216;author&#8217; variable.</p>
<pre>processing.php?text=Bla+bla+bla&#038;author=Bob&#038;catid=2</pre>
<p>Maybe I want to post my message in a special category &#8211; one that isn&#8217;t normally open to me.  Again, it&#8217;s as simple as changing the catid variable to another number.</p>
<p>Now, a well designed script will notice these data flukes and either ignore them or spit out an error.  However, if you have too much faith in form input &#8211; and you blindly used the values from your form &#8211; you&#8217;d soon be in a mess of trouble.</p>
<h4>But Post Is More Secure&#8230; Right?</h4>
<p>The &#8220;Post&#8221; method is somewhat more secure.  Why?  Because all of the information is transmitted behind the scenes.</p>
<p>When I was younger, I tried to meddle with the $_GET variables of an online game that I played.  I never would have known enough about the workings of the script to spoof a &#8220;post&#8221; form &#8211; but it was so blatantly obvious with &#8220;get.&#8221;</p>
<p>However, someone &#8220;in the know&#8221; can easily spoof a form using the post method as well.  One way to do this is&#8230; write your own form.</p>
<p>If I look at the HTML source of your form, I can easily see what values you&#8217;re submitting to the processing script.  I can recreate those input elements on a form on <strong>my</strong> site &#8211; and then set the action to your processing script.</p>
<p>In this way, I could make the hidden and select statements regular text inputs &#8211; and input whatever values I want.  If you&#8217;re form processing script isn&#8217;t savvy, my custom values will get passed on to the database.</p>
<p>One thing you can do to prevent this is to check the referring URL coming into your script.  This will prevent someone from simply creating a form off-site to spoof your script.</p>
<h4>Sending an Actual HTTP Request</h4>
<p>The final, and most devious way, to spoof a form is to craft your own HTTP request in PHP.  This is essentially what a form does &#8211; but you can manually craft the request to your liking instead of letting a form do it.</p>
<p>How do we do this?</p>
<p>First, we create a stream context.  To do that we create a simple array and then call the function stream_context_create.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$params</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'http'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'method'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'post'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'http'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'headers'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Referer: http://www.google.com'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'http'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'content'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$data</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// A regular query string with parameters</span>
&nbsp;
<span style="color: #000088;">$context</span> <span style="color: #339933;">=</span> <span style="color: #990000;">stream_context_create</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$processingUrl</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'r'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #000088;">$context</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<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></pre></div></div>

<p>The actual form values are placed in the $data variable.  This is in a typical query string format &#8211; key=var&#038;key2=var2, etc.  The values should also be url encoded.</p>
<p>The nifty thing here is that you can manually specify HTTP headers to be sent with your request.  In this example, we set the referring URL to Google.  If someone knew your processing script checked the referrer&#8217;s address &#8211; they could simply spoof it with an HTTP header.</p>
<h4>Never Trust Input</h4>
<p>The bottom line is &#8211; never trust input that you haven&#8217;t thoroughly tested, validated, and cleaned.</p>
<p>Even if an input item is supposed to be hidden or supposed to come from a specific set of values &#8211; you can&#8217;t be sure that the form processing script is going to receive the correct value.  The script needs to check the info and make sure that it got what it was looking for.</p>
<p>It&#8217;s pretty simple for someone to spoof your form and send any input they like to your processing script.  If you don&#8217;t close the security holes, then someone will eventually blow them open for you.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/25/spoof-forms-security/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to Create Multi-Page Forms in PHP, Revisited</title>
		<link>http://www.earn-web-cash.com/2008/02/08/multi-page-form-revisted/</link>
		<comments>http://www.earn-web-cash.com/2008/02/08/multi-page-form-revisted/#comments</comments>
		<pubDate>Sat, 09 Feb 2008 00:59:26 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/08/multi-page-form-revisted/</guid>
		<description><![CDATA[A couple weeks ago, I wrote a short article about how to create a multi-page form. The simple solution I suggested involved a foreach loop that cycled through the $_POST array and sent every value along in a hidden input element. After a bit of reflection (and some useful comments), I realized there&#8217;s a teeny [...]]]></description>
			<content:encoded><![CDATA[<p>A couple weeks ago, I wrote a short article about <a href="http://www.earn-web-cash.com/2008/01/27/multi-page-form/">how to create a multi-page form</a>.</p>
<p>The simple solution I suggested involved a foreach loop that cycled through the $_POST array and sent every value along in a hidden input element.  After a bit of reflection (and some useful comments), I realized there&#8217;s a teeny tiny security hole in that approach &#8211; so I&#8217;ve slightly modified it to close the loophole.<br />
<span id="more-103"></span></p>
<h5>The Multi-Page Form Snippet</h5>
<p>The old snippet of code looked like this&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;input type=&quot;hidden&quot; name=&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">.</span> 
    <span style="color: #0000ff;">'&quot; value=&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$val</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot; /&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h5>The Security Loophole</h5>
<p>What&#8217;s the problem?  If someone enters a quote into the form, it could break all of the hidden elements.  Here&#8217;s an example.</p>
<p>Let&#8217;s say I type this into a text input.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&quot; /&gt;&lt;input type=&quot;text&quot; name=&quot;newField&quot; value=&quot;newValue</pre></div></div>

<p>The next page would create this&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;hidden&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;firstField&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;newField&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;newValue&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span></pre></div></div>

<p>By adding a quote, you prematurely end the first hidden element and can add new html tags.  You can combine this with javascript for some potentially nasty effects.  I guess.  I&#8217;m really not sure <strong>what</strong> you could do with this&#8230; but it&#8217;s better to be safe than sorry, eh?</p>
<h5>How to Fix the Multi-Page Form Snippet</h5>
<p>We can close this loophole once and for all with a simple function call &#8211; htmlentities.  With the proper parameter, this will encode all html entity characters (including quotes), so that the user can&#8217;t input a quote or create new HTML tags.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;input type=&quot;hidden&quot; name=&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot; value=&quot;'</span> 
    <span style="color: #339933;">.</span> <span style="color: #990000;">htmlentities</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">ENT_QUOTES</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot; /&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>No more problems.</p>
<p>It&#8217;s also important to note that this problem isn&#8217;t just a security loophole.  By not validating the input before creating the hidden element, you could accidentally break the form for a legitimate user.  If the user enters a &#8221; inside a text field, it would break the hidden element on the next page.</p>
<h5>Why Not Use Sessions or a Database?</h5>
<p>Some people have suggested using sessions or a database to accomplish the same thing.  That&#8217;s certainly possible, but I feel this is a better design choice.</p>
<p>I like to keep the php and html as separate as possible.  This allows us to keep all of the input inside of the form until we&#8217;re ready to process it.  Meanwhile, our processing script sees all of the input as coming from a single form &#8211; even if it came from different pages.</p>
<p>By contrast, you need to break up your processing script to use databases.  You also need to mix and match $_POST and $_SESSION to use sessions to store the information.  It&#8217;s functional, but doesn&#8217;t seem elegant to me.</p>
<p>With that in mind, this is something of a preference.  It&#8217;s not better than the other methods &#8211; but I think it is semantically more logical.  So use what you prefer.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/08/multi-page-form-revisted/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Understanding the Difference Between MD5 and Crypt Functions</title>
		<link>http://www.earn-web-cash.com/2008/02/02/md5-vs-crypt-password/</link>
		<comments>http://www.earn-web-cash.com/2008/02/02/md5-vs-crypt-password/#comments</comments>
		<pubDate>Sat, 02 Feb 2008 15:38:34 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[encryption]]></category>
		<category><![CDATA[password]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[user]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/02/md5-vs-crypt-password/</guid>
		<description><![CDATA[If you're working with a user-management system in PHP (or any scripting language), than you're likely using either the md5() or crypt() function to encode and store the user's password.

An oft-asked question - and oft-misunderstood - is what is the difference?  Is one better than the other?]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re working with a user-management system in PHP (or any scripting language), than you&#8217;re likely using either the md5() or crypt() function to encode and store the user&#8217;s password.</p>
<p>An oft-asked question &#8211; and oft-misunderstood &#8211; is what is the difference?  Is one better than the other?<br />
<span id="more-95"></span><br />
First, let&#8217;s take a quick look at how each one works.</p>
<h5>How MD5 Works</h5>
<p>MD5 creates a &#8220;hash&#8221; value based on an input string.  It uses a one-way algorithm to turn the password into an unintelligible garble of words.  Here&#8217;s a sample usage.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Bananas&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<pre>Output:
1ee31b77d0697c36914b99d1428f7f32</pre>
<p>This long string (32 characters) is the md5 hash value of &#8220;Bananas.&#8221;  You can now store it in the database, and when a user wants to log in you compare <code>md5($passwordInput)</code> to this hash value.</p>
<h5>Using Crypt to Encrypt a Password</h5>
<p>Crypt has very similar functionality, but with a unique twist.</p>
<p>Crypt still encrypts a phrase in a one-way algorithm to create a garbled bunch of characters.  Here&#8217;s an example&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #990000;">crypt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Bananas&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<pre>Output:
$1$upPJosTV$HC4n2bUsQFZk2IDN1CLdg.</pre>
<p>What&#8217;s unique about the crypt() function is that it uses an encryption key (or &#8220;salt&#8221;) to vary the encryption process.  This means that you can encrypt the same password (&#8220;Bananas&#8221;) multiple times and get different hash values to store in your database.</p>
<h5>Do I Have to Store the Encryption Key or Salt?</h5>
<p>This is where the crypt() function gets a bit confusing.  No, you don&#8217;t&#8230;  because the encryption key is stored in the password itself.</p>
<p>Take another look at this output.</p>
<pre><strong>$1$upPJosTV$</strong>HC4n2bUsQFZk2IDN1CLdg.</pre>
<p>The bold bit of text ($1$upPJosTV$) is the stored salt or encryption key (read <a href="http://www.earn-web-cash.com/2008/01/22/crypt-function-php/" title="How to Use the Crypt() Function to Encrypt and Check Passwords in PHP">how to use crypt()</a> for more information on how this salt is created).</p>
<p>So, no.  You don&#8217;t have to store the salt anywhere, because it&#8217;s stored in the hash&#8217;ed text.  You can simply use that as the salt in the future to check if the password is correct.  Like this&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">crypt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$passwordInput</span><span style="color: #339933;">,</span> <span style="color: #000088;">$hashedPassword</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$hashedPassword</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">//  Ok, log in and stuff.  }</span></pre></div></div>

<h5>Wait&#8230; My Encryption Key Is Stored <strong>IN</strong> the Password?!?</h5>
<p>This is where most people think that crypt() is a waste of time.  If the great thing about crypt() is that it uses a customizable encryption key, then isn&#8217;t it self-defeating to include the encryption key in the stored password?</p>
<p>Not entirely.</p>
<p>The problem with md5() is that everyone knows the encryption algorithm.  Sure, you can&#8217;t go backwards&#8230; but you can easily build a dictionary of known passwords and known hashes.  By storing these in a database, it is feasible to do a simple dictionary check to see if you have a password that goes with a known hash.</p>
<h5>Why You Should Use Crypt() Instead</h5>
<p>Since each crypt() call can use a unique encryption key, there can be no stored dictionary of password hashes.</p>
<p>Here&#8217;s an example.  Try running this script to see how different salts or encryption keys can change the output of a crypt() call.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #990000;">crypt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Bananas&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ab'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">crypt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Bananas&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ed'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">crypt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Bananas&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'pz'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">crypt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Bananas&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'qp'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">;</span></pre></div></div>

<pre>Output:
abBGdAR.aTnBE
edHaZeqWhmLpw
pzqz3tbQxuuRI
qp/4Jsj38Cq0Y</pre>
<p>This illustrates the strength of crypt().  By using a different encryption key, the same password (&#8220;Bananas&#8221;) can be turned into many different hashes.  You can&#8217;t simply create a lookup table with known passwords and known hashes &#8211; because the hash changes based on the encryption key.</p>
<p>The bottom line is that crypt() doesn&#8217;t make your passwords unbreakable or protect them from brute force attacks.  If someone wants to take the time to check every possible permutation of characters to get to your password, they can do it.</p>
<p>But crypt() does prevent the use of a password dictionary that contains known passwords and known hashes.  This would be a far more efficient way to hack a password than a simple brute force attack.  So by using crypt() you&#8217;re getting an extra layer of security.</p>
<p>[Note:  This article assumes you are using an md5 hash as the salt for crypt().  You could use an extended DES salt or a Blowfish salt for slightly different functionality, but these are not supported on all servers - including mine.]</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/02/md5-vs-crypt-password/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>How to Create a Random Password for Users</title>
		<link>http://www.earn-web-cash.com/2008/02/01/random-password/</link>
		<comments>http://www.earn-web-cash.com/2008/02/01/random-password/#comments</comments>
		<pubDate>Fri, 01 Feb 2008 21:56:49 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[password]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[snippet]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[user]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/01/random-password/</guid>
		<description><![CDATA[If you're working on a user-management system, you may find it useful to be able to create a random password for users.

Some systems generate these initially and have the user log in to set a permanent password.  You might also have a "Reset" button, where the script generates a random password and e-mails it to the user.

This quick tutorial will show you how to create an 8 character random password containing a mix of letters and numbers.  Or, if you're impatient, jump straight to the <a href='http://www.earn-web-cash.com/wp-content/uploads/2008/02/randpassword.txt' title='Random Password Generator'>function's source code</a>]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re working on a user-management system, you may find it useful to be able to create a random password for users.</p>
<p>Some systems generate these initially and have the user log in to set a permanent password.  You might also have a &#8220;Reset&#8221; button, where the script generates a random password and e-mails it to the user.</p>
<p>This quick tutorial will show you how to create an 8 character random password containing a mix of letters and numbers.  Or, if you&#8217;re impatient, jump straight to the <a href='http://www.earn-web-cash.com/wp-content/uploads/2008/02/randpassword.txt' title='Random Password Generator'>function&#8217;s source code</a><br />
<span id="more-90"></span><br />
There are plenty of ways to do this.  The simplest method would be to take a random number, generate an md5 hash, and then use the first 8 characters as the password.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">8</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>But this doesn&#8217;t guarantee you an even mix of upper case letters, lower case letters, and numbers.  To do that, we&#8217;ll need to use a few simple php functions and build a short script.</p>
<h5>Build a Loop to Create Eight Characters</h5>
<p>We&#8217;ll start our script by creating a blank string, looping eight times, and entering a character in the string each time.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">8</span><span style="color: #339933;">;</span> <span style="color: #000088;">$x</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000088;">$password</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'a'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This creates a blank string ($password) and iterates a loop eight times.  At this point, the loop simply enters the letter &#8216;a&#8217; into $password &#8211; so you end output should be &#8216;aaaaaaaa.&#8217;</p>
<h5>Generate Random Characters</h5>
<p>Now we need to generate random characters to go inside of the string.  To do this, we can make use of the rand() and chr() functions.  Replace the loop contents with this line.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #990000;">chr</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">60</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">95</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>chr() takes an integer and returns the ASCII equivalent of that number.  In this case, we&#8217;re using rand() to get a number between 60 and 95 &#8211; so we should get an uppercase letter in return.  Our random password should now contain eight random upper-case letters.</p>
<h5>Randomly Enter Uppercase, Lowercase, and Numbers</h5>
<p>To make the random password more secure, we should randomize whether the new character is a number, an upper-case letter, or a lower-case letter.  We can execute a simple &#8220;switch&#8221; statement to randomly choose which type of character to enter.</p>
<p>Replace the loop contents with this new snippet.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">//  Add a random digit, 0-9</span>
   <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">:</span>
   <span style="color: #000088;">$password</span> <span style="color: #339933;">.=</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;">9</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">//  Add a random upper-case letter</span>
   <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">:</span>
   <span style="color: #000088;">$password</span> <span style="color: #339933;">.=</span> <span style="color: #990000;">chr</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">65</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">90</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">//  Add a random lower-case letter</span>
   <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">:</span>
   <span style="color: #000088;">$password</span>  <span style="color: #339933;">.=</span> <span style="color: #990000;">chr</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">97</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">122</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We&#8217;re using rand(1, 3) to randomly choose which case to execute.  Each case then enters a different type of character in the string.  The first case simply returns a random digit.  The second and third cases use chr() and rand() to return a random character.</p>
<p>At this point, the script should give you an eight-character password with a random mix of uppercase letters, lowercase letters, and numbers.  Now you can e-mail the password to the user, take a hash of the password, and store it in the database.</p>
<p>For reference, here&#8217;s the entire script placed in a function.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> randPassword<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">8</span><span style="color: #339933;">;</span> <span style="color: #000088;">$x</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">//  Add a random digit, 0-9</span>
      <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">:</span>
      <span style="color: #000088;">$password</span> <span style="color: #339933;">.=</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;">9</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">//  Add a random upper-case letter</span>
      <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">:</span>
      <span style="color: #000088;">$password</span> <span style="color: #339933;">.=</span> <span style="color: #990000;">chr</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">65</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">90</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">//  Add a random lower-case letter</span>
      <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">:</span>
      <span style="color: #000088;">$password</span>  <span style="color: #339933;">.=</span> <span style="color: #990000;">chr</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">97</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">122</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #b1b100;">return</span> <span style="color: #000088;">$password</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/01/random-password/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>How to Use the Crypt() Function to Encrypt and Check Passwords</title>
		<link>http://www.earn-web-cash.com/2008/01/22/crypt-function-php/</link>
		<comments>http://www.earn-web-cash.com/2008/01/22/crypt-function-php/#comments</comments>
		<pubDate>Wed, 23 Jan 2008 02:48:13 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[encryption]]></category>
		<category><![CDATA[login]]></category>
		<category><![CDATA[password]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/01/22/how-to-use-the-crypt-function-to-encrypt-and-check-passwords/</guid>
		<description><![CDATA[Every php-script that involves some kind of user login and database interaction has one very important feature &#8211; password checking and encryption. There are a bunch of ways you could create and check passwords &#8211; from an insecure string in a database to an encrypted &#8220;hash&#8221; that you check against user input. This tutorial will [...]]]></description>
			<content:encoded><![CDATA[<p>Every php-script that involves some kind of user login and database interaction has one very important feature &#8211; password checking and encryption.</p>
<p>There are a bunch of ways you could create and check passwords &#8211; from an insecure string in a database to an encrypted &#8220;hash&#8221; that you check against user input.  This tutorial will show you how to use the <a href="http://us.php.net/manual/en/function.crypt.php" title="Crypt() function on php.net"><code>crypt()</code></a> function to store and check passwords in a php script.<br />
<span id="more-57"></span></p>
<h5>What Does the Crypt() Function Do?</h5>
<p>The <code>crypt()</code> function takes two parameters &#8211; the first parameter is the actual input (the password to test) and the second parameter is a &#8220;salt&#8221; or encryption key that is used to encrypt the password phrase.</p>
<p>Let&#8217;s take a look at what the <code>crypt()</code> function does with some input.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #990000;">crypt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Gobble&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;xt&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Would yield the output&#8230;</p>
<pre><code>xt0iPj3UKFQSM</code></pre>
<p>The function used the encryption key &#8220;xt&#8221; to turn &#8220;Gobble&#8221; into an encrypted mess.  Now, a person looking through the database won&#8217;t be able to find out a person&#8217;s password.  They&#8217;ll only find the encrypted password &#8211; which won&#8217;t work if you enter it into a script.</p>
<h5>The Crypt() Function Stores the Encryption Key in the Output</h5>
<p>There&#8217;s an important pattern here, though, that we can see if we look at a couple of <code>crypt()</code> calls in a row.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #990000;">crypt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Gobble&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;ab&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">crypt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Gobble&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;td&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">crypt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Gobble&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;pz&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Would yield the output&#8230;</p>
<pre><code>ab30/okS7bRdo
tdylLlJ9zwOss
pz0u5z5fgyCK.</code></pre>
<p>You can break each piece of output into two pieces &#8211; the first two characters and the last 11 characters.</p>
<p>The first two characters &#8220;ab,&#8221; &#8220;td,&#8221; and &#8220;pz&#8221; are the three &#8220;salts&#8221; or encryption keys that we used in our <code>crypt()</code> calls.  The last 11 characters are the actual encrypted pass phrases.</p>
<p>This simple point is crucial to the functioning of <code>crypt()</code>.  It stores the encryption key inside the encrypted phrase, so that you can use it to encrypt a new phrase &#8211; and compare them.  If you use the encrypted phrase as your &#8220;salt&#8221; (the second parameter for <code>crypt()</code>), the function will isolate the encryption key and ignore the rest.</p>
<p>So this example would output &#8220;Passwords match!&#8221;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Gobble&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// User input</span>
<span style="color: #000088;">$salt</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;ab&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Encryption key</span>
<span style="color: #000088;">$encrypted</span> <span style="color: #339933;">=</span> <span style="color: #990000;">crypt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span><span style="color: #339933;">,</span> <span style="color: #000088;">$salt</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">crypt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span><span style="color: #339933;">,</span> <span style="color: #000088;">$encrypted</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> 
    <span style="color: #000088;">$encrypted</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Passwords match!&quot;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span></pre></div></div>

<p>In this case we&#8217;re using the encrypted password ($encypt) to perform the encryption algorithm on the user&#8217;s input ($password) to see if they match.  Normally, you would have $encrypted stored in the database to perform comparisons in your script.</p>
<h5>Use an MD5 Hash Salt to Encrypt Phrases Over 8 Characters</h5>
<p>The final thing to keep in mind about <code>crypt()</code> is that it can use different kinds of salts or encryption keys.  The two-character salt we&#8217;ve been using is pretty weak.  It also has a flaw in functionality &#8211; using a two-character encryption key, the <code>crypt()</code> function will ignore everything past the first 8 characters of the phrase to be encrypted.</p>
<p>So both of these statements would have the same output.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #990000;">crypt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Gobbledeygook&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;ab&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">crypt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Gobbledeygah&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;ab&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The <code>crypt()</code> function is only encrypting the first eight characters &#8211; &#8220;Gobblede&#8221;.  The rest is ignored.</p>
<p>You can change this by using a special type of encryption key &#8211; a md5 hash.  Under normal circumstances, this is enabled in php, but you can double check by seeing if the constant <code>CRYPT_MD5</code> is set to &#8217;1&#8242;.</p>
<p>An md5 hash salt is formatted like this &#8211; <code>$1$xxxxxxxx$</code>.  &#8220;$1$&#8221;, followed by eight random characters, followed by a closing &#8220;$&#8221;.  You could create one yourself to use as a salt.</p>
<p>However, in most cases if you provide no salt or encryption key at all, php will generate a random salt for you.  So, for example, when you are entering a new password into the database you can use this statement&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #990000;">crypt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Gobble&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This variable ($password) can now be stored in the database.  Remember that $password holds both the encrypted phrase (Gobble) and the random encryption key.  So to check if a user entered the correct password you would fetch $password from the database and use this statement&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">crypt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$userInput</span><span style="color: #339933;">,</span> <span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">//  Ok, the passwords matched }</span></pre></div></div>

<p>Now that you know how <code>crypt()</code> works, get to it.  Start creating user-authentication scripts and work your encryption magic.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/01/22/crypt-function-php/feed/</wfw:commentRss>
		<slash:comments>32</slash:comments>
		</item>
		<item>
		<title>Protect Against Shell Script Hacks</title>
		<link>http://www.earn-web-cash.com/2008/01/12/protect-shell-script/</link>
		<comments>http://www.earn-web-cash.com/2008/01/12/protect-shell-script/#comments</comments>
		<pubDate>Sat, 12 Jan 2008 14:57:11 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Redirected]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[hack]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/01/12/protect-shell-script/</guid>
		<description><![CDATA[While browsing through Technorati, I just stumbled on a post about a shell script attack. It seems the poor chap got a shell script uploaded to his server, and the attacker used it to create a bunch of bogus files full of hyperlinks. The original post has some header information about this particular hack (a [...]]]></description>
			<content:encoded><![CDATA[<p>While browsing through Technorati, I just stumbled on a post about a <a href="http://cocaman.ch/wp/2008/01/websites-injected-cool-little-tool/" title="Post about Shell Script Attack, by Geekness">shell script attack</a>.  It seems the poor chap got a shell script uploaded to his server, and the attacker used it to create a bunch of bogus files full of hyperlinks.</p>
<p>The original post has some header information about this particular hack (a modified c100 shell), as well as a link to some search results about the file.  I looked through the <a href="http://www.leetupload.com/database/UNIX/shell.txt">source code for the shell script</a> and tested it out on my local server &#8211; getting some link-filled files is the least that this script could do.<br />
<span id="more-31"></span><br />
Once the script is loaded on your server, anyone can access it remotely and have full access to your system.</p>
<p>The script allows you to navigate through the server&#8217;s directories like any remote file-manager.  I noticed that it could even go up out of the web server root and into my local folders as well.</p>
<p><a href='http://www.earn-web-cash.com/wp-content/uploads/2008/01/shell_script_utils.png' title='Screenshot of Shell Script Utilities'><img class='alignright' src='http://www.earn-web-cash.com/wp-content/uploads/2008/01/shell_script_utils.thumbnail.png' alt='Screenshot of Shell Script Utilities' /></a>The script gathers up all the details on your computer &#8211; like operating system build, running processes, ip address, etc.  The user can run shell commands, create files, upload files, and do other kinds of nasty things.</p>
<p>It would be pretty easy for someone to use this to find the mysql username/password, hack your database, do whatever they want, and pretty wreck your entire site.  Or worse, they could use this to do some nasty things to the server itself &#8211; potentially wrecking other peoples&#8217; sites.</p>
<p>So how do you protect against this?  Well, I&#8217;m not sure what you would do to protect against the shell script once it&#8217;s loaded up.  It looks like it&#8217;s built to bypass most security precautions and give the hacker access to whatever he or she wants.</p>
<p>Your best bet is to be vigilant in restricting front-end uploads to your site.  If you&#8217;ve got an upload script, be sure you restrict what file extensions can be uploaded.  </p>
<p>This script needs to be named with a file extension that is read as php &#8211; so you should never allow users to upload php files (or html if you set up your server to execute those as php).</p>
<p>Being more restrictive is better than less restrictive &#8211; so ban all file extensions <strong>except</strong> the ones you know are safe.  So, for example, you might allow &#8220;.jpg, .gif, .png&#8221; for pictures, and &#8220;.doc, .odt, .pdf, .txt, .rtf&#8221; for documents.</p>
<p>If you&#8217;ve got any other suggestions for security against this sort of thing, please comment away.  Otherwise, take a look at the script so that you are aware of what it can do.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/01/12/protect-shell-script/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

