All write-ups

CTF Write-up · Cryptography

Scattered

Recovering image tiles that have been renamed to their MD5 digests, then reassembling them in grid order to reveal the flag.

Source: EOman 2022 Category: Cryptography 80 points Easy

Challenge

The flag is distributed across many image files inside a zip archive and must be reassembled into the correct order.

Analysis

Each file has been renamed to the MD5 hash of its true name, and the true names encode a grid position in the form r x c, a row and a column matching the regular expression ([0-9]x[0-9]). Because that name space is extremely small, the hashes can be inverted by exhaustive search, after which the tiles can be placed into their grid positions.

Solution

Each file name is brute-forced back to its coordinate by hashing every candidate from the pattern until the digest matches:

import os, hashlib, exrex, itertools
from PIL import Image

def md5_f(hash):
    for i in itertools.count(1):
        for s in exrex.generate('([0-9]x[0-9])'):
            if hashlib.md5(s.encode('utf-8')).hexdigest() == hash:
                return s

Each file is renamed to its recovered r x c .jpg coordinate. The grid size and tile dimensions are read from the recovered names, a blank canvas of the full size is created, and every tile is pasted into its position, reconstructing the complete image and revealing the flag.

for i in range(rows):
    for j in range(cols):
        file = str(i) + 'x' + str(j) + '.jpg'
        if file in files:
            img = Image.open(file)
            result.paste(img, (j*width, i*height))

result.show()
← Back to all write-ups