Objective
Learn how to exploit Path Traversal vulnerabilities to access files outside the intended directory by manipulating file paths. Understand how to prevent these attacks by validating user input, using absolute file paths, and restricting file access to specific directories.
Scenario
You are testing a web application that allows users to download files by providing the file name as a URL parameter. Due to insufficient input validation, an attacker can manipulate the file path to access sensitive system files.
Lab Setup
Prerequisites:
- Basic knowledge of PHP and web servers.
- XAMPP/LAMP/WAMP stack installed (or any web server with PHP support).
- A code editor (e.g., VSCode, Sublime Text).
Step 1: Create the Vulnerable Web Application
- PHP Script for File Download (Vulnerable)
- Create a file
download.php
:<?php if (isset($_GET['file'])) { $file = $_GET['file']; $filepath = "uploads/" . $file; if (file_exists($filepath)) { header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename=' . basename($filepath)); readfile($filepath); exit; } else { echo "File not found."; } } else { echo "No file specified."; } ?> <h2>Download a File</h2> <form method="GET" action=""> File Name: <input type="text" name="file" required> <button type="submit">Download</button> </form>
- Create a file
- Create the Uploads Directory
- Create a directory named
uploads
in the web server’s root (htdocs
for XAMPP). - Add a sample file
example.txt
in theuploads
folder.
- Create a directory named
- Running the Application
- Start the Apache server.
- Place
download.php
in the web server’s root directory (htdocs
for XAMPP). - Open
http://localhost/download.php
in your browser.
Exploitation Steps
Step 1: Downloading Files Normally
- Enter
example.txt
in the form and click Download. - Expected Result:
- The file
example.txt
downloads successfully.
- The file
Step 2: Exploiting Path Traversal
- Enter the following payload to access sensitive files:
../../../../etc/passwd
- Expected Result:
- The server responds with the contents of
/etc/passwd
.
- The server responds with the contents of
- On Windows systems, try accessing system files:
..\..\..\..\Windows\System32\drivers\etc\hosts
Expected Result:
- The server displays the contents of the
hosts
file.
Solution and Prevention
Problem Analysis
- The application directly appends user input to the file path without validation, allowing directory traversal.
Fixing the Vulnerability
- Validate and Sanitize User Input
- Restrict input to filenames only (no special characters):
$file = basename($_GET['file']); $filepath = "uploads/" . $file;
- Restrict input to filenames only (no special characters):
- Restrict Access to Specific Directories
- Use a predefined allow-list of files or directories:
$allowed_files = ['example.txt', 'document.pdf']; if (in_array($file, $allowed_files)) { $filepath = "uploads/" . $file; } else { die("Invalid file request."); }
- Use a predefined allow-list of files or directories:
- Use Real Path Validation
- Ensure the requested file is within the allowed directory:
$filepath = realpath("uploads/" . $file); if (strpos($filepath, realpath("uploads")) === 0 && file_exists($filepath)) { header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename=' . basename($filepath)); readfile($filepath); exit; } else { echo "Invalid file request."; }
- Ensure the requested file is within the allowed directory:
- Use a Content Delivery System for Downloads
- Store sensitive files in a secure, external storage system, separating application logic from file storage.
Testing After Fix
- Attempt to download
../../../../etc/passwd
. - Expected Result:
- The application blocks the request and displays an error.
- Attempt normal downloads with valid filenames.
- Expected Result:
- The valid file downloads successfully.
Conclusion
In this lab, you exploited a Path Traversal vulnerability to access sensitive files outside the intended directory. You also learned how to prevent this vulnerability by validating user input, using absolute paths, and restricting file access to specific directories.
0 Comments