Safe Penetration Testing Labs · beginner · ~12 min
Identify common firmware file types by their first few bytes.
Bounds-check, then compare prefix bytes against a small table; return a tag.
Magic-byte sniffing is the gateway to every other forensic tool.
Before you even mount or extract a firmware image, you need to know
what it is. binwalk and file lead with the same trick: compare
the first 4–8 bytes against a table of known magic numbers.
| Magic | Meaning |
|---|---|
4D 5A |
PE / DOS executable |
7F 45 4C 46 |
ELF |
27 05 19 56 |
U-Boot uImage (big-endian) |
68 73 71 73 |
Squashfs (hsqs) |
19 85 |
JFFS2 |
Implement int detect_firmware_type(const uint8_t *buf, size_t n).
Return:
If n is too short to even check a given magic, fall through to the
next; if none match, return 0.
n < 4.Five magics, five tags. Check length before each comparison.