<?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>PHP Software Developing &#187; Security</title>
	<atom:link href="http://www.phpdeveloping.co.za/category/security/feed" rel="self" type="application/rss+xml" />
	<link>http://www.phpdeveloping.co.za</link>
	<description>for the love of PHP Development</description>
	<lastBuildDate>Tue, 29 Sep 2009 15:38:14 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>register_globals evilness</title>
		<link>http://www.phpdeveloping.co.za/security/register_globals-evilness.html</link>
		<comments>http://www.phpdeveloping.co.za/security/register_globals-evilness.html#comments</comments>
		<pubDate>Fri, 24 Jul 2009 09:21:15 +0000</pubDate>
		<dc:creator>chris</dc:creator>
				<category><![CDATA[Security]]></category>
		<category><![CDATA[register_globals]]></category>

		<guid isPermaLink="false">http://www.phpdeveloping.co.za/?p=57</guid>
		<description><![CDATA[If you&#8217;re a PHP developer you know all about the register_globals directive and all the evilness that comes with it.  You can probably skip this post, because I&#8217;d just like to explain to the rest of the people what it is about.
In PHP you have superglobal arrays, which stores information that is being passed [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re a PHP developer you know all about the register_globals directive and all the evilness that comes with it.  You can probably skip this post, because I&#8217;d just like to explain to the rest of the people what it is about.</p>
<p>In PHP you have superglobal arrays, which stores information that is being passed to you either by the browser or the server.  These arrays are:</p>
<ul>
<li>$_COOKIE &#8211; stores information about cookies from the browser</li>
<li>$_GET &#8211; stores form information</li>
<li>$_FILES &#8211; stores information on files a user wants to upload to your server</li>
<li>$_POST &#8211; stores form information</li>
<li>$_SERVER &#8211; stores various information about the server</li>
<li>$_SESSION &#8211; stores session data</li>
</ul>
<p>What register_globals does when it is on is create a variable for each of the items in those arrays.  I.e. if you have $_GET['var'] it will create $var automatically.  </p>
<p>Look at the following examples.</p>
<p>HTML Form:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;form method=&quot;post&quot; action=&quot;script.php&quot;&gt;
  &lt;input type=&quot;text&quot; name=&quot;var&quot;&gt;
  &lt;input type=&quot;submit&quot;&gt;
&lt;/form&gt;</pre></div></div>

<p>If register_globals is on, you can access it like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;The value of the &quot;</span><span style="color: #000000; font-weight: bold;">var</span><span style="color: #0000ff;">&quot; control is <span style="color: #006699; font-weight: bold;">$var</span>&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>With register_globals off, you need to access it like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;The value of the &quot;</span><span style="color: #000000; font-weight: bold;">var</span><span style="color: #0000ff;">&quot; control is &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'var'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Here is example of how someone can exploit a script when register_globals is enabled, courtesy of <a target=_blank href="http://purl.co.za/CAI8g">Dreamhost</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$admin</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'foo'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$admin</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'pass'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'bar'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$admin</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span> AND <span style="color: #000088;">$admin</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'pass'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">/* Give administrator access */</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>When you first look at this, it all looks fine.  It&#8217;s check for the username and the password, so it must be fine?  Wrong.</p>
<p>Let&#8217;s say you access the page as <em>page.php?admin=asdf</em></p>
<ul>
<li>Because register_globals is on, $admin = &#8216;asdf&#8217;, because $_GET['admin'] = &#8216;adsf&#8217;</li>
<li>$admin['user'] = &#8216;foo&#8217;; sets the first char of &#8216;asdf&#8217; to &#8216;f&#8217;</li>
<li>$admin['pass'] = &#8216;bar&#8217;; sets the first char of &#8216;fsdf&#8217; to &#8216;b&#8217;</li>
<li>$admin['user'] == $_GET['username'] tests if &#8216;b&#8217; == $_GET['username']</li>
<li>$admin['pass'] == $_GET['password'] tests if &#8216;b&#8217; == $_GET['password'] </li>
</ul>
<p>To get administrator access to this page, you simply access it as <em>page.php?admin=asdf&#038;username=b&#038;password=b</em>.  See how that can affect you badly?  So repeat after me, register_globals are evil.</p>
<p>Another example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</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: #000088;">$logged_in</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">/* give access to things */</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In the above example, you can make use of the $_SESSION['logged_in'] variable that automatically makes $logged_in available.  Now if you call that page as <em>page.php?logged_in=1</em> $_GET['logged_in'] will also register $logged_in and over write it with value of 1.  <strong>Note</strong> that this is dependent on the variable_order directive which dictates which value will overwrite which one, but I hope you can see that register_globals can be very evil.</p>
<p>Another reason to start avoiding to use register_globals is that in PHP6 there is no support for register_globals.  You cannot turn it on, because it is not there.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpdeveloping.co.za/security/register_globals-evilness.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
