TExcelTAD
Home › Malware Analysis, Digital Forensics & Incident Response › Triage First: File Types and Magic Numbers

Triage First: File Types and Magic Numbers

The ticket says "PDF attachment". The file is called statement.pdf. Before you open it in a PDF tool, or load it into a disassembler, ask the cheapest question of all: what is this file, really? Extensions are chosen by whoever named the file, and attackers choose them to mislead: statement.pdf.exe, a .doc that is really an RTF, an .iso or .lnk wrapped in a ZIP, an .html attachment that rebuilds a file inside the browser. File type identification from content is the first step of every triage, because it decides which tools come next.

Definitions

  • Magic number (file signature): fixed bytes at a known offset, usually 0, that a format starts with. MZ (4D 5A) for DOS/Windows executables, 7F 45 4C 46 (\x7fELF) for ELF, %PDF- for PDF, PK\x03\x04 for ZIP and every format built on it (DOCX, XLSX, JAR, APK), D0 CF 11 E0 A1 B1 1A E1 for the old OLE2 Office format, 89 50 4E 47 for PNG, 1F 8B for gzip, {\rtf for RTF, #! for scripts.
  • file: the Unix tool that tests a file against a database of magic patterns (/usr/share/misc/magic) and prints a description; --mime-type gives a MIME type instead.
  • Container format: a format that holds other files (ZIP, ISO, OLE2, PDF with attachments). Its type says little; its contents matter.
  • Polyglot: a file that is valid in two formats at once (a file that is both a ZIP and a PDF, for example), built because different parsers read from different ends.
  • TrID, DIE (Detect It Easy): identification tools with larger signature sets, which also recognise compilers, packers and installers.

Identifying lab files by their bytes

The session creates a small set of synthetic files whose names lie, the way delivered attachments often do, and lets file and the first bytes speak. All files are harmless: the "executable" is two magic bytes and padding, the "document" is a ZIP containing a text file.

bash · try it yourself
python3 - <<'PY'
import zipfile, gzip, io
open("statement.pdf", "wb").write(b"MZ" + bytes(62) + b"This program cannot be run in DOS mode.\r\n")
with zipfile.ZipFile("report.docx", "w") as z:
    z.writestr("readme.txt", "lab sample: not a real Word document")
open("photo.jpg", "wb").write(b"%PDF-1.7\n1 0 obj << /Type /Catalog >> endobj\n%%EOF\n")
open("invoice.txt", "wb").write(b"#!/bin/sh\necho lab-only script\n")
open("notes.png", "wb").write(gzip.compress(b"lab notes " * 20, mtime=0))
open("plain.txt", "wb").write("Meeting notes, café at 10:00\n".encode("utf-16"))
PY
for f in statement.pdf report.docx photo.jpg invoice.txt notes.png plain.txt; do
  printf '%-14s %-30s %s\n' "$f" "$(file -b --mime-type "$f")" "$(xxd -p -l 8 "$f")"
done
echo "---"
file statement.pdf report.docx photo.jpg plain.txt
echo "--- inside the 'document'"
python3 -c "import zipfile; print(zipfile.ZipFile('report.docx').namelist())"
statement.pdf  application/x-dosexec          4d5a000000000000
report.docx    application/zip                504b030414000000
photo.jpg      application/pdf                255044462d312e37
invoice.txt    text/x-shellscript             23212f62696e2f73
notes.png      application/gzip               1f8b080000000000
plain.txt      text/plain                     fffe4d0065006500
---
statement.pdf: MS-DOS executable, MZ for MS-DOS
report.docx:   Zip archive data, at least v2.0 to extract, compression method=store
photo.jpg:     PDF document, version 1.7
plain.txt:     Unicode text, UTF-16, little-endian text
--- inside the 'document'
['readme.txt']

Every name lies except the last one, and even there the content is UTF-16 with a byte-order mark (ff fe), which matters for strings. The .docx is a ZIP, as a real DOCX would be, but a real one contains [Content_Types].xml and a word/ folder; this one contains a text file, so "ZIP whose content doesn't match its claimed Office type" is itself a finding.

A short table of formats analysts meet

Signature (hex / text) Format What to do next
4D 5A (MZ) + PE\0\0 at e_lfanew Windows PE executable or DLL PE triage
7F 45 4C 46 ELF (Linux) readelf, ELF headers
50 4B 03 04 (PK) ZIP, Office Open XML, JAR, APK list contents; Office: look for macros (vbaProject.bin)
D0 CF 11 E0 A1 B1 1A E1 OLE2 (old .doc/.xls, .msi) oledump.py, olevba (REMnux) for macros
25 50 44 46 (%PDF) PDF pdfid, pdf-parser for /JavaScript, /OpenAction, /EmbeddedFile
7B 5C 72 74 66 ({\rtf) RTF rtfobj for embedded objects
4C 00 00 00 Windows shortcut (.lnk) LECmd or lnkparse for the target command line
43 44 30 30 31 at offset 0x8001 ISO 9660 image list contents; ISO and IMG were popular because files inside escaped the "downloaded from the Internet" marking (Microsoft fixed that in November 2022)
23 21 (#!) script read it: it is text

REMnux includes most of the tools in the last column.

How type mismatches are detected

  • Mail and web gateways check the real type against the extension and the declared MIME type, and block executables, scripts and containers by real type, not by name.
  • Endpoint rules: a process whose image is not a PE but was started (a script host running a .txt), or Office writing a file whose magic says executable.
  • YARA rules test the magic first (uint16(0) == 0x5A4D), which also makes them fast (YARA basics).

How it is prevented

  • Show file extensions on Windows (Explorer hides known extensions by default) and block double extensions at the mail gateway.
  • Block by real type the formats that ordinary users never need by mail: executables, scripts, .lnk, .iso/.img, .hta, password-protected archives from outside.
  • Keep the Mark of the Web (the Zone.Identifier stream on files from the Internet) working: it is what makes Office block macros in downloaded documents by default.

Pitfalls

  • file is a guess. It reports the first pattern that matches; a polyglot or a file with junk prepended can fool it. Confirm with a parser for the format.
  • Containers hide the real payload. "Zip archive" is the start of the analysis, not the end; extract (inside the lab) and identify every member.
  • Text encodings: UTF-16 text looks like binary to naive tools.
  • Overlays and appended data: a valid file can carry extra bytes after its end (overlays).

Practice

1. A file starts with 50 4B 03 04 and is named invoice.xlsx. What is your next step?

List the archive members (inside the lab). A real XLSX has [Content_Types].xml and xl/… parts; look for vbaProject.bin (macros, which an .xlsx should not have), embedded objects, and members whose own type is suspicious.

2. Why does the first line of the session print ff fe for plain.txt?

Python's "utf-16" codec writes a byte-order mark: FF FE means UTF-16 little-endian. The text then follows two bytes per character.

3. Which formats would you block by real type at a mail gateway for ordinary staff, and why not just by extension?

Executables (PE), scripts (.js, .vbs, .ps1, .hta), shortcuts (.lnk), disk images (.iso, .img) and encrypted archives from outside. By real type because the extension is attacker-controlled.

Test yourself

Which bytes begin a ZIP file (and therefore a DOCX)?

  1. 4D 5A
  2. 50 4B 03 04
  3. 25 50 44 46
  4. 7F 45 4C 46

What does file base its answer on?

  1. The file extension
  2. Patterns of bytes (magic numbers) in the content
  3. The file's owner
  4. A hash lookup

A file named photo.jpg begins with %PDF-. What is it most likely to be?

  1. A JPEG image
  2. A PDF document
  3. A Windows executable
  4. An ELF binary

What is a polyglot file?

  1. A file translated into several languages
  2. A file that is valid in two formats at once
  3. A compressed executable
  4. A file with a double extension