Skip to content

Instantly share code, notes, and snippets.

@auscompgeek
Last active January 2, 2018 05:18
Show Gist options
  • Save auscompgeek/1e60032d1c84ecea81c3 to your computer and use it in GitHub Desktop.
Save auscompgeek/1e60032d1c84ecea81c3 to your computer and use it in GitHub Desktop.
hack.lu CTF - packed - brute-force the xor cipher
#!/usr/bin/python3
# hack.lu CTF - packed - brute-force the xor cipher
CIPHERTEXT = "H51\\\'Ux2J&+(3Z;Uxcx0Xxs\x13h\x014$V!R($R>\t/)R!\x01<.\x13,N-aP4M4aRuG1-VuU0 GuH+a@0W=3R9\x01>(_0\x01,8C0Rx GuN6\"V|\x1ezKZ3\x014$]}R!2\x1d4S?7\x1au\x1fxs\t_\x01xa\x13<Gx)R&Ip2J&\x0f93T#zj\x1c\x1ap\x13rk\x00g\x01e|\x13g\x19ju\x0ba\x18jt\x02o+xa\x13u\x01xa\x13%S1/Gu\x03\x1b.\\:N7.\\:N4o\x13\x0cN-3\x133M9&\x13<Rx A2WjiZ{DvaX0Xjh\x136N6\"R!\x01\x07rC0p\x138a\x1dc22ieu\x161Fw+=-@0\x1bRa\x13u\x01(3Z;UxcR\'F.s\x1c>D!s\x13<Rx,Z&R1/Tw+R"
import itertools
import string
def code(key, cipher):
while len(key) < len(cipher):
key = key * 2
return "".join(map(chr, [ord(a)^ord(b) for a, b in zip(cipher, key)]))
possible_chars = set(string.printable.upper())
possible_key = [[], [], [], [], []]
for i in range(5):
print('finding', i)
for c in possible_chars:
plain = code(c, CIPHERTEXT[i::5])
r = repr(plain)
if '\\x' not in r: # filter out most unprintables
print(c, r)
possible_key[i].append(c)
print()
for i in itertools.product(*possible_key):
key = ''.join(i)
plain = code(key, CIPHERTEXT)
if 'print' in plain: # the program almost certainly prints
print(key)
print(plain)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment