Magic Bytes: How File Type Detection Really Works

Why filenames can lie, and how the first few bytes of a file tell the truth.

The filename extension is just a convention — anybody can rename photo.png to photo.exe and the bytes don't change. The OS picks the application based on the extension; if you want to know what a file actually is, you have to look inside.

What are magic bytes?

A magic-byte signature is a short, fixed sequence of bytes at the start of a file (and sometimes at fixed offsets within it) that uniquely identifies the format. They're called "magic" because they have no meaning on their own — they exist purely to label the file.

Common signatures

FormatMagic bytes (hex)ASCII
JPEGFF D8 FF—
PNG89 50 4E 47.PNG
GIF47 49 46 38GIF8
PDF25 50 44 46%PDF
ZIP50 4B 03 04PK..
RAR52 61 72 21Rar!
ELF7F 45 4C 46.ELF
Windows EXE4D 5AMZ

When signatures collide

Not every detection is unambiguous. PK.. identifies a ZIP archive — but DOCX, XLSX, PPTX, JAR, EPUB, ODT, APK, and many other formats are also ZIPs internally. To tell them apart, a detector reads further into the archive's table of contents and looks for marker files (e.g., [Content_Types].xml for DOCX).

Why this matters for security

Attackers send files like invoice.pdf.exe. Default Windows settings hide the final .exe, so a target sees "invoice.pdf" and double-clicks. Magic-byte detection catches this even before the file is opened, by reporting the actual format. Combined with double-extension warnings, it's a useful first line of defense.

Try it yourself

  • The File Identity Checker reads the magic bytes and reports the format with a confidence score.
  • The Extension Validator cross-checks the extension against the detected type and flags double extensions.
  • The Safe File Preview has a Hex view that shows the raw bytes, so you can see the signature with your own eyes.