Hackvent 2019: Day 9

Hackvent 2019310

Challenge

HV19.09 Santas Quick Response 3.0

Introduction

Visiting the following railway station has left lasting memories.

Hackvent 2019 - Day 9 - Railway Station with overlay pattern

Santas brand new gifts distribution system is heavily inspired by it. Here is your personal gift, can you extract the destination path of it?

Hackvent 2019 - Day 9 - Destination Path QR Code

Solution

We know that the QR code system is inspired by the first image so we do a reverse image lookup to try and find out more information about it. We find many articles such as this one discussing a pattern called Rule 30.

After some light research, we discover that rule 30 can be used to generate a pattern downwards starting from a single black pixel (or 1). The algorithm is:

s = s[i-1] XOR (s[i] OR s[i+1])

This video does a better job explaining it than I ever could:

Next, we notice that our invalid QR code has two QR alignment patterns in the correct positions (top left and top right corners) but is missing one in the bottom left corner. That, combined with the fact that the Rule 30 algorithm generates a tree like pattern is a very strong indication that we should overlap the rule 30 pattern over the QR code and use it as  mask from the middle of the first row (so that the current QR alignment patterns are left intact).

This was our approach. For illustration purposes, this is the area that would be left intact:

Hackvent 2019 - Day 9 - QR Code Intact Section

We attempt common boolean operators such as and, or and xor and check to see if the bottom left QR alignment pattern suddenly appears. Unfortunately, it initial does not appear. After some head scratching we later find out that xor is the right boolean operation and we have to shift the rule30 pattern by 1 to the right.

This is what the final Rule 30 mask looked like:

Hackvent 2019 - Day 9 - Final Rule 30 Mask

Note that the mask is not perfect as it eventually overlaps with itself but is was large enough to fully cover our 33x33 QR code.

Finally, here is our python script:

# Hackvent 2019 - Day 9
# Mo Beigi (https://mobeigi.com)

import PIL
from PIL import Image

bitmap = [
 [1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1],
 [1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1],
 [1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 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1, 0, 1, 1, 0, 1]
]

# QR
qr_modules = 33

# Rule 30 parameters
rule30_arr = [[0 for x in range(50)] for y in range(50)] # 50x50 grid for pattern, has to be significantly larger than our QR code so it overlaps it
rule30_arr[0][16] = 1 # initial black pixel to start pattern
START_INDEX = 16 # guess, middle of first row
offset = 0 # to expand horizontally after progressing to next row

for i in range(len(bitmap)):
    for j in range(max(START_INDEX-offset, -50), min(START_INDEX+offset+1, 50)):
        rule30_arr[i+1][j] = rule30_arr[i][j-1] ^ (rule30_arr[i][j] | rule30_arr[i][j+1]) # s = s[i-1] XOR (s[i] OR s[i+1])
        
        # Constrain bounds of QR code to [0, qr_modules]
        if j >= 0 and j <= qr_modules - 1:
            # XOR with current QR data in bitmap
            # Pattern has to be shifted to the right by 1
            bitmap[i][j] ^= rule30_arr[i+1][j-1]
    
    # Increase offset for next row
    offset += 1
        
            
# Generate QR code
im = Image.new('RGB', (qr_modules, qr_modules))
data = []
 
for row in bitmap:
  for bit in row:
      if bit == 0:
        data.append((255,255,255))
      elif bit == 1:
        data.append((0,0,0))
    
im.putdata(data)
im.save('qr.png')

Running our Python script gave us the following QR code which scans to give us our flag!

Hackvent 2019 - Day 9 - QR Code Final

Flag:

HV19{Cha0tic_yet-0rdered}

Leave a comment

(required)(will not be published)(required)

Comments

There are no comments yet. Be the first to add one!