diff --git a/receiver/challenges/Phew.py b/receiver/challenges/Phew.py index e8f9ce7..0482406 100644 --- a/receiver/challenges/Phew.py +++ b/receiver/challenges/Phew.py @@ -9,7 +9,7 @@ class Phew(Challenge): flag_location = 'flags/phew.txt' history_location = 'history/phew.txt' - _CONTAINER = "phew_container" + _CONTAINER = "phew_container" _SERVICE_CMD = ["docker", "exec", "-i", _CONTAINER, "python3", "/home/ctfuser/chall/src/chall.py"] _HEX_RE = re.compile(r'^[0-9a-fA-F]+$') @@ -76,6 +76,7 @@ class Phew(Challenge): def check(self): try: + # parity check with open(self.flag_location, 'r') as f: host_flag = f.read().strip() container_flag = self._read_container_flag() @@ -93,12 +94,12 @@ class Phew(Challenge): out = self._read_until(proc, "\n", timeout=3.0) ct_hex = self._expect_hex_field(out, "ct") self._read_until(proc, "> ", timeout=3.0) - self._send_line(proc, "4") + self._send_line(proc, "0") try: proc.wait(timeout=2.0) except subprocess.TimeoutExpired: proc.kill() - raise AssertionError("Program did not exit after option 4 (encrypt)") + raise AssertionError("Program did not exit after exit command (encrypt)") return ct_hex finally: if proc.poll() is None: @@ -115,38 +116,59 @@ class Phew(Challenge): out = self._read_until(proc, "\n", timeout=3.0) pt_hex = self._expect_hex_field(out, "pt") self._read_until(proc, "> ", timeout=3.0) - self._send_line(proc, "4") + self._send_line(proc, "0") try: proc.wait(timeout=2.0) except subprocess.TimeoutExpired: proc.kill() - raise AssertionError("Program did not exit after option 4 (decrypt)") + raise AssertionError("Program did not exit after exit command (decrypt)") return pt_hex finally: if proc.poll() is None: proc.kill() - def run_bingo_once() -> str: + def run_keyct_once() -> str: proc = self._spawn() try: self._read_until(proc, "> ", timeout=10.0) - self._send_line(proc, "2") + self._send_line(proc, "4") out = self._read_until(proc, "\n", timeout=3.0) ct_hex = self._expect_hex_field(out, "ct") - # exit fast self._read_until(proc, "> ", timeout=3.0) - self._send_line(proc, "4") + self._send_line(proc, "0") try: proc.wait(timeout=2.0) except subprocess.TimeoutExpired: proc.kill() - raise AssertionError("Program did not exit after option 4 (bingo)") + raise AssertionError("Program did not exit after exit command (keyct)") return ct_hex finally: if proc.poll() is None: proc.kill() - ct1 = run_encrypt_once("414243444546") + def run_bingo_reject_wrong_key(): + wrong_key_hex = "00" * 66 + proc = self._spawn() + try: + self._read_until(proc, "> ", timeout=10.0) + self._send_line(proc, "2") + self._read_until(proc, "key (hex)", timeout=3.0) + self._read_until(proc, "> ", timeout=3.0) + self._send_line(proc, wrong_key_hex) + line = self._read_until(proc, "\n", timeout=3.0).strip() + assert "nope" in line.lower(), f"bingo did not reject wrong key; got: {line!r}" + self._read_until(proc, "> ", timeout=3.0) + self._send_line(proc, "0") + try: + proc.wait(timeout=2.0) + except subprocess.TimeoutExpired: + proc.kill() + raise AssertionError("Program did not exit after exit command (bingo)") + finally: + if proc.poll() is None: + proc.kill() + + ct1 = run_encrypt_once("414243444546") assert ct1 and self._HEX_RE.match(ct1), "encrypt(1) did not return hex" self.logger.info("[ok] encrypt produced hex") @@ -161,31 +183,14 @@ class Phew(Challenge): assert ct_a.lower() != ct_b.lower(), "Encryption appears deterministic for same plaintext" self.logger.info("[ok] encrypt randomness") - b1 = run_bingo_once() - b2 = run_bingo_once() - assert b1.lower() != b2.lower(), "Bingo ciphertexts reused randomness" - self.logger.info("[ok] bingo randomness") + k1 = run_keyct_once() + k2 = run_keyct_once() + assert k1.lower() != k2.lower(), "key? ciphertexts reused randomness" + self.logger.info("[ok] key? randomness") - # Skip alarm test during SLA checks (takes 10+ seconds and may be patched) - # import time as _time - # start = _time.time() - # proc_alarm = self._spawn() - # try: - # try: - # proc_alarm.wait(timeout=20.0) - # except subprocess.TimeoutExpired: - # proc_alarm.kill() - # raise AssertionError("Alarm did not fire within 20s (possible patch/removal)") - # elapsed = _time.time() - start - # self.logger.info(f"[info] observed lifetime for alarm: {elapsed:.2f}s") - # if not (8.0 <= elapsed <= 12.0): - # raise AssertionError(f"Alarm window changed (expected ~10s): observed {elapsed:.2f}s") - # self.logger.info("[ok] signal.alarm(10) integrity") - # finally: - # if proc_alarm.poll() is None: - # proc_alarm.kill() + run_bingo_reject_wrong_key() + self.logger.info("[ok] bingo rejects wrong key") - # self.logger.info('Check passed for phew (custom cipher + alarm intact)') return True except Exception as e: diff --git a/services/phew/dist/chall.py b/services/phew/dist/chall.py index 64d2935..5816d6e 100644 --- a/services/phew/dist/chall.py +++ b/services/phew/dist/chall.py @@ -1,31 +1,56 @@ #!/usr/bin/env python3 -import signal +import os from Pailier import * from Crypto.Util.number import * -signal.alarm(50) + with open("/flag.txt", "rb") as f: - flag = f.read() -flag = bytes_to_long(flag) + flag_bytes = f.read() + +key = os.urandom(66) +key_int = bytes_to_long(key) + cipher = pailier() + while True: print("1. encrypt") print("2. bingo") print("3. decrypt") - print("4. exit") + print("4. key?") inp = int(input("> ")) - if inp==1: + + if inp == 1: print("pt (hex)") inp = input("> ") - ct = cipher.encrypt(int(inp,16)) - print('ct : ','{0:x}'.format(ct)) - elif inp==2: - ct = cipher.encrypt(flag) - print('ct : ','{0:x}'.format(ct)) - elif inp==3: + ct = cipher.encrypt(int(inp, 16)) + print('ct : ', '{0:x}'.format(ct)) + + elif inp == 2: + print("key (hex)") + user_hex = input("> ").strip() + try: + user_key = bytes.fromhex(user_hex) + except Exception: + print("nope") + continue + + if len(user_key) == 66 and user_key == key: + try: + print(flag_bytes.decode()) + except Exception: + print(flag_bytes.hex()) + else: + print("nope") + + elif inp == 3: print("ct (hex)") inp = input("> ") - pt = cipher.decrypt(int(inp,16)) - print('pt : ','{0:x}'.format(pt)) + pt = cipher.decrypt(int(inp, 16)) + print('pt : ', '{0:x}'.format(pt)) + + elif inp == 4: + ct = cipher.encrypt(key_int) + print('ct : ', '{0:x}'.format(ct)) + else: exit() diff --git a/services/phew/src/chall.py b/services/phew/src/chall.py index 64d2935..5816d6e 100644 --- a/services/phew/src/chall.py +++ b/services/phew/src/chall.py @@ -1,31 +1,56 @@ #!/usr/bin/env python3 -import signal +import os from Pailier import * from Crypto.Util.number import * -signal.alarm(50) + with open("/flag.txt", "rb") as f: - flag = f.read() -flag = bytes_to_long(flag) + flag_bytes = f.read() + +key = os.urandom(66) +key_int = bytes_to_long(key) + cipher = pailier() + while True: print("1. encrypt") print("2. bingo") print("3. decrypt") - print("4. exit") + print("4. key?") inp = int(input("> ")) - if inp==1: + + if inp == 1: print("pt (hex)") inp = input("> ") - ct = cipher.encrypt(int(inp,16)) - print('ct : ','{0:x}'.format(ct)) - elif inp==2: - ct = cipher.encrypt(flag) - print('ct : ','{0:x}'.format(ct)) - elif inp==3: + ct = cipher.encrypt(int(inp, 16)) + print('ct : ', '{0:x}'.format(ct)) + + elif inp == 2: + print("key (hex)") + user_hex = input("> ").strip() + try: + user_key = bytes.fromhex(user_hex) + except Exception: + print("nope") + continue + + if len(user_key) == 66 and user_key == key: + try: + print(flag_bytes.decode()) + except Exception: + print(flag_bytes.hex()) + else: + print("nope") + + elif inp == 3: print("ct (hex)") inp = input("> ") - pt = cipher.decrypt(int(inp,16)) - print('pt : ','{0:x}'.format(pt)) + pt = cipher.decrypt(int(inp, 16)) + print('pt : ', '{0:x}'.format(pt)) + + elif inp == 4: + ct = cipher.encrypt(key_int) + print('ct : ', '{0:x}'.format(ct)) + else: exit()