ISSessions CTF 2021 Programming - Morse

· 2 min read

Decoding morse code strings and submitting answers programmatically using Python.

Morse

Given a morse code string, convert it back to ASCII, and submit within 3 seconds.

This one needed a bit more work than I could do in bash. Python it is!

Solution

import requests
from lxml import html

MORSE_CODE_DICT = {
    'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.',
    'F': '..-.', 'G': '--.', 'H': '....', 'I': '..', 'J': '.---',
    'K': '-.-', 'L': '.-..', 'M': '--', 'N': '-.', 'O': '---',
    'P': '.--.', 'Q': '--.-', 'R': '.-.', 'S': '...', 'T': '-',
    'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-', 'Y': '-.--',
    'Z': '--..', '1': '.----', '2': '..---', '3': '...--',
    '4': '....-', '5': '.....', '6': '-....', '7': '--...',
    '8': '---..', '9': '----.', '0': '-----', ', ': '--..--',
    '.': '.-.-.-', '?': '..--..', '/': '-..-.', '-': '-....-',
    '(': '-.--.', ')': '-.--.-',
}

def decrypt(message):
    message += ' '
    decipher = ''
    citext = ''
    for letter in message:
        if letter != ' ':
            i = 0
            citext += letter
        else:
            i += 1
            if i == 2:
                decipher += ' '
            else:
                decipher += list(MORSE_CODE_DICT.keys())[
                    list(MORSE_CODE_DICT.values()).index(citext)
                ]
                citext = ''
    return decipher

def morse():
    s = requests.session()
    url = "http://progmorse.ctf.issessions.ca/"
    page = s.get(url)
    tree = html.fromstring(page.content)
    htmlimg = tree.xpath('//p[@class="data"]/text()')
    code = decrypt(htmlimg[0]).lower().strip()
    print(code)
    resp = s.get(url + "?answer=" + code)
    print(resp.content)

This heavily relied on existing StackOverflow code to convert between morse code and ASCII, since I wanted to get this done as soon as possible for some quick points.