Soccer

FIFA World Cup™ is here. Get your VPN 80% off

FIFA World Cup™ is here.
Get your VPN 80% off

Claim Now
Wc2026 Mobile

Expressvpn Glossary

Directory traversal attack

Directory traversal attack

What is a directory traversal attack?

A directory traversal attack (also known as path traversal, dot-dot-slash attack, directory climbing, or backtracking) is a web security vulnerability that may allow an attacker to access files or directories outside the location an application is intended to expose.

How does a directory traversal attack work?

A directory traversal attack works by manipulating file paths constructed from user-controlled input. If the application doesn't properly validate or sanitize this input, the attacker can modify the path to reference unintended locations on the server.

This is typically done using traversal sequences such as ../, which move up one directory level. Repeating these sequences allows the attacker to escape the intended directory and access other parts of the file system.

How a directory traversal attack works.

Impact of a directory traversal attack

A successful directory traversal attack can expose application source code, configuration files, system files, logs, and other internal resources. In some cases, it may also allow writing to arbitrary files on the server, potentially enabling modification of application data or behavior and leading to full system compromise.

This access can also help attackers map the server's directory structure, supporting later stages of an attack.

Common attack surfaces

Directory traversal vulnerabilities typically appear in features that handle file paths or dynamically load local resources. Common examples include:

  • File download features: Parameters specifying download filenames can be altered to retrieve unintended files.
  • Image or document viewers: Applications that load media from user-supplied paths may allow access outside the intended directory.
  • Template rendering: Server-side features that render pages using templating languages, such as Hypertext PreProcessor (PHP), Jinja, or Handlebars, can be exploited if an attacker can control which file is loaded as a template. Because these languages can execute code, supplying an attacker-controlled file (such as an uploaded file or a log file containing injected content) can escalate directory traversal to code execution.
  • Content management systems (CMSs) and plugin environments: CMSs and third-party plugins often perform file operations, increasing the likelihood of improper path handling.

Risks and privacy concerns

Directory traversal can lead to serious security and privacy consequences when exposed files contain sensitive information:

  • Credential and key disclosure: Configuration or system files may contain passwords, API keys, or authentication tokens.
  • User data exposure: Personal or account-related information stored in accessible files may be retrieved.
  • Internal structure leakage: Exposed directory paths, source code, or configuration details can give attackers insight into the system’s organization, aiding further exploitation.
  • Compliance violations: Unauthorized access to regulated or sensitive data may breach data protection requirements.
  • Arbitrary code execution: Directory traversal can enable indirect server-side template injection (SSTI), allowing attackers to execute arbitrary code on the server.

How to prevent a directory traversal attack?

The primary defenses against directory traversal include avoiding the use of user-supplied input in file system calls, validating input against an allowlist of permitted values, canonicalizing file paths and verifying they remain within the intended base directory, and applying the principle of least privilege to limit what the application can access on the file system.

Further reading

FAQ

What is the difference between directory traversal and file inclusion?

Directory traversal is a technique for accessing files outside an intended directory by manipulating file paths. File inclusion involves loading a file into an application, which may expose file contents or, in some cases, lead to code execution. The two are closely related, as file inclusion functionality is a primary target for directory traversal probing due to the potential impact of controlling which file gets loaded.

Can directory traversal attacks lead to remote code execution?

Directory traversal is most commonly used to read files, but in some cases, it can also allow writing to arbitrary files when a vulnerable application writes, uploads, extracts, or saves files based on user-controlled paths. In certain environments, this can lead to code execution.

For example, in a Hypertext PreProcessor (PHP) application, if an attacker can write a file outside a directory designated for static content and into one where PHP files are executed server-side, they may be able to run arbitrary code by requesting the file in a browser. Exposed information from read access, such as credentials or internal paths, can also support remote code execution through other weaknesses.

What files do attackers usually target?

Attackers often target configuration files, system files, logs, and files that may contain credentials, API keys, or application data. The exact targets depend on the server environment and application setup.

How can developers prevent directory traversal?

Developers can reduce risk by validating and restricting file paths, avoiding direct use of user input in file operations, and ensuring that applications only access intended directories.

Is directory traversal still a common web security issue?

Yes. It remains a recurring issue in applications that rely on dynamic file access and don't safely handle file path input.
Get Started