I read many q/a on SO about the risk of session fixation/hijacking and many people suggest to change php.ini
directives like session.use_only_cookies
to ON
and others php.ini directives to make the server more secure...
I wanted to see with my eyes if I could replicate a simple attack scenario on my localhost server based on PHP5 + Apache.
On my localhost session.use_only_cookies
is OFF
so according to the q/a above my localhost is basically unprotected, which is what I need to do the test.
I 1st read this simple article on how a session fixation attack is perfomed:
In order to replicate the scenario described in the article, I created two very simple PHP scripts (code is below), but the attack does not work, this is what I did:
(Pretending to be Mallory) I say to Alice: “hello go visit http://localhost/login.php?PHPSESSID=mysessionid”
Then (pretending to be Alice) I went to http://localhost/login.php?PHPSESSID=mysessionid
As admin of my localhost server I saw the session being created on server disk (it's cerated as a file with the name
sess_ mysessionid
), so I thought: cool, it's working!!!Then (pretending to be Alice) I logged in entering “joe” as credential
Alice logs in and she is redirected to
insession_ok.php
, and at this point (according to the wikipedia article above) Mallory should be able to seeinsession_ok.php
too because he fixated the session tomysessionid
, but this is not true, because when Alice logs in a new session is created on serversess_vdshg238cnfb4vt7ahpnp1p522
, so I don't understand at this point how Mallory is supposed to fixate/hijack the session, as explained in the article???
login.php
<?php
session_start();
//if user credentials are ok, let's put him in session
if( @$_POST['usr'] === 'joe' )
$_SESSION['in_session'] = TRUE;
//if user is already logged in, let's redirect him to the account page "insession_ok.php"
if( isset($_SESSION['in_session']) )
{
$webpage = 'http://' . $_SERVER['HTTP_HOST'] . '/insession_ok.php';
header("Location: " . $webpage, TRUE, 302);
}
?>
<form method="POST" action="login.php">
<input name="usr" type="text">
<input type="submit" value="Submit">
</form>
<script type="text/javascript">
alert(document.cookie); //to view cookies
</script>
insession_ok.php
<?php
session_start();
if(@$_SESSION['in_session'] === TRUE)
echo "in session ok";
else //user is not in session cause he did not login, let's redirect him to login page
{
$webpage = 'http://' . $_SERVER['HTTP_HOST'] . '/login.php';
header("Location: " . $webpage, TRUE, 302);
}
?>
Any clue/idea is always appreciated!