Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 37 ยท Static Analysis

Identifying File Types and Magic Bytes

Learn how magic bytes reveal a file's real type, why extensions lie, how Office, PDF, archive and script files differ, and how to write a simple file-type checker.

A file's name is just a label that anyone can change. A file called holiday.jpg may be a Windows program; a file with no extension may be a PDF. Before analyzing anything, you must learn its real type, because the type decides which tools and questions apply: you inspect an executable's imports, but you inspect a Word document's macros.

The most reliable quick check is to read the first few bytes. Many formats begin with a fixed signature called a magic number or magic bytes.

Common signatures

Format Magic bytes (hex) As text
Windows executable / DLL (PE) 4D 5A MZ
Linux executable (ELF) 7F 45 4C 46 .ELF
ZIP (also DOCX, XLSX, JAR, APK) 50 4B 03 04 PK..
PDF 25 50 44 46 %PDF
Old Office (DOC, XLS) / OLE2 D0 CF 11 E0 A1 B1 1A E1 not printable
RAR 52 61 72 21 1A 07 Rar!..
7-Zip 37 7A BC AF 27 1C 7z....
GZIP 1F 8B not printable
PNG 89 50 4E 47 0D 0A 1A 0A .PNG....
JPEG FF D8 FF not printable
GIF 47 49 46 38 GIF8
Windows shortcut (LNK) 4C 00 00 00 01 14 02 00 L.......
Mach-O (macOS) CF FA ED FE not printable
Java class CA FE BA BE not printable

Scripts (PowerShell, JavaScript, batch, Python) have no magic bytes; you recognize them by content, such as a #! line, keywords or encoding. The command file on Linux and the Python library python-magic do this with a large signature database.

Extensions lie

Attackers deliberately mismatch the extension and the content. Typical tricks:

  • Wrong extension: a program named photo.jpg.
  • Double extension: invoice.pdf.exe (see Handling Samples Safely).
  • Right-to-left override: a hidden Unicode control character (U+202E) that reverses the display order of the characters after it, so a file whose real name ends in .exe can be shown with what looks like a document extension. Look for control characters when file names look odd.
  • Container files: a ZIP or ISO that wraps an executable, because the outer file may pass email filters.

The check is simple: compare the claimed type (from the extension) with the detected type (from the bytes).

Python A magic-byte file identifier

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

holiday.jpg is flagged because its first two bytes are MZ, the signature of a Windows executable. In a real investigation this single check would turn an ordinary-looking picture into an urgent case.

Peeking at raw bytes: a hex dump

A hex dump shows each byte as two hex digits with the matching printable characters on the right. It is how you read a file's start by eye. Writing your own dump function is a good way to learn the format.

Python Write a hex dump

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The text "This program cannot be run in DOS mode." appears in almost every Windows executable: it is part of a tiny DOS stub left over for compatibility. Seeing it confirms you are looking at a PE file.

Documents that hide code

Types that are not programs can still be dangerous:

  • Office documents may hold macros (VBA code). Old formats (OLE2) and new macro-enabled formats (.docm, .xlsm) are common delivery vehicles.
  • PDFs can contain JavaScript, embedded files and links.
  • ZIP/ISO/IMG containers hide executables from filters and strip the "downloaded from the internet" mark on some systems.
  • LNK shortcuts can run a command line when opened.
  • Scripts (JS, VBS, PowerShell, batch, HTA) run directly through built-in interpreters.

A .docx is a ZIP archive; renaming it to .zip shows its XML parts. A .docx file with a vbaProject.bin inside is unusual, because plain .docx files cannot carry macros. The next example lists a fake archive's members and flags this pattern.

Python Check an Office file's parts

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Common mistakes

  • Trusting the extension or the icon.
  • Assuming a ZIP is just an archive: look inside (carefully, in the lab).
  • Forgetting that a file can be two things (polyglots), such as valid as both an image and a script.
  • Opening a document in its normal application just to see what it is.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which two bytes mark the beginning of a Windows PE executable?

    PE files begin with MZ (the initials of Mark Zbikowski), a legacy of the DOS executable format.
  2. 2. A file named photo.jpg starts with 4D 5A. What should you conclude?

    The content decides the type. A JPEG begins with FF D8 FF; MZ means a Windows program.
  3. 3. Why is a .docx that contains vbaProject.bin notable?

    Macro-enabled documents use .docm. A docx with a VBA project deserves a closer look.
  4. 4. Which kind of file has no magic bytes and is recognised by its content?

    Scripts are plain text, so you identify them by their syntax, shebang line or keywords.

Summary

  • Magic bytes at the start of a file reveal its true type; MZ is a Windows executable, PK a ZIP or Office file, %PDF a PDF.
  • Compare the extension with the detected type to catch disguises.
  • Scripts have no signature; documents and containers can hide code.
  • A hex dump lets you read the first bytes by eye.
  • Identify before you analyze: the type chooses your tools.

Next: Extracting Strings