Null byte injection in LFI (%00 path truncation)
Null byte injection LFI uses a %00 (NUL) byte to truncate the string at the C level, cutting off an extension the application appends (for example '.php') so an arbitrary file is opened instead. It is a legacy technique: it works on PHP before 5.3.4 and other code that passes strings to C file APIs without rejecting NUL.
How it works
To constrain which files can be opened, code often appends a fixed suffix, e.g. include($_GET['page'] . '.php'). The attacker wants to read /etc/passwd, but the appended .php would turn it into /etc/passwd.php, which does not exist.
A NUL byte (%00 in the URL) is the C string terminator. When the language passes the string to a C-level file function without length-checking, everything after the NUL is ignored. So page=/etc/passwd%00 becomes /etc/passwd at the syscall, and the appended .php is discarded.
The bug was fixed in PHP 5.3.4 (NUL in paths now raises an error) and modern languages reject embedded NUL in filenames, so this is primarily relevant to legacy stacks and native code that still trusts raw strings.
When it works and when it fails
Works when
- A fixed extension or suffix is concatenated after user input.
- The runtime passes the path to a C file API without rejecting embedded NUL (PHP < 5.3.4, some old CGI/Perl).
- The transport preserves the NUL byte to the vulnerable call (raw %00, sometimes double-encoded %2500).
Fails when
- PHP >= 5.3.4 or any modern runtime that rejects NUL bytes in path arguments.
- Input is validated against an allowlist, or path separators / NUL are stripped before use.
- The framework normalises or percent-rejects control characters before the file call.
Representative payloads
Adapt these to your authorized target; they are shown in canonical form. Payloads are never translated.
../../../../etc/passwd%00Linux / UnixNull byteTruncates an appended .php (or similar) suffix so /etc/passwd is opened instead of /etc/passwd.php.
../../../../etc/passwd%00.phpLinux / UnixNull byteSame truncation shown explicitly with the suffix the app would append; the %00 ends the string before .php.
../../../../etc/passwd%2500Any OSNull byteDouble-encoded NUL for stacks that URL-decode twice, so %2500 -> %00 -> NUL reaches the file API.
..\..\..\..\windows\win.ini%00WindowsNull byteWindows variant truncating an appended extension to read win.ini on legacy stacks.
Detection
In logs
- Literal or encoded NUL bytes in parameters: %00 and the double-encoded %2500.
- Requests combining traversal with a trailing %00 before where an extension would be.
- Application errors referencing an unexpected filename lacking its usual extension.
In code
- Concatenation of a fixed suffix after user input before a file call (… . '.php', … + '.html').
- File operations on legacy PHP (< 5.3.4), old Perl/CGI, or native wrappers that do not reject embedded NUL.
- No rejection of control characters / NUL in the input validation path.
Detection rule: Any %00 (or %2500) in a request parameter is anomalous for web input and should be alerted on and rejected.
Remediation
- Reject any input containing a NUL byte (\0) outright — it has no legitimate place in a filename parameter.
- Upgrade off end-of-life runtimes; PHP >= 5.3.4 already rejects NUL in path functions.
- Do not rely on an appended extension as a security control — validate against an allowlist of keys or filenames instead.
- Canonicalize and run a base-directory prefix check after decoding, so a truncated path still fails validation.
Vulnerable (php)
// pre-5.3.4 behaviour: %00 truncates '.php'
include($_GET['page'] . '.php');Fixed
$pages = ['home' => 'home.php', 'about' => 'about.php'];
$key = (string) ($_GET['page'] ?? '');
if (str_contains($key, "\0") || !isset($pages[$key])) {
http_response_code(400);
exit('Invalid page');
}
include __DIR__ . '/pages/' . $pages[$key];Reject NUL bytes and resolve against an allowlist instead of trusting an appended extension.
FAQ
- Does null byte injection still work today?
- Rarely. PHP fixed it in 5.3.4 and modern runtimes reject NUL in path arguments. It remains relevant for legacy PHP, old Perl/CGI, and native code that passes raw strings to C file APIs.