Skip to content

Instantly share code, notes, and snippets.

@jartigag
Created September 30, 2021 07:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jartigag/b0c35d09876274451a50a8689ddd4151 to your computer and use it in GitHub Desktop.
Save jartigag/b0c35d09876274451a50a8689ddd4151 to your computer and use it in GitHub Desktop.
CSAW - Gotta Decrypt Them All [crypto]
#!/usr/bin/env python3
__authors__ = ['blast_o', 'jartigag']
import base64
import os
import sys
from pwn import *
import time
morseAlphabet = {'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': '--..','0': '-----','1': '.----','2': '..---','3': '...--','4': '....-','5': '.....','6': '-....','7': '--...','8': '---..','9': '----.',
'=': '-...-'}
inverseMorseAlphabet = dict((v, k) for (k, v) in morseAlphabet.items())
exceptions = ["Wobbuffet", "Arctovish", "Bouffalant", "Froakie", "Arctozolt"]
def decodeMorse(code):
output_morse = ""
for letter in code.strip('\n').split(" "):
if "/" in letter:
output_morse += " "
letter = letter.split("/")[1]
if letter in inverseMorseAlphabet:
output_morse += inverseMorseAlphabet[letter]
return output_morse
def cipher_decrypt(ciphertext, key):
print(ciphertext)
decrypted = ""
for c in ciphertext:
if c.isupper():
c_index = ord(c) - ord('A')
# shift the current character to left by key positions to get its original position
c_og_pos = (c_index - key) % 26 + ord('A')
c_og = chr(c_og_pos)
decrypted += c_og
elif c.islower():
c_index = ord(c) - ord('a')
c_og_pos = (c_index - key) % 26 + ord('a')
c_og = chr(c_og_pos)
decrypted += c_og
elif c.isdigit():
# if it's a number,shift its actual value
c_og = (int(c) - key) % 10
decrypted += str(c_og)
else:
# if its neither alphabetical nor a number, just leave it like that
decrypted += c
return decrypted
def fromDecimal(stringNumbers):
output_str = ""
numbers_list = stringNumbers.split(" ")
print(stringNumbers)
for number in numbers_list:
print(number)
output_str += chr(int(number))
return output_str
def fromBase64(base64str):
base64_message = base64str
base64_bytes = base64_message.encode('ascii')
message_bytes = base64.b64decode(base64_bytes)
message = message_bytes.decode('ascii')
return message
def get_chall(p):
"""
function by: @CTFKris
https://ctf.rip/write-ups/crypto/csaw-gottadecrypt/
"""
z = p.recvuntil((b'What does this mean?', b'flag{'))
if b'flag{' in z:
rest = p.recvline()
log.info(z.decode() + rest.decode())
quit()
p.recvline()
chal = p.recvuntil(b'>> ')
return chal[:-3].decode().strip()
if __name__ == '__main__':
# online version:
#
#conn = remote('crypto.chal.csaw.io',5001)
#for i in range(6):
# received = get_chall(conn)
# offline version:
#
received = open('input').read()
word = received.split("What does this mean?\n")[1].strip('\n').strip().strip("\n>>")
#churro = (fromBase64(fromDecimal(decodeMorse(word))))
for w in decodeMorse(word).split(" "): print(w)
n = churro.split("\n")[0].split("=")[1].strip()
c = churro.split("\n")[2].split("=")[1].strip()
result = os.system("python3 RsaCtfTool.py --verbosity DEBUG --attack cube_root -n {n} -e 3 --uncipher {c} --output result.txt".format(n=n, c=c))
with open('result.txt') as result_file:
result_str = result_file.read()
result_cesar = cipher_decrypt(result_str, 13)
os.system("rm result.txt".format(n=n, c=c))
# online version:
#
conn.send(result_cesar + '\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment