Objective
Learn how to exploit Insufficient Session Expiration vulnerabilities to hijack user sessions after logout or inactivity. Understand how to implement secure session management practices to mitigate this risk.
Scenario
You are evaluating a web application that allows users to log out but does not properly invalidate session tokens. This flaw enables an attacker to reuse an old session token and gain unauthorized access to the victim’s account.
Lab Setup
Prerequisites:
- Basic knowledge of PHP and session management.
- XAMPP/LAMP/WAMP stack installed (or any web server with PHP support).
- A code editor (e.g., VSCode, Sublime Text).
- Browser developer tools or Burp Suite for capturing session tokens.
Step 1: Create the Vulnerable Web Application
- PHP Script for Login and Dashboard
- Create a file
login.php
:<?php session_start(); $users = ["alice" => "alice123", "bob" => "bob123"]; if (isset($_POST['login'])) { $username = $_POST['username']; $password = $_POST['password']; if (isset($users[$username]) && $users[$username] === $password) { $_SESSION['user'] = $username; header("Location: dashboard.php"); } else { echo "<h2>Invalid Credentials!</h2>"; } } ?> <h2>Login</h2> <form method="POST" action=""> Username: <input type="text" name="username" required><br> Password: <input type="password" name="password" required><br> <button type="submit" name="login">Login</button> </form>
- Create a file
- PHP Script for Dashboard Access
- Create a file
dashboard.php
:<?php session_start(); if (isset($_SESSION['user'])) { echo "<h2>Welcome, " . $_SESSION['user'] . "!</h2>"; echo "<a href='logout.php'>Logout</a>"; } else { echo "<h2>Access Denied. Please log in.</h2>"; echo "<a href='login.php'>Login</a>"; } ?>
- Create a file
- PHP Script for Logout (Vulnerable)
- Create a file
logout.php
:<?php session_start(); session_destroy(); header("Location: login.php"); ?>
- Create a file
- Running the Application
- Start the Apache server.
- Place the files in the web server’s root directory (
htdocs
for XAMPP). - Open
http://localhost/login.php
in your browser.
Exploitation Steps
Step 1: Capturing the Session Token
- Log in as alice using:
- Username:
alice
- Password:
alice123
- Username:
- Open browser developer tools → Application → Cookies.
- Copy the session ID (
PHPSESSID
).
Step 2: Hijacking the Session Post-Logout
- Log out by clicking Logout.
- Open another browser window or use an incognito tab.
- Manually set the stolen session ID (
PHPSESSID
) in the new session. - Access
http://localhost/dashboard.php
.
Expected Result:
- The attacker regains access to Alice’s dashboard despite the logout.
Solution and Prevention
Problem Analysis
- The session is not properly invalidated upon logout, allowing attackers to reuse session tokens.
Fixing the Vulnerability
- Regenerate Session IDs on Login
- Prevent session fixation:
session_regenerate_id(true); $_SESSION['user'] = $username;
- Prevent session fixation:
- Properly Invalidate Session on Logout
- Modify
logout.php
:<?php session_start(); $_SESSION = []; if (ini_get("session.use_cookies")) { $params = session_get_cookie_params(); setcookie(session_name(), '', time() - 42000, $params["path"], $params["domain"], $params["secure"], $params["httponly"] ); } session_destroy(); header("Location: login.php"); ?>
- Modify
- Implement Session Timeout
- Auto-expire inactive sessions:
if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 900)) { session_unset(); session_destroy(); } $_SESSION['LAST_ACTIVITY'] = time();
- Auto-expire inactive sessions:
- Use Secure and HttpOnly Cookies
- Set cookie attributes in
php.ini
:session.cookie_httponly = 1 session.cookie_secure = 1 session.use_strict_mode = 1
- Set cookie attributes in
Testing After Fix
- Log in as Alice and capture the session ID.
- Log out.
- Attempt to reuse the session ID in a new browser window.
Expected Result:
- The server rejects the reused session, denying access to the dashboard.
Conclusion
In this lab, you exploited Insufficient Session Expiration to hijack a session after logout. You also learned how to prevent this vulnerability by properly invalidating sessions, regenerating session IDs, enforcing session timeouts, and securing session cookies.
0 Comments