Report an Incident Become a Partner Careers Contact
Book a Demo
Fundamentals

What Is Path Traversal? How Directory Traversal Attacks Read Files They Shouldn't, And How To Stop Them

M Mahdi Alabdallah · Offensive Security Engineer 30 November 2026 8 min read
A folder tree for C:\inetpub\app with appsettings.json, .env and an images folder; a dashed arrow climbs from images up to the .env file, labelled ?file=..\.env, above the request GET /download?file=..\.env, beside the headline "Two dots that climb out".

Path traversal, also called directory traversal, is a vulnerability that lets an attacker read or write files outside the folder an application meant to use. It happens when an application builds a file path from user input without checking where that path ends up. By adding sequences like ../, which means "go up one folder", the attacker walks out of the intended directory and into the rest of the file system.

The classic example is a web page that serves files by name: /download?file=report.pdf. If the application simply joins the name onto its documents folder, a request for /download?file=../../../../etc/passwd or ?file=..\..\web.config may return a file the developer never intended anyone to see.

Path traversal is one of the oldest web vulnerabilities. It is also back near the top of attackers' lists, because the files it exposes increasingly contain keys to everything else.

How a path traversal attack works

Suppose an application stores user-uploaded images in C:\inetpub\app\images\ and serves them with code that does, in effect:

path = "C:\inetpub\app\images\" + request.filename
return read_file(path)

A normal request for cat.png reads C:\inetpub\app\images\cat.png. A request for ..\appsettings.json reads C:\inetpub\app\appsettings.json, the application's configuration file, often with a database connection string in it. Add more ..\ and the attacker can reach anything the application's account can read.

Common variations

Developers often try to block ../ with a simple filter. Attackers have well-known ways around that:

  • URL encoding: %2e%2e%2f for ../, or double encoding (%252e%252e%252f) when the input is decoded twice.
  • Backslashes on Windows: ..\ works where only ../ was blocked.
  • Nested sequences: ....// turns back into ../ after a filter strips the inner ../ once.
  • Absolute paths: some functions ignore the base folder entirely if the input starts with / or a drive letter.
  • Traversal through static routes: requests like /static/../.env aimed at a framework or proxy that normalises paths differently from the application behind it.

What attackers go after

A decade ago the textbook proof of a path traversal was reading /etc/passwd or win.ini. Today attackers go straight for secrets. Research published in November 2026 found that credential harvesting rose from about 1% of observed exploitation in January to between 25% and 35% by the autumn. Path traversal (CWE-22) was behind 25 of the 127 exploited vulnerabilities used to steal credentials. Typical targets:

  • Environment files: .env, which often holds database passwords, cloud keys and API tokens.
  • Application configuration: web.config, appsettings.json, framework logs that echo secrets.
  • Process environment: /proc/self/environ on Linux, which exposes the variables the app was started with.
  • Cloud and developer credentials: .aws/credentials, Azure CLI token caches, .git-credentials, .npmrc, SSH private keys.

Once an attacker has those, they may never need to touch your server again. They sign in to your cloud or database directly, with valid credentials. We look at what that means for patching in The Exploit Didn't Want A Shell. It Wanted Your .env File.

When traversal can write, not just read

Path traversal on a write operation is worse. If a file upload or archive extraction (the "zip slip" pattern) lets an attacker choose where a file lands, they can drop a script into a folder the web server executes. That gives them a web shell and full remote code execution.

Path traversal vs related vulnerabilities

  • Local file inclusion (LFI) is a close cousin. Instead of returning the file's contents, the application includes and executes it. LFI often uses traversal to reach the file.
  • Directory listing is a misconfiguration that shows a folder's contents. It helps an attacker find files but doesn't on its own let them leave the intended folder.
  • Server-side request forgery (SSRF) makes the server fetch a URL rather than read a local file, but attackers use it for the same prize: credentials, often from the cloud metadata service. See What Is SSRF?

How to test for path traversal

Any feature that touches files by name is a candidate: downloads, image and document viewers, templates, language or theme selectors, import and export, archive handling, and static-file routes in frameworks and reverse proxies. Testers send traversal sequences in different encodings and look for responses that contain the content of known files. Automated scanners find the obvious cases. Logic-heavy features and multi-layer setups, where a proxy and an application interpret paths differently, need a human. That is why traversal still turns up regularly in penetration tests of applications that passed a scanner.

How to prevent path traversal

  1. Don't build file paths from user input. The safest design maps a user-supplied ID to a file the application chooses, for example a database lookup from id=42 to a stored path. The user never supplies a path at all.
  2. If you must accept names, validate against an allow-list. Accept only expected characters and formats, such as letters, digits and one known extension, and reject everything else rather than trying to strip bad sequences.
  3. Canonicalise, then check. Resolve the full path first, for example with Path.GetFullPath in .NET or realpath elsewhere. Then confirm it still starts with the intended base folder before opening it.
  4. Run the application with least privilege. An app pool or container account that can only read its own content folder limits what a traversal can reach.
  5. Keep secrets out of files the app can read. On Azure, a managed identity with Key Vault removes the .env file worth stealing. See secrets management.
  6. Patch frameworks and components. Many traversal bugs live in libraries and dev servers rather than your own code, so you need an inventory of what's inside each application.
  7. Add a web application firewall as a second layer. A WAF in blocking mode stops many traversal patterns, but treat it as backup for fixed code, not a replacement.

How to detect path traversal attempts

Traversal attempts are noisy and recognisable in web logs. If your IIS logs go to Microsoft Sentinel, this is a starting point to tune:

W3CIISLog
| where TimeGenerated > ago(7d)
| extend Req = strcat(csUriStem, "?", csUriQuery)
| where Req has_any ("../", "..\\", "%2e%2e", "%252e", ".env", "web.config", "appsettings", "/proc/self/")
| summarize Requests = count(), Codes = make_set(scStatus), Samples = make_set(Req, 5) by cIP, sSiteName
| order by Requests desc

Most hits will be scanners receiving 400 or 404 responses. Focus on 200 responses to traversal or secret-file requests. Treat any secret in a file that was served as compromised, and rotate it.

Frequently asked questions

Is path traversal the same as directory traversal?

Yes. The two terms mean the same thing. It is also catalogued as CWE-22, "Improper Limitation of a Pathname to a Restricted Directory".

How serious is a path traversal vulnerability?

It depends on what the application can read or write. A read-only traversal can be scored as medium severity yet expose cloud keys, database passwords and API tokens, which makes it critical in practice. A traversal that can write files often leads to remote code execution.

Does a WAF stop path traversal?

A WAF blocks many common traversal payloads and is worth having, but encoded or application-specific variants can get past it. Fix the code, and use the WAF as defence in depth.

Does running on Windows or Linux make a difference?

The principle is the same. The details differ: Windows accepts both \ and / as separators and has drive letters, while Linux exposes useful files under /proc. Filters written with only one platform in mind are a common source of bypasses.

What should I do if I find a path traversal flaw has been exploited?

Patch or block it, then assume every file the application could read was taken. Rotate the secrets in those files, check for use of the old credentials, and if the flaw could write files, search for web shells.

How Wizard Cyber helps

Our offensive security team tests web applications for traversal and the logic flaws scanners miss. Our managed WAF and vulnerability management services help close what we find. Our 24/7 SOC watches for the moment a stolen credential is used. To talk it through, book a demo.

Path TraversalDirectory TraversalCWE-22Web Application SecurityCredential HarvestingSecrets ManagementPenetration TestingWAFKQLFundamentals

Ready to see the agents work?

Book a demo of CYBERSHIELD AI against a real scenario.