Guide

Detect file type spoofing: extension, MIME type and magic bytes

A file can pretend to be a different type than it is. Extension and MIME type are easy to change, while the first bytes show the actual format. ScanBeforeOpen compares both locally. A consistent type is still no safety guarantee.

Responsible operator: Tom Silas Helmke, operator of ScanBeforeOpen. Content revised: .

Open scanner

Four clues to the file type

The extension is only a label in the name. The MIME type is a statement by the source or browser and is often derived from the extension. The magic bytes are the first bytes of the file, for example 25 50 44 46 (%PDF) for PDF, 50 4B for ZIP, 89 50 4E 47 for PNG, FF D8 FF for JPEG and 4D 5A (MZ) for Windows programs. The fourth level is the inner structure, such as the XML parts of an Office package.

A .docx correctly carries the ZIP signature, because it is a ZIP package. That is not spoofing. It becomes suspicious when the signature belongs to a different format than the extension claims.

File extension, MIME type and file signature compared
usually derivedno matchFile nameinvoice.docx.pdfExtension (last part).pdf, just a labelMIME type (claim)application/pdfMagic bytes (actual)89 50 4E 47 = PNG, conflictContent structureHeader, chunks, ZIP parts

The sequence in words

  1. The file name is freely chosen. With double extensions such as .docx.pdf the last part counts. Some interfaces hide extensions.
  2. Operating system and browser usually derive the file type from it. The extension is only a label and does not check the content.
  3. The MIME type is a statement by the source or browser, often derived from the extension. It does not prove the content.
  4. The first bytes reveal the actual format. Here they read 89 50 4E 47, the signature of a PNG file, although .pdf is claimed. That is a conflict.
  5. Even when extension, MIME type and signature agree, the content can be manipulated or faulty. A recognized file type is not a safety clearance.

Typical spoofing patterns

With double extensions such as invoice.pdf.exe the last part counts. Some views show only part of the name, and depending on settings Windows hides known extensions. The name can then look harmless. A file can also switch type, such as an image with the extension .pdf. Unicode control characters in the name can reverse the order of characters.

Add polyglots, meaning files that are valid in two formats, and archives with a program inside. The behavior depends on the program that opens the file.

Why a recognized type is no clearance

The signature check only says which format the first bytes claim. A genuine PDF can contain script actions, a valid Office package can contain macros or external links. A program stays a program when extension and signature agree.

Browsers can reinterpret content by its bytes when servers give no clear information. Site operators therefore set the header X-Content-Type-Options nosniff. For uploads, OWASP recommends allowing extensions by an allow list, not trusting the Content-Type header, checking signatures and storing files under their own names.

How to check a file

Make your system show file extensions in full and read the whole name. Choose the file in the scanner. A finding about extension and actual file type, about multiple extensions or about a risky extension is a reason not to open the file and to ask the sender. The example file invoice.docx.pdf contains a PNG graphic.

The scanner compares the extension only for known types (PDF, ZIP, Office packages, PNG, JPEG, GIF, WebP, exe and dll). For other extensions such as .txt there is no comparison. It shows the MIME type but does not judge it against the signature.

Reproducible synthetic example

The file invoice.docx.pdf is actually a small PNG graphic. file.signature.mismatch reports the conflict between the .pdf extension and the PNG signature, name.extensions the double extension. It is harmless but shows the pattern.

The findings come from an actual local scan of this test file. Download it and choose it in the scanner. No real personal data or malware.

Download test file: invoice.docx.pdf

Example inspection coverage: partial.

Practical checklist

  • Make the system show file extensions in full
  • Read the whole file name, not only the beginning
  • Do not open a file when extension and signature conflict
  • Never open an unexpected .exe, .scr or .js
  • Confirm the source through an independent channel
  • Do not treat a recognized file type as clearance

Clear limits

The scanner reads the first bytes and compares them with known extensions. It neither checks every format nor the full structure, and the MIME type is not judged against the content. A recognized type is not a safety clearance.

FAQ

Why does my .docx have the ZIP signature?

Office files are ZIP packages. The signature PK is correct for docx, xlsx and pptx and is no sign of spoofing.

Is it enough when extension and signature agree?

No. The type says nothing about the content. A genuine PDF or Office package can also hold active or external content.

Does ScanBeforeOpen detect every wrong file type?

No. The comparison covers only a list of known formats. Other extensions and deliberately built polyglots remain incompletely assessed.

Related reading

Technical sources