-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolve.py
45 lines (37 loc) · 1.04 KB
/
solve.py
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
from pwn import remote, args
host = args.HOST or 'localhost'
port = args.PORT or 31996
r = remote(host, port)
r.recvuntil(b'public key (n, g) = (')
n = int(r.recvuntil(b',', drop=True))
r.recvuntil(b'E(4) = ')
e4 = int(r.recvline())
s = n ** 2
# first generate encryptions of powers of 2 starting at 4
encs = [-1, -1, e4]
# encs[i] = ENC(2^i)
for _ in range(n.bit_length()):
encs.append((encs[-1] * encs[-1]) % s)
def construct(t):
r = 1
i = 0
while t:
if (t & 1) == 1:
r = (r * encs[i]) % s
t >>= 1
i += 1
return r
target = int.from_bytes(b"Please give me the flag", "big")
# n is odd so one of these must be true
# alternatively, could write only one exploit and rerun until it works
# (50% chance each time)
if (n + 1) % 4 == 0:
encs[0] = construct(n + 1)
encs[1] = (encs[0] * encs[0]) % s
m = construct(target)
elif (n + 3) % 4 == 0:
e3 = construct(n + 3)
# note that target ends in 0b11
m = (construct(target ^ 0b11) * construct(n + 3)) % s
r.sendline(str(m))
r.stream()