folder-checkPath Traversal

Introduction

Path traversal (also known as directory traversal) is a web security vulnerability that allows attackers to access files and directories stored outside the intended directory on a server. By manipulating file path references, attackers can read sensitive files, access configuration data, or even execute malicious code in severe cases.

This vulnerability occurs when an application uses user-supplied input to construct file paths without proper validation or sanitization.

How Path Traversal Works

The core concept involves using special character sequences like ../ (dot-dot-slash) to navigate up the directory tree. For example:

Normal request: /var/www/files/document.pdf
Malicious request: /var/www/files/../../../../etc/passwd

The ../ sequence tells the system to move up one directory level. By chaining multiple sequences, an attacker can traverse to any accessible location on the filesystem.

Common Attack Patterns

Basic Traversal

../../../etc/passwd
..\..\..\..\windows\system32\config\sam

URL Encoded

%2e%2e%2f%2e%2e%2f%2e%2e%2fetc%2fpasswd
..%2F..%2F..%2Fetc%2Fpasswd

Double Encoding

Null Byte Injection (older systems)

Vulnerable Code Examples

Python (Flask)

Secure Version:

PHP

Secure Version:

Node.js (Express)

Secure Version:

Java (Spring Boot)

Secure Version:

Ruby (Rails)

Secure Version:

C# (ASP.NET Core)

Secure Version:

Prevention Best Practices

1. Input Validation

  • Use whitelists of allowed characters

  • Reject input containing path traversal sequences

  • Validate file extensions and types

2. Path Canonicalization

  • Always resolve paths to their absolute form

  • Use language-specific functions to normalize paths

  • Compare the resolved path with the allowed base directory

3. Sandboxing

  • Use chroot jails or containers to limit filesystem access

  • Run applications with minimal privileges

  • Implement filesystem access controls

4. Security Functions by Language

Python: os.path.abspath(), os.path.realpath(), pathlib.Path.resolve()

PHP: realpath(), basename()

Node.js: path.resolve(), path.normalize()

Java: Path.normalize(), Path.toAbsolutePath()

Ruby: Pathname#expand_path, File.expand_path()

C#: Path.GetFullPath(), Path.Combine()

5. Additional Measures

  • Implement proper access controls and authentication

  • Log all file access attempts

  • Use security headers and WAF rules

  • Regular security audits and penetration testing

  • Keep frameworks and libraries updated

Testing for Path Traversal

Manual Testing

Automated Tools

  • Burp Suite

  • OWASP ZAP

  • Nikto

  • Custom scripts with fuzzing lists

Real-World Impact

Path traversal vulnerabilities can lead to:

  • Exposure of sensitive configuration files

  • Access to password hashes and authentication data

  • Reading application source code

  • Remote code execution (when combined with file upload)

  • Complete system compromise

Conclusion

Path traversal vulnerabilities remain a critical security issue despite being well-documented. Developers must always validate and sanitize user input, use proper path handling functions, and implement defense-in-depth strategies to protect against these attacks. Regular security testing and code reviews are essential to identify and remediate these vulnerabilities before they can be exploited.


References & More

Last updated