<?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>Server-Side Magazine &#187; sessions</title>
	<atom:link href="http://www.serversidemagazine.com/tag/sessions/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.serversidemagazine.com</link>
	<description>Cutting edge server-side tutorials &#38; news</description>
	<lastBuildDate>Tue, 31 Jan 2012 07:24:33 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Session Hijacking</title>
		<link>http://www.serversidemagazine.com/php/session-hijacking/</link>
		<comments>http://www.serversidemagazine.com/php/session-hijacking/#comments</comments>
		<pubDate>Fri, 05 Dec 2008 02:57:50 +0000</pubDate>
		<dc:creator>Stelian Mocanita</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[attack]]></category>
		<category><![CDATA[hijacking]]></category>
		<category><![CDATA[php5]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[sessions]]></category>

		<guid isPermaLink="false">http://www.serversidemagazine.com/?p=270</guid>
		<description><![CDATA[The usage of sessions is the php developer&#8217;s most common use since we constantly need to transact data from step to step. An average programmer would say that using sessions is far more secure than letís say cookies since the session data is server side data, thing that is partially correct.

The fact that the attacker [...]]]></description>
			<content:encoded><![CDATA[<p>The usage of sessions is the php developer&#8217;s most common use since we constantly need to transact data from step to step. An average programmer would say that using sessions is far more secure than letís say cookies since the session data is server side data, thing that is partially correct.</p>

<p><p>The fact that the attacker can&#8217;t have a clear look at what and where you store comes to your advantage but a more dedicated attacker can go a bit further than this presumption.</p>
<p>A must have for the attacker in a session hijack is the Session Identifier so he can impersonate the attack. Let&#8217;s presume for example that you have your website hosted on a shared hosting on which PHP is installed as an Apache module, thing that makes session files belong to the web user, in other words: accessible.</p></p>

<p><span id="more-270"></span>
<p>Some more elaborated attacks could be categorized as follows:</p>
<ul>
    <li><a href="#prediction" title="Session Prediction">Prediction</a></li>
    <li><a href="#capture" title="Session Capture">Capture</a></li>
    <li><a href="#fixation" title="Session Fixation">Fixation</a></li>
</ul>
<p><strong id="prediction">Prediction</strong> refers to guessing a session identifier, approach that can be rather irrelevant since the native php session identifier is far too random to be analyzed at a glance so the next one can be predicted, meaning the attacker&#8217;s focus is not usually here.</p>
<p><strong id="capture">Capture</strong> on the other hand is the session hijacker&#8217;s best practice since it is rather versatile. Having the fact that the session id is being propagated by numerous resorts such as HTTP Headers, Cookies, E-mail Headers and so on, accessing it couldn&#8217;t be too hard for the attacker could it? </p>
<p>The worst of this is that each of these ways of session id propagation can be a door for attackers so which one is the best for us? Well, on a small scale, cookies are a bit less exposed than the $<em>GET data for example, so if you have cookies enabled you can work with that as browsers are generally speaking secure &#8211;  there were just a few miss-behaviors in Internet Explorer.</p>
<p><strong id="fixation">Fixation</strong> is not a very complex method but if you rely your session security on a mere session</em>start() you are in trouble. Since you only work with the default Session Identifier, reproducing a common user&#8217;s HTTP Header isn&#8217;t that difficult, so how do we work with this?</p>
<p>Let&#8217;s base an example on a simple HTTP Header such as:</p></p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>5
6
7
8
9
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">   GET <span style="color: #000000; font-weight: bold;">/</span> HTTP<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">1.1</span>
   Host: example.org
   User-Agent: Mozilla<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">5.0</span>
   Accept: text<span style="color: #000000; font-weight: bold;">/</span>xml, image<span style="color: #000000; font-weight: bold;">/</span>png, image<span style="color: #000000; font-weight: bold;">/</span>jpeg, image<span style="color: #000000; font-weight: bold;">/</span>gif, <span style="color: #000000; font-weight: bold;">*/*</span>
   Cookie: <span style="color: #007800;">PHPSESSID</span>=<span style="color: #000000;">1234</span></pre></td></tr></table></div>


<p>So we have the HTTP Header in its simplest form yet we have some data to work with. Taking for example that seconds after you get this another request from a different user agent. </p>

<p><p>As an educated developer one must assume the worst case scenario: this might come from an attacker and not from a user using two browsers. A common use in this case might be re-asking for the user&#8217;s credentials, thing that would not impose any issue on the user&#8217;s behalf but would disarm the attacker at this stage. </p>
<p>An easy approach to this would be generating a <em>MD5</em> for the User Agent but than again, <em>MD5</em> strings are quite easy to recognize so why not using a more elaborated approach and binding a passkey to the algorithm?</p></p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">   <span style="color: #000088;">$token</span>  <span style="color: #339933;">=</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: #000088;">$token</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'ABRACADABRA'</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #000088;">$securetoken</span> <span style="color: #339933;">=</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$token</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>


<p>This approach can help you prevent some of the attacks but not all of them as you can imagine.</p>

<p><p><strong>Another best practice to preventing the evil session hijack is using the session&#8217;s session<em>regenerate</em>id()</strong> once the user logs in so if his session identifier has been &#8216;sniffed&#8217; somewhere along the way the chase ends there and so the attacker would have to go all over again.</p>
<p>So we know now how this works, some best practices but how can we make it full proof?
Unfortunately, I do not have an answer for the 100% but I have some answers that will make your application safe to most attacks.</p>
<h3>Secure practices</h3>
<ol>
<li><strong>Do not trust users.</strong>
<p>Common users are not security educated users so they can easily fall for <a href="http://www.serversidemagazine.com/php/php-security-measures-against-csrf-attacks" title="PHP Security Measrues Against CSRF Attacks">CSRF attacks</a> and as long as the user is a victim, so are you. If you have any doubt whatsoever on a user&#8217;s action do not hesitate to ask for credentials. He wouldn&#8217;t mind as long as you tell him that it is for his own protection.</p></li>
<li><strong>If on a shared hosting make use of session<em>save</em>path()</strong>
<p>This will allow you to set a new path for your session data storage, a more secure one that the default setting up. Make sure that the folder you store the data is secured and make use of your <em>.htaccess</em> file to limit access there.</p></li>
<li><strong>Wondered why there are both $<em>COOKIE and $</em>SESSION?</strong>
<p>What you must take into consideration here is that cookies have a different purpose than storing session data so why not leave that as it is? <a href="http://www.serversidemagazine.com/php/4-most-important-php-security-measures" title="4 Most Important PHP Security Measures">XSS Faltering</a> is quite common so your cookies are not safe.</p></li>
<li><strong>Do not pass your session identifier in URLs</strong>
<p>By now you should be aware that the session identifier is the attacker&#8217;s must have and that your main task is to keep it well hidden so why just serving it to the attacker? As you can imagine this goes for $_GET as well.</p></li>
<li><strong>If skeptical on the entire HTTP Headers issue, use a security token at all time</strong>
<p>If nothing in the world could determine you to filter every incoming message than the least you can do is to use a security token at all time. A light example of such a token could be:</p></p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">   <span style="color: #000088;">$token</span> <span style="color: #339933;">=</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">uniqid</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>


<p></ol>
<p>Keeping all these in mind should make your applications far more safer so it is definitely worth your while.</p></p>
]]></content:encoded>
			<wfw:commentRss>http://www.serversidemagazine.com/php/session-hijacking/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->