Insecure Php coding
While testing a web application today, i noticed an unusual 302 HTTP response. Normally a 302 response just has a header and no html code, becuase its meant to be redirecting you to the page cited in the ‘Location’ field of the http header.� The 302 response had the html code which will be presented to the authenticated admin user, but, we didnt have the admin credentials. So, how are we seeing this code. After analyzing the 302 redircect response, we concluded that this was the result of insecure coding. The following example explains this issue in php.
insecure code:
<?
session_start();
include (”../config.php”);
echo $loggedin;
if ($loggedin != “1″){
header(”Location: http://www.google.com”); /* Redirect browser */
}
{
echo “Will this code Get executed?”;
}?>
In this example the code echo “Will this code Get executed?”; will indeed gets executed irrespective of the value of $loggedin. This is characteristics of php, and you wont see this behaviour in asp .net. To secure this code, follow this:
<?
session_start();
include (”../config.php”);
echo $loggedin;
if ($loggedin != “1″){
header(”Location: http://www.google.com”); /* Redirect browser */
}
else
{
echo “Will this code Get executed?”;
}?>
Alternatively, this code can be secured by:
<?
session_start();
include (”../config.php”);
echo $loggedin;
if ($loggedin != “1″){
header(”Location: http://www.google.com”); /* Redirect browser */
die;
}
{
echo “Will this code Get executed?”;
}?>
It is very easy for a pentester to miss out this issue, becuase in most of the cases you get redirected so fast then this page is not rendered by your browser. Unless you go through each 302 request manually, i dont think you will be able to spot it. In this case, even webinspect wasnt able to spot it. ![]()
Another post for this topic:
http://yaisb.blogspot.com/2006/08/authentication-bypass_07.html
Thanks for information.
Comment by Yns :: March 24, 2007 @ 2:26 pmHi,
Comment by Tejaswy :: March 27, 2007 @ 9:58 amJust checking out your blog.
I don’t really think that this is a very big issue, bad coding and bad php knowledge, nothing special about this, it is logical to use else, but anyway the thing to use die(); or exit(); after the redirect is indeed good way of coding, the php manual even has written about it in examples.
Comment by Kestas :: April 7, 2007 @ 3:27 pmI have to disagree with Kestas if you forget the “else” then it is your own fault yes, but it is very interesting that code doesn’t error and I believe that is a PHP bug.
It would be a major pain not to spot this in a script and keep wondering why your script is messing up only to find its a missing else.
Comment by Andrew Fenn :: July 23, 2007 @ 7:24 am