Zh3r0 CTF V2 | b00tleg

#zh3ro_CTF_2021

まあこれはやらなくていい

from ptrlib import Socket, xor, chunks

sock = Socket("nc crypto.zh3r0.cf 1111")
sock.sendlineafter(">>> ", "2")
sock.sendlineafter("hex:", b'hello world! Lets get going'.hex())

sock.sendlineafter(">>> ", "2")
sock.sendlineafter("hex:", b'Nothing fancy, just standard bytes_to_int'.hex())

# flag = bytes.fromhex(sock.recvlineafter("flag: ").decode())
# 
# plaintext = bytes([i for i in range(256)])
# sock.sendlineafter(">>> ", "1")
# sock.sendlineafter("hex:", plaintext.hex())
# ciphertext = bytes.fromhex(sock.recvline().decode())
# plain_flag = bytes([plaintext[ciphertext.find(f)] for f in flag])
plain_flag = b'mono substitutions arent that creative'

sock.sendlineafter(">>> ", "2")
sock.sendlineafter("hex:", plain_flag.hex())

# flag_hex = sock.recvlineafter("flag: ").decode()
# known = list(b'creating different substitutions for each char')
# 
# from tqdm import tqdm
# while len(known) < len(flag_hex) // 2:
#     for c in tqdm(range(0x20, 0x7f)):
#         sock.sendlineafter(">>> ", "1")
#         sock.sendlineafter("hex:", bytes(known + [c]).hex())
#         res = sock.recvline().decode()
#         if flag_hex.startswith(res):
#             known.append(c)
#             print(bytes(known))
#             break

plain_flag = b'creating different substitutions for each char'

sock.sendlineafter(">>> ", "2")
sock.sendlineafter("hex:", plain_flag.hex())

flag = b"Glad that you figured out the invariant"

sock.sendlineafter(">>> ", "2")
sock.sendlineafter("hex:", flag.hex())
# flag_hex = sock.recvlineafter("flag: ").decode()
# 
# plaintext = "".join([bytes([x]).hex() * 200 for x in range(0x20, 0x7f)])
# sock.sendlineafter(">>> ", "1")
# sock.sendlineafter("hex:", plaintext)
# res = chunks(sock.recvline().decode(), 4)
# table = {}
# p = 0
# for c in range(0x20, 0x7f):
#     for i in range(200):
#         table[res[200*p+i]] = c
#     p += 1
# 
# flag_chunks = chunks(flag_hex, 4)
# for c in flag_chunks:
#     if c in table:
#         print(chr(table[c]))
#     else:
#         print("[!]" + c)


flag = b'Here we append the key with your shit, please dont tell anyone'

sock.sendlineafter(">>> ", "2")
sock.sendlineafter("hex:", flag.hex())

sock.interactive()