Pwn2Win CTF 2017 – Top Secret – Electronics

Molly was able to take pictures of a strange digital circuit sketch, along with an also strange message. All of these things were inside an envelope in a safe, which was labeled “Top Secret”.

We believe it might contain Butcher Corp’s plans for the future, can you help us read the message?

Strange Circuit.jpg
Message.txt

To start off we are given a circuit diagram and an input file. Taking a look at the circuit, I recognized the JK Flip-Flop zero to seven counter in the upper right, with the current value being fed into a MUX. Following this, we see that the MUX is sending a positive value to one of eight transistors which in turn provide a ground to one row of diodes. Looking over the circuit, we can note that there are 9 (0-8) inputs, this matches the Message.txt file we got. Input 0 is used to clock the JK Flip-Flop circuit, while inputs 1-8 are fed to the grid of diodes.

At this point, I was a little stuck. I didn’t understand how to get anything useful from this. So I showed the diagram to an Electrical Engineering friend of mine. He suggested that the diode grid was actually a Light Emitting Diode grid. Immediately I realized that had to be it, it’s an LED matrix! So some quick python code to read in the file and…

Well that doesn’t look right… I think I see “CTF” in there, but… Well no, something went wrong. Back to the circuit! Then I realized, inputs 2,3,7, and 8 are inverted! They go through two transistors! Let’s invert those inputs in the code!

There it is! We got a flag!

Here’s the code I used to generate the correct flag.

from PIL import Image

#Black and white pixels
OFF = (0,0,0)
ON = (255,255,255)

#Read in the file and split it into "letters"
with open("Message.txt", 'r') as f:
    blocks = f.read().split("\n\n")

#Create an empty image with the right dimensions
img = Image.new("RGB", (len(blocks)*8,8))

for n,block in enumerate(blocks):
    #Set the x offset depending on the character
    dx = n*8
    
    #Split the block into lines only using positive clocked lines
    #We don't care about the zero lines
    chunked = []
    for line in block.split("\n"):
        if line[0] == "1":
            chunked.append(line.split(" ")[1:])

    print(chunked)
    
    #Draw the pixels
    for y,chunk in enumerate(chunked):
        for x,val in enumerate(chunk):
            #Should this one be inverted?
            if x in [1,2,6,7]:
                if val == "0":
                    pix = OFF
                else:
                    pix = ON
            else:
                if val == "1":
                    pix = OFF
                else:
                    pix = ON

            img.putpixel((x+dx,y), pix)

img.save("solved.png")

~T