Binary File CarvingRutvij Khatri•9 min read•Published 2026-10-14

Polyglot Files: When an Image is Simultaneously a Valid Executable or Script

Understand how cyber adversaries craft polyglot files that function simultaneously as valid JPEGs/PNGs and executable Java JARs, HTML scripts, or PDFs.

Executive Verdict & Direct Answer
✓ 100% Client-Side In-Browser Engine

Understand how cyber adversaries craft polyglot files that function simultaneously as valid JPEGs/PNGs and executable Java JARs, HTML scripts, or PDFs. Polyglots satisfy multiple file format parsers simultaneously, allowing malicious executables to bypass restrictive image upload validators.

The Concept of Binary Polyglots

A "polyglot" in computer security is a single file that can be parsed as two or more completely different file formats simultaneously, with each parser treating the file as 100% valid according to its specification.

For example, a JPEG-JAR polyglot functions as a valid JPEG image when loaded in a web browser, but when executed by the Java Virtual Machine (`java -jar image.jpg`), it executes malicious Java bytecode.

Polyglots exploit architectural differences in how file parsers validate headers. Image parsers validate magic bytes at offset 0 (like `FF D8` for JPEG or `89 50 4E 47` for PNG), while archive parsers (like ZIP/JAR) scan backward from the end of the file for the central directory record.

Key Investigative Takeaway

Polyglots satisfy multiple file format parsers simultaneously, allowing malicious executables to bypass restrictive image upload validators.

The Anatomy of a GIFAR and HTML-Image Polyglot

1. GIFAR (GIF + JAR): Pioneered in 2008, combining a GIF header with a Java Archive. The GIF parser reads the image dimensions and terminates; Java reads the embedded manifest and executes code.

2. Image-HTML/SVG XSS Polyglot: Embedding `<script>` tags inside image comments or EXIF fields. If a web application serves the image with a `Content-Type: text/html` header, the browser executes the embedded script.

Detecting and Disarming Polyglots with OverlayCheck

OverlayCheck parses complete file structures from both offset 0 and reverse offsets, detecting secondary valid file headers embedded within image bodies and warning security auditors of potential polyglot risks.

Polyglot Defense: File Verification Approaches

Evaluating protection against polyglot file execution attacks.

Feature / DimensionOverlayCheck (Client-Side)MIME-Type & Extension CheckingTrue Content Re-Encoding (Canvas/WASM)
Polyglot Attack Neutralization
100% Neutralized (Re-encodes pixel raster)
Fails Completely (Polyglot passes MIME checks)Neutralized (Strips non-pixel payloads)

Command-Line Automation & CLI Workflows

For security engineers and forensic examiners automating pipelines or conducting terminal audits, the equivalent low-level commands can be executed natively:

Detect Multiple File Signatures via File Command
bash
file -k suspect_polyglot.jpg
Technical Breakdown: Uses the -k flag in the Linux file utility to continue searching for secondary magic bytes throughout the file rather than stopping at the first match.

Frequently Asked Technical Questions

Q1:How do web applications defend against polyglot uploads?

The definitive defense is re-encoding uploaded images through a graphics library (like Canvas or libvips), which strips extraneous binary data and writes a clean, single-format image.

#polyglot files security#image is executable polyglot#gifar vulnerability#pdf polyglot image#bypass file upload polyglot
RK
Rutvij KhatriSecurity Analyst & Aspiring Security Engineer

Security Analyst and Aspiring Security Engineer passionate about client-side media verification, binary parsing, web application security auditing, CTF steganography challenges, and zero-knowledge privacy architecture.

Connect on LinkedIn

Companion Verification & Privacy Toolkits

Cross-Cluster Workflows

Related Deep Dives in Binary File Carving