swiss hacking challenge 2024 - office-encryption
Difficulty: baby
Category: crypto
Author: xnull
I heard about this nation state actor I’m not sure why actors would attack companies but we need encryption to secure our systems. Please add the encryption program I made to every software we had so we are secure!
Files
We are given a encrypt.py
containing the following:
from random import shuffle
from collections import Counter
def generate_substitution_cipher(text):
alphabet = "abcdefghijklmnopqrstuvwxyz"
shuffled_alphabet = list(alphabet)
shuffle(shuffled_alphabet)
cipher_map = {
original: substituted
for original, substituted in zip(alphabet, shuffled_alphabet)
}
encrypted_text = ""
for char in text:
if char.lower() in cipher_map:
encrypted_char = cipher_map[char.lower()]
if char.isupper():
encrypted_char = encrypted_char.upper()
encrypted_text += encrypted_char
else:
encrypted_text += char
return encrypted_text, cipher_map
text = "shc2024{fake_flag}"
encrypted_text, cipher_map = generate_substitution_cipher(text)
print(encrypted_text, cipher_map)
Additionally, there are cipher_map.txt
:
{'a': 'k', 'b': 'n', 'c': 'o', 'd': 'r', 'e': 'v', 'f': 'q', 'g': 'i', 'h': 'w', 'i': 'x', 'j': 'd', 'k': 'h', 'l': 'm', 'm': 'l', 'n': 'y', 'o': 'u', 'p': 'b', 'q': 'f', 'r': 'p', 's': 's', 't': 'z', 'u': 't', 'v': 'a', 'w': 'c', 'x': 'j', 'y': 'g', 'z': 'e'}`
And a cipher.txt
:
swo2024{jytmm_ruvs_opgbzu_mum}
Exploitation
We can just invert the mapping and keep characters that aren’t part of the map:
cipher_map = eval(open("cipher_map.txt").read())
map_reverse = {v: k for k, v in cipher_map.items()}
cipher = open("cipher.txt").read()
for c in cipher:
if c in map_reverse:
print(map_reverse[c], end="")
else:
print(c, end="")
Flag
shc2024{xnull_does_crypto_lol}
Conclusion
Was one of the “harder” baby challenges, as I had to write an actual solve script. Great job btw @xnull