Skip to content
File upload Write-up Intermediate

File Upload Bypass to RCE == $$$$

The file upload feature allowed bypassing extension restrictions to upload executable PHP files, leading to remote code execution. The attacker bypassed three successive fixes using case variation, null byte injection, and magic byte manipulation with keyword filtering evasion.

Sagar Sajeev Follow

Published on sagarsajeev.medium.com

In this writeup, I’ll explain how I was able to bypass a File upload feature on the target and chain it to an RCE. Thus increasing the severity.

Sagar Sajeev, on sagarsajeev.medium.com

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.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 6 min read.

Context

This write-up covers a file upload feature on a target web application that the author, Sagar Sajeev, was able to bypass and chain to remote code execution (RCE). The report does not name the target, the product or the specific upload feature beyond describing it as a file upload that was eventually restricted to images only.

The notable part of the story is that the target's security team deployed a fix three separate times for the same vulnerability, and the author bypassed each fix. Three bounties were awarded for the three bypasses, which the author describes as his first four-digit bounty.

Image Credit — Wallarm From the original on sagarsajeev.medium.com

Setup

The report does not describe any accounts, roles or specific tooling beyond the following:

  • The author works on Linux and uses the built-in hex editor and xxd to view and edit magic bytes. Any hex editor can be used to achieve the same result.
  • One of John Hammond's videos helped with the keyword-filter bypass in the third scenario.

Finding it

The author does not describe how the upload feature was initially discovered. The write-up begins from the point where a .php payload was already known to be rejected, and the work consists of finding ways around each successive restriction the security team introduced.

Exploitation

Scenario #1: mixed-case extension

  1. A payload with a .php extension was not allowed.
  2. The payload was renamed from payload.php to payload.pHp5, changing the extension to a mix of upper and lower case, which bypassed the restriction.
  3. The author notes that such payloads only work if the target has client-side validation only. Sometimes the payload may go through the frontend but no callback is received because it has been blocked by the IDS or a backend firewall.
  4. In this case the callback was received and RCE was established.

Bounty #1 was awarded. The security team deployed a fix and asked the author to confirm it, and the author was able to bypass it again.

Scenario #2: null byte

  1. A payload with a .php extension was still not allowed.
  2. The payload was renamed from payload.php to payload.php\x00.png. Appending \x00.png to the end bypassed the restriction (null byte).
  3. Right-clicking and choosing view image in new tab triggered the script.
  4. RCE was achieved.

The author adds two notes:

  • In some cases .inc, .phps and .phtml can also be used.
  • When using this, make sure to change the Content-Type accordingly. The author also notes that stored XSS was possible here.

Bounty #2 was awarded. The security team deployed a fix once again and asked the author to confirm it, and the author bypassed it again.

Scenario #3: magic bytes and keyword filter

This time it took a while to find a valid bypass. The team had set up a strict rule to only allow images. The author could not initially figure out how the web app was verifying that the data was indeed an image, but after research found that it was checking the magic bytes of the payload.

Applications sometimes identify file types based on their first signature bytes, and adding or replacing them in a file can trick the application. The author explains that a magic byte is the first few bytes of a file used to recognise a file; it is not visible when the file is opened and a special hex editor is needed to view it.

The author also found that the backend filters and removes certain keywords, for example it removes the term .php. A file can therefore be renamed payload.p.phphp, so that when the filter removes .php the file name becomes payload.php. Since the firewall has been bypassed at this stage, the script will be executed.

The steps were:

  1. The magic bytes of a PNG file:
Text
89 50 4e 47 0d 1a 0a
  1. Appending them to the payload:
Bash
echo "89 50 4e 47 0d 1a 0a" | xxd -p -r >> payload.p.phphp
  1. Uploading the script and getting a full-fledged RCE.

Bounty #3 was awarded. The security team deployed the fix again, and this time the author was not able to find a bypass, so the work stopped there. The author was informed that the team had hardened the validation and verification processes of a file and had implemented a sandbox environment.

Bonus tip

Once the security team has deployed a fix to a reported vulnerability, they will ask the researcher to confirm that the bug has indeed been fixed. The author recommends checking whether a bypass is possible; if it is, it can bring additional bounty.

Timeline

Event Date
Submitted 02-08-2022
Accepted 03-08-2022
Bounty #1 04-08-2022
Bounty #2 06-08-2022
Bounty #3 09-08-2022
Resolved 10-08-2022

Root cause

The report does not include source code, so the following is inferred from the behaviour described.

  • Scenario #1: the extension check rejected .php but accepted a mixed-case variant such as .pHp5. The author also notes that this class of bypass only works when validation is client-side only, which suggests the check was enforced in the browser rather than on the server.
  • Scenario #2: appending \x00.png to payload.php bypassed the restriction, and the script executed when the image was opened. The author's note about changing Content-Type suggests the server also relied on the client-supplied content type.
  • Scenario #3: the server checked the magic bytes to confirm the file was an image, and adding the PNG signature tricked that check. Separately, a keyword filter removed .php from the filename, which could be defeated by nesting the keyword (payload.p.phphp), because removing .php from that string leaves payload.php.

In all three cases the existing defences did not stop the attack.

Impact

In the scenarios described, the author uploaded a script and obtained remote code execution on the server. The author achieved RCE in all three scenarios. In scenario #2 the author also notes that stored XSS was possible.

Fix

It states only that after the third fix the author could not find a bypass, and that the team hardened the validation and verification processes of a file and implemented a sandbox environment.

Variants to test

These are general techniques suggested by the scenarios above; the report does not claim they were tested on this target beyond what is described.

  1. Mixed-case extensions. Try variants such as .pHp5 where a .php blocklist is in place. A positive result is the upload being accepted and the script executing when requested.
  2. Null byte in the filename. Try appending \x00.png after a .php extension, and adjust the Content-Type to match. A positive result is the file being stored and executed as PHP.
  3. Alternative PHP extensions. Try .inc, .phps and .phtml. A positive result is the server executing the file as PHP.
  4. Magic byte prefixing. Prepend the PNG signature 89 50 4e 47 0d 1a 0a to a PHP payload and upload it. A positive result is the upload passing an image check and the script still executing.
  5. Nested keyword filter bypass. Name the file payload.p.phphp so that removing .php leaves payload.php. A positive result is the stored filename ending in .php and the script executing.
  6. Client-side-only validation. Intercept the upload request and change the filename and content type. A positive result is the server accepting the file even though the browser would have rejected it.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 12 min read.

What happened, in plain words

A security researcher named Sagar Sajeev found a feature on a website that let people upload files. That feature was supposed to be safe, but it was not. He found a way to upload a file that the server would later run as a program. When a server runs code that an attacker uploaded, that is called remote code execution, or RCE for short. It is one of the most serious things that can go wrong on a website, because the attacker is no longer just looking at pages: they are running their own instructions on the machine.

The interesting part is what happened next. The website's security team fixed the problem. Sagar checked the fix and got around it. They fixed it again. He got around it again. They fixed it a third time, and he got around it a third time. Each of the three bypasses earned him a separate bounty, which is a reward paid for reporting a security problem responsibly. He describes this as his first four-digit bounty, meaning a reward in the thousands.

This article walks through the same three bypasses, in the same order, but explains every idea from scratch. You only need to know how to use a browser and a computer to follow along.

Image Credit — Wallarm From the original on sagarsajeev.medium.com

The image is credited to Wallarm.

The idea behind the bug: file upload

Almost every website that lets you set a profile picture, attach a document or submit a CV has a file upload feature. Your browser takes a file from your computer and sends it to the server, which stores it somewhere so it can be shown or downloaded later.

The danger is that a server does not only store files. Some servers are configured to run certain kinds of files as programs. The classic example is PHP, a programming language used to build websites. If a server sees a file ending in .php, it may treat the contents of that file as instructions to execute, not as text to display. So if you can upload a .php file and then make the server open it, you have effectively given the server a new program to run, written by you.

That is why upload features usually have a check: they refuse files whose names end in .php. The whole story here is about how those checks were written, and how each one could be slipped past.

A useful everyday comparison: imagine a nightclub with a bouncer holding a list of banned names. If the bouncer only checks the exact spelling on the list, someone whose name is written slightly differently walks straight in. Each fix in this story is a new bouncer with a new rule, and each bypass is a new way of writing the name.

The setup

The report does not name the target website, the company or the exact upload feature. It only says it was a file upload that was eventually restricted to images only.

A few practical details are given:

  • The author works on Linux, an operating system, and uses its built-in hex editor and a command-line tool called xxd to look at and change the raw bytes of a file. Any hex editor would do the same job.
  • One of John Hammond's videos helped with the keyword-filter bypass in the third scenario.

The report does not explain how the upload feature was first found. It starts from the point where a .php file was already known to be rejected, and the work is about getting around each new restriction as it appeared.

Scenario #1: changing the case of the extension

  1. A file with a .php extension was not allowed.
  2. The file was renamed from payload.php to payload.pHp5, mixing upper and lower case in the extension, and that got past the check.
  3. The author notes that this kind of trick only works if the target checks the file in the browser only. Sometimes the file gets past the frontend but no callback arrives, because an intrusion detection system (IDS) or a firewall on the server side blocked it. An IDS is software that watches network traffic for signs of an attack; a firewall is a filter that blocks traffic according to rules.
  4. In this case the callback did arrive and RCE was achieved.

Why does changing the case work? Because computers compare text in two different ways. A case-sensitive comparison treats pHp5 and php5 as different strings, the way a strict bouncer would. A case-insensitive comparison treats them as the same. If the check was written to look for the exact lowercase text .php, then .pHp5 simply does not match, and the file is let through. The server, meanwhile, may still recognise the file as something it can run.

The note about client-side validation matters. Client-side means the check happens in your own browser, before the file is even sent. That is easy to get around, because the browser is under your control: you can intercept the request and change it. Server-side validation happens on the server, where you cannot reach. A check that only exists in the browser is like a bouncer who stands inside your own house.

Bounty #1 was awarded. The security team deployed a fix and asked the author to confirm it. He was able to bypass it again, which leads to the second scenario.

Scenario #2: the null byte

  1. A file with a .php extension was still not allowed.
  2. The file was renamed from payload.php to payload.php\x00.png. Appending \x00.png to the end got past the restriction. This is called a null byte.
  3. Right-clicking and choosing view image in new tab triggered the script.
  4. RCE was achieved.

What is a null byte? In computing, text is stored as a sequence of numbers, one per character. The value zero, written \x00, is used as a marker meaning "the string ends here". Many older programs, especially those written in the C language, stop reading a piece of text as soon as they hit that zero. So the same filename can look like two different things depending on who is reading it:

  • A check that stops at the null byte sees only the part before it.
  • A check that reads the whole string sees payload.php\x00.png and thinks the file is a PNG image, because it ends in .png.

The mismatch between those two readings is the bug. The file passes the check under one interpretation and is stored or executed under another. The everyday comparison is a form where someone writes two answers in one box: the clerk reads the first line, the computer reads the second.

The author adds two notes:

  • In some cases .inc, .phps and .phtml can also be used. These are other file extensions that some servers also treat as PHP code.
  • When using this, make sure to change the Content-Type accordingly. Content-Type is a label that travels with a file or a web request saying what kind of data it is, for example image/png for a picture. If the server trusts that label, you have to set it to match the disguise. The author also notes that stored XSS was possible here. Stored XSS means a script is saved on the server and later runs in the browser of anyone who views the page.

Bounty #2 was awarded. The security team deployed a fix once again and asked the author to confirm it, and he bypassed it again.

Scenario #3: magic bytes and a keyword filter

This time it took a while to find a valid bypass. The team had set up a strict rule to only allow images. The author could not initially work out how the web app was checking that the data really was an image, but after research he found that it was checking the magic bytes of the file.

Magic bytes are the first few bytes at the start of a file that identify its type. A PNG image, for example, always begins with a particular sequence. Applications sometimes decide what a file is by looking only at those first bytes, and adding or replacing them can trick the application. The author explains that a magic byte is the first few bytes of a file used to recognise it; it is not visible when you open the file, and you need a special hex editor to see it. A hex editor is a program that shows a file as raw numbers instead of as text or a picture.

The author also found that the backend filters out and removes certain keywords, for example the term .php. So a file can be named payload.p.phphp: when the filter removes .php from that name, what is left is payload.php. Since the firewall has been bypassed at this stage, the script will be executed.

This is the same trick as a word filter in a chat app that blanks out a rude word: if you write the word with an extra piece inside it, the filter removes the inner part and the original word appears. The filter does the attacker's work for them.

The steps were:

  1. The magic bytes of a PNG file:
Text
89 50 4e 47 0d 1a 0a

This line is the signature of a PNG image, written as hexadecimal numbers. Hexadecimal is just a way of writing numbers using sixteen symbols instead of ten, and it is the usual way to show raw bytes. Each pair of characters is one byte. These seven bytes at the start of a file are what makes an image checker say "this is a PNG".

  1. Prepending them to the payload:
Bash
echo "89 50 4e 47 0d 1a 0a" | xxd -p -r >> payload.p.phphp

Line by line:

  • echo "89 50 4e 47 0d 1a 0a" prints that text to the screen, or in this case passes it along to the next command.
  • The | symbol is a pipe: it takes the output of the command on the left and feeds it as input to the command on the right.
  • xxd -p -r is the tool that converts between hexadecimal text and raw bytes. The -p option means the input is plain hexadecimal, and -r means reverse, so it turns the hex text back into actual bytes.
  • >> means append: it adds the result to the end of the named file instead of overwriting it.
  • payload.p.phphp is the file being written to. Its name is chosen so that the keyword filter, which removes .php, will turn it into payload.php.

So the command takes the PNG signature, turns it into real bytes, and appends them to the malicious file. To an image checker that only looks at the first bytes, the file now looks like a PNG. To the server, once the name has been filtered, it is still a PHP script.

  1. Uploading the script and getting a full-fledged RCE.

Bounty #3 was awarded. The security team deployed the fix again, and this time the author could not find a bypass, so the work stopped there. He was informed that the team had hardened the validation and verification processes for files and had implemented a sandbox environment. A sandbox is an isolated space where code can run without being able to touch the rest of the system, like a playpen.

Bonus tip

Once the security team has deployed a fix to a reported vulnerability, they will ask the researcher to confirm that the bug really is fixed. The author recommends checking whether a bypass is possible; if it is, it can bring an additional bounty. This is how the three bypasses in this story came about: each fix was an invitation to look again.

Timeline

Event Date
Submitted 02-08-2022
Accepted 03-08-2022
Bounty #1 04-08-2022
Bounty #2 06-08-2022
Bounty #3 09-08-2022
Resolved 10-08-2022

Why each fix failed

The report does not include source code, so the following is inferred from the behaviour described.

  • Scenario #1: the extension check rejected .php but accepted a mixed-case variant such as .pHp5. The author also notes that this kind of bypass only works when validation is client-side only, which suggests the check was enforced in the browser rather than on the server.
  • Scenario #2: appending \x00.png to payload.php bypassed the restriction, and the script executed when the image was opened. The note about changing Content-Type suggests the server also relied on the content type supplied by the client.
  • Scenario #3: the server checked the magic bytes to confirm the file was an image, and adding the PNG signature tricked that check. Separately, a keyword filter removed .php from the filename, which could be defeated by nesting the keyword (payload.p.phphp), because removing .php from that string leaves payload.php.

In all three cases the existing defences did not stop the attack.

Impact

In the scenarios described, the author uploaded a script and obtained remote code execution on the server. The author achieved RCE in all three scenarios. In scenario #2 he also notes that stored XSS was possible.

Fix

It states only that after the third fix the author could not find a bypass, and that the team hardened the validation and verification processes for files and implemented a sandbox environment.

Variants to test

These are general techniques suggested by the scenarios above; the report does not claim they were tested on this target beyond what is described.

  1. Mixed-case extensions. Try variants such as .pHp5 where a .php blocklist is in place. A positive result is the upload being accepted and the script executing when requested.
  2. Null byte in the filename. Try appending \x00.png after a .php extension, and adjust the Content-Type to match. A positive result is the file being stored and executed as PHP.
  3. Alternative PHP extensions. Try .inc, .phps and .phtml. A positive result is the server executing the file as PHP.
  4. Magic byte prefixing. Prepend the PNG signature 89 50 4e 47 0d 1a 0a to a PHP payload and upload it. A positive result is the upload passing an image check and the script still executing.
  5. Nested keyword filter bypass. Name the file payload.p.phphp so that removing .php leaves payload.php. A positive result is the stored filename ending in .php and the script executing.
  6. Client-side-only validation. Intercept the upload request and change the filename and content type. A positive result is the server accepting the file even though the browser would have rejected it.

Glossary

  • Bounty: a reward paid by a company to someone who reports a security problem in their product responsibly.
  • Callback: a signal sent back to the attacker when their uploaded code runs, confirming that it executed.
  • Client-side validation: a check performed in the user's own browser before data is sent to the server.
  • Content-Type: a label attached to a file or web request saying what kind of data it is, such as image/png.
  • Firewall: a filter that blocks network traffic according to rules.
  • Hex editor: a program that shows and lets you edit a file as raw bytes rather than as text or an image.
  • Hexadecimal: a way of writing numbers using sixteen symbols, commonly used to display raw bytes.
  • IDS (intrusion detection system): software that watches network traffic for signs of an attack.
  • Magic bytes: the first few bytes of a file that identify its type.
  • Null byte: the byte with value zero, written \x00, often used as a marker that a string ends.
  • Pipe (|): a shell symbol that feeds the output of one command into another.
  • RCE (remote code execution): a situation where an attacker can make a server run their own code.
  • Sandbox: an isolated environment where code can run without affecting the rest of the system.
  • Server-side validation: a check performed on the server, where the user cannot change it.
  • Stored XSS: a script that is saved on a server and later runs in the browser of anyone who views the page.
  • xxd: a command-line tool that converts between hexadecimal text and raw bytes.

More by Sagar Sajeev

View all

7 more disclosed reports, best first.

Similar reports

View all

Same bug class and techniques, clearest first.

Unrestricted file upload

The lesson behind this report.

An upload feature accepts a file that the server or other users' browsers then treat as code or as something trusted.