A photo page that opened a password file
Path traversal is when a file name typed by a visitor walks out of the folder a website meant, using ../. An unsafe upload is a file a visitor sends that the server later treats as a program.
A gallery shows pictures with an address ending ?file=cat.png. Someone changes it to ?file=../../config.php. The page shows the site's database password. The same site lets visitors upload pictures, and checks only the name.
The one rule
Warning
This lab is a website that is insecure on purpose. Attack your own private copy and nothing else; it has no way out to the internet. A real system needs the owner's written permission first. Without it, in the UK, this is a crime under the Computer Misuse Act 1990.
By the end of this lesson
Note
I can keep a file name and an upload inside the folder a site meant.
Quick check: the rule
Have a go
Quick check: the rule
3 questionsKey words
| Word | What it means | Example |
|---|---|---|
| Path traversal | using ../ in a name to leave the intended folder | ../../etc/passwd |
| File inclusion | a page loading another file named in the request | |
| Canonical path | a path with every . and .. worked out | |
| Web root | the folder a web server serves files from | |
| Magic bytes | the first bytes that say what a file really is | \x89PNG |
| Web shell | an uploaded script that runs commands for its sender |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
3 questionsEvery ../ climbs one folder
A program that builds a path by joining a folder and a typed name trusts the name. Each ../ in the name climbs one folder up.
Enough of them reach the top, and from there any file the server can read. A name that starts with / is worse: joined in many languages, it replaces the folder completely.
Worked example: one folder, four names
| Name typed | Path after joining to /var/www/photos | Inside the folder? |
|---|---|---|
| cat.png | /var/www/photos/cat.png | Yes |
| 2026/cat.png | /var/www/photos/2026/cat.png | Yes |
| ../config.php | /var/www/config.php | No, one folder up |
| ../../../etc/passwd | /etc/passwd | No, at the top |
The cells students fill in are left blank in this preview.
Working out where a name really lands
import posixpath
base = "/var/www/photos"
# Change name to "../../../etc/passwd", then to "/etc/passwd", and press Run.
name = "cat.png"
full = posixpath.normpath(posixpath.join(base, name))
print(full)
print("inside" if full.startswith(base + "/") else "outside: refuse")
Make a free account to save your work and track your progress.
Check
Have a go
Check
3 questionsLook at the two pages in your lab
Your lab runs DVWA again. Start it, then sign in with the first two Runs. The third opens the File Inclusion page with one of its own files, the way its links do. The next three print the lines of PHP that pick the file, at the low, medium and impossible levels. The last two read the upload code at low and impossible.
Read the sign-in form's token
T=$(curl -s -c jar http://dvwa/login.php | sed -n "s/.*name=.user_token. value=.\([a-f0-9]*\).*/\1/p"); echo ${#T}Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Sign in as the lab's admin
curl -s -b jar -c jar -d "username=admin&password=password&Login=Login&user_token=$T" -o /dev/null -w "%{http_code} %{redirect_url}\n" http://dvwa/login.phpShown here, not run. Press Start lab to type it into your own lab’s terminal.
Open one of the page's own files
curl -s -b jar "http://dvwa/vulnerabilities/fi/?page=file1.php" | grep -o "File 1"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
How the low level picks the file
docker exec dvwa grep -n "_GET" /var/www/html/vulnerabilities/fi/source/low.phpShown here, not run. Press Start lab to type it into your own lab’s terminal.
How the medium level filters it
docker exec dvwa grep -n "str_replace" /var/www/html/vulnerabilities/fi/source/medium.phpShown here, not run. Press Start lab to type it into your own lab’s terminal.
How the impossible level picks it
docker exec dvwa grep -n "in_array\|file1" /var/www/html/vulnerabilities/fi/source/impossible.phpShown here, not run. Press Start lab to type it into your own lab’s terminal.
What the low upload checks
docker exec dvwa grep -n "target_path\|move_uploaded" /var/www/html/vulnerabilities/upload/source/low.phpShown here, not run. Press Start lab to type it into your own lab’s terminal.
What the impossible upload checks
docker exec dvwa grep -n "random_bytes\|getimagesize\|imagecreatefrom" /var/www/html/vulnerabilities/upload/source/impossible.phpShown here, not run. Press Start lab to type it into your own lab’s terminal.
What the answers tell you
The page answers File 1. Low takes $_GET[ 'page' ] on line 4 and includes it as it is. Medium removes ../ once, on line 8. Impossible accepts only four names, file1.php among them, checked by in_array on line 14.
Low's upload keeps the sender's name (lines 5 and 6) and any type. Impossible makes a random name on line 19, asks getimagesize on line 29, and re-encodes the picture on lines 33 and 37.
A common mistake
Warning
Many people think checking that an upload's name ends in .jpg makes it a picture. It does not: the name is typed by the sender, and so is the type the browser claims. Read the file's magic bytes, re-encode pictures, and store them under a name the server makes.
The fix: names you choose, folders you check
For files a visitor may open, map an id or a short allow list to real names. If a name must be typed, work out the canonical path and refuse anything outside the folder.
For uploads: limit the size, allow only a few types, check the magic bytes, and re-encode pictures. Save each upload under a random name, outside the web root, where the server never runs scripts.
Check
Have a go
Check
3 questionsHow to detect traversal and bad uploads
Search access logs for ../, %2e%2e and requests for files such as /etc/passwd. Alert on any request that asks the uploads folder for a script.
Watch the uploads folder itself. A new file whose content is not a picture, or one that ends .php, is an incident. A file integrity tool or a simple listing compared each day will show it.
What is in the uploads folder now
docker exec dvwa ls -l /var/www/html/hackable/uploadsShown here, not run. Press Start lab to type it into your own lab’s terminal.
Assignment: safe file names and uploads
Write two functions. safe_join(base, name) returns the canonical path of name inside the folder base, or None if the name is empty, holds a NUL character or a backslash, starts with /, or lands outside base. upload_problem(filename, data, max_bytes) returns "" for a good upload or the first problem: "too big" if data is longer than max_bytes, "name" unless the name has exactly one dot, "type" unless the extension is png, jpg or jpeg in any case, and "content" unless the bytes start like that type (\x89PNG for png, \xff\xd8\xff for jpg and jpeg).
import posixpath
ALLOWED = ["png", "jpg", "jpeg"]
def safe_join(base, name):
# Return the full path inside base, or None if name could leave it.
return posixpath.join(base, name)
def upload_problem(filename, data, max_bytes):
# Return "" or the first problem: "too big", "name", "type" or "content".
return ""
Make a free account to save your work and track your progress.
Exit quiz
Have a go