[CSAW '23] 1black0white

Converting Binary Data into a QR code.

Distribution

We were provided a data file with some decimal data.

data.txt
533258111
274428993
391005533
391777629
390435677
273999169
534074751
99072
528317354
446173689
485174588
490627992
105525542
421383123
132446300
431853817
534345998
496243321
365115424
302404521
289808374
1437979
534308692
272742168
391735804
391385911
391848254
273838450
534645340

Solution

  • The data was in decimal, not binary

  • Not all the lines were the same length.

data = open('data.txt').read().strip().split()
binary = ''.join(bin(int(num))[2:].zfill(29) for num in data)

Now, we can use PIL (the Python Imaging Library) to create an image from the binary data. I made a new image and then looped through the binary data, setting each pixel to black or white based on the value of the binary digit.

index = 0
for x in range(size):
    for y in range(size):
        if index < len(binary):
            pixels[x, y] = int(binary[index])
            index += 1
large = img.resize((10 * size, 10 * size))
large.show()

This generates an image that gives us the flag when we scan it.

Full Solution

solve.py
from PIL import Image

data = open('data.txt').read().strip().split()
binary = ''.join(bin(int(num))[2:].zfill(29) for num in data)

size = int(len(binary) ** 0.5)
img = Image.new('1', (size, size))
pixels = img.load()

index = 0
for x in range(size):
    for y in range(size):
        if index < len(binary):
            pixels[x, y] = int(binary[index])
            index += 1

large = img.resize((10 * size, 10 * size))
large.show()

Last updated