Labs / Web Attacks / The web app's attack surface

Lab 5 of 5 · The web app's attack surface

Path traversal: reading files outside the archive

A document archive opens files by name joined onto its folder. Hand it `../` and it reads any file the server can reach, including /etc/passwd.

Start this lab 4 steps

The idea

Beatrix has an authorised engagement against Glenmara Archive, a public records site that serves scanned documents. You request a document by its file name and the archive hands back its contents — exactly what it is for.

The question a tester asks is: what counts as a file name? If the archive takes your name and opens it without checking that it stays inside the archive folder, then a name is not only charter-1897.txt — it can be ../ something, or an absolute path, and the same endpoint will read a file the archive was never meant to serve. That is path traversal, and it turns "download a document" into "read any file the server process can".

The resolved path leaves the archive
The resolved path leaves the archive

Steps

  1. The archive opens files by name

    You are on Beatrix's box, kali, with the archive at 10.0.1.20. Open it in the Viewer to see the reading room, then use the API the way it is meant to be used: list the documents, and fetch one by name. The point of this step is to prove the shape of the endpoint — you give it a name, it opens that file and returns the bytes.

    kali:~# curl -s http://10.0.1.20/api/documents
    kali:~# curl -s -G --data-urlencode "name=charter-1897.txt" http://10.0.1.20/api/document
  2. Climb out of the archive

    Test whether the name is contained. A ../ segment means "parent directory"; enough of them walk from the archive folder up to the file system root, and from there you can name any absolute path. Ask for /etc/passwd by climbing out — the classic proof a reader is not contained.

    kali:~# curl -s -G --data-urlencode "name=../../../../etc/passwd" http://10.0.1.20/api/document
  3. Reach the app's own secret

    /etc/passwd proves the hole; now use it on something that matters. Applications keep their configuration beside the code, not in the public folder — the archive's accession notes even mention the configuration is "kept separately from this archive". From the archive folder, the app's directory is one level up, and a recovery config sits in config/ there. Climb out only far enough to read it.

    kali:~# curl -s -G --data-urlencode "name=../config/secret.conf" http://10.0.1.20/api/document
  4. Swap seats: contain the path

    Now you run Glenmara. The wrong fix is to search the name for .. and reject it — attackers have encodings (%2e%2e, ....//), absolute paths and symlinks that slip past string filters. The right fix is to resolve the final path and require it to stay inside the archive. The correct endpoint, /api/file, does exactly that. Send it the same traversal and watch it refused.

    kali:~# curl -s -o /dev/null -w "%{http_code}" -G --data-urlencode "name=../config/secret.conf" http://10.0.1.20/api/file
    kali:~# curl -s -G --data-urlencode "name=reading-room.txt" http://10.0.1.20/api/file

Run these steps on real machines

Keep reading

Learn it on real machines.

Every lab is a small network of real Linux machines in your browser. Sign in with Google or email to start.

Start this lab