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
- Attacker crafts filename:
shell.php%00.jpg(orshell.php\x00.jpg) - Validation layer checks: "Does this end with .jpg? Yes! ✓"
- File system receives:
shell.php(everything after%00is truncated) - 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.