File Inclusion Vulnerability Fundamentals

Beginner

Introduction

Welcome to our beginner-friendly course on web application vulnerabilities, specifically focusing on file inclusion vulnerabilities, which are some of the most prevalent issues in web applications.

In this module, we'll start by dissecting the concept of 'file inclusion' in the context of web applications. We'll then build upon that foundation to elucidate what makes a 'file inclusion vulnerability.' Lastly, we will delve into the two primary categories of file inclusion vulnerabilities.

In the subsequent section of this module, we'll delve deeper into 'Local File Inclusion Vulnerabilities' and 'Remote File Inclusion Vulnerabilities,' providing you with a comprehensive understanding of these critical security issues.

Key Learning Outcomes
  • Comprehend the fundamental concept of file inclusion in web applications.
  • Grasp the underlying principles of file inclusion vulnerabilities.
  • Learn about the two main types of file inclusion vulnerabilities: Local and Remote.

Understanding File Inclusion

Let's delve into the concept of "file inclusion" and its potential vulnerabilities, especially tailored for beginners.

File inclusion is a common practice in programming, particularly in the context of code reusability. Developers often encapsulate frequently used functions or modules into separate files. When these functions or modules are required, they simply include these files in their code, eliminating the need to write the same code repeatedly.

In PHP, there are four primary functions for file inclusion:

  1. include()
  2. require()
  3. include_once()
  4. require_once()

Here's a brief overview of each:

  • include(): This function includes a file when it is invoked. If the file is not found, PHP issues a warning, but the script continues to run.
  • require(): This function includes a file before the script starts running. If the file is not found, PHP issues a fatal error and halts the script.
  • include_once() and require_once(): These functions work similarly to include() and require(), but they ensure the file is included only once, even if the function is called multiple times.

File inclusion methods can be broadly categorized into two types:

  1. Static inclusion
  2. Dynamic inclusion

The distinction between these two types will be clarified with an example in the following sections.

In our course, we will be exploring three .php files located in the /home/labex/project/ directory. These are:

  • lfi_static.php
  • lfi_dynamic.php
  • echo.php

Let's understand what each file does:

  • lfi_static.php: This file includes the echo.php file in a static manner. In other words, the path to the echo.php file is hard-coded and doesn't change. This method is secure and doesn't expose any vulnerabilities.

    <?php
    include("./echo.php");
    ?>
  • lfi_dynamic.php: This file includes other files based on a 'file' parameter in the URL. If this parameter isn't provided, it simply provides a hint on how to use it. However, this method can be risky if the parameter is controlled by an attacker.

    <?php
    if (isset($_GET['file'])) {
        include($_GET['file']);
    }
    else{
        echo "You can use the 'file' parameter to include files";
    }
    ?>
  • echo.php: This file simply outputs a success message when included.

    <?php
    echo 'Great, now you have successfully included the content of echo.php!'
    ?>

Use the following commands in terminal to prepare the image for lfi test:

cd ~/projects
docker build -t lfi-image .

When the image build done, you can use the image to start a container for test the lfi vuln:

docker run -d --name lfi -p 80:80 lfi-image

Now you can test these files in your browser. For example, to access the lfi_static.php file, you would enter the following URL:

http://127.0.0.1/LFI/lfi_static.php

You'll see that the lfi_static.php file successfully includes the content of echo.php. Remember, this static method of including files is secure.

Now, let's look at the lfi_dynamic.php file. Access it using this URL:

http://127.0.0.1/LFI/lfi_dynamic.php

You'll see a prompt telling you to use the 'file' parameter. Let's do that and include echo.php:

http://127.0.0.1/LFI/lfi_dynamic.php?file=./echo.php

You'll see that echo.php is successfully included. This demonstrates how file inclusion works. It's important to remember that while this is a PHP example, the principles apply to other programming languages, even though the specific functions used might be different.

Understanding File Inclusion Vulnerability

We recently learned how to include the echo.php file by using the file parameter in lfi_dynamic.php. But a question arises:

Is it possible to include other files using the file parameter?

The answer is a resounding yes!

If the web developer neglects to validate the file parameter, it can potentially be exploited to include sensitive files located on the server.

Here are some examples:

  • On a Linux system, you might be able to include the /etc/passwd file.
  • On a Windows system, you could potentially include the C:\Windows\System32\drivers\etc\hosts file.

Let's illustrate this with an example. Suppose we set the file parameter to /etc/passwd:

http://127.0.0.1/LFI/lfi_dynamic.php?file=/etc/passwd

Open the URL in the browser, you will see we successfully included and displayed the contents of the /etc/passwd file. This indicates the presence of a Local File Inclusion (LFI) vulnerability, which is a serious security issue.

Remember, understanding these vulnerabilities is the first step in preventing them. Always ensure to validate and sanitize all user inputs in your web applications.

Types of File Inclusion Vulnerabilities

File inclusion vulnerabilities, a critical issue in web security, are generally split into two main types:

  1. Local File Inclusion (LFI): This type of vulnerability occurs when local files on the targeted server are included.

  2. Remote File Inclusion (RFI): This type of vulnerability happens when files located on a different, remote server are included.

In the example we discussed earlier, the inclusion of the local /etc/passwd file is a textbook instance of a Local File Inclusion (LFI) vulnerability. Understanding the distinction between these two types of vulnerabilities is crucial to effectively securing your web applications.

Summary

In this lab, you have learned the method and process of initiating an attack through the file inclusion functionality. Let's review the important points covered in this section:

  • What is file inclusion?
  • What is a file inclusion vulnerability?
  • What are the two types of file inclusion vulnerabilities?

Other Tutorials you may like