Path traversal
Path traversal manipulates file path parameters so the application reads files outside the intended directory.
Material
Path traversal abuses the way an application builds a filename from user input. Parameters such as file, path, or template are concatenated raw into a server-side path, so ../ sequences push the file location outside the directory the app meant to expose.
How it works
The server usually calls a filesystem function such as readFile or open with a string-built path. A relative path like ../../etc/passwd is resolved by the operating system relative to the working directory, so the app silently steps outside the document or upload folder it meant to expose.
GET /download?file=../../../../etc/passwd HTTP/1.1
Host: lab.cidadaxsec.local
Cookie: session=3f9a1c7e
Accept: */*
Connection: closecurl -sS -o passwd.txt "https://lab.cidadaxsec.local/download?file=..%2f..%2f..%2f..%2fetc%2fpasswd"
head -n 3 passwd.txt
# jika ../ diblokir, coba path absolut dan filter non-rekursif
curl -sS -o out.bin "https://lab.cidadaxsec.local/download?file=/etc/passwd"
curl -sS -o out.bin "https://lab.cidadaxsec.local/download?file=....//....//etc/passwd"Attack vectors
- Direct traversal: ../../secret.txt through the path parameter.
- Double URL encoding: %252e%252e%252f slips through because the value is decoded twice, once by the proxy and once by the app.
- Absolute path: /etc/passwd often bypasses filters that only block the ../ substring.
- Non-recursive filter: ....// still contains ../ after a single removal pass.
- Null byte: invoice.pdf%00.php makes extension validation stop at the null byte on legacy runtimes.
Because filters run on the raw string before normalization, successful attacks almost always depend on a processing order that differs between the proxy, the framework, and the app code. The reliable approach is to follow that processing order instead of guessing which filter is in place.
Preventing
- Canonicalize the path, then compare it against the allowed directory: the resolved path must still start with the root plus a separator.
- Never build paths from user-supplied filenames; use an ID or token the server maps to a filename.
- Use an allowlist of extensions and MIME types rather than a blacklist, and reject names that are not a single simple path component.
- Normalize repeatedly before comparing: decode several times, then resolve, so ....// and %252e collapse too.
- Do not rely on string truncation at a null byte; rely on a modern runtime and final MIME-based validation.
from pathlib import Path
ROOT = Path("/srv/app/files").resolve()
def safe_read(name: str) -> bytes:
candidate = (ROOT / name).resolve()
if not candidate.is_relative_to(ROOT):
raise ValueError("path escapes the upload directory")
if candidate.suffix.lower() not in {".pdf", ".png", ".txt"}:
raise ValueError("extension not allowed")
return candidate.read_bytes()Labs
- File path traversal, simple case
- File path traversal, traversal sequences blocked with absolute path bypass
- File path traversal, traversal sequences stripped non-recursively
- File path traversal, traversal sequences stripped with superfluous URL-decode
- File path traversal, validation of start of path
- File path traversal, validation of file extension with null byte bypass
Labs in this topic
Labs run on a domain separate from this site, so a payload cannot reach your account session.
Labs for this category land in Phase 2. The material is readable now.
Challenges in this category
Challenges from the PortSwigger Web Security Academy. The original labs run on PortSwigger domains, so the link opens there.