Skip to content

LFI via PHP session files and uploaded temp files

LFI via PHP session file inclusion turns a file-inclusion bug into RCE by writing attacker PHP into a file the server creates — a session file under /tmp/sess_<id> (via a reflected session value) or an uploaded temporary file — then including that path so the PHP executes. Like log poisoning, it needs no wrappers, only a readable server-created file whose contents the attacker influences.

Linux / UnixRemote code executionNo filter

How it works

PHP's default session handler stores each session on disk at /tmp/sess_<SESSIONID> (the id comes from the PHPSESSID cookie the attacker already holds). If any user-controlled value is stored in the session, its serialized form lands in that file.

The attacker makes the app store <?php system($_GET['c']); ?> in a session variable (for example a username or preference that is reflected into $_SESSION), then uses the LFI to include /tmp/sess_<their id>. include executes the embedded PHP.

A related variant abuses file uploads: PHP writes each upload to a temporary path (e.g. /tmp/phpXXXXXX) for the duration of the request. With a race or an info leak of the temp name (sometimes via a PHP_SESSION_UPLOAD_PROGRESS trick that creates a predictable session file), the attacker includes the uploaded content as PHP.

When it works and when it fails

Works when

  • User input reaches include/require and the session/upload temp directory (/tmp) is readable.
  • A user-controlled value is persisted into the session (so it reaches the sess_ file).
  • The PHPSESSID is known to the attacker (their own cookie), making the session path predictable.

Fails when

  • Sessions are stored outside the filesystem (Redis/DB) or outside readable scope.
  • Input is allowlisted so the session/temp path can never be included.
  • open_basedir / separate tmp confinement blocks reading /tmp session files.

Representative payloads

Adapt these to your authorized target; they are shown in canonical form. Payloads are never translated.

  • ../../../../tmp/sess_<PHPSESSID>
    Linux / UnixNo filter

    Includes the attacker's own session file after a PHP payload has been stored in it; replace <PHPSESSID> with the value of your session cookie.

  • <?php system($_GET['c']); ?>
    Linux / UnixNo filter

    The PHP to get persisted into a session variable (e.g. a reflected username/theme) so it ends up inside /tmp/sess_<id>.

    Note: Injection step: set this as the value the app stores in $_SESSION.

  • ../../../../tmp/sess_<PHPSESSID>
    Linux / UnixNo filter

    With session.upload_progress enabled, uploading while setting the progress name writes attacker content to a predictable sess_ file to include.

  • ../../../../tmp/phpXXXXXX
    Linux / UnixNo filter

    Includes a PHP upload temp file (name must be leaked or won by race); the uploaded body is executed as PHP.

Detection

In logs

  • Inclusion of /tmp/sess_* or /tmp/php* paths in file parameters.
  • Session values or upload fields containing <?php or shell commands.
  • A burst of requests setting a session value then immediately including a sess_ path.

In code

  • User input reaching include/require with default file-based sessions on a readable /tmp.
  • User-controlled data persisted verbatim into $_SESSION.
  • session.upload_progress enabled together with an includable session directory.

Detection rule: Alert on include targets matching /tmp/(sess_|php) and on session/upload content containing PHP tags.

Remediation

  • Never pass user input to include/require; resolve a key through an allowlist to fixed local files.
  • Store sessions outside the filesystem (Redis, database) or in a directory the web process cannot include, and keep /tmp out of read scope via open_basedir.
  • Do not persist raw user input into the session; validate and encode what you store.
  • Disable session.upload_progress if unused, and run with least privilege + disable_functions to contain any execution.

Vulnerable (php)

include($_GET['page']); // ../../tmp/sess_<id>

Fixed

$pages = ['home' => 'home.php', 'account' => 'account.php'];
$key = (string) ($_GET['page'] ?? 'home');
if (!isset($pages[$key])) { http_response_code(404); exit; }
include __DIR__ . '/pages/' . $pages[$key];

Allowlisted includes mean a poisoned session or upload file is never executed.

FAQ

How does the attacker know which session file to include?
The session file name is sess_ followed by the PHPSESSID value, which the attacker already has in their own session cookie — so /tmp/sess_<theirid> is fully predictable.

References