What is Directory Traversal? Complete Guide with Examples (2026)
What is Directory Traversal? Complete Guide with Examples (2026)
Directory Traversal is a critical web security vulnerability that allows attackers to access files and directories outside the intended path. It is commonly found in poorly secured file‑handling systems and API endpoints that expose file‑download or file‑read functionality.
- What is Directory Traversal (Path Traversal)?
- How the attack works
- Real directory traversal attack scenario
- Impact of the vulnerability
- Common mistakes that enable it
- Prevention techniques
- Practice and lab tips
- FAQ for developers
What is Directory Traversal?
Directory Traversal (also known as Path Traversal) occurs when an application allows user input to control file paths without proper validation. Attackers can manipulate this input to access restricted files on the server, often outside the web root or intended directory.
Example vulnerable URL
https://example.com/file?name=report.pdf
Attacker input
../../../../etc/passwd
If the server naively concatenates the user‑provided name to a base path, it may end up reading /var/www/html/../../../../etc/passwd — which resolves to the system’s password file.
How the Directory Traversal Attack Works
- Application reads a file path directly from user input (URL, form, API param).
- No validation or sanitization is performed on the path.
- Attacker injects
../sequences to move up the directory tree. - Server opens and returns the requested file, which may be system files, config files, or environment variables.
..\..\Windows\System32\drivers\etc\hosts can be used to achieve the same goal across different OSes.
Real Directory Traversal Scenario
An attacker finds a file‑download endpoint such as /download?file=manual.pdf. By changing the parameter to:
download?file=../../../../config/db.json
they can access configuration files that contain API keys, database credentials, or environment settings.
In more aggressive cases, this can even lead to remote code execution if the attacker can read script files, overwrite configuration, or chain it with other vulnerabilities.
Impact of Directory Traversal
- Access to system files like
/etc/passwdorC:\Windows\System32\drivers\etc\hosts. - Leak of sensitive configuration files, environment variables, API keys, and credentials.
- Exposure of application source code or internal logs.
- Potential remote code execution when combined with file‑upload or log‑injection weaknesses.
Because such attacks often have low implementation complexity but high impact, they are treated as high‑severity vulnerabilities in bug‑bounty programs and pentests.
Common Mistakes That Enable Traversal
- Allowing direct file‑path input from users without constraints.
- Not sanitizing or normalizing sequences like
../,..\..,%2e%2e%2f. - Exposing the internal file‑system layout via predictable paths or error messages.
- Using raw user input in constructs like
readFileSync(path),file_get_contents(path), or equivalent APIs.
How to Prevent Directory Traversal
- Never trust user input when building file paths; always validate and sanitize it.
- Use strict allowlists of allowed filenames or short IDs instead of full paths.
- Restrict file access to a clearly defined base directory (e.g.,
/uploads/or/public/). - Avoid exposing raw file‑path arguments; use hashes, IDs, or tokens mapped server‑side to real paths.
Pro tips for secure file handling
- Normalize paths before processing (resolve all
../and join against a base directory). - Use secure, framework‑provided file‑handling APIs instead of raw system calls.
- Run applications with minimal file‑system permissions.
- Log and monitor suspicious file‑access patterns.
Practice Safely: Try It in Labs
You can safely practice directory traversal in controlled environments such as:
- DVWA (Damn Vulnerable Web App) – offers a “File Inclusion / Path Traversal” lab.
- PortSwigger Web Security Academy – path traversal labs with step‑by‑step guidance.
Never test on real‑world sites without explicit permission.
Conclusion
Directory Traversal is easy to exploit but also 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 sanitize and normalize, and restrict access to a tightly controlled directory.
Comments
Post a Comment