PICOCTF 2019 • FORENSICS • FILE SIGNATURES

Extensions: Magic Bytes & File Format Discrepancies

By Abdo•Aug 31, 2026
Extensions Analysis
Official Challenge Prompt

“This is a really weird text file. Can you find the flag?”

● Category: Forensics / Magic Bytes● Points: 150 PTS● Flag Format: picoCTF{...}
Provided File (Download & Practice)Download flag.txtSize: 9.9 KB • Misnamed File
Real Type: PNG Image Data

Forensic Principle: Magic Bytes vs File Extensions

Operating system shells often rely on file extensions to associate default handler applications, but the true file format is determined by its header signature (magic bytes). If a PNG file is renamed with a .txt extension, text editors fail or display binary noise, while inspecting the initial 8 bytes immediately exposes the standard PNG magic signature: 89 50 4E 47 0D 0A 1A 0A.

METHOD A: HANDS-ON HEADER ANALYSIS

1. Inspecting File Signature (Magic Bytes)

$ Format-Hex -Path .\flag.txt -Count 16

           00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00000000   89 50 4E 47 0D 0A 1A 0A 00 00 00 0D 49 48 44 52  .PNG........IHDR
Terminal Rename Command:
Copy-Item flag.txt flag.png; Start-Process flag.png
METHOD B: AUTOMATED PYTHON PARSER & ONELINER

2. Automated Signature Verification (`solve.py`)

# Verify magic bytes and rename file programmatically
with open('flag.txt', 'rb') as f:
    header = f.read(8)

# Check for PNG File Signature (89 50 4E 47 0D 0A 1A 0A)
if header == b'\x89PNG\r\n\x1a\n':
    print("[+] Valid PNG Signature detected! Renaming flag.txt -> flag.png")
    with open('flag.png', 'wb') as out_f:
        with open('flag.txt', 'rb') as in_f:
            out_f.write(in_f.read())
    print("[+] File successfully recovered as flag.png!")
Terminal One-Liner (PowerShell / Bash):
python -c "import shutil; shutil.copy('flag.txt', 'flag.png'); print('Saved as flag.png')"

3. Rendered Image Flag

Extracted Secret Flag:

picoCTF{now_you_know_about_extensions}

4. The Complete Investigation Path & Mental Roadmap

STEP 1
File Behavior & Corruption Check

Opened flag.txt in a text editor and found non-printable binary characters, indicating a file type mismatch.

STEP 2
Magic Byte Verification

Inspected the first 8 bytes and matched 89 50 4E 47 0D 0A 1A 0A to the official Portable Network Graphics (PNG) standard.

STEP 3
Extension Correction & Flag Rendering

Renamed file to flag.png and opened it to read the rendered flag text: picoCTF{now_you_know_about_extensions}.