Hack This Site - Programming - Analyze the picture and find the ascii code

Wed, Jun 30, 21

Welcome back to my walkthrough of hackthissite.org’s CTF missions. I will be going through my thought process of how I solved these missions, and therefore also giving away the solutions. If you came across this to give you hints, watch out for spoilers! Good luck, have fun.

This mission gives us 15 seconds to decode a morse message that is hidden within a .png file. Each white pixel represents an ASCII code in relation to the position of the last white pixel and the current pixel position. There are 100 pixels per row with numbering of the pixels beginning at 0, with a depth of 30. Here is the code that I wrote to solve this mission:

import png
from morse3 import Morse

def interpretImage():
    r = png.Reader(filename="download.png").asRGB()
    return r

def imageToASCII(image):
    asciiList = []
    rgbValues = image[2]
    px = 0
    prevPx = 0
    for row in rgbValues:
        for i in range(0, len(row), 3):
            r = row[i]
            g = row[i+1]
            b = row[i+2]
            if (r == g == b == 255):
                asciiList.append(px - prevPx)
                prevPx = px
            px += 1
    return asciiList

if __name__ == "__main__":
    image = interpretImage()
    asciiList = imageToASCII(image)
    morseString = ''.join(list(map(chr, asciiList)))
    print(Morse(morseString).morseToString())

To aid the extraction of RGB values of each pixel I used PyPNG to make my life easier, furthermore, I used Morse3 to help my use of morse decoding. There is a new image that is generated each time, so you have to be quick with the download.