Skip to content
XSS Write-up Advanced

Exploiting Xss With Javascript Jpeg Polyglot

A JPEG file can be crafted to also contain JavaScript, and when included as a script with a specific charset, the browser executes the embedded code. An attacker could use this to bypass file upload filters that only check for image extensions and signatures, leading to cross-site scripting.

Medusa Follow

Published on infosecwriteups.com

Just like PNG, JPEG, and DOC are valid file types, polyglots are a combination of two different file types. For example Phar + JPEG (PHP archive and JPEG file), GIFAR (Gif and Rar file) Javascript + JPEG, etc.

Medusa, on infosecwriteups.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 application allowed uploading files that passed image checks but also contained executable JavaScript, and the browser executed the file when included as a script with a non-UTF-8 charset.

What the attacker needed

Ability to upload a file; victim uses a vulnerable browser; script tag with ISO-8859-1 charset

How it was found

  1. Take a JPEG image and open it in a hex editor.
  2. Replace the length bytes of the JPEG header with the start of a JavaScript comment.
  3. Insert the JavaScript payload after the comment start.
  4. Pad the remaining space with null bytes to match the new length.
  5. Close the JavaScript comment before the end-of-image marker.
  6. Include the modified image as a script tag with ISO-8859-1 charset to execute the code.

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 polyglot is a file that is valid in two formats at once, like an image that is also a script. Here, a JPEG image was modified so that its bytes also form JavaScript code. When a web page loads that file as a script, the browser runs the hidden code, which can be used to attack users.

What is XSS?

Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.

More by Medusa

View all

10 more disclosed reports, best first.

Similar reports

View all

Same bug class and techniques, clearest first.

Cross-site scripting (XSS)

The lesson behind this report.

Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.