Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Path traversal and unsafe file uploads

After this lesson: I can keep a file name and an upload inside the folder a site meant.

60 min lesson9 commandsDVWA lab: practise SQL injection and XSS on the Damn Vulnerable Web Application

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Key words

WordWhat it meansExample
Path traversalusing ../ in a name to leave the intended folder../../etc/passwd
File inclusiona page loading another file named in the request
Canonical patha path with every . and .. worked out
Web rootthe folder a web server serves files from
Magic bytesthe first bytes that say what a file really is\x89PNG
Web shellan 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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Every ../ 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 typedPath after joining to /var/www/photosInside the folder?
cat.png/var/www/photos/cat.pngYes
2026/cat.png/var/www/photos/2026/cat.pngYes
../config.php/var/www/config.phpNo, one folder up
../../../etc/passwd/etc/passwdNo, at the top

The cells students fill in are left blank in this preview.

Working out where a name really lands

Code: python
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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Look 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

Command
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

Command
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.php

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Open one of the page's own files

Command
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

Command
docker exec dvwa grep -n "_GET" /var/www/html/vulnerabilities/fi/source/low.php

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

How the medium level filters it

Command
docker exec dvwa grep -n "str_replace" /var/www/html/vulnerabilities/fi/source/medium.php

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

How the impossible level picks it

Command
docker exec dvwa grep -n "in_array\|file1" /var/www/html/vulnerabilities/fi/source/impossible.php

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

What the low upload checks

Command
docker exec dvwa grep -n "target_path\|move_uploaded" /var/www/html/vulnerabilities/upload/source/low.php

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

What the impossible upload checks

Command
docker exec dvwa grep -n "random_bytes\|getimagesize\|imagecreatefrom" /var/www/html/vulnerabilities/upload/source/impossible.php

Shown 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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

How 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

Command
docker exec dvwa ls -l /var/www/html/hackable/uploads

Shown 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).

Code: python
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

Exit quiz

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.