Skip to content

LFI via zip:// and phar:// wrappers (archive inclusion)

LFI via the phar wrapper RCE and the zip:// wrapper lets an attacker who can upload an archive include a PHP file packed inside it: zip://upload.zip#shell.php or phar://upload.phar/shell.php reaches the inner file through include and executes it. The phar:// wrapper additionally triggers object deserialization from the archive's metadata, enabling RCE through gadget chains even when the file is only read.

Any OSRemote code executionPHP wrapper

How it works

PHP's zip:// and phar:// stream wrappers address files inside an archive. If the attacker can place an archive on the server (a file upload that keeps or lets them control the path) and the LFI reaches include, they can point include at a PHP entry inside the archive.

zip://path/to/upload.zip#inner.php (the fragment names the inner file) makes include execute inner.php from the uploaded zip. The archive can have any extension — only its contents matter — so an 'image upload' that is really a zip often suffices.

phar:// goes further: whenever a phar path is accessed by many filesystem functions, PHP unserializes the phar's serialized metadata. A crafted phar whose metadata is a malicious object can trigger a POP gadget chain on deserialization — so even file_exists/include on a phar:// path can lead to RCE without the inner file ever being 'run'.

When it works and when it fails

Works when

  • The attacker can upload or plant an archive and knows/controls its server path.
  • User input reaches include/require or a filesystem function and the zip://phar:// wrappers are enabled.
  • For phar deserialization: a usable gadget chain exists in the loaded code (framework/library classes).

Fails when

  • The phar:// / zip:// wrappers are disabled, or uploads are stored outside readable scope with randomised names.
  • Input is allowlisted so no wrapper string reaches the file call.
  • PHP 8+ phar read-only/Phar.readonly and hardened deserialization reduce the metadata-gadget surface.

Representative payloads

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

  • zip://../../uploads/avatar.zip#shell.php
    Any OSPHP wrapper

    Includes shell.php packed inside an uploaded zip (any extension works); the inner PHP executes. The # names the inner file.

  • phar://../../uploads/avatar.phar/shell.php
    Any OSPHP wrapper

    Includes an inner PHP file from an uploaded phar archive for direct execution.

  • phar://../../uploads/image.jpg/test.txt
    Any OSPHP wrapper

    Merely accessing a phar path (even one disguised as .jpg) deserializes its metadata; a crafted object can fire a gadget chain for RCE.

    Note: Requires a POP gadget chain in the application's loaded classes.

Detection

In logs

  • zip:// or phar:// schemes in request parameters, especially with a # fragment.
  • File parameters pointing into the uploads directory combined with a wrapper scheme.
  • Uploaded files whose real magic bytes are an archive/phar despite an image extension.

In code

  • User input reaching include/require or filesystem functions where zip://phar:// can be supplied.
  • File uploads stored with attacker-influenced names/paths and not validated by content.
  • Phar.readonly off, or deserialization-heavy libraries present (gadget surface).

Detection rule: Alert on phar:// and zip:// in parameters; flag uploads whose magic bytes indicate an archive/phar regardless of extension.

Remediation

  • Never pass user input to include/require or filesystem functions; use an allowlist of keys to fixed local files.
  • Validate uploads by content type and re-encode/rename them to random paths outside the web root; reject archives where not expected.
  • Disable the phar:// and zip:// wrappers for untrusted code paths, keep Phar.readonly on, and avoid passing user paths to functions that trigger phar deserialization.
  • Reject any input containing a URL/stream scheme, and run with least privilege to contain any execution.

Vulnerable (php)

include($_GET['tpl']); // zip://uploads/x.zip#shell.php

Fixed

$templates = ['default' => 'default.php', 'dark' => 'dark.php'];
$key = (string) ($_GET['tpl'] ?? 'default');
if (!isset($templates[$key])) { http_response_code(404); exit; }
include __DIR__ . '/templates/' . $templates[$key];

An allowlist keeps zip:// / phar:// strings out of include and out of filesystem calls.

FAQ

Why can phar:// cause RCE without running the inner file?
Accessing a phar path makes PHP unserialize the archive's metadata. If that metadata is a malicious serialized object and a suitable gadget chain exists in loaded code, the deserialization itself executes attacker-controlled logic.

References