PHP: Preventing register_global problemsNowadays it is widely accepted that using the register_globals feature of PHP leads to security problems, but it wasn't always like this (if you don't know what this feature is then you are probably not using it; but, hey, read on the discussion is informative). In fact, the register_globals feature was turned on by default until version 4.2.0. As a result of that, many applications that exist depend on this feature (for more details have a look at http://www.php.net/register_globals). If you can choose, it is better to refactor and rewrite the code to not use this feature. But if you cannot afford to do that for some reason or another, you can use mod_security to protect an application from a known vulnerability. Problematic bits of code usually look like this: <?php And the attacker would take advantage of this simply by adding an additional parameter to the URL. For example, http://www.modsecurity.org/examples/test.php?authorised=1. Rejecting all requests that explicitly supply the parameter in question will be sufficient to protect the application from all attackers: <Location "/vulnerable-application/"> The filter above rejects all requests where the variable "authorised" is not empty. You can also see that we've added the <Location>...</Location> directives to limit filter only to those parts of the web server that really need it. Update: As Mike Schroll (LogicX) points out, this article fails to address all register_globals problems properly. The directive given above closes the GET and POST holes, but it fails to address automatically created variables from cookies (C in EGPCS). To address this issue another directive isneeded: SecFilterSelective COOKIE_authorised "!^$". With this in place, adversaries can still create variables using request headers (it never ends, does it?). However, PHP will prefix such variables with "HTTP_" and convert them to uppercase. So the chances of this being exploited are small unless the critical variable is all uppercase and begins with "HTTP_" :) [22 September 2004] |


