Skip to content
On this page

Web Shell Upload via Content-Type Restriction Bypass (ENG 🇬🇧 | PL 🇵🇱 ) ​

🇬🇧 ENG:

Link: https://portswigger.net/web-security/file-upload/lab-file-upload-web-shell-upload-via-content-type-restriction-bypass

In this lab, we will be uploading a shell and bypassing a certain security measure that involves checking the "Content-Type".

Click "Access the lab", we see a blog:

blog

Exactly as described in the task, we log in to the account wiener:peter, previously going to "My Account".

login panel

Our task is to exploit a vulnerability in the file upload location, and then read the "secret" file in "carlos's" directory.

after logging panel

Click "Choose file". I have prepared a webshell (webshell.php) - for now, just to check if there are any protections regarding the extension of the given file. If we are to upload an avatar, a file with the .php extension should not pass.

We learn that there is validation:

php validation

Sorry, file type text/php is not allowed Only image/jpeg and image/png are allowed Sorry, there was an error uploading your file.

Let's see if we can bypass this in Burp Suite. I find the request that was used to send the file:

request

Here you can see the file name:

webshell in the request

I right-click and send to "Repeater":

send to repeater

In the "Repeater", I send our request again to check if everything works:

check the request

The application detected that it is a php file and correctly entered "Content-Type":

response

request

The question is whether we can modify the "Content-Type" and whether it will affect the security.

We have information in the response that image/jpeg and image/png files are allowed.

Let's replace the "Content-Type" in the request:

request changed

It turns out that this time after sending the request we receive a status code 200:

200 status

HTTP/2 200 OK
Date: Mon, 19 Feb 2024 18:06:05 GMT
Server: Apache/2.4.41 (Ubuntu)
Vary: Accept-Encoding
Content-Type: text/html; charset=UTF-8
X-Frame-Options: SAMEORIGIN
Content-Length: 133

The file avatars/webshell.php has been uploaded.<p><a href="/my-account" title="Return to previous page">« Back to My Account</a></p>

In the browser, let's go back. We can see that something tried to load:

image

On the image, right-click and choose the option "Copy image address":

image address

In a new tab, paste the image address:

image address copied

The page loaded, but it's white:

white page

We still need to provide the CMD parameter:

web-security-academy.net/files/avatars/webshell.php?cmd=whoami

Click "Enter".

Now we see that we are "carlos":

carlos page

Let's change the "whoami" command to one that allows us to read "carlos's" secret:

web-security-academy.net/files/avatars/webshell.php?cmd=cat /home/carlos/secret

Success:

carlos secret

8ZYWsxxNmgBMX6BTUfQlgQevsyvImkAM

We copy this value and submit it as the answer by clicking "Submit solution":

submit solution button

Lab solved:

lab solved


🇵🇱 PL:

Link: https://portswigger.net/web-security/file-upload/lab-file-upload-web-shell-upload-via-content-type-restriction-bypass

W tym labie będziemy uploadować shella i będziemy omijać pewne zabezpieczenie, które polega na tym, że sprawdzamy jest "Content-Type".

Klikamy "Access the lab", widzimy bloga:

blog

Dokładnie tak, jak w opisie zadania, logujemy się na konto wiener:peter, wcześniej przechodząc do "My Account".

login panel

Naszym zadaniem jest wykorzystać podatność w miejscu uploadu pliku, a następnie przeczytać plik "secret" w katalogu "carlosa".

after logging panel

Klikamy "Choose file". Ja mam przygotowany webshell (webshell.php) - póki co tylko po to, aby sprawdzić, czy są zabezpieczenia odnośnie rozszerzenia danego pliku. Jeśli mamy wczytać avatar, plik z rozszerzeniem .php nie powinien przejść.

Dowiadujemy się, że jest walidacja:

php validation

Sorry, file type text/php is not allowed Only image/jpeg and image/png are allowed Sorry, there was an error uploading your file.

Sprawdźmy, czy w Burp Suite możemy to obejść. Odnajduję request, który służył do wysyłania pliku:

request

Tutaj można zobaczyć, że jest nazwa pliku:

webshell in the request

Klikam prawym przyciskiem myszy i wysyłam do "Repeatera":

send to repeater

W "Repeaterze" wysyłam jeszcze raz nasz request, aby sprawdzić, czy wszystko działa:

check the request

Aplikacja wykryła, że jest to plik php, oraz odpowiednio wpisała "Content-Type":

response

request

Pytanie, czy jesteśmy w stanie modyfikować "Content-Type" i czy to wpłynie na zabezpieczenie.

Mamy w responsie informację, że pliki image/jpeg i image/png są dozwolone.

Podmieńmy w requeście "Content-Type":

request changed

Okazuje się, że tym razem po wysłaniu requestu otrzymujemy status kod 200:

200 status

HTTP/2 200 OK
Date: Mon, 19 Feb 2024 18:06:05 GMT
Server: Apache/2.4.41 (Ubuntu)
Vary: Accept-Encoding
Content-Type: text/html; charset=UTF-8
X-Frame-Options: SAMEORIGIN
Content-Length: 133

The file avatars/webshell.php has been uploaded.<p><a href="/my-account" title="Return to previous page">« Back to My Account</a></p>

W przeglądarce cofnijmy się. Widać, że coś próbowało się załadować:

image

Na obrazku klikamy prawym przyciskiem myszy i wybieramy opcjÄ™ "Copy image address":

image address

W nowej karcie wklejamy adres obrazka:

image address copied

Strona załadowała się, ale jest biało:

white page

Musimy jeszcze podać parametr CMD:

web-security-academy.net/files/avatars/webshell.php?cmd=whoami

Klikamy "Enter".

Teraz widzimy, że jesteśmy "carlosem":

carlos page

Zmieńmy komendę "whoami" na taką, która pozwoli na odczytanie sekretu "carlosa":

web-security-academy.net/files/avatars/webshell.php?cmd=cat /home/carlos/secret

Sukces:

carlos secret

8ZYWsxxNmgBMX6BTUfQlgQevsyvImkAM

Kopiujemy tę wartość i wysyłamy jako odpowiedź, klikając "Submit solution":

submit solution button

Lab rozwiÄ…zany:

lab solved