Files
attack-defense-platform/services/sheesh/src/chall.py
T

98 lines
2.3 KiB
Python
Raw Normal View History

2025-10-25 14:18:49 +07:00
#!/usr/bin/env python3
2025-10-27 08:26:11 +07:00
import os, sys, signal, binascii, random
2025-10-25 14:18:49 +07:00
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
2025-10-27 00:53:16 +07:00
from Crypto.Util.number import bytes_to_long, long_to_bytes
2025-10-25 14:18:49 +07:00
2025-10-27 00:53:16 +07:00
random.seed(os.urandom(16))
2025-10-27 08:26:11 +07:00
K0 = os.urandom(16)
K1 = os.urandom(16)
S0 = os.urandom(16)
S1 = os.urandom(16)
M0 = os.urandom(16)
M1 = os.urandom(16)
2025-10-25 14:18:49 +07:00
2025-10-27 08:30:18 +07:00
with open("/flag.txt","rb") as f:
2025-10-25 14:18:49 +07:00
flag = f.read()
2025-10-27 00:53:16 +07:00
def hex_input(q):
s = input(q).strip()
2025-10-27 08:26:11 +07:00
try: return binascii.unhexlify(s)
2025-10-27 00:53:16 +07:00
except: print("err"); return None
2025-10-27 08:26:11 +07:00
def enc1(b16: bytes) -> bytes:
x = AES.new(K1, AES.MODE_ECB).encrypt(b16)
return bytes(a ^ b for a, b in zip(x, b16))
2025-10-27 00:53:16 +07:00
2025-10-27 08:26:11 +07:00
def enc2(iv: bytes, m: bytes) -> bytes:
return AES.new(K0, AES.MODE_CBC, iv=iv).encrypt(pad(m, 16))
2025-10-27 00:53:16 +07:00
2025-10-27 08:26:11 +07:00
def enc3(m: bytes) -> bytes:
return AES.new(K1, AES.MODE_CBC, iv=b"\x00"*16).encrypt(pad(m, 16))[-16:]
2025-10-27 00:53:16 +07:00
2025-10-27 08:26:11 +07:00
def T(iv: bytes, ct: bytes):
2025-10-27 00:53:16 +07:00
n = len(ct)
2025-10-27 08:26:11 +07:00
if n < 96 or (n & 15):
return None
v = memoryview(ct)
W = [bytes(v[i:i+16]) for i in range(0, n, 16)]
2025-10-27 00:53:16 +07:00
m = len(W)
2025-10-27 08:26:11 +07:00
r = ((iv[0] & 7) + 2) % m
if r:
W = W[r:] + W[:r]
2025-10-27 00:53:16 +07:00
2025-10-27 08:26:11 +07:00
j = 1 + (W[0][0] & 1)
if len(W) <= j:
return None
del W[j]
if len(W) < 2:
return None
2025-10-25 14:18:49 +07:00
2025-10-27 08:26:11 +07:00
a0 = long_to_bytes(bytes_to_long(W[0]) ^ bytes_to_long(M0))
a1 = long_to_bytes(bytes_to_long(W[1]) ^ bytes_to_long(M1))
return b"".join((S0, a0, S1, a1, *W[2:]))
2025-10-25 14:18:49 +07:00
2025-10-27 08:26:11 +07:00
def C(iv: bytes, ct: bytes) -> bool:
z = T(iv, ct)
2025-10-27 00:53:16 +07:00
if z is None:
ok = False
else:
try:
2025-10-27 08:26:11 +07:00
x = AES.new(K0, AES.MODE_CBC, iv=iv).decrypt(z)
unpad(x, 16)
2025-10-27 00:53:16 +07:00
ok = True
except:
ok = False
2025-10-27 08:26:11 +07:00
if random.random() < 0.08:
2025-10-27 00:53:16 +07:00
ok = not ok
return ok
2025-10-25 14:18:49 +07:00
2025-10-27 00:53:16 +07:00
iv = os.urandom(16)
2025-10-27 08:26:11 +07:00
MK = enc3(iv + iv)
H0 = (flag + b"\x00"*16)[:16]
H1 = bytes(a ^ b for a, b in zip(H0, MK))
pt = H1 + flag[16:]
ct = enc2(iv, pt)
2025-10-25 14:18:49 +07:00
2025-10-27 00:53:16 +07:00
print("iv:", iv.hex())
print("ct:", ct.hex())
print()
2025-10-25 14:18:49 +07:00
while True:
2025-10-27 00:53:16 +07:00
try:
2025-10-27 08:26:11 +07:00
blob = hex_input("blob: ")
if blob is None:
print("err\n"); continue
L = len(blob)
if L == 16:
y = enc1(blob)
2025-10-27 00:53:16 +07:00
print("blk:", y.hex()); print()
elif L >= 32 and (L % 16) == 0:
2025-10-27 08:26:11 +07:00
iv, ct = blob[:16], blob[16:]
print("ok\n" if C(iv, ct) else "no\n")
2025-10-25 14:18:49 +07:00
else:
2025-10-27 08:26:11 +07:00
print("err\n")
2025-10-27 00:53:16 +07:00
except EOFError:
2025-10-27 08:26:11 +07:00
break