Course contentsShow
← Security
Lesson 975 of 3,05220. Web Security - File Upload and ProcessingFree lesson

Polyglot Files and Format Confusion

Examine files that are valid in multiple formats and exploitation of content-type confusion.

Polyglot Files and Format Confusion

What you'll learn: How attackers craft files that are valid in multiple formats simultaneously to bypass security checks.

What Are Polyglot Files?

A polyglot file is a specially crafted file that is valid and executable in multiple formats at the same time. Think of it like a clever riddle that makes sense in two different languages—one person reads it as English, another as Spanish, and both interpretations work perfectly.

For example, an attacker might create a file that:

  • Appears as a valid GIF image to an upload filter checking magic bytes
  • Contains executable PHP code that runs when accessed directly
  • Embeds malicious JavaScript that executes if parsed as HTML

How Attackers Exploit Format Confusion

Security systems often check files at different stages:

  1. Upload validation checks the file extension and magic bytes
  2. Storage system saves the file based on extension
  3. Web server serves the file based on MIME type or extension

Attackers exploit inconsistencies between these stages. A polyglot passes the first check (looks like an image) but executes as code when the web server interprets it differently.

Common Polyglot Scenarios

  • GIF + PHP: A file starting with GIF89a (valid GIF header) followed by <?php system($_GET['cmd']); ?> (PHP code)
  • JPEG + JavaScript: Image data containing embedded script tags that execute if served with wrong content-type
  • PDF + HTML: A file that renders as PDF in viewers but executes JavaScript if opened in browsers

The danger arises from format confusion—different systems interpreting the same file differently based on their validation rules, content-type headers, or file extensions.

Key Takeaway: Polyglot files exploit inconsistent validation across systems by being valid in multiple formats simultaneously. Defense requires consistent content validation, proper content-type headers, and never trusting file extensions alone.