Skip to main content
Create your own
Lesson illustration

Bypassing LFI Filters: Null-Byte and Path Truncation

Hello and welcome back!

In the previous lesson, we established a solid foundation for Local File Inclusion (LFI), learning how to identify the vulnerability and bypass several common filters like non-recursive stripping and basic encoding. We briefly touched upon null-byte injection as a way to handle filters that append file extensions.

Today, we will dive deeper into two powerful techniques specifically designed to defeat extension-appending defenses: null-byte injection and path truncation. Mastering these will significantly enhance your ability to exploit LFI vulnerabilities, especially on older or misconfigured systems which you'll frequently encounter in real-world engagements. This lesson will help you understand the low-level mechanics that make these bypasses possible, connecting directly to your computer science background.

1. Deeper Dive: Null-Byte Injection

As we saw previously, a common defense against LFI is to append a required extension to the user-provided filename. For instance, code like include('pages/' . $_GET['page'] . '.php'); ensures that only .php files are included.

The null byte (%00 when URL-encoded) is a control character that signifies the end of a string in C-based languages, which includes many of the underlying functions used by web languages like PHP. By injecting a null byte, you can trick the application into ignoring anything that comes after it, including the appended file extension.

Lab: File Path Traversal with Null Byte Bypass
This title slide from a PortSwigger lab, which we will explore, clearly states the objective: bypassing file extension validation using a null byte to read a sensitive file.

Let's break down how and why this works.

Directory Traversal | Complete Guide

To start, let's review the theory. The video "Directory Traversal | Complete Guide" by Rana Khalil provides a concise explanation of several bypass techniques, including null-byte injection.

Watch the segment from 18:50 to 19:48. Pay close attention to the explanation of why an application might require a specific extension and how the null byte is used to bypass this requirement.

Now that you've seen the theory, let's watch it in action. A practical demonstration is the best way to solidify your understanding.

Directory Traversal - Lab #6 Validation of file extension with null byte bypass | Short Version

This lab walkthrough, also by Rana Khalil, demonstrates the entire process of exploiting an LFI vulnerability using a null-byte bypass. It's a perfect practical application of the theory.

Watch the full video. It's short and to the point. Observe how: The initial, basic LFI payload fails. The null byte payload (%00) is constructed. The server's response changes, confirming the bypass was successful and the contents of /etc/passwd are returned.

Key Limitation: It's critical to know that null-byte injection is a classic technique that was patched in PHP version 5.3.4. It no longer works on modern, patched PHP applications. However, you will still find unpatched, legacy systems in corporate environments and bug bounty programs, making this a vital skill to have in your arsenal. The "HackTricks" page on File Inclusion also lists this as a primary bypass technique.

2. The Path Truncation Attack

What do you do when the application is running a modern version of PHP where null-byte injection doesn't work, but it still appends an extension? This is where a more clever and complex technique comes into play: path truncation.

The core idea is to create a filename so long that the server truncates it, cutting off the appended extension. This attack relies on two specific behaviors of the server environment:

  1. Filename Length Limit: On many systems (especially Linux), file paths are silently truncated if they exceed a certain length (often 4096 bytes). The extra characters, including the appended extension, are simply discarded.
  2. Path Normalization: File systems often treat paths like /etc/passwd/././. as equivalent to /etc/passwd. Trailing slashes or dot-slash sequences are effectively ignored.

By combining these two behaviors, you can craft a payload that bypasses the extension filter.

How can I use this path bypass/exploit Local File Inclusion?

The following resource provides one of the best explanations of the path truncation attack. It's written for someone with a technical background and breaks down the 'why' behind the attack perfectly.

Read the sections under the headings 'Background' and 'The attack'. Focus on understanding the two 'Facts' about PHP file handling and how they are combined to construct the sophisticated attack that bypasses the appended '.php' extension.

To summarize the attack from the reading:

  • Vulnerable Code: include("includes/".$_GET['param'].".php");
  • Goal: Read /etc/passwd.
  • Problem: A simple payload like ../../etc/passwd results in the server looking for the non-existent file /etc/passwd.php.
  • Path Truncation Payload: ../../etc/passwd/./././././././<...repeat hundreds of times...>
  • How it works:
    1. The application constructs the full path: includes/../../etc/passwd/././././.../.php
    2. This string exceeds the 4096-byte limit. The kernel truncates it, dropping the .php at the end.
    3. The resulting path is something like: includes/../../etc/passwd/././././.../
    4. The file system normalizes this path, ignoring the trailing ./ sequences, and ultimately accesses includes/../../etc/passwd, which resolves to /etc/passwd.

This is an elegant bypass that leverages a deep understanding of how different system layers (web application, language runtime, operating system) interact.

File Inclusion/Path traversal

The HackTricks page on File Inclusion also describes this technique. It's a good, concise reference to complement the detailed explanation you just read.

Quickly review the 'Path Truncation Technique' section. Notice the payload structure, which often starts with a fake directory (a/) and uses a large number of traversals or dots.

Test your understanding!

You are pentesting a web application. You've discovered an LFI vulnerability in the template parameter: https://example.com/index.php?template=main. The application's code is functionally equivalent to include('templates/' . $_GET['template'] . '.html');.

You've determined through other means that the server is running a modern, fully patched version of PHP (version 7.4). Your goal is to read /etc/config/app.conf.

Which of the two techniques discussed in this lesson should you use, and why? Craft a conceptual payload (you don't need to write out 4096 characters).

Show answer

You should use the Path Truncation technique.

Reasoning: Since the server is running a modern version of PHP (post-5.3.4), null-byte injection (%00) will not work; it will be treated as a literal character or ignored, but it will not terminate the string. Path truncation, which relies on OS-level filename length limits, is the appropriate bypass here.

Conceptual Payload:
?template=../../etc/config/app.conf/./././././././[...repeat './' until the total path length is over 4096 bytes...]

The server will try to include templates/../../etc/config/app.conf/././.../.html, which will be truncated before the .html is processed, allowing you to read the configuration file.

Conclusion

In this lesson, you've added two crucial LFI filter bypass techniques to your toolkit. While they both target the same defense mechanism—appended file extensions—they operate on completely different principles.

Key Takeaways:

  • Null-Byte Injection (%00) is a string termination attack that tricks the application into ignoring appended data. It is powerful but generally only works on legacy systems (e.g., PHP < 5.3.4).
  • Path Truncation is a more complex attack that leverages OS file path length limits to discard an appended extension. It works by creating an excessively long path string filled with components like ./ that are later normalized by the file system.
  • Understanding the underlying system mechanics (string handling, file system behavior) is essential for discovering and executing these advanced bypasses.

Next Lesson Preview:
So far, we have focused on including files that are already on the server (Local File Inclusion). What happens if a vulnerable application allows you to include a file from anywhere on the internet? In our next lesson, we will explore Remote File Inclusion (RFI). This is often a more critical vulnerability, as it can allow you to execute your own malicious code on the server, leading directly to Remote Code Execution (RCE).

Can't find a good explanation? Sign up and we'll make it for you

Sign up