Blog

Poetry

800 lines of a poem are read out to a socket port. Each line needs to be SHA1 hashed, and sent back.

Impractical to do this manually. I estimated it’d take about 10 seconds per line, or 8000 seconds. Way too much time to waste on a small value challenge.

Solution

Using code I wrote for the Hacktivity CTF, which had a similar challenge, I set it up hash the line. The challenge that I found was “what” the actually desired part of the line is. After a few manual attempts, I found that it wanted everything on the line starting with Line: , excluding the space.

This worked for a few, until I hit an issue. The sneaky challenge designers used UTF-8 for some characters, as they had accents. This foced me to do a UTF-8 decode.

I also fought some gremlins, that because it was UTF-8 encoded, I had to remove the byte array flag from the string. After that, away it ran!

import socket
import hashlib

def poetry():

    HOST = 'challenges.ctf.issessions.ca'
    PORT = 30500
 
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.connect((HOST, PORT))
        while True:
            time.sleep(0.2)
            data = s.recv(8192)
            data = data.decode("utf-8")
            lines = str(data).split("\n")
            print(lines)
            for line in lines:
 
                if "Line: " in line:
                    line = line.replace("Line: ", "")
                    line = line.replace("b'", "")
                    print("_{0}_".format(line))
                    h1 = hashlib.sha1(line.encode("utf-8"))
                    hexdigest = str.encode(h1.hexdigest() + '\n').lower()
                    s.send(hexdigest)