How to Tell a File's Real Type from Its Magic Bytes

Forense Digital Nível 2/5 ~45 s 2026-06-20

O desafio

Um usuário enviou isto como 'avatar.png', mas o servidor só verificou o nome. Seus bytes reais não começam com a assinatura PNG 89 50 4E 47. Leia o cabeçalho abaixo, compare seus primeiros bytes destacados com a referência de bytes mágicos, e digite a VERDADEIRA extensão do arquivo (como .png ou .pdf).

O que você vai aprender

  • Understand what magic bytes (a file signature) are and where they live in a file
  • Read a hex dump alongside its ASCII gutter to recognize a file's true format
  • Explain why a file's name and claimed Content-Type prove nothing about its contents
  • Recognize how attackers disguise one file type as another to defeat extension checks
  • Describe how server-side signature validation hardens a file-upload pipeline

Habilidades testadas

Reading and interpreting a raw hex dumpFile-signature (magic byte) recognitionFile-upload security reasoningBasic file-format forensics

Pré-requisitos

  • Comfort reading hexadecimal and basic ASCII
  • Awareness that files have a structured binary layout, not just an extension

Como funciona

Almost every file format begins with a short, fixed sequence of bytes called a magic number or file signature. Programs read these leading bytes to decide what a file actually is before doing anything with it. A PNG image, for example, always opens with 89 50 4E 47 (the bytes spell .PNG in the ASCII gutter), a JPEG starts FF D8 FF, a PDF starts 25 50 44 46 (%PDF), and a ZIP archive starts 50 4B 03 04 (PK..). The signature lives at the very start of the file and does not change just because someone renames the file or sets a different Content-Type header.

That is the whole point of this challenge: the upload is named avatar.png, but a name is just a label a user chooses. The truth is in the bytes. Opening the file in a hex view shows two columns - the raw hexadecimal on the left and a printable-ASCII gutter on the right. When the leading bytes do not match the signature the extension promises, the file is not what it claims to be. The ASCII gutter is your shortcut: many signatures are human-readable text, so a quick glance often tells you the real format faster than memorizing hex.

This matters because file type is a security boundary. An image pipeline expects to decode pixels; if it is instead handed an executable, a script, or an archive, the assumptions break - and an attacker who can smuggle the wrong type past a weak check has a foothold. Reading magic bytes is the forensic skill that closes that gap.

Erros comuns

  • Trusting the file extension. A .png ending is just text in the filename; anyone can rename anything. The extension is a hint, never proof.
  • Trusting the Content-Type header. The browser or client sets it, and a client is fully attacker-controlled. image/png in a request says nothing about the actual bytes.
  • Reading only the hex and skipping the ASCII gutter. Many signatures spell out recognizable letters on the right-hand column - ignoring it makes you work harder than you need to.
  • Assuming a valid-looking start means the whole file is safe. Matching magic bytes confirm the format, but they do not guarantee the rest of the file is benign or well-formed.

Como se proteger

When you accept file uploads, never decide a file's type from its name or its Content-Type header. Read the actual leading bytes server-side and verify them against the signature for the format you expect, then enforce an allow-list of permitted types rather than a block-list of forbidden ones.

Layer additional defenses so that even a correctly-typed file cannot do harm:

  • Re-encode or process accepted images through a trusted library, which rejects malformed or disguised inputs.
  • Store uploads outside the web root and serve them from a separate domain with a forced Content-Disposition so they are never executed or interpreted in your origin.
  • Generate your own random filenames and ignore the user-supplied name entirely.
  • Cap file size and validate dimensions before any heavy processing.

Solução completa

Membros Pro e Max desbloqueiam o passo a passo completo.

Assinar Pro

Estatísticas da comunidade

36 resoluções
86% taxa de sucesso
Varythor Primeiro sangue
13.000+ Hackers 100+ Labs & Cursos Grátis
Comece Grátis