Course contentsShow
← Security
Lesson 967 of 3,05220. Web Security - File Upload and ProcessingFree lesson

Null Byte Injection in Filenames

Learn how null bytes truncate strings to bypass extension validation and execute arbitrary files.

Null Byte Injection in Filenames

What you'll learn: How attackers use null bytes to truncate filenames and bypass extension validation during file uploads.

The Core Idea

A null byte (\0 or %00 in URL encoding) is a special character that many programming languages interpret as a string terminator—it signals "end of string here, ignore everything after." Attackers exploit this behavior by injecting null bytes into filenames to trick server-side validation logic.

Here's how it works: An application might validate that uploaded files end with .jpg or .png. An attacker uploads a file named malicious.php%00.jpg. The validation code sees .jpg at the end and approves it. However, when the server processes the filename (especially in older languages like PHP or when interacting with file systems), the null byte terminates the string early—so the file is actually saved as malicious.php, which can then be executed as server-side code.

The Classic Attack Pattern

  1. Attacker crafts filename: shell.php%00.jpg (or shell.php\x00.jpg)
  2. Validation layer checks: "Does this end with .jpg? Yes! ✓"
  3. File system receives: shell.php (everything after %00 is truncated)
  4. Result: PHP web shell uploaded and executable

Why This Worked

This vulnerability was particularly dangerous in PHP versions before 5.3 and in C-based file handling systems that use null-terminated strings. The validation happened in one context (full string), but the storage happened in another (truncated string).

Modern Mitigations

Most modern frameworks and updated language runtimes now sanitize null bytes automatically or reject filenames containing them. However, legacy systems and custom implementations may still be vulnerable.

Key Takeaway: Null byte injection exploits the difference between how validation code sees a filename (full string) and how the underlying system processes it (truncated at \0), allowing attackers to bypass extension checks and upload dangerous file types.