A magic number (or file signature) is a fixed sequence of bytes that a file format requires at a known position, almost always offset 0. A PNG always begins 89 50 4E 47 0D 0A 1A 0A, a PDF 25 50 44 46 2D (%PDF-), a ZIP archive 50 4B 03 04 (PK followed by two control bytes) and a Linux executable 7F 45 4C 46 (.ELF, the first byte being 7F). Reading those first bytes is the most reliable way to tell what a file actually is, whatever its name says.
Magic numbers explain a lot of everyday computing: why renaming a file never converts it, how an upload form rejects the wrong file type, how forensic tools recover deleted photos, and how malware disguises itself. This guide covers what signatures are, a table of 26 common ones checked against each format’s own specification, and how to read them yourself with the File Inspector.
What is a magic number?
When a program writes a PNG image, the PNG specification requires it to write the bytes 89 50 4E 47 0D 0A 1A 0A before anything else. So when any program later wants to know “is this a PNG?”, it does not look at the name; it reads the first eight bytes and compares.
The name comes from programming, where an arbitrary constant chosen to mark a file or data structure is called a magic number. Many signatures are readable as text on purpose: a PDF begins with %PDF-, a GIF with GIF89a, a ZIP archive with PK (as in PKWARE, the company that publishes the ZIP specification) and a Java class file with the hex value CAFEBABE. Others deliberately include bytes that cannot be text. The PNG signature shows the thinking behind a good one. According to the PNG specification’s rationale, the first byte (89) is not ASCII, so a text file is unlikely to be mistaken for PNG and a transfer that clears the top bit is caught; the CR-LF pair (0D 0A) and the final lone LF (0A) catch transfers that convert line endings in either direction; and the Ctrl-Z (1A) stops the file being displayed by the MS-DOS type command.
Why the extension can’t be trusted
A file’s extension, the .jpg or .pdf at the end of its name, is stored by the file system as part of the name, not inside the file. It is a hint for people and a shortcut the operating system uses to pick a default app. Rename report.pdf to report.jpg and the bytes inside are unchanged.
That gap between the claimed type and the real type is a security problem. A common trick is to give an executable a harmless-looking name and icon, such as invoice.pdf.exe, or a .jpg whose contents start with 4D 5A (MZ, the start of a Windows program). A person glancing at the icon is fooled; a tool reading the signature is not. That is why upload validators, antivirus software and forensic tools identify files by content and treat the extension as an untrusted label.
File signature table (26 formats)
Offsets are in bytes from the start of the file. Hex bytes are shown in file order. In the ASCII column a dot stands for a byte that is not printable text. Each row names the document that defines the signature.
| Format | Extensions | Offset (bytes) | Signature (hex) | ASCII | Defined in |
|---|---|---|---|---|---|
| PNG image | .png | 0 | 89 50 4E 47 0D 0A 1A 0A | .PNG.... | W3C PNG Specification, §5.2 |
| JPEG image | .jpg .jpeg .jfif | 0 | FF D8 FF | — | ITU-T T.81: FF D8 is the SOI (start of image) marker, and the next marker begins FF. JFIF files continue E0 with JFIF at offset 6; Exif files continue E1 with Exif at offset 6 |
| GIF image | .gif | 0 | 47 49 46 38 37 61 or 47 49 46 38 39 61 | GIF87a / GIF89a | GIF89a Specification (CompuServe, 1990), §17 |
| BMP image | .bmp .dib | 0 | 42 4D | BM | Microsoft, BITMAPFILEHEADER (bfType = 0x4D42) |
| TIFF image | .tif .tiff | 0 | 49 49 2A 00 (little-endian) or 4D 4D 00 2A (big-endian) | II*. / MM.* | TIFF Revision 6.0 (Adobe, 1992): byte order then the number 42 |
| WebP image | .webp | 0 and 8 | 52 49 46 46 + 4-byte size + 57 45 42 50 | RIFF....WEBP | RFC 9649 (WebP), RIFF header |
| WAV audio | .wav | 0 and 8 | 52 49 46 46 + 4-byte size + 57 41 56 45 | RIFF....WAVE | Microsoft RIFF documentation |
| AVI video | .avi | 0 and 8 | 52 49 46 46 + 4-byte size + 41 56 49 20 | RIFF....AVI | Microsoft AVI RIFF File Reference |
| MP4 / MOV / M4A / HEIC / AVIF / 3GP (ISO base media) | .mp4 .m4a .mov .heic .avif .3gp | 4 | 66 74 79 70, after a 4-byte big-endian box size; brand at offset 8 | ....ftyp | ISO/IEC 14496-12; Apple QuickTime File Format. Brands such as isom, mp41, mp42, heic, mif1, avif are registered with the MP4RA |
| Matroska / WebM | .mkv .mka .webm | 0 | 1A 45 DF A3 | — | RFC 8794 (EBML header element ID); RFC 9559 (Matroska). The DocType inside reads matroska or webm |
| Ogg (Vorbis, Opus, Theora, FLAC-in-Ogg) | .ogg .oga .ogv .opus | 0 | 4F 67 67 53 | OggS | RFC 3533 (page capture pattern) |
| FLAC audio | .flac | 0 | 66 4C 61 43 | fLaC | RFC 9639 (FLAC) |
| MP3 with ID3v2 tag | .mp3 | 0 | 49 44 33 | ID3 | ID3v2.4.0 structure. An untagged MP3 starts straight with an MPEG audio frame sync such as FF FB, which is not a true magic number |
| PDF document | 0 | 25 50 44 46 2D | %PDF- | ISO 32000-1:2008, §7.5.2 (followed by a version such as 1.7) | |
| ZIP archive and ZIP-based formats | .zip .docx .xlsx .pptx .odt .jar .apk .epub | 0 | 50 4B 03 04 (an empty archive: 50 4B 05 06) | PK.. | PKWARE APPNOTE.TXT 6.3.10 (local file header 0x04034b50; end of central directory 0x06054b50) |
| gzip | .gz .tgz | 0 | 1F 8B 08 | — | RFC 1952: ID1 = 0x1F, ID2 = 0x8B, then CM = 8 (deflate) |
| xz | .xz .txz | 0 | FD 37 7A 58 5A 00 | .7zXZ. | The .xz File Format 1.x (Tukaani), Header Magic Bytes |
| 7-Zip | .7z | 0 | 37 7A BC AF 27 1C | 7z..'. | 7-Zip 7zFormat.txt, SignatureHeader |
| RAR 5.0 | .rar | 0 | 52 61 72 21 1A 07 01 00 | Rar!.... | RARLAB, RAR 5.0 archive format |
| RAR 1.5–4.x | .rar | 0 | 52 61 72 21 1A 07 00 | Rar!... | RARLAB, RAR 5.0 archive format (comparison note) |
| tar (POSIX ustar) | .tar | 257 | 75 73 74 61 72 00 30 30 (old GNU tar: 75 73 74 61 72 20 20 00) | ustar.00 / ustar . | GNU tar manual, “Basic Tar Format” (POSIX.1 header) |
| Windows executable (PE) | .exe .dll .sys | 0, then the offset stored at 0x3C | 4D 5A; PE signature 50 45 00 00 | MZ / PE.. | Microsoft PE Format (MS-DOS stub; signature at the file offset given at 0x3C) |
| ELF executable / library | (none) .so .o | 0 | 7F 45 4C 46 | .ELF | System V ABI, ELF header e_ident (ELFMAG0–3) |
| Java class file | .class | 0 | CA FE BA BE | — | Java Virtual Machine Specification, §4.1 (also used by Mach-O universal binaries) |
| WebAssembly module | .wasm | 0 | 00 61 73 6D 01 00 00 00 | .asm.... | W3C WebAssembly Core Specification, binary module preamble (magic + version 1) |
| SQLite 3 database | .sqlite .sqlite3 .db | 0 | 53 51 4C 69 74 65 20 66 6F 72 6D 61 74 20 33 00 | SQLite format 3. | SQLite Database File Format, §1.3 (16-byte header string) |
| Microsoft Compound File (Office 97–2003) | .doc .xls .ppt | 0 | D0 CF 11 E0 A1 B1 1A E1 | — | [MS-CFB] Compound File Binary File Format, Header Signature |
Signatures that need a second look
The first four bytes settle most cases, but several common formats need one more step:
- RIFF files (WAV, AVI, WebP) all start
RIFF. Bytes 4–7 are the little-endian size of the rest of the file, and bytes 8–11 name the form:WAVE,AVI(with a trailing space) orWEBP. - MP4 and its relatives are made of boxes, each starting with a 4-byte size and a 4-byte type. The file-type box
ftypis normally the first box, so its type sits at offset 4 and the “major brand” that says MP4, QuickTime, HEIC or AVIF sits at offset 8. Apple’s QuickTime specification calls the box optional but says that when present it must come first, so very old .mov files can start with another box. - Windows executables begin with the MS-DOS header (
MZ). The 4-byte value at offset0x3Cgives the position of the real signature,PE\0\0(50 45 00 00). A file withMZbut no PE signature is an old DOS program, not a Windows one. - CA FE BA BE means a Java class file or a Mach-O universal (“fat”) binary. libmagic separates them by the next four bytes: in a class file they are the minor and major class-file version (43 and up), in a fat binary they are the number of architectures (a small number).
- tar keeps its magic at offset 257 inside the first 512-byte header, so a tool must read past the file name field to find it.
- RAR self-extracting archives put an executable in front of the archive; RARLAB says to search for the signature from the start of the file up to the maximum SFX module size.
Not just the start: footers and trailers
Some formats also define bytes at the end of the file. A JPEG ends with the EOI (end of image) marker FF D9; a GIF with the trailer byte 3B; a PNG with the empty IEND chunk, whose last eight bytes are always 49 45 4E 44 AE 42 60 82 (the chunk type and its CRC). The last line of a PDF must contain only %%EOF, and a PDF that has been edited by incremental update contains one %%EOF per revision. A ZIP archive ends with its end-of-central-directory record (50 4B 05 06), which can be followed by an archive comment of up to 65,535 bytes.
Footers matter for two reasons. First, they let a tool detect truncation: if the start says PNG but there is no IEND, the file is incomplete. Second, the space after a valid footer is a favourite hiding place: many viewers stop reading at the end marker, so data appended past it stays invisible in a normal viewer but is easy to find with a tool that looks.
Containers and shared signatures
One signature does not always mean one format. Many modern document formats are containers: a wrapper format holding a structured set of files. DOCX, XLSX, PPTX, JAR, APK and EPUB are all ZIP archives with a particular internal layout, so they all begin with PK. To identify them, open the archive and look at its contents: Office Open XML packages store a [Content_Types].xml item (ECMA-376 Part 2); a Java JAR has META-INF/MANIFEST.MF; an EPUB must have a first file named mimetype containing application/epub+zip, stored uncompressed so the text is readable at a fixed position. This is why robust identification parses structure instead of stopping at four bytes, which the File Inspector’s format parsers do automatically.
Carving, polyglots, and spoofing
Because signatures are predictable, you can scan a whole blob of bytes for them and pull out embedded files, a forensic technique called file carving. It is how investigators recover photos from a raw disk image: find each FF D8 FF, read to the matching FF D9, and you have a JPEG with no file system at all. (Real carvers have to cope with Exif thumbnails, which are complete JPEGs nested inside the main one, each with its own start and end markers.)
The flip side is deliberate trickery. A polyglot is a single file built to be valid as two formats at once. A classic pairing is an image and a ZIP archive: an image is read from its start, while a ZIP reader finds its directory from the end of the file, so both can be satisfied. Attackers use the same flexibility to slip payloads past filters that check only one signature. Signature-aware tools defend against this by carving for all known signatures rather than trusting the first one they see.
How software puts this to work
The classic implementation is the Unix file command and its libmagic database, which matches files against thousands of signature rules (for example, the rule for Windows programs is simply “offset 0, string MZ”). Web frameworks validate uploads by content rather than name, and browsers follow the WHATWG MIME Sniffing Standard, which lists byte patterns such as 89 50 4E 47 0D 0A 1A 0A for PNG and 25 50 44 46 2D for PDF, to decide how to treat a response whose declared type is missing or unreliable. The rule is always the same: the bytes are the evidence, the name is a label.
See it for yourself
The quickest way to make this concrete is to look at real bytes. Drop any file into the File Inspector and read the first row of the hex view: you will see the signature, the format identified beside it, and any extension mismatch flagged. The Hex Viewer shows any byte range of a file, which is handy for signatures that are not at offset 0, such as ftyp at 4 or ustar at 257. To go the other way and look up what a given extension or MIME type should be, use the MIME Lookup.
Sources
Each signature was checked on 5 October 2026 against these documents:
- W3C, Portable Network Graphics (PNG) Specification (Third Edition), §5.2 PNG signature and §11.2.4 IEND: w3.org/TR/png-3; signature rationale from PNG Specification 1.2, §12.12 (libpng.org).
- ITU-T Recommendation T.81 (1992), Digital compression and coding of continuous-tone still images (JPEG), Table B.1 marker codes (SOI
X'FFD8', EOIX'FFD9'); E. Hamilton, JPEG File Interchange Format 1.02 (C-Cube, 1992): both hosted by W3C at w3.org/Graphics/JPEG. - CompuServe, Graphics Interchange Format Version 89a (1990), §17 Header and §27 Trailer: w3.org/Graphics/GIF/spec-gif89a.txt.
- Microsoft Learn: BITMAPFILEHEADER structure; PE Format; Resource Interchange File Format (RIFF); AVI RIFF File Reference; [MS-CFB] Compound File Binary File Format, §2.2 Compound File Header.
- Adobe, TIFF Revision 6.0 (1992), Image File Header.
- IETF RFC 9649 WebP Image Format (2024); RFC 3533 The Ogg Encapsulation Format Version 0 (2003); RFC 9639 Free Lossless Audio Codec (FLAC) (2024); RFC 8794 Extensible Binary Meta Language (2020); RFC 9559 Matroska Media Container Format Specification (2024); RFC 1952 GZIP file format specification version 4.3 (1996): rfc-editor.org.
- Apple, QuickTime File Format: File type compatibility atom (‘ftyp’); MP4 Registration Authority, registered brands: mp4ra.org.
- ID3.org, ID3 tag version 2.4.0 – Main Structure (2000).
- ISO 32000-1:2008 Document management – Portable document format – Part 1: PDF 1.7 (the copy published by Adobe), §7.5.2 File Header and §7.5.5 File Trailer.
- PKWARE, APPNOTE.TXT – .ZIP File Format Specification, version 6.3.10 (2022): pkware.cachefly.net.
- ECMA-376 Part 2, Office Open XML – Open Packaging Conventions, 5th edition (2021); W3C, EPUB 3.3 (2023), OCF ZIP container; Oracle, JAR File Specification (Java SE 21).
- Tukaani Project, The .xz File Format, version 1.x; 7-Zip, 7zFormat.txt (github.com/ip7z/7zip); RARLAB, RAR 5.0 archive format (rarlab.com/technote.htm); GNU, GNU tar manual, Basic Tar Format.
- System V Application Binary Interface (gABI), ELF Header: refspecs.linuxfoundation.org.
- Oracle, The Java Virtual Machine Specification, Java SE 21 Edition, §4.1 The ClassFile Structure.
- W3C, WebAssembly Core Specification, binary format, Modules (magic and version preamble).
- SQLite, Database File Format, §1.3 The Database Header: sqlite.org/fileformat.html.
- WHATWG, MIME Sniffing Standard; the
file/libmagic project, magic database filesmsdos,cafebabe,jpeg,matroska(github.com/file/file).
Frequently asked questions
What is a magic number in a file?
A short, fixed sequence of bytes at a known position, almost always the very start of the file, that identifies its format. Every PNG begins with the eight bytes 89 50 4E 47 0D 0A 1A 0A, and every PDF with %PDF- (25 50 44 46 2D). Software reads these bytes to learn what a file really is, whatever its name says.
Can two formats share the same magic number?
Yes. DOCX, XLSX, PPTX, JAR, APK and EPUB all begin with the ZIP signature 50 4B 03 04 because they are ZIP archives inside; you tell them apart by the files in the archive ([Content_Types].xml for Office, META-INF/MANIFEST.MF for a JAR, a first file called mimetype for EPUB). CA FE BA BE is shared by Java class files and Mach-O universal binaries.
Is the signature always at the start of the file?
Usually, not always. MP4, MOV, HEIC and AVIF files have the box type ftyp at offset 4, after a 4-byte size. A tar archive has ustar at offset 257. A Windows EXE starts with MZ, but its real PE signature (50 45 00 00) sits at the offset stored at 0x3C.
Why not just trust the file extension?
The extension is part of the file name and anyone can change it with a rename; it says nothing about the bytes. The magic number is written inside the file by the program that created it, so it is the better evidence, although a determined attacker can still forge it or build a file that is valid in two formats at once.
How do I see a file's magic number?
Open the file in a hex viewer such as the Hex Viewer or File Inspector and read the first row of bytes, then compare it with the table in this article. On Linux and macOS, the file command does the same comparison against its libmagic database.