fixed phew
This commit is contained in:
+37
-32
@@ -76,6 +76,7 @@ class Phew(Challenge):
|
|||||||
|
|
||||||
def check(self):
|
def check(self):
|
||||||
try:
|
try:
|
||||||
|
# parity check
|
||||||
with open(self.flag_location, 'r') as f:
|
with open(self.flag_location, 'r') as f:
|
||||||
host_flag = f.read().strip()
|
host_flag = f.read().strip()
|
||||||
container_flag = self._read_container_flag()
|
container_flag = self._read_container_flag()
|
||||||
@@ -93,12 +94,12 @@ class Phew(Challenge):
|
|||||||
out = self._read_until(proc, "\n", timeout=3.0)
|
out = self._read_until(proc, "\n", timeout=3.0)
|
||||||
ct_hex = self._expect_hex_field(out, "ct")
|
ct_hex = self._expect_hex_field(out, "ct")
|
||||||
self._read_until(proc, "> ", timeout=3.0)
|
self._read_until(proc, "> ", timeout=3.0)
|
||||||
self._send_line(proc, "4")
|
self._send_line(proc, "0")
|
||||||
try:
|
try:
|
||||||
proc.wait(timeout=2.0)
|
proc.wait(timeout=2.0)
|
||||||
except subprocess.TimeoutExpired:
|
except subprocess.TimeoutExpired:
|
||||||
proc.kill()
|
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
|
return ct_hex
|
||||||
finally:
|
finally:
|
||||||
if proc.poll() is None:
|
if proc.poll() is None:
|
||||||
@@ -115,37 +116,58 @@ class Phew(Challenge):
|
|||||||
out = self._read_until(proc, "\n", timeout=3.0)
|
out = self._read_until(proc, "\n", timeout=3.0)
|
||||||
pt_hex = self._expect_hex_field(out, "pt")
|
pt_hex = self._expect_hex_field(out, "pt")
|
||||||
self._read_until(proc, "> ", timeout=3.0)
|
self._read_until(proc, "> ", timeout=3.0)
|
||||||
self._send_line(proc, "4")
|
self._send_line(proc, "0")
|
||||||
try:
|
try:
|
||||||
proc.wait(timeout=2.0)
|
proc.wait(timeout=2.0)
|
||||||
except subprocess.TimeoutExpired:
|
except subprocess.TimeoutExpired:
|
||||||
proc.kill()
|
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
|
return pt_hex
|
||||||
finally:
|
finally:
|
||||||
if proc.poll() is None:
|
if proc.poll() is None:
|
||||||
proc.kill()
|
proc.kill()
|
||||||
|
|
||||||
def run_bingo_once() -> str:
|
def run_keyct_once() -> str:
|
||||||
proc = self._spawn()
|
proc = self._spawn()
|
||||||
try:
|
try:
|
||||||
self._read_until(proc, "> ", timeout=10.0)
|
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)
|
out = self._read_until(proc, "\n", timeout=3.0)
|
||||||
ct_hex = self._expect_hex_field(out, "ct")
|
ct_hex = self._expect_hex_field(out, "ct")
|
||||||
# exit fast
|
|
||||||
self._read_until(proc, "> ", timeout=3.0)
|
self._read_until(proc, "> ", timeout=3.0)
|
||||||
self._send_line(proc, "4")
|
self._send_line(proc, "0")
|
||||||
try:
|
try:
|
||||||
proc.wait(timeout=2.0)
|
proc.wait(timeout=2.0)
|
||||||
except subprocess.TimeoutExpired:
|
except subprocess.TimeoutExpired:
|
||||||
proc.kill()
|
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
|
return ct_hex
|
||||||
finally:
|
finally:
|
||||||
if proc.poll() is None:
|
if proc.poll() is None:
|
||||||
proc.kill()
|
proc.kill()
|
||||||
|
|
||||||
|
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")
|
ct1 = run_encrypt_once("414243444546")
|
||||||
assert ct1 and self._HEX_RE.match(ct1), "encrypt(1) did not return hex"
|
assert ct1 and self._HEX_RE.match(ct1), "encrypt(1) did not return hex"
|
||||||
self.logger.info("[ok] encrypt produced 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"
|
assert ct_a.lower() != ct_b.lower(), "Encryption appears deterministic for same plaintext"
|
||||||
self.logger.info("[ok] encrypt randomness")
|
self.logger.info("[ok] encrypt randomness")
|
||||||
|
|
||||||
b1 = run_bingo_once()
|
k1 = run_keyct_once()
|
||||||
b2 = run_bingo_once()
|
k2 = run_keyct_once()
|
||||||
assert b1.lower() != b2.lower(), "Bingo ciphertexts reused randomness"
|
assert k1.lower() != k2.lower(), "key? ciphertexts reused randomness"
|
||||||
self.logger.info("[ok] bingo randomness")
|
self.logger.info("[ok] key? randomness")
|
||||||
|
|
||||||
# Skip alarm test during SLA checks (takes 10+ seconds and may be patched)
|
run_bingo_reject_wrong_key()
|
||||||
# import time as _time
|
self.logger.info("[ok] bingo rejects wrong key")
|
||||||
# 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()
|
|
||||||
|
|
||||||
# self.logger.info('Check passed for phew (custom cipher + alarm intact)')
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
Vendored
+39
-14
@@ -1,31 +1,56 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import signal
|
import os
|
||||||
from Pailier import *
|
from Pailier import *
|
||||||
from Crypto.Util.number import *
|
from Crypto.Util.number import *
|
||||||
signal.alarm(50)
|
|
||||||
with open("/flag.txt", "rb") as f:
|
with open("/flag.txt", "rb") as f:
|
||||||
flag = f.read()
|
flag_bytes = f.read()
|
||||||
flag = bytes_to_long(flag)
|
|
||||||
|
key = os.urandom(66)
|
||||||
|
key_int = bytes_to_long(key)
|
||||||
|
|
||||||
cipher = pailier()
|
cipher = pailier()
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
print("1. encrypt")
|
print("1. encrypt")
|
||||||
print("2. bingo")
|
print("2. bingo")
|
||||||
print("3. decrypt")
|
print("3. decrypt")
|
||||||
print("4. exit")
|
print("4. key?")
|
||||||
inp = int(input("> "))
|
inp = int(input("> "))
|
||||||
if inp==1:
|
|
||||||
|
if inp == 1:
|
||||||
print("pt (hex)")
|
print("pt (hex)")
|
||||||
inp = input("> ")
|
inp = input("> ")
|
||||||
ct = cipher.encrypt(int(inp,16))
|
ct = cipher.encrypt(int(inp, 16))
|
||||||
print('ct : ','{0:x}'.format(ct))
|
print('ct : ', '{0:x}'.format(ct))
|
||||||
elif inp==2:
|
|
||||||
ct = cipher.encrypt(flag)
|
elif inp == 2:
|
||||||
print('ct : ','{0:x}'.format(ct))
|
print("key (hex)")
|
||||||
elif inp==3:
|
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)")
|
print("ct (hex)")
|
||||||
inp = input("> ")
|
inp = input("> ")
|
||||||
pt = cipher.decrypt(int(inp,16))
|
pt = cipher.decrypt(int(inp, 16))
|
||||||
print('pt : ','{0:x}'.format(pt))
|
print('pt : ', '{0:x}'.format(pt))
|
||||||
|
|
||||||
|
elif inp == 4:
|
||||||
|
ct = cipher.encrypt(key_int)
|
||||||
|
print('ct : ', '{0:x}'.format(ct))
|
||||||
|
|
||||||
else:
|
else:
|
||||||
exit()
|
exit()
|
||||||
|
|||||||
+39
-14
@@ -1,31 +1,56 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import signal
|
import os
|
||||||
from Pailier import *
|
from Pailier import *
|
||||||
from Crypto.Util.number import *
|
from Crypto.Util.number import *
|
||||||
signal.alarm(50)
|
|
||||||
with open("/flag.txt", "rb") as f:
|
with open("/flag.txt", "rb") as f:
|
||||||
flag = f.read()
|
flag_bytes = f.read()
|
||||||
flag = bytes_to_long(flag)
|
|
||||||
|
key = os.urandom(66)
|
||||||
|
key_int = bytes_to_long(key)
|
||||||
|
|
||||||
cipher = pailier()
|
cipher = pailier()
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
print("1. encrypt")
|
print("1. encrypt")
|
||||||
print("2. bingo")
|
print("2. bingo")
|
||||||
print("3. decrypt")
|
print("3. decrypt")
|
||||||
print("4. exit")
|
print("4. key?")
|
||||||
inp = int(input("> "))
|
inp = int(input("> "))
|
||||||
if inp==1:
|
|
||||||
|
if inp == 1:
|
||||||
print("pt (hex)")
|
print("pt (hex)")
|
||||||
inp = input("> ")
|
inp = input("> ")
|
||||||
ct = cipher.encrypt(int(inp,16))
|
ct = cipher.encrypt(int(inp, 16))
|
||||||
print('ct : ','{0:x}'.format(ct))
|
print('ct : ', '{0:x}'.format(ct))
|
||||||
elif inp==2:
|
|
||||||
ct = cipher.encrypt(flag)
|
elif inp == 2:
|
||||||
print('ct : ','{0:x}'.format(ct))
|
print("key (hex)")
|
||||||
elif inp==3:
|
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)")
|
print("ct (hex)")
|
||||||
inp = input("> ")
|
inp = input("> ")
|
||||||
pt = cipher.decrypt(int(inp,16))
|
pt = cipher.decrypt(int(inp, 16))
|
||||||
print('pt : ','{0:x}'.format(pt))
|
print('pt : ', '{0:x}'.format(pt))
|
||||||
|
|
||||||
|
elif inp == 4:
|
||||||
|
ct = cipher.encrypt(key_int)
|
||||||
|
print('ct : ', '{0:x}'.format(ct))
|
||||||
|
|
||||||
else:
|
else:
|
||||||
exit()
|
exit()
|
||||||
|
|||||||
Reference in New Issue
Block a user