updated sheesh
This commit is contained in:
Vendored
+81
-92
@@ -1,109 +1,98 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import os
|
||||
import binascii
|
||||
import hashlib
|
||||
import signal
|
||||
import os, sys, signal, binascii, random
|
||||
from Crypto.Cipher import AES
|
||||
from Crypto.Util.Padding import pad, unpad
|
||||
from Crypto.Util.number import bytes_to_long, long_to_bytes
|
||||
|
||||
signal.alarm(50)
|
||||
seed_bits = 23
|
||||
seed_max = 1 << seed_bits
|
||||
seed_len = (seed_bits + 7) // 8
|
||||
key = os.urandom(16)
|
||||
random.seed(os.urandom(16))
|
||||
K0 = os.urandom(16)
|
||||
K1 = os.urandom(16)
|
||||
S0 = os.urandom(16)
|
||||
S1 = os.urandom(16)
|
||||
M0 = os.urandom(16)
|
||||
M1 = os.urandom(16)
|
||||
|
||||
def hash_seed(seed_int: int) -> bytes:
|
||||
sb = seed_int.to_bytes(seed_len, "big")
|
||||
return hashlib.sha256(sb).digest()[:16]
|
||||
|
||||
seed = int.from_bytes(os.urandom(4), "big") % seed_max
|
||||
seed2 = int.from_bytes(os.urandom(4), "big") % seed_max
|
||||
K1 = hash_seed(seed)
|
||||
K2 = hash_seed(seed2)
|
||||
|
||||
with open("/flag.txt", "rb") as f:
|
||||
with open("/flag.txt","rb") as f:
|
||||
flag = f.read()
|
||||
|
||||
def read_hex(prompt: str):
|
||||
s = input(prompt).strip()
|
||||
try:
|
||||
return binascii.unhexlify(s)
|
||||
except Exception:
|
||||
print("hmm")
|
||||
def hex_input(q):
|
||||
s = input(q).strip()
|
||||
try: return binascii.unhexlify(s)
|
||||
except: print("err"); return None
|
||||
|
||||
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))
|
||||
|
||||
def enc2(iv: bytes, m: bytes) -> bytes:
|
||||
return AES.new(K0, AES.MODE_CBC, iv=iv).encrypt(pad(m, 16))
|
||||
|
||||
def enc3(m: bytes) -> bytes:
|
||||
return AES.new(K1, AES.MODE_CBC, iv=b"\x00"*16).encrypt(pad(m, 16))[-16:]
|
||||
|
||||
def T(iv: bytes, ct: bytes):
|
||||
n = len(ct)
|
||||
if n < 96 or (n & 15):
|
||||
return None
|
||||
v = memoryview(ct)
|
||||
W = [bytes(v[i:i+16]) for i in range(0, n, 16)]
|
||||
m = len(W)
|
||||
|
||||
r = ((iv[0] & 7) + 2) % m
|
||||
if r:
|
||||
W = W[r:] + W[:r]
|
||||
|
||||
j = 1 + (W[0][0] & 1)
|
||||
if len(W) <= j:
|
||||
return None
|
||||
del W[j]
|
||||
if len(W) < 2:
|
||||
return None
|
||||
|
||||
def enc_cfb(pt: bytes) -> bytes:
|
||||
iv = os.urandom(16)
|
||||
aes = AES.new(key, AES.MODE_CFB, iv=iv, segment_size=128)
|
||||
ct = aes.encrypt(pt)
|
||||
return iv + ct
|
||||
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:]))
|
||||
|
||||
def enc_cbc(data: bytes, iv1: bytes, iv2: bytes, padd: bool) -> bytes:
|
||||
x = pad(data, 16) if padd else data
|
||||
c1 = AES.new(K1, AES.MODE_CBC, iv=iv1).encrypt(x)
|
||||
c2 = AES.new(K2, AES.MODE_CBC, iv=iv2).encrypt(c1)
|
||||
return c2
|
||||
def C(iv: bytes, ct: bytes) -> bool:
|
||||
z = T(iv, ct)
|
||||
if z is None:
|
||||
ok = False
|
||||
else:
|
||||
try:
|
||||
x = AES.new(K0, AES.MODE_CBC, iv=iv).decrypt(z)
|
||||
unpad(x, 16)
|
||||
ok = True
|
||||
except:
|
||||
ok = False
|
||||
if random.random() < 0.08:
|
||||
ok = not ok
|
||||
return ok
|
||||
|
||||
def menu():
|
||||
print("""
|
||||
1. encrypt
|
||||
2. profit
|
||||
3. get third
|
||||
4. exit
|
||||
""")
|
||||
|
||||
third = 0
|
||||
iv11 = None
|
||||
iv22 = None
|
||||
iv = os.urandom(16)
|
||||
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)
|
||||
|
||||
print("iv:", iv.hex())
|
||||
print("ct:", ct.hex())
|
||||
print()
|
||||
|
||||
while True:
|
||||
menu()
|
||||
op = input("> ").strip()
|
||||
|
||||
if op == "1":
|
||||
data = read_hex("pt: ")
|
||||
if data is None:
|
||||
print()
|
||||
continue
|
||||
out = enc_cfb(data)
|
||||
print("ct: ", out.hex())
|
||||
print()
|
||||
|
||||
elif op == "2":
|
||||
if iv11 is not None and iv22 is not None:
|
||||
iv1, iv2 = iv11, iv22
|
||||
iv11 = iv22 = None
|
||||
try:
|
||||
blob = hex_input("blob: ")
|
||||
if blob is None:
|
||||
print("err\n"); continue
|
||||
L = len(blob)
|
||||
if L == 16:
|
||||
y = enc1(blob)
|
||||
print("blk:", y.hex()); print()
|
||||
elif L >= 32 and (L % 16) == 0:
|
||||
iv, ct = blob[:16], blob[16:]
|
||||
print("ok\n" if C(iv, ct) else "no\n")
|
||||
else:
|
||||
iv1 = os.urandom(16)
|
||||
iv2 = os.urandom(16)
|
||||
ct = enc_cbc(flag, iv1, iv2, padd=True)
|
||||
print("iv1: ", iv1.hex())
|
||||
print("iv2: ", iv2.hex())
|
||||
print("ct: ", ct.hex())
|
||||
print()
|
||||
|
||||
elif op == "3":
|
||||
if third:
|
||||
print("sheesh")
|
||||
continue
|
||||
block = read_hex("pt: ")
|
||||
if block is None:
|
||||
print()
|
||||
continue
|
||||
if len(block) != 16:
|
||||
print("hmmm\n")
|
||||
continue
|
||||
iv1 = os.urandom(16)
|
||||
iv2 = os.urandom(16)
|
||||
ct = enc_cbc(block, iv1, iv2, padd=False)
|
||||
iv11, iv22 = iv1, iv2
|
||||
print("ct: ", ct.hex())
|
||||
third = 1
|
||||
print()
|
||||
|
||||
elif op == "4":
|
||||
print("err\n")
|
||||
except EOFError:
|
||||
break
|
||||
else:
|
||||
print("mabokkkk?")
|
||||
|
||||
@@ -1,109 +1,98 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import os
|
||||
import binascii
|
||||
import hashlib
|
||||
import signal
|
||||
import os, sys, signal, binascii, random
|
||||
from Crypto.Cipher import AES
|
||||
from Crypto.Util.Padding import pad, unpad
|
||||
from Crypto.Util.number import bytes_to_long, long_to_bytes
|
||||
|
||||
signal.alarm(50)
|
||||
seed_bits = 23
|
||||
seed_max = 1 << seed_bits
|
||||
seed_len = (seed_bits + 7) // 8
|
||||
key = os.urandom(16)
|
||||
random.seed(os.urandom(16))
|
||||
K0 = os.urandom(16)
|
||||
K1 = os.urandom(16)
|
||||
S0 = os.urandom(16)
|
||||
S1 = os.urandom(16)
|
||||
M0 = os.urandom(16)
|
||||
M1 = os.urandom(16)
|
||||
|
||||
def hash_seed(seed_int: int) -> bytes:
|
||||
sb = seed_int.to_bytes(seed_len, "big")
|
||||
return hashlib.sha256(sb).digest()[:16]
|
||||
|
||||
seed = int.from_bytes(os.urandom(4), "big") % seed_max
|
||||
seed2 = int.from_bytes(os.urandom(4), "big") % seed_max
|
||||
K1 = hash_seed(seed)
|
||||
K2 = hash_seed(seed2)
|
||||
|
||||
with open("/flag.txt", "rb") as f:
|
||||
with open("/flag.txt","rb") as f:
|
||||
flag = f.read()
|
||||
|
||||
def read_hex(prompt: str):
|
||||
s = input(prompt).strip()
|
||||
try:
|
||||
return binascii.unhexlify(s)
|
||||
except Exception:
|
||||
print("hmm")
|
||||
def hex_input(q):
|
||||
s = input(q).strip()
|
||||
try: return binascii.unhexlify(s)
|
||||
except: print("err"); return None
|
||||
|
||||
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))
|
||||
|
||||
def enc2(iv: bytes, m: bytes) -> bytes:
|
||||
return AES.new(K0, AES.MODE_CBC, iv=iv).encrypt(pad(m, 16))
|
||||
|
||||
def enc3(m: bytes) -> bytes:
|
||||
return AES.new(K1, AES.MODE_CBC, iv=b"\x00"*16).encrypt(pad(m, 16))[-16:]
|
||||
|
||||
def T(iv: bytes, ct: bytes):
|
||||
n = len(ct)
|
||||
if n < 96 or (n & 15):
|
||||
return None
|
||||
v = memoryview(ct)
|
||||
W = [bytes(v[i:i+16]) for i in range(0, n, 16)]
|
||||
m = len(W)
|
||||
|
||||
r = ((iv[0] & 7) + 2) % m
|
||||
if r:
|
||||
W = W[r:] + W[:r]
|
||||
|
||||
j = 1 + (W[0][0] & 1)
|
||||
if len(W) <= j:
|
||||
return None
|
||||
del W[j]
|
||||
if len(W) < 2:
|
||||
return None
|
||||
|
||||
def enc_cfb(pt: bytes) -> bytes:
|
||||
iv = os.urandom(16)
|
||||
aes = AES.new(key, AES.MODE_CFB, iv=iv, segment_size=128)
|
||||
ct = aes.encrypt(pt)
|
||||
return iv + ct
|
||||
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:]))
|
||||
|
||||
def enc_cbc(data: bytes, iv1: bytes, iv2: bytes, padd: bool) -> bytes:
|
||||
x = pad(data, 16) if padd else data
|
||||
c1 = AES.new(K1, AES.MODE_CBC, iv=iv1).encrypt(x)
|
||||
c2 = AES.new(K2, AES.MODE_CBC, iv=iv2).encrypt(c1)
|
||||
return c2
|
||||
def C(iv: bytes, ct: bytes) -> bool:
|
||||
z = T(iv, ct)
|
||||
if z is None:
|
||||
ok = False
|
||||
else:
|
||||
try:
|
||||
x = AES.new(K0, AES.MODE_CBC, iv=iv).decrypt(z)
|
||||
unpad(x, 16)
|
||||
ok = True
|
||||
except:
|
||||
ok = False
|
||||
if random.random() < 0.08:
|
||||
ok = not ok
|
||||
return ok
|
||||
|
||||
def menu():
|
||||
print("""
|
||||
1. encrypt
|
||||
2. profit
|
||||
3. get third
|
||||
4. exit
|
||||
""")
|
||||
|
||||
third = 0
|
||||
iv11 = None
|
||||
iv22 = None
|
||||
iv = os.urandom(16)
|
||||
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)
|
||||
|
||||
print("iv:", iv.hex())
|
||||
print("ct:", ct.hex())
|
||||
print()
|
||||
|
||||
while True:
|
||||
menu()
|
||||
op = input("> ").strip()
|
||||
|
||||
if op == "1":
|
||||
data = read_hex("pt: ")
|
||||
if data is None:
|
||||
print()
|
||||
continue
|
||||
out = enc_cfb(data)
|
||||
print("ct: ", out.hex())
|
||||
print()
|
||||
|
||||
elif op == "2":
|
||||
if iv11 is not None and iv22 is not None:
|
||||
iv1, iv2 = iv11, iv22
|
||||
iv11 = iv22 = None
|
||||
try:
|
||||
blob = hex_input("blob: ")
|
||||
if blob is None:
|
||||
print("err\n"); continue
|
||||
L = len(blob)
|
||||
if L == 16:
|
||||
y = enc1(blob)
|
||||
print("blk:", y.hex()); print()
|
||||
elif L >= 32 and (L % 16) == 0:
|
||||
iv, ct = blob[:16], blob[16:]
|
||||
print("ok\n" if C(iv, ct) else "no\n")
|
||||
else:
|
||||
iv1 = os.urandom(16)
|
||||
iv2 = os.urandom(16)
|
||||
ct = enc_cbc(flag, iv1, iv2, padd=True)
|
||||
print("iv1: ", iv1.hex())
|
||||
print("iv2: ", iv2.hex())
|
||||
print("ct: ", ct.hex())
|
||||
print()
|
||||
|
||||
elif op == "3":
|
||||
if third:
|
||||
print("sheesh")
|
||||
continue
|
||||
block = read_hex("pt: ")
|
||||
if block is None:
|
||||
print()
|
||||
continue
|
||||
if len(block) != 16:
|
||||
print("hmmm\n")
|
||||
continue
|
||||
iv1 = os.urandom(16)
|
||||
iv2 = os.urandom(16)
|
||||
ct = enc_cbc(block, iv1, iv2, padd=False)
|
||||
iv11, iv22 = iv1, iv2
|
||||
print("ct: ", ct.hex())
|
||||
third = 1
|
||||
print()
|
||||
|
||||
elif op == "4":
|
||||
print("err\n")
|
||||
except EOFError:
|
||||
break
|
||||
else:
|
||||
print("mabokkkk?")
|
||||
|
||||
Reference in New Issue
Block a user