1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
| import ast import math import re import socket import sys from hashlib import sha256, sha512
from Crypto.Cipher import AES from Crypto.Util.Padding import unpad from sage.all import GF, ZZ, EllipticCurve, Matrix, Zmod, block_matrix, identity_matrix, vector, zero_matrix from sage.groups.generic import discrete_log from sage.modules.free_module_integer import IntegerLattice
P = 2**255 - 19 A = 486662 N = 77 SECRET_BITS = 30 * 8 CHOICE_PROMPT = b"Channel your intent (m=message, e=exchange):" MSG_PROMPT = b"Offer your spiritual essence (int):" EXCH_PROMPT = b"Forge your spirit formation (int):"
CANDIDATES = [ { "order": 729380, "x": 54007250996756016609984692531703789983497466652543853648505076115209714422153, "y": 11420948079344624061811117947033700600731869911104045029545364046925771560076, }, { "order": 3017190559154, "x": 39782261549721368487544338429217710875590504917981451751598775240647005804997, "y": 27058902655726988991642482147940031647344453312080849459903019379414322243475, }, { "order": 809863230466, "x": 21339865532795799406936306284210884594099822460002277878372278469343891360746, "y": 32182642312486764320242949115055469439114620593482803708238522045960937213144, }, { "order": 860319, "x": 13550293931699101904714641619878495026615649507264721134559143223932928862386, "y": 11612790386843864019292277240308042421041359280444393244236636574190211403688, }, { "order": 2337977514391113965, "x": 21133093359692079394854028785492482788766880358441260098104401216605063074380, "y": 53530943094771985304629905502141040624590200283766154915618701800559943124530, }, { "order": 888210, "x": 40506358286513472400985913263522669061494354309206609486915726034358945994147, "y": 36220157836862305911637606530382611288202887461403242698515771373540756378673, }, { "order": 1023697, "x": 19484570579711046539651373485362066824795512357288131780385143876336351578393, "y": 52656309065833811451596525672860538290986318899927877747856848400355980031706, }, { "order": 165704196346, "x": 13929110909751300203916093363322431336425294736761806612242601099507985842813, "y": 23276731423880951278521450484902716348381544670150601982335984310115542744750, }, ]
def pack_point(x, y): return (int(x) << 256) | int(y)
def unpack_point(n): return n >> 256, n & ((1 << 256) - 1)
def int_to_vec(value, q): coeffs = [] for _ in range(N): coeffs.append(value % q) value //= q return coeffs
def find_zero_hash_msg(): x = 0 while True: if int(sha512(str(x).encode()).hexdigest(), 16) & 0xFF == 0: return x x += 1
ZERO_HASH_MSG = find_zero_hash_msg()
class Tube: def __init__(self, host, port): self.sock = socket.create_connection((host, port)) self.sock.settimeout(30) self.buf = b""
def recvuntil(self, marker): while marker not in self.buf: chunk = self.sock.recv(4096) if not chunk: raise EOFError("connection closed") self.buf += chunk idx = self.buf.index(marker) + len(marker) out = self.buf[:idx] self.buf = self.buf[idx:] return out
def recvline(self): return self.recvuntil(b"\n")
def sendline(self, data): self.sock.sendall(data + b"\n")
def parse_round_header(data): text = data.decode(errors="ignore") master_pub = int(re.search(r"Celestial Master's Seal:\s*(\d+)", text).group(1)) enc_flag = bytes.fromhex(re.search(r"Heavenly Cipher:\s*([0-9a-fA-F]+)", text).group(1)) q = int(re.search(r"Crystal domain's modulus for today:\s*(\d+)", text).group(1)) return master_pub, enc_flag, q
def validate_secret(q, rows, samples, secret): for row, (_, b) in zip(rows, samples): lhs = sum((ai * si) % q for ai, si in zip(row, secret)) % q err = (b - lhs) % q if err > 3 and q - err > 3: return False return True
def recover_secret_vector(q, samples): rows = [int_to_vec(a_int, q) for a_int, _ in samples] A_full = Matrix(Zmod(q), rows) pivot_rows = list(A_full.pivot_rows()) order = pivot_rows + [i for i in range(len(samples)) if i not in pivot_rows]
A = Matrix(Zmod(q), [rows[i] for i in order]) b = vector(ZZ, [samples[i][1] for i in order])
A1 = A[:N] A2 = A[N:] A1_inv = A1.inverse() Aprime = (A2 * A1_inv).change_ring(ZZ)
extra = len(samples) - N basis = block_matrix( ZZ, [ [identity_matrix(ZZ, N), zero_matrix(ZZ, N, extra)], [Aprime, q * identity_matrix(ZZ, extra)], ], )
lattice = IntegerLattice(basis.transpose(), lll_reduce=True) closest = vector(ZZ, lattice.babai(b)) y1 = vector(Zmod(q), list(closest[:N])) secret = [int(x) for x in (A1_inv * y1)]
if not validate_secret(q, rows, samples, secret): closest = vector(ZZ, lattice.closest_vector(b)) y1 = vector(Zmod(q), list(closest[:N])) secret = [int(x) for x in (A1_inv * y1)] return secret
def export_priv(q, secret): acc = 0 mul = 1 for coeff in secret: acc += coeff * mul mul *= q return acc
def craft_exchange_input(priv, target_x, target_y): low = priv & ((1 << 256) - 1) high = priv & ~((1 << 256) - 1) high_mod = high % P tx = (target_x - high_mod) % P ty = (target_y - high_mod) % P return pack_point(low ^ tx, low ^ ty)
def dlog_on_candidate(candidate, response_int): curve_c = (candidate["y"] * candidate["y"] - candidate["x"] ** 3 - A * candidate["x"]) % P curve = EllipticCurve(GF(P), [A, curve_c]) base = curve(candidate["x"], candidate["y"]) if response_int == 0: return 0 rx, ry = unpack_point(response_int) point = curve(rx, ry) return int(discrete_log(point, base, ord=candidate["order"], operation="+"))
def crt_pair(a1, m1, a2, m2): g = math.gcd(m1, m2) lcm = m1 // g * m2 k = ((a2 - a1) // g) * pow(m1 // g, -1, m2 // g) % (m2 // g) return (a1 + m1 * k) % lcm, lcm
def decrypt_flag(ciphertext, master_sec): key = sha256(str(master_sec).encode()).digest() plain = AES.new(key, AES.MODE_ECB).decrypt(ciphertext) return unpad(plain, 32)
def solve(host, port): io = Tube(host, port) residue, modulus = 0, 1
banner = io.recvuntil(CHOICE_PROMPT) _, enc_flag, q = parse_round_header(banner)
for candidate in CANDIDATES: samples = [] for _ in range(100): io.sendline(b"m") io.recvuntil(MSG_PROMPT) io.sendline(str(ZERO_HASH_MSG).encode()) io.recvuntil(b"Resonance echoes: ") samples.append(ast.literal_eval(io.recvline().decode().strip()))
secret = recover_secret_vector(q, samples) priv = export_priv(q, secret)
io.sendline(b"e") io.recvuntil(EXCH_PROMPT) payload = craft_exchange_input(priv, candidate["x"], candidate["y"]) io.sendline(str(payload).encode()) io.recvuntil(b"Domain resonance: ") response_int = int(io.recvline().decode().strip())
residue_i = dlog_on_candidate(candidate, response_int) residue, modulus = crt_pair(residue, modulus, residue_i, candidate["order"])
if modulus.bit_length() > SECRET_BITS: break
banner = io.recvuntil(CHOICE_PROMPT) _, _, q = parse_round_header(banner)
flag = decrypt_flag(enc_flag, residue) print(flag.decode())
if __name__ == "__main__": solve(sys.argv[1], int(sys.argv[2]))
|