From 75e13cc39de16b778940c6759d50bf6928eb125d Mon Sep 17 00:00:00 2001 From: Maxime Vorwerk Date: Thu, 1 Aug 2024 00:51:52 +0200 Subject: [PATCH] XtraORdinary --- xtraordinary/encrypt.py | 43 +++++++++++++++++++++++++++++++ xtraordinary/output.txt | 1 + xtraordinary/sol.py | 57 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+) create mode 100755 xtraordinary/encrypt.py create mode 100755 xtraordinary/output.txt create mode 100755 xtraordinary/sol.py diff --git a/xtraordinary/encrypt.py b/xtraordinary/encrypt.py new file mode 100755 index 0000000..e99d606 --- /dev/null +++ b/xtraordinary/encrypt.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 + +from random import randint + +with open('flag.txt', 'rb') as f: + flag = f.read() + +with open('secret-key.txt', 'rb') as f: + key = f.read() + +def encrypt(ptxt, key): + ctxt = b'' + for i in range(len(ptxt)): + a = ptxt[i] + b = key[i % len(key)] + ctxt += bytes([a ^ b]) + return ctxt + +ctxt = encrypt(flag, key) + +random_strs = [ + b'my encryption method', + b'is absolutely impenetrable', + b'and you will never', + b'ever', + b'ever', + b'ever', + b'ever', + b'ever', + b'ever', + b'break it' +] + +for random_str in random_strs: + for i in range(randint(0, pow(2, 8))): + for j in range(randint(0, pow(2, 6))): + for k in range(randint(0, pow(2, 4))): + for l in range(randint(0, pow(2, 2))): + for m in range(randint(0, pow(2, 0))): + ctxt = encrypt(ctxt, random_str) + +with open('output.txt', 'w') as f: + f.write(ctxt.hex()) \ No newline at end of file diff --git a/xtraordinary/output.txt b/xtraordinary/output.txt new file mode 100755 index 0000000..b58dce1 --- /dev/null +++ b/xtraordinary/output.txt @@ -0,0 +1 @@ +57657535570c1e1c612b3468106a18492140662d2f5967442a2960684d28017931617b1f3637 \ No newline at end of file diff --git a/xtraordinary/sol.py b/xtraordinary/sol.py new file mode 100755 index 0000000..4fb3d05 --- /dev/null +++ b/xtraordinary/sol.py @@ -0,0 +1,57 @@ +#!python3 +from pwn import * +from itertools import chain, combinations + +choice = 6 +length = 7 + +def powerset(iterable): + "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)" + s = list(iterable) + return chain.from_iterable(combinations(s, r) for r in range(len(s)+1)) + +log.info("reading file") +with open("output.txt", 'r') as f: + c = int(f.read().strip(), 16) +log.info("read in cyphertext: {:x}".format(c)) + +c = pack(c, 'all', 'big', False) + +random_strs = [ + b'my encryption method', + b'is absolutely impenetrable', + b'and you will never', + b'ever', + b'break it' +] + +def decrypt(ptxt, key): + ctxt = b'' + for i in range(len(ptxt)): + a = ptxt[i] + b = key[i % len(key)] + ctxt += bytes([a ^ b]) + return ctxt + +log.info("generating key candidates...") +i = 0 +candidates = [] +for ss in powerset(random_strs): + _c = c + for s in ss: + _c = decrypt(_c, s) + _c = decrypt(_c, b'picoCTF{') + _c = _c.decode() + if _c[0:8].isprintable(): + log.indented("{:2}: {}".format(i, _c[0:8])) + candidates.append((_c[0:8], ss)) + i += 1 + +key = candidates[choice][0][0:length].encode() +decrypt_set = candidates[choice][1] +log.success(key.decode()) +for word in decrypt_set: + c = decrypt(c, word) +c = decrypt(c, key) +log.success("flag found: {}".format(c)) +