Practitioner · updated

Path traversal

PortSwigger Web Security Academy · Practitioner

  • #Path traversal
LevelsApprenticePractitioner

File Path Traversal, Traversal Sequences Blocked with Absolute Path Bypass

The vulnerability

The application blocks ../. You don’t need it, because it also accepts a full path like /etc/passwd.

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. The application blocks traversal sequences but treats the supplied filename as being relative to a default working directory. 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 image files are visible in HTTP history.

2. Intercept an Image Request: On the website, browse to a product page. In HTTP history, find an image request (e.g., GET /image?filename=1.jpg) and Send to Repeater.

3. Test for ../ Traversal (Fails): In Repeater, modify the filename parameter to ../../../etc/passwd and send the request. The HTTP/2 400 Bad Request response confirms that the application blocks the traversal sequences.

4. Bypass with Absolute Path: Change the filename parameter to the absolute path of the target file: /etc/passwd. Send the request. An HTTP/2 200 OK response with the content of /etc/passwd in the body will be returned, and the lab is solved.

Tip

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

File Path Traversal, Traversal Sequences Stripped Non-Recursively

The vulnerability

The application removes ../, but only once. ....// turns into ../ after that.

Fix: Don’t try to clean the path. Resolve it and check where it points.

The lab

Open the lab on PortSwigger

This lab contains a path traversal vulnerability in the display of product images. The application strips path traversal sequences from the user-supplied filename before using it. 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 image files are visible in HTTP history.

2. Intercept an Image Request: On the website, browse to a product page. In HTTP history, find an image request (e.g., GET /image?filename=1.jpg) and Send to Repeater.

3. Test for ../ Traversal (Fails): In Repeater, modify the filename parameter to ../../../etc/passwd and send the request. The HTTP/2 400 Bad Request response confirms that the ../ sequences are blocked.

4. Bypass with a Recursive Payload: Change the filename parameter to a specially crafted payload that will resolve to ../../../etc/passwd after the non-recursive filter is applied. A common payload for this is ....//....//....//etc/passwd. Send the request. An HTTP/2 200 OK response with the content of /etc/passwd will be returned, and the lab is solved.

Tip

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

File Path Traversal, Traversal Sequences Stripped with Superfluous URL-Decode

The vulnerability

The application checks for ../ and decodes the filename afterwards. If you encode it twice, the check doesn’t notice.

Fix: Decode first, then check.

The lab

Open the lab on PortSwigger

This lab contains a path traversal vulnerability in the display of product images. The application blocks input containing path traversal sequences. It then performs a URL-decode of the input before using it. 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 image files are visible in HTTP history.

2. Intercept an Image Request: On the website, browse to a product page. In HTTP history, find an image request (e.g., GET /image?filename=1.jpg) and Send to Repeater.

3. Test for ../ Traversal (Fails): In Repeater, modify the filename parameter to ../../../etc/passwd and send the request. The HTTP/2 400 Bad Request response confirms that the traversal sequences are blocked.

4. Test with Single URL-Encoding (Fails): URL-encode the ../ sequence to %2e%2e%2f and add it to the filename parameter: %2e%2e%2f%2e%2e%2f%2e%2e%2fetc/passwd. The HTTP/2 400 Bad Request response indicates that the filter still catches this.

5. Bypass with Double URL-Encoding: Double URL-encode the ../ sequence. The double-encoded payload for ../ is %252e%252e%252f. Use this to create the filename parameter: %252e%252e%252f%252e%252e%252f%252e%252e%252fetc/passwd. Send the request. An HTTP/2 200 OK response with the content of /etc/passwd will be returned, and the lab is solved.

Tip

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

File Path Traversal, Validation of Start of Path

The vulnerability

The filename has to start with /var/www/images. After that you can still go back up with ../.

Fix: Resolve the full path before you check how it starts.

The lab

Open the lab on PortSwigger

This lab contains a path traversal vulnerability in the display of product images. The application transmits the full file path via a request parameter, and validates that the supplied path starts with the expected folder. 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 image files are visible in HTTP history.

2. Intercept an Image Request: On the website, browse to a product page. In HTTP history, find an image request (e.g., GET /image?filename=/var/www/images/1.jpg). This time, the full path is visible. Right-click and Send to Repeater.

3. Test for Direct Access (Fails): In Repeater, modify the filename parameter to /etc/passwd and send the request. The HTTP/2 400 Bad Request response confirms that the application blocks direct absolute paths without the required prefix.

4. Bypass with ../ After the Prefix: Add the required prefix to your payload, then use ../ to traverse to the root and access /etc/passwd. Change the filename parameter to /var/www/images/../../../etc/passwd. Send the request. An HTTP/2 200 OK response with the content of /etc/passwd will be returned, and the lab is solved.

Tip

An active scan in Burp Suite may not find this vulnerability automatically but give an indication of “File path manipulation.”

File Path Traversal, Validation of File Extension with Null Byte Bypass

The vulnerability

The filename has to end in .jpg. With a null byte in front of it, the check is happy, but the server stops reading at the null byte.

Fix: Reject null bytes, and check the resolved path.

The lab

Open the lab on PortSwigger

This lab contains a path traversal vulnerability in the display of product images. The application validates that the supplied filename ends with the expected file extension. 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 image files are visible in HTTP history.

2. Intercept an Image Request: On the website, browse to a product page. In HTTP history, find an image request (e.g., GET /image?filename=1.jpg) and Send to Repeater.

3. Test for ../ Traversal (Fails): In Repeater, modify the filename parameter to ../../../etc/passwd and send the request. The HTTP/2 400 Bad Request response confirms that the traversal sequences are blocked because the file extension is missing.

4. Bypass with Null Byte: Change the filename parameter to include the traversal payload, followed by a URL-encoded null byte (%00), and then the required extension (.jpg). The payload will be ../../../etc/passwd%00.jpg. Send the request. An HTTP/2 200 OK response with the content of /etc/passwd in the body will be returned, and the lab is solved.

Tip

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