Skip to content
File upload Write-up Intermediate

How I Bypassed File Upload Restrictions Using a phar Polyglot

The file upload feature blocked .php files but allowed .phar files and only checked magic bytes, so an attacker could upload a file containing PHP code with a GIF header and get it executed by the server. This led to remote code execution and full server compromise.

medium.com

Published on medium.com

During my security testing, I identified a vulnerability in a file upload functionality that could potentially lead to arbitrary file upload and server-side code execution.

0xKemzx, 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 checked the file extension against a blocklist and trusted magic bytes, without verifying the file's actual content or preventing execution of uploaded files.

Impact

An attacker could execute arbitrary system commands, leading to remote code execution, full server compromise, data exfiltration, and privilege escalation.

What the attacker needed

Access to file upload feature; server allows .phar extension; uploaded files are executable

The fix

The upload validation should check file content properly and prevent execution of uploaded files, for example by storing them outside the web root or disabling script execution in the upload directory.

How it was found

  1. Go to the file upload feature.
  2. Try to upload a .php file and see it rejected.
  3. Change the file extension to .phar.
  4. Add valid magic bytes to the file to pass the type check.
  5. Upload the file and access it via its public path with a command parameter.

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 files but tried to stop dangerous ones by blocking only the .php ending. The attacker simply used a different ending (.phar) and put a fake image header at the start so the site thought it was a picture. Because the server still ran the hidden code, the attacker could run commands on the server.

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.

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.