Course contentsShow
← Security
Lesson 955 of 3,05220. Web Security - File Upload and ProcessingFree lesson

Magic Byte Verification and File Type Detection

Learn to validate files using magic bytes (file signatures) instead of relying on MIME types or extensions.

Magic Byte Verification and File Type Detection

What you'll learn: How to validate uploaded files by examining their actual binary signatures instead of trusting file extensions or MIME types.

When users upload files, attackers can easily lie about what they're sending. They might rename malicious.php to malicious.jpg or manipulate the Content-Type header to claim a web shell is an innocent image. Relying on these user-controlled indicators is like trusting a burglar's claim that the crowbar in their bag is "just a decorative item."

What Are Magic Bytes?

Magic bytes (also called file signatures) are specific byte sequences at the beginning of files that identify their true format. For example:

  • JPEG files start with FF D8 FF
  • PNG files begin with 89 50 4E 47 0D 0A 1A 0A
  • GIF files start with 47 49 46 38 (ASCII: "GIF8")
  • PDF files begin with 25 50 44 46 (ASCII: "%PDF")

These bytes are embedded by the programs that create these files and are part of the file format specification itself.

Why This Matters for Security

An attacker might upload a PHP backdoor named evil.jpg with the MIME type image/jpeg. If your validation only checks the extension or MIME type, you'll incorrectly trust it. But if you read the first bytes of the file, you'll see it actually starts with <?php instead of FF D8 FF, revealing the deception.

How It Works

  1. Read the first 8-20 bytes of the uploaded file
  2. Compare against known magic byte signatures for allowed file types
  3. Reject files where the signature doesn't match expected types
  4. Combine with other defenses like extension validation and content scanning

Key Takeaway: Always validate files by reading their magic bytes—the actual binary signature that reveals their true type—rather than trusting user-controlled extensions or MIME types.