Skip to content

LFI path truncation bypass (trailing dots and slashes)

LFI path truncation bypass drops an appended extension without a null byte by exceeding the filesystem path-length limit (or exploiting trailing-dot/slash trimming) so the suffix is cut off. The target path is padded with harmless sequences like /./ or trailing dots until the appended '.php' falls past the truncation point. It targets legacy PHP (before 5.3) where NUL byte tricks were patched.

Linux / UnixArbitrary file readSource code disclosureInformation disclosureTruncation

How it works

When code appends a fixed extension (… . '.php') and the null-byte trick is unavailable, path length can be abused instead. Older PHP silently truncated paths longer than the OS limit (around 4096 bytes on Linux), discarding the appended suffix.

The path is padded so the real filename sits before the limit and the appended extension sits beyond it. Padding uses sequences that resolve to the same file: a long run of /./ (current directory), a trailing /, or a run of trailing dots after the filename — PHP/the OS collapse or ignore these while they consume length.

Leading ./ padding (./././…/etc/passwd) and trailing dots (/etc/passwd followed by many .) are the common forms. A trailing slash alone (/etc/passwd/.) also makes an appended .php invalid because /etc/passwd/. plus .php is not a file.

When it works and when it fails

Works when

  • Legacy PHP (roughly < 5.3) that truncates over-length paths instead of erroring.
  • A fixed extension is appended to user input before the file call.
  • No canonicalization/allowlist runs on the value before use.

Fails when

  • Modern PHP / runtimes that do not silently truncate long paths.
  • Canonicalization collapses the padding and the result fails a prefix/allowlist check.
  • The target is selected via an allowlist key rather than a built path.

Representative payloads

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

  • ../../../../etc/passwd/././././././././././././././././././././././.
    Linux / UnixTruncation

    Trailing /./ padding consumes path length so an appended extension falls past the truncation limit while the path still resolves to /etc/passwd.

    Note: Real payloads repeat the padding to ~4096 bytes; shown abbreviated.

  • ../../../../etc/passwd........................................
    Linux / UnixTruncation

    A long run of trailing dots after the filename; legacy PHP trims them, and the length pushes the appended suffix out.

  • ../../../../etc/passwd/.
    Linux / UnixTruncation

    The /. suffix makes an appended .php invalid (/etc/passwd/.php is not a file) while /etc/passwd/. still resolves to the file.

  • ./././././././././././././././../../../../etc/passwd
    Linux / UnixTruncation

    Leading ./ padding does the same length work before the traversal, used when trailing characters are stripped.

Detection

In logs

  • Abnormally long request parameters (hundreds to thousands of bytes) for a filename field.
  • Long runs of ./ , trailing dots, or /. after a recognisable filename.
  • Repeated requests to the same parameter with incrementing padding length.

In code

  • Appending a fixed extension to user input before a file call on an old PHP version.
  • No maximum-length check on a filename parameter.
  • No canonicalization (realpath) that would collapse the padding before validation.

Detection rule: Alert on filename parameters over a sane length (e.g. > 255 bytes) and on runs of /./ or trailing dots.

Remediation

  • Enforce a strict maximum length on filename parameters (a real filename is short).
  • Upgrade off end-of-life PHP; modern versions do not silently truncate over-length paths.
  • Canonicalize with realpath and verify the result is inside the base directory — padding collapses and the check fails.
  • Select files through an allowlist of keys so a padded path is never used to open a file.

Vulnerable (php)

include('/var/www/pages/' . $_GET['page'] . '.php');

Fixed

$page = (string) ($_GET['page'] ?? '');
if (strlen($page) > 64) { http_response_code(400); exit; }
$base = '/var/www/pages/';
$path = realpath($base . $page . '.php');
if ($path === false || !str_starts_with($path, $base)) {
    http_response_code(400); exit('Invalid page');
}
include $path;

Length limit plus realpath prefix check defeats padding-based truncation.

FAQ

How is truncation different from the null byte trick?
Both drop an appended extension, but the null byte terminates the C string at %00, while truncation relies on the OS/PHP discarding path length beyond a limit. Truncation was the fallback after %00 was patched, and it too fails on modern runtimes.

References