Skip to content
File upload Write-up Intermediate

Remote Code Execution via Polyglot Web Shell Upload

An upload filter that checked file content for a valid image could still be bypassed by uploading a polyglot file that is both a valid PNG and a valid PHP script. An attacker could then execute PHP code on the server and read a secret file.

Kaustubh Asthana Follow

Published on medium.com

Every upload filter in this category so far has been reading the filename and nothing else. Rename the extension, slip in a null byte, and the server never actually looked hard enough to notice.

Kaustubh Asthana, on 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.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the upload validation only confirmed the file was a valid image but did not ensure it could not also be interpreted as executable code, and the server executed files based on their extension.

Impact

An attacker could execute arbitrary PHP code on the server and read sensitive files, such as another user's secret token.

What the attacker needed

A user account; ability to upload an avatar; server executes .php files

The fix

The server should not execute uploaded files as code; it should store them outside the web root or serve them with a content type that prevents execution, and validate that the file is exclusively an image.

How it was found

  1. Log in and upload a plain PNG to confirm uploads work.
  2. Try uploading a PHP file directly and observe the content-based rejection.
  3. Try filename tricks (null byte, double extension) and see they also fail.
  4. Use exiftool to embed PHP code in the Comment metadata of a valid PNG, outputting to a .php file.
  5. Upload the polyglot file, which passes the image check.
  6. Fetch the uploaded file to execute the PHP and read the secret between START and END markers.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

A website let people upload profile pictures. It checked that the file really was an image, not just that the name ended in .png. But the attacker made a file that was a real image and also contained hidden computer code. When the server ran that file, the hidden code executed and revealed a secret.

What is File upload?

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

More by Kaustubh Asthana

View all

6 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.