Skip to content

How to Identify a File by Its Magic Bytes

Identify a file type from its magic bytes. Compare JPEG and PNG headers, spot a wrong extension, and test a truncated PNG in a hands-on lab.

By Sourabh6 min read

If a file is called picture.jpg but its first bytes are 89 50 4e 47 0d 0a 1a 0a, the contents start with a PNG signature. The filename can give you one answer while the file gives you another. Read the bytes, identify the likely format, then check whether the rest of the file follows that format.

You will work through three practice files here. One is a JPEG, one is a PNG with a .jpg extension, and one is an incomplete PNG. The first commands will spot the mismatch. The last check shows why even a correct signature can give you an incomplete answer.

What JPEG and PNG Magic Bytes Look Like

A byte is a small unit of data. Hexadecimal, or hex, is a compact way to write it using two characters. In ff d8 ff, each pair is one byte. A file signature is a recognizable byte pattern that a format places at a predictable location. You may also hear it called magic bytes or a magic number.

For PNG, the opening eight bytes form a fixed signature. A JPEG starts with the ff d8 start-of-image marker and normally shows another marker beginning with ff immediately after it. The marker and following data can vary. A JPEG with a different byte sequence after ff d8 ff can still be a JPEG.

The highlighted JPEG marker uses two bytes. The next ff begins another marker in this sample. The entire PNG signature uses eight bytes.

A signature gives you a candidate format. Sometimes formats share a container or need evidence beyond the opening bytes. A Word .docx file, for instance, normally uses a ZIP container, so recognizable ZIP bytes alone do not describe everything inside. Plain-text files may have no distinctive signature at all.

Step 1. Check the File With the File Command

On macOS or Linux, run file on a working copy. The command tests the contents against recognizable patterns and reports the result. If you have an extension and a tool report, write both down. The discrepancy is part of the observation.

bash
file camera.jpg renamed.jpg partial.png
Summary of the ForensicSpot file command run. The highlighted row shows the mismatch between the .jpg extension and the PNG content.

In the ForensicSpot practice run, camera.jpg contains JPEG image data. renamed.jpg contains PNG image data even though its name ends in .jpg. partial.png also produces a PNG description because the generated sample retains the signature and the image header that supplies its dimensions. The name and a tool's first result are two leads you can check against the content.

Step 2. Read the File Header With xxd

Run the command below on one file at a time. The -l 16 option limits the display to 16 bytes and -g 1 prints single-byte groups. Apply the same command to camera.jpg and partial.png if you want to compare them.

bash
xxd -l 16 -g 1 renamed.jpg

The address on the left, 00000000, means you are looking at the beginning of the file. Read the pairs in the center. The right side is a text interpretation of those same bytes. A dot usually marks a byte that cannot be shown as an ordinary character. Use the middle column to compare signatures, including bytes that look like dots on the right.

The first eight bytes of renamed.jpg are 89 50 4e 47 0d 0a 1a 0a. Those same eight bytes also appear in partial.png. Comparing only those bytes cannot tell you which file is complete.

Step 3. Test Whether the File Is Readable

Use a decoder or format-aware validator to check beyond the header. A PNG has a sequence of blocks called chunks. The image header is in the IHDR chunk, the compressed pixel data is carried in IDAT chunks, and the IEND chunk marks the end. A recognizer can learn the dimensions from the header before it reads enough data to display any pixels.

The ForensicSpot lab used Python's Pillow library to load the three images. Loading the pixels is a useful second check for this example. A strict format check may be appropriate if you need to verify a specific requirement, such as chunk integrity or trailing content. Tool behavior and tolerance can vary.

The diagram follows the actual practice files. Both headers were recognized. Pillow loaded the intact PNG and returned UnidentifiedImageError for the truncated copy.

Both complete images decoded in the lab. The truncated PNG produced an error. A short file that retains its first bytes can therefore look right in a header check and still be unusable. Record the tool's actual error if you see one. A failed decode tells you about that file and decoder. The surrounding evidence helps explain why it failed.

Build Your Own Practice Set

Use photos or drawings you are free to work with. Save one locally as JPEG and another as PNG. Make two copies of the PNG and keep the original. Rename the first copy renamed.jpg. If you choose to use the terminal, create the second copy from the PNG's first 33 bytes. The sample will keep the signature and the entire first header chunk.

bash
head -c 33 original.png > partial.png
file renamed.jpg partial.png

Use a separate destination filename. The command writes output to that pathname and will replace a file already there. You can then view the first bytes with xxd and try to load the pixels of both copies. This exercise works with your own images and does not require altering any evidence. If Pillow is already installed in your Python environment, you can reproduce the pixel check with this script.

python
from PIL import Image

for name in ("camera.jpg", "renamed.jpg", "partial.png"):
    try:
        with Image.open(name) as image:
            image.load()
            print(name, image.format, image.size)
    except OSError as error:
        print(name, type(error).__name__, error)

Set the filenames in the first line of the loop to match your copies. On a system without Pillow, try opening each practice image in an image viewer and record whether it actually displays. If your viewer recovers part of a damaged image, note what displayed and what was missing.

Predict the Results Before You Run It

  • What format would you record for renamed.jpg if the header and decoder both say PNG?
  • Why might file print image dimensions for partial.png?
  • What would you check if a .docx file appeared to start with ZIP bytes?

For the first, record PNG content with a .jpg extension. For the second, the dimensions live in the retained header, before the missing pixel data. For the third, inspect the container for the expected document entries before assigning a more specific file type.

Report the Evidence You Actually Have

In the lab, a concise note could read: “renamed.jpg has a PNG signature at byte offset 0. The file command described PNG image data, and the decoder successfully loaded the pixels. The extension is .jpg.” For the truncated copy, record the matching signature and the failed load. Document the original filename, where the working copy came from, and the tools used when that information is relevant to the examination.

A mismatched extension tells you the name and content differ. It does not tell you who renamed the file or why. File carving solves a related task: it uses patterns in raw data to locate candidate files when a normal file record is unavailable. With the three practice files here, you already have discrete files and are checking what each one contains.