Behind the link

What are magic bytes?

Definition

Magic bytes are the first few bytes of a file, which reveal its real format whatever its name says. A PDF starts with %PDF, for example, so checking them catches a program renamed to look like a document.

Also called: File signature, Magic number, File header

How do magic bytes work?

Magic bytes work because most file formats begin with a fixed pattern that the format requires. Programs that read the format look for that pattern, so it is there in every valid file. A PNG image always opens with the same eight bytes, and a ZIP archive opens with the letters PK, the initials of Phil Katz, who created the format.

Because the pattern sits inside the file, renaming the file does not change it. That makes magic bytes a more honest signal than the file extension or the MIME type, which are labels anyone can edit. The file command on Mac and Linux identifies files this way.

Why do file hosts check magic bytes?

File hosts check magic bytes to catch files that pretend to be something else. The classic case is a program renamed to look harmless, such as setup.exe saved as setup.pdf. Windows programs start with the letters MZ, so a host that reads the first bytes sees through the new name. A magic-byte check helps a host:

  • Refuse programs and scripts, even when they are renamed.
  • Make sure a file really opens in the viewer it is sent to.
  • Stop broken files, such as an image whose header is missing or damaged.

What can a magic-byte check not catch?

A magic-byte check confirms the format, not whether the contents are safe. A real PDF can still hold a phishing link, and a real image can still show something that breaks the rules. That is why hosts follow it with content moderation.

Some formats also share a signature. Word, Excel and PowerPoint files (.docx, .xlsx and .pptx) are ZIP archives inside, so they all start with PK. Plain text formats such as HTML, CSV and code have no magic bytes at all, so hosts check them in other ways.

Magic bytes of common file types
File typeStarts with (hex)Readable part
PDF25 50 44 46%PDF
PNG image89 50 4E 47PNG
JPEG imageFF D8 FFNone
ZIP, DOCX, XLSX, PPTX50 4B 03 04PK
Windows program (.exe)4D 5AMZ

Magic bytes: common questions

Are magic bytes the same as a file extension?

No. The extension is part of the file name and anyone can change it. Magic bytes are inside the file and only change if the file itself changes.

How can I see a file's magic bytes?

Open the file in a hex viewer and read the start of the first row. On Mac or Linux, the file command in the terminal reads them for you and names the format.

Do all files have magic bytes?

No. Plain text formats such as .txt, .csv, .html and code files have no fixed start, so tools identify them by their contents and name instead.

Why is a .docx file detected as a ZIP?

Because it is one. Modern Office files are ZIP archives of XML files, so they start with the same PK bytes, and a checker has to look inside to tell them apart.

Is a magic-byte check enough to make a file safe?

No. It proves the format, not the contents. A real PDF or image can still be harmful, so hosts run content checks as well.

Go deeper

Updated

Share a file as a link in seconds

Upload any file and get one clean link, with every setting on this page.

Try Link in Seconds

Optional cookies for analytics, ads and session replay.