照烟照烟
WP

强网杯-2025

check-little

源码

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
from Crypto.Util.number import *
from Crypto.Util.Padding import pad
from Crypto.Cipher import AES
import os

flag, key = open("secret").read().split("\n")

e = 3

while 1:
p = getPrime(1024)
q = getPrime(1024)
phi = (p - 1) * (q - 1)
if phi % e != 0:
break
N = p * q
c = pow(key, e, N)

iv = os.urandom(16)
ciphertext = (
AES.new(key=long_to_bytes(key)[:16], iv=iv, mode=AES.MODE_CBC)
.encrypt(pad(flag.encode(), 16))
.hex()
)

f = open("output.txt", "w")
f.write(f"N = {N}\n")
f.write(f"c = {c}\n")
f.write(f"iv = {iv}\n")
f.write(f"ciphertext = {ciphertext}\n")

分析

e 好像很小,是不是有关呢?

真的有关吗?

感觉这题很技巧性啊,主要问题在于 m(即 AES 的 key)和 n 不互素,夹带了因子 p,导致算出来的 c=me−tn=(kp)e−tpq=p(kepe−1−tq) 与 n=pq 能直接 gcd 提取出 p。

EXP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
N = 18795243691459931102679430418438577487182868999316355192329142792373332586982081116157618183340526639820832594356060100434223256500692328397325525717520080923556460823312550686675855168462443732972471029248411895298194999914208659844399140111591879226279321744653193556611846787451047972910648795242491084639500678558330667893360111323258122486680221135246164012614985963764584815966847653119900209852482555918436454431153882157632072409074334094233788430465032930223125694295658614266389920401471772802803071627375280742728932143483927710162457745102593163282789292008750587642545379046283071314559771249725541879213
c = 10533300439600777643268954021939765793377776034841545127500272060105769355397400380934565940944293911825384343828681859639313880125620499839918040578655561456321389174383085564588456624238888480505180939435564595727140532113029361282409382333574306251485795629774577583957179093609859781367901165327940565735323086825447814974110726030148323680609961403138324646232852291416574755593047121480956947869087939071823527722768175903469966103381291413103667682997447846635505884329254225027757330301667560501132286709888787328511645949099996122044170859558132933579900575094757359623257652088436229324185557055090878651740
iv = b"\x91\x16\x04\xb9\xf0RJ\xdd\xf7}\x8cW\xe7n\x81\x8d"
ciphertext = "bf87027bc63e69d3096365703a6d47b559e0364b1605092b6473ecde6babeff2"

from Crypto.Util.number import *
from Crypto.Cipher import AES
from math import gcd

p = gcd(c, N)
q = N // p
phi = (p - 1) * (q - 1)
d = pow(3, -1, phi)
key_int = pow(c, d, N)
key = long_to_bytes(key_int)[:16]
print(AES.new(key=key, iv=iv, mode=AES.MODE_CBC).decrypt(bytes.fromhex(ciphertext)))
# flag{m_m4y_6e_divIS1b1e_by_p?!}

ezran

源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from Crypto.Util.number import *
from random import *

f = open("flag.txt", "r")
flag = f.read().encode()

gift = b""
for i in range(3108):
r1 = getrandbits(8)
r2 = getrandbits(16)
x = (pow(r1, 2 * i, 257) & 0xFF) ^ r2
c = long_to_bytes(x, 2)
gift += c

m = list(flag)
for i in range(2025):
shuffle(m)

c = "".join(list(map(chr, m)))

f = open("output.txt", "w")
f.write(f"gift = {bytes_to_long(gift)}\n")
f.write(f"c = {c}\n")

分析

在 3108 次循环中,虽然 pow(r1, 2 * i, 257) 不可知,但是 & 0xFF 只保留了这个结果的低 8 位,高 8 位全为 0,也就是说 r2 = getrandbits(16) 的高 8 位我们是可知的(与 0 异或保留原数)

getrandbits(x) 的本质是截取 MT19937 内部状态字的高 x 位。其内部共有 624×32=19968 个状态比特,而我们拥有 8×3108=24864 比特,解超定方程即可还原出内部状态。

提取高 8 比特时注意索引从 0 开始,即 each_msb = long_to_bytes(gift)[::2]

这里使用 gf2bv 库。最终重建 RNG 状态后 shuffle 逆向打乱顺序数组状态,就能映射出原始 flag。

EXP

参考了 DJ 师傅的 WP

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
import sys
from gf2bv import LinearSystem
from gf2bv.crypto.mt import MT19937
from Crypto.Util.number import *

sys.set_int_max_str_digits(0)
c = ")9Lsu_4s_eb__otEli_nhe_tes5gii5sT@omamkn__ari{efm0__rmu_nt(0Eu3_En_og5rfoh}nkeoToy_bthguuEh7___u"
gift = 0
exec(open("output.txt").read())

# 只保留每两个字节的高位字节
each_msb = long_to_bytes(gift)[::2]
msb_list = list(each_msb)

# 创建生成元 并将其赋给 MT19937 作为内部状态
lin = LinearSystem([32] * 624)
mt = lin.gens()
rng = MT19937(mt)

zeros = []
for o in msb_list:
# 模拟 r1 = getrandbits(8) 调整 rng 位置
rng.getrandbits(8)
# 约束高 8 位和已知 msb 相同
# 异或结果为 0 即相等
zeros.append((rng.getrandbits(16) >> 8) ^ o)
zeros.append(mt[0] ^ 0x80000000)

for sol in lin.solve_all(zeros):
rng = MT19937(sol)
# 将 rng 状态赋到 Random 对象上
pyrand = rng.to_python_random()
RNG = pyrand
# 若调用原版 Random.getstate() 会返回三元组(3, (num1, ..., num623, index), None)
# 这里先取(num1, ..., num623, index) 再 [:-1] 去掉 index 进而只保留状态数
STATE = RNG.getstate()[1][:-1]
# 元组拼接语法 得到(num1, ..., num623, 624)
# 这里的逗号用于说明后者为元组 不能省略
STATE = STATE + (len(STATE),)

# 重设随机数发生器状态
RNG.setstate((3, STATE, None))
for i in range(3108):
RNG.getrandbits(8)
RNG.getrandbits(16)

# 我们没有 flag 但可以拿一串长度相同的下标列表模拟 flag 被混淆后的位置状态
x = [i for i in range(len(c))]
for i in range(2025):
RNG.shuffle(x)
# 比如此时 x [0] = 18 那么就代表 flag 的第一位 f 漂移到了 index 为 18 的位置
res = [None] * len(c)
for i in range(len(c)):
res[x[i]] = c[i]
flag = "".join(res)
if flag.startswith("flag{"):
print(flag)
break
# flag{7hE_numbEr_0f_biT5_i5_Enou9h_@L5o_ThE_r4nk_must_3n0ugh}(some_noise_to_make_sure_you_get_it)

sk

源码

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
from random import randint
from Crypto.Util.number import getPrime, inverse, long_to_bytes, bytes_to_long
from math import gcd
import signal
from secret import flag

def gen_coprime_num(pbits):
lbits = 2 * pbits + 8
lb = 2**lbits
ub = 2**(lbits + 1)
while True:
r = randint(lb, ub)
s = randint(lb, ub)
if gcd(r, s) == 1:
return r, s

def mult_mod(A, B, p):
result = [0] * (len(A) + len(B) - 1)
for i in range(len(A)):
for j in range(len(B)):
result[i + j] = (result[i + j] + A[i] * B[j]) % p

return result

def gen_key(p):
f = [randint(1, 2**128) for i in ":)"]
h = [randint(1, 2**128) for i in ":("]

R1, S1 = gen_coprime_num(p.bit_length())
R2, S2 = gen_coprime_num(p.bit_length())

B = [[randint(1, p - 1) for i in ":("] for j in ":)"]

P = []
for b in B:
P.append(mult_mod(f, b, p))

Q = []
for b in B:
Q.append(mult_mod(h, b, p))

for i in range(len(P)):
for j in range(len(P[i])):
P[i][j] = P[i][j] * R1 % S1
Q[i][j] = Q[i][j] * R2 % S2

sk = [(R1, S1), (R2, S2), f, h, p]
pk = [P, Q, p]

return sk, pk

def encrypt(pk, pt):
P, Q, p = pk
pt = bytes_to_long(pt)

PP = 0
QQ = 0

for i in range(len(P)):
u = randint(1, p)
for j in range(len(P[0])):
PP = PP + P[i][j] * (u * pt**j % p)
QQ = QQ + Q[i][j] * (u * pt**j % p)

return PP, QQ

def decrypt(sk, ct):
RS1, RS2, f, h, p = sk
R1, S1 = RS1
R2, S2 = RS2

P, Q = ct
invR1 = inverse(R1, S1)
invR2 = inverse(R2, S2)
P = (P * invR1 % S1) % p
Q = (Q * invR2 % S2) % p

f0q = f[0] * Q % p
f1q = f[1] * Q % p
h0p = h[0] * P % p
h1p = h[1] * P % p

a = f1q + p - h1p % p
b = f0q + p - h0p % p

pt = -b * inverse(a, p) % p
pt = long_to_bytes(pt)

return pt

signal.alarm(30)
p = getPrime(256)
sk, pk = gen_key(p)
ticket = long_to_bytes(randint(1, p))
enc_ticket = encrypt(pk, ticket)
print(pk)
print(enc_ticket)

for i in range(2):
op = int(input("op>").strip())
if op == 1:
msg = input("pt:").strip().encode()
ct = encrypt(pk, msg)
print(f"ct: {ct}")
elif op == 2:
user_input = input("ct:").strip().split(" ")
if len(user_input) == 2:
ct = [int(user_input[0]), int(user_input[1])]
else:
print("invalid ct.")
break

user_input = input("your f:").strip().split(" ")
if len(user_input) == 2:
user_f = [int(user_input[0]), int(user_input[1])]
else:
print("invalid f.")
break

user_input = input("your h:").strip().split(" ")
if len(user_input) == 2:
user_h = [int(user_input[0]), int(user_input[1])]
else:
print("invalid h.")
break

user_input = input("your R1 S1:").strip().split(" ")
if len(user_input) == 2:
user_r1s1 = [int(user_input[0]), int(user_input[1])]
else:
print("invalid R1 S1.")
break

user_input = input("your R2 S2:").strip().split(" ")
if len(user_input) == 2:
user_r2s2 = [int(user_input[0]), int(user_input[1])]
else:
print("invalid R2 S2.")
break

pt = decrypt((user_r1s1, user_r2s2, user_f, user_h, p), ct)
if pt == ticket:
print(flag)
else:
print(pt.hex())
else:
print("invalid op.")
break

print("bye!")

分析

一个类似 NTRU 的密码体制,我们先关注密钥生成。

程序随机选取两个系数大小为 128bits 的多项式 f=f0+f1x,h=h0+h1x ,接着挑选 520bits 的盲化系数 R,S 和系数大小为 256bits 的多项式列表 B=[b0,b1] ,计算 P=[b0∗f,b1∗f] 和 Q=[b0∗h,b1∗h] 。最后对得到的多项式组系数做盲化处理( Pij⋅R1modS1,Qij⋅R2modS2 )。

对于加密,生成 256bits 的随机数 u ,计算:

PP=∑i=01∑j=02Pij′(ui⋅mjmodp)

其中 P′ 是经过盲化后的多项式组。乍一看可能很奇怪(事实上也确实很奇怪),但是这个式子如果去除模 p 的包裹,就变成了 PP=∑i=01ui∑j=02(Pij′⋅mj) ,也就是代入 x=m ,计算 u0P0′(m)+u1P1′(m) ,相对看着顺眼一些,所以我们要尝试把这个整数域的式子降到有限域中(这一点在解密过程中有体现)

对于解密,计算 R−1(modS) 。由于多项式系数是被 Pij⋅RmodS 混淆过的,且最终的 PP 是多项累加来的,所以我们只需讨论乘以逆元能不能恢复原始的 Pij (即有没有发生模回绕)

由于卷积运算是在模 p 下进行的,所以 Pij<p ,又显然有 ui⋅mj(modp)<p ,故有严格大小关系:

6p2<6×2512<2515<2520<S

因此没有发生模回绕,故用逆元乘以 PP 就能得到 PPdec=∑i=01∑j=02Pij(ui⋅mjmodp) ,再对整体取 modp 就能得到上文的等式 PPdec=u0P0(m)+u1P1(m) 。同理,有 QQdec=u0Q0(m)+u1Q1(m) 。代入 P(m)=f(m)∗b(m) , Q(m)=h(m)∗b(m) ,有:

PPdec=u0b0(m)f(m)+u1b1(m)f(m)=f(m)(u0b0(m)+u1b1(m))QQdec=u0b0(m)h(m)+u1b1(m)h(m)=h(m)(u0b0(m)+u1b1(m))

交叉相乘,有 h(m)⋅PPdec−f(m)⋅QQdec≡0(modp) 。展开 f(m)=f0+f1m,h(m)=h0+h1m 之后就和解密函数最后的代码照应:

(f1QQdec−h1PPdec)m+(f0QQdec−h0PPdec)≡0(modp)
1
2
3
4
f0q = f[0] * Q % p
f1q = f[1] * Q % p
h0p = h[0] * P % p
h1p = h[1] * P % p

记 a=f1QQdec−h1PPdec,b=f0QQdec−h0PPdec ,那么最终解出 m 就是极其简单的同余方程求解:

am+b≡0(modp)

这道题的攻击点在于密文生成时是在整数域进行的,使得明文和密文之间退化成了背包问题。

记 v→=[u0,u0⋅ptmodp,u0⋅pt2modp,u1,u1⋅ptmodp,u1⋅pt2modp,1] , P→=[P0,0,P0,1,P0,2,P1,0,P1,1,P1,2] , Q→=[Q0,0,Q0,1,Q0,2,Q1,0,Q1,1,Q1,2] 。

于是有方程:

∑k=05Pkvk−PP=0∑k=05Qkvk−QQ=0

这里的 Pk 是被混淆后的,大小和 S 相近,520bits。 vk 大概在 256bits。

在格构造方面,左边拼一个单位矩阵把数据偷下来,右边拼两列等式就可以,权重要足够大以确保方程归零。

L=(1000000P0⋅WQ0⋅W0100000P1⋅WQ1⋅W0010000P2⋅WQ2⋅W0001000P3⋅WQ3⋅W0000100P4⋅WQ4⋅W0000010P5⋅WQ5⋅W000000p−PP⋅W−QQ⋅W)

最终得到 (v0,…,v5,p,0,0) 。因为 v1≡v0⋅pt(modp) ,故有 pt=v1⋅v0−1(modp) 。

还原 ticket 之后,由于服务端没有对输入做校验,我们可以直接传入特殊参数。取 R1,R2=1,S1,S2=2 ,密文 ct=[1,1] , f=[ticket,0],h=[0,1] 。这样有:

f0⋅Q≡ticket⋅1≡ticket(modp)f1⋅Q≡0⋅1≡0(modp)h0⋅P≡0⋅1≡0(modp)h1⋅P≡1⋅1≡1(modp)

于是解密结果 ptres≡−ticket⋅(−1)≡ticket(modp) 恒为 ticket。

EXP

规约得到的有可能是齐次解,需要多爆破几次。

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
from pwn import *
from sage.all import *
import ast


def solve(host, port):
io = remote(host, port)

data_list = io.recvuntil(b"op>").decode().split("\n")
sk, enc_ticket = ast.literal_eval(data_list[0]), ast.literal_eval(data_list[1])

P_blind, Q_blind, p = sk[0], sk[1], sk[2]
P_vec, Q_vec = [x for row in P_blind for x in row], [
x for row in Q_blind for x in row
]
PP, QQ = enc_ticket[0], enc_ticket[1]

L = Matrix(ZZ, 7, 9)
W = 2**1024
for i in range(6):
L[i, i] = 1
L[i, 7] = P_vec[i] * W
L[i, 8] = Q_vec[i] * W
L[6, 6] = p
L[6, 7] = -PP * W
L[6, 8] = -QQ * W

L = L.LLL(algorithm="flatter")
for row in L:
if row[-1] == 0 and row[-2] == 0 and abs(row[-3]) == p:
if row[-3] == -p:
row = -row
v0 = int(row[0])
v1 = int(row[1])
break
ticket = (pow(v0, -1, p) * v1) % p
return io, ticket


while True:
io, ticket = solve("localhost", 9999)
io.sendline(b"2")
io.sendlineafter(b"ct:", b"1 1")
io.sendlineafter(b"your f:", f"{ticket} 0".encode())
io.sendlineafter(b"your h:", b"0 1")
io.sendlineafter(b"your R1 S1:", b"1 2")
io.sendlineafter(b"your R2 S2:", b"1 2")
candidate_flag = io.recvline().decode().strip()
if candidate_flag.startswith("flag{"):
print(candidate_flag)
break
io.close()

Blurred

源码

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
from sage.all import *
from sage.misc import prandom
import random
from Crypto.Cipher import AES
from Crypto.Hash import SHA256
from Crypto.Util.Padding import pad

q = 1342261
n = 1031
PR = PolynomialRing(Zmod(q), "x")
x = PR.gens()[0]
Q = PR.quotient(x**n + 1)
flag = b"flag{*****************************}"


def sample(rand):
return (rand.getrandbits(1) - rand.getrandbits(1)) * (
rand.getrandbits(1) * rand.getrandbits(1)
)


def GenNTRU():
f = [sample(prandom) for _ in range(n)]
g1 = [sample(prandom) for _ in range(n)]
g2 = [sample(prandom) for _ in range(n)]
g1x = Q(g1)
g2x = Q(g2)

while True:
fx = Q(f)
try:
h1 = g1x / fx
h2 = g2x / fx
return (h1.lift(), h2.lift(), fx)
except:
prandom.shuffle(f)
continue


for _ in range(20259):
c = int(input("c :"))
if c == 1:
coin = random.getrandbits(1)
if coin == 0:
pk1, pk2, fx = GenNTRU()
else:
pk1, pk2, fx = (
Q.random_element().lift(),
Q.random_element().lift(),
Q([sample(prandom) for _ in range(n)]),
)

print("Hints:", pk1.list(), pk2.list())

elif c == 2:
SHA = SHA256.new()
SHA.update(str(random.getrandbits(256)).encode())
KEY = SHA.digest()
cipher = AES.new(KEY, AES.MODE_ECB)
flag = pad(flag, AES.block_size)
print("Flag:", cipher.encrypt(flag))
else:
break

分析

题目给的是一个二选一 oracle:每轮先掷硬币 coin = random.getrandbits(1),为 0 时返回一对真正的 NTRU 公钥 h1=g1/f,h2=g2/f ( f,g1,g2 都是小系数多项式),为 1 时直接返回两个环上均匀随机的多项式。加密 flag 的 AES 密钥为 sha256(random.getrandombits(256)),与 coin 出自同一个 random 模块。所以分两步:先利用公钥把每一轮的 coin 判出来,再拿这串 coin 还原 MT19937 的状态。

先看判别。关键在于找到把环上等式降维的视角: n=1031 是奇数,故 (−1)n+1=0 ,在 x=−1 处取值与模 xn+1 相容(取 x=1 就不行, 1n+1=2≢0(modq) ),也就是说 p↦p(−1) 是环同态,一举把 1031 维的问题压成一维。对 h1f=g1 、 h2f=g2 两边取值,再交叉相乘消去未知的 f(−1) :

h2(−1)⋅g1(−1)−h1(−1)⋅g2(−1)≡0(modq)

而 sample 生成的系数分布是可以精确算出的: (b1−b2)(b3b4) 中 b3b4 以 34 的概率为 0,即便为 1, b1−b2 也只有一半概率非零,故 P(0)=78,P(±1)=116 。 f(−1) 本质上是 1031 个这种系数的带符号求和,方差 1031×18≈129 ,标准差约 11(目标向量范数中位数不到 18)。于是 (f(−1),g1(−1),g2(−1)) 就是上述同余式的一个极短解,对应三维格:

L=(1h1(−1)h2(−1)0−q000−q)

其中向量 (x,y,z) 满足 y≡x⋅h1(−1),z≡x⋅h2(−1)(modq) 。coin 为 0 时目标向量范数约 20;coin 为 1 时 h1,h2 均匀随机,格的行列式为 q2 ,由高斯启发式最短向量约 q2/3≈1.2×104 量级,差了整整两个数量级,LLL 之后拿 norm < 100 当阈值判 coin 绰绰有余。不过判错一个 coin,后面的方程组就炸了,好在余量很大。


接下来还原 MT19937,套路与 ezran 一脉相承,但有两个事实值得强调:

其一,sage 的 prandom 用的是 randstate 里独立 seed 的 Random 实例,与 stdlib 的 random 互不干扰,所以采样、shuffle 这些操作完全不会碰 random 的流。整场交互中 random 只被消费了两种东西:19968 个 coin(每轮一次 getrandbits(1))和最后一轮的 getrandbits(256),中间没有任何别的消耗,故这串 coin 是连续的输出。

其二,getrandbits(1) 的本质是消耗一整个 32bit 状态字,只把 tempering 后的最高位吐出来,而 MT19937 的 twist 与 tempering 全是 GF(2) 线性变换,所以每个 coin 恰好贡献一条线性方程。

19968=624×32 正是 MT19937 内部状态的比特总数,题目给到 20259 轮,足够榨干整个状态。用 gf2bv 建 624 个 32bit 生成元逐轮塞约束,最后补一条 mt[0] ^ 0x80000000 消掉秩亏方向(同 ezran)。解出状态后重放 19968 次 getrandbits(1) 把这些 coin 消费掉,紧接着的 getrandbits(256) 就是加密 flag 的种子,SHA256 之后 AES-ECB 解密即可。

EXP

实际耗时非常久,主要在于靶机自身生成数据要很长时间

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
from sage.all import *
from pwn import *
from gf2bv import LinearSystem
from gf2bv.crypto.mt import MT19937
from Crypto.Cipher import AES
from hashlib import sha256
from re import findall
from ast import literal_eval

q = 1342261
n = 1031
PR = PolynomialRing(Zmod(q), "x")
x = PR.gens()[0]
Q = PR.quotient(x**n + 1)


def handle(pk1, pk2) -> int:
h1 = Q(pk1)
h2 = Q(pk2)
h1_val = h1.lift()(-1)
h2_val = h2.lift()(-1)
L = Matrix(ZZ, [[1, h1_val, h2_val], [0, -q, 0], [0, 0, -q]])
res = L.LLL()[0]
if int(res.norm()) < 100:
return 0
return 1


def mt19937(coins, enc_flag):
lin = LinearSystem([32] * 624)
mt = lin.gens()
rng = MT19937(mt)
zeros = []
for c in coins:
zeros.append(rng.getrandbits(1) ^ int(c))
zeros.append(mt[0] ^ int(0x80000000))
for sol in lin.solve_all(zeros):
rng = MT19937(sol)
pyrand = rng.to_python_random()
RNG = pyrand
STATE = RNG.getstate()[1][:-1]
STATE = STATE + (len(STATE),)
RNG.setstate((3, STATE, None))
for i in range(19968):
RNG.getrandbits(1)
key = sha256(str(RNG.getrandbits(256)).encode()).digest()
print(AES.new(key=key, mode=AES.MODE_ECB).decrypt(enc_flag))


io = remote("localhost", 9999)

coins = []
for i in range(19968):
print(f"[debug] Times{i}")
io.sendlineafter(b"c :", b"1")
line = io.recvline().decode().strip()
ls = findall(r"\[.*?\]", line)
pk1 = literal_eval(ls[0])
pk2 = literal_eval(ls[1])
coins.append(handle(pk1, pk2))

io.sendlineafter(b"c :", b"2")
enc_flag = literal_eval(io.recvline().decode().strip().split("Flag: ")[1])
mt19937(coins, enc_flag)