Your Server Files Are Not Safe — Directory Traversal Explained (2026)
Directory Traversal Attacks — How Attackers Access Your Server Files (2026 Guide)
Most applications assume users will only request files they're allowed to access. But with a simple trick like ../, attackers can escape that boundary and read sensitive system files that developers never intended to expose.
This is how directory traversal turns a small mistake into a serious security breach. What looks like a minor file-handling issue can expose your entire server — configuration files, private keys, source code, database credentials — all readable by anyone who knows the trick.
I'm writing this because directory traversal is one of the most commonly exploited vulnerabilities in bug bounty programs, and it's completely preventable if you understand what's happening.
- What is directory traversal (path traversal)?
- How the attack actually works
- Real attack scenarios and examples
- Step-by-step attack flow
- Impact on your system
- Common mistakes that enable it
- Complete prevention techniques
- Tools and safe practice
- FAQ for developers
What is Directory Traversal?
Directory Traversal (also called Path Traversal) occurs when an application allows user input to control file paths without proper validation. Attackers manipulate this input using sequences like ../ to move up the directory tree and access files outside the intended directory.
The Core Problem
When you build a file path from user input without validation, you're essentially giving attackers a key to your filesystem. The ../ sequence means "go up one directory." Repeat it enough times, and you can reach sensitive files anywhere on the system.
If your app reads /uploads/report.pdf, an attacker sends ../../../../etc/passwd which resolves to /etc/passwd — a system file containing user information.
How Directory Traversal Actually Works
- Application reads a file path directly from user input (URL parameter, form field, API parameter).
- No validation or sanitization is performed on the path.
- Attacker injects
../sequences to escape the intended directory. - Server opens and returns the requested file — which may be system files, config files, or environment variables.
- Attacker now has access to sensitive information they should never see.
Real Attack Scenario
Let me walk you through a real example:
Vulnerable Application Flow
A file-download endpoint exists: /download?file=manual.pdf. The application naively constructs: base_path + user_input
Attacker requests: /download?file=../../../../config/db.json
The server constructs: /var/www/uploads/../../../../config/db.json
This resolves to: /config/db.json — exposing database credentials!
Attacker now has API keys, database passwords, and internal secrets. This single file enables account takeover, data theft, or complete system compromise.
Step-by-Step Attack Flow
✅ How Attackers Exploit This
- Find endpoint: Identify any feature that reads files (download, image rendering, logs, backups)
- Test normal input: Request
file=test.txtto confirm the feature works - Try traversal payloads: Request
file=../../etc/passwd - Check response: If you see file contents, it's vulnerable
- Try encoding bypasses: If blocked, try
%2e%2e%2f(URL encoded) or other variations - Escalate: Access database files, config files, source code
Impact of Directory Traversal
- System file access: Read
/etc/passwd,/etc/shadow, or Windows system files - Credential exposure: Leak database passwords, API keys, AWS access tokens stored in files
- Source code disclosure: Access application code to find more vulnerabilities
- Configuration leaks: Expose environment variables, database connections, third-party service credentials
- Remote code execution: When combined with file upload or log injection, can lead to RCE
Common Mistakes That Enable Directory Traversal
🚩 Developer Errors
- Trusting user input: Building file paths directly from parameters without any filtering
- Incomplete sanitization: Only removing
../but not..\\or URL-encoded variants - Wrong validation order: Checking permissions AFTER constructing the path
- Exposing filesystem layout: Providing detailed error messages that reveal directory structure
- Using dangerous APIs: Raw file operations without framework protections
How to Prevent Directory Traversal
✅ Prevention Techniques
- Never trust user input in file paths: This is rule #1. Always assume user input is malicious.
- Use allowlists: Maintain a list of allowed filenames or IDs, not full paths
- Map IDs to paths server-side: Use file IDs (1, 2, 3) instead of filenames. Store the actual path in your database.
- Canonicalize paths: Resolve all
../sequences and validate the final path is within your base directory - Use framework APIs: Modern frameworks provide safe file-handling methods — use them
- Restrict permissions: Run applications with minimal file-system access
Tools to Test Directory Traversal
- Burp Suite: Modify requests and test payloads systematically
- ffuf: Fuzz file paths and directory structures automatically
- OWASP ZAP: Automated scanning for traversal vulnerabilities
- Postman: Test API endpoints manually
- curl: Test from command line
Practice Safely: Lab Environments
Never test on real systems without permission. Practice in safe, legal environments:
- DVWA (Damn Vulnerable Web App): Includes a directory traversal lab
- PortSwigger Web Security Academy: Free path traversal labs with step-by-step guidance
- HackTheBox: Real-world scenarios in sandboxed environments
Real-World Impact
Directory traversal has been responsible for some of the largest data breaches. In many cases, it's the first vulnerability that leads to deeper system access. When combined with file upload functionality, log injection, or other features, it becomes an entry point to RCE.
- Never build file paths from user input
- Use allowlists and IDs instead of filenames
- Canonicalize and validate all paths
- Directory traversal can lead to complete system compromise
- This vulnerability is completely preventable with proper validation
Conclusion
Directory traversal is easy to exploit but equally easy to fix if proper validation and safe path-handling practices are implemented. Developers must never trust user-controlled input when constructing file paths. Always validate, always restrict to a base directory, and always use framework-provided safe APIs instead of raw system calls.
Directory Traversal — FAQs
../ to see if the application allows directory escape. If basic attempts fail, they use encoded payloads (%2e%2e%2f), double encoding, or automated tools to systematically test variations. The goal is to identify whether the application improperly trusts user-controlled input in file path construction.../, but attackers bypass using encoding techniques (URL encoding, Unicode), alternate path separators (..\\), or mixed layers. Additionally, many applications normalize input incorrectly — a malicious path may pass validation but later resolve into a dangerous path during execution. Effective prevention requires canonicalization first, then strict validation — not just pattern blocking./etc/passwd is often used as proof-of-concept. Beyond that, attackers target environment files (.env), application configs, database credentials, API keys, logs, and backups. These files often contain secrets that enable account takeover, privilege escalation, or broader system compromise.../), then validate against a strict allowlist to ensure they remain within a designated base directory. Combined with proper file permissions and least-privilege access, this significantly reduces exploitation risk.
Comments
Post a Comment