fixed phew
This commit is contained in:
+39
-34
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user