Lesson 6 of 29 ↓
- 1.Rhel 9 Administration Guide
- 2.Ubuntu Boot Process Guide
- 3.Linux Basic Commands Guide
- 4.Comprehensive Unix Shells Guide
- 5.Mastering Vim Deep & Practical Guide
- 6.Magic Numbers in Linux Deep Systems Guide
- 7.Wildcards & Globbing in Linux Deep Cli Guide
- 8.Linux Users & Groups Deep Administration Guide
- 9.Journaling in Linux Filesystems Deep Systems Guide
- 10.Getopt & Getoptlong Deep System-level Option Parsing Guide
- 11.Su Command in Linux Complete Guide with Environment Variables
- 12.Linux File Permissions and Ownership Complete Practical Guide
- 13.Ubuntu Package Management Apt Complete Guide
- 14.Bash Scripting Basics
- 15.Linux File System Essentials
- 16.Process and Thread Management
- 17.Session 10 : Introduction to Networking & the Application Layer
- 18.Linux Networking Cli & Introduction to Yocto for Machine Routing
- 19.Comprehensive Guide to Ssh (secure Shell)
- 20.Logical Volume Management (lvm) Guide
- 21.C Build Process & Libraries Guide
- 22.Archiving and Compression in Linux
- 23.Linux Package Managers: a Detailed Guide
- 24.Misra-c Guidelines Detailed Cheat Sheet
- 25.Mqtt Server Setup (pc as Server)
- 26.Detailed Explanation of Tty and Pts in Linux
- 27.Vim Cheat Sheet
- 28.Difference Between apt and apt-get
- 29.Difference Between apt Upgrade and apt Full-upgrade
3 min read
Magic Numbers in Linux Deep Systems Guide
A low-level, systems-oriented, production-grade guide to understanding how Linux determines file types using magic numbers. This document goes far beyond basic definitions and explains kernel vs user-space behavior, libmagic internals, binary layouts, security implications, and real system programming workflows.
What Magic Numbers really Are
Magic numbers are constant byte sequences embedded into binary data structures to provide early identification hints for:
-
File formats
-
Executables
-
Disk layouts
-
Network protocols
They act as a weak contract between producers and consumers of binary data.
Key properties:
-
Positioned at well-known offsets
-
Endian-aware when numeric
-
Designed for fast rejection of invalid data
Why File Extensions Fail
| Problem | Explanation |
|---|---|
| User-controlled | Filenames are arbitrary |
| Ambiguous | .dat, .bin mean nothing |
| Spoofable | evil.exe → photo.jpg |
Linux always trusts content over name.
Magic Numbers vs Mime Types
| Aspect | Magic Numbers | MIME |
|---|---|---|
| Source | Binary content | Mapping rules |
| Trust | High | Medium |
| Used by | Kernel / tools | Desktop / browsers |
file --mime-type bridges both worlds.
Kernel vs User Space Detection
Kernel
-
Does not rely on file magic
-
Executes ELF only if loaders exist
-
Uses shebang (
#!) interception
User Space
-
file, desktops, scanners -
Powered by libmagic
Libmagic Architecture
Execution pipeline:
open() → read() → pattern engine
→ indirect offsets
→ strength scoring
→ description synthesisCapabilities:
-
Conditional rules
-
Nested patterns
-
Offset arithmetic
File Header Layouts
Example (PNG):
Offset Size Meaning
0x00 8 Signature
0x08 4 IHDR lengthMagic numbers rarely act alone — they validate structure presence, not full correctness.
Common Magic Numbers (extended)
| Format | Hex | Offset |
|---|---|---|
| ELF | 7F 45 4C 46 | 0x00 |
| 25 50 44 46 | 0x00 | |
| PE | 4D 5A | 0x00 |
| Mach-O | FE ED FA CE | 0x00 |
| GPT | 45 46 49 20 50 41 52 54 | 0x200 |
Notice: not all magic appears at offset 0.
Elf Internals
Beyond magic:
-
e_ident[EI_CLASS]
-
e_ident[EI_DATA]
-
Program headers
Use:
readelf -h fileExecutable Detection & Shebang
#! works by kernel loader redirection:
#! /usr/bin/python3Kernel steps:
-
Reads first 2 bytes
-
Parses interpreter path
-
Re-execs
Disk Structures as Magic Numbers
Mbr
- Offset 510 →
0x55AA
Gpt
-
Primary header at LBA 1
-
Magic =
EFI PART
Disk formats are file formats.
Byte-level Inspection
Essential tools:
xxd -l 64 file
hexdump -C file
od -An -tx1 fileWriting File Detectors
Python example:
magic = f.read(4)C example:
memcmp(buf, "\x7FELF", 4)Avoid trusting magic alone.
Advanced Magic Files
Features:
-
Indirect offsets
-
Numeric comparisons
-
Strength modifiers
Example:
0 string ELF
>4 byte =2 64-bit ELFSecurity Implications
Threats:
-
Polyglot files
-
Content spoofing
-
Malformed headers
Mitigations:
-
Full structure validation
-
Size & bounds checking
-
Sandboxing
Reverse Engineering
Magic numbers help identify:
-
Packed binaries
-
Firmware
-
Unknown dumps
Paired with:
-
binwalk -
strings -
radare2
Cross-platform Notes
| OS | Format |
|---|---|
| Linux | ELF |
| Windows | PE |
| macOS | Mach-O |
Multi-format binaries exist.
Performance Considerations
-
Early short-circuit
-
Read minimal bytes
-
Cache detection results
Used heavily by antivirus engines.
Practical Labs
Beginner → dump headers Intermediate → parse MBR Advanced → implement detector Expert → fuzz malformed headers
Further Reading
-
man magic -
libmagic source
-
OSDev Wiki
-
ELF Specification
✅ This document is written for Linux system programmers, reverse engineers, security researchers, and low-level developers.
Understanding magic numbers means understanding how systems recognize reality from raw bytes.
Happy hacking 🐧
Comments
Loading the discussion…