from Pwn4Sage.pwn import * from Crypto.Util.number import * import itertools
#part1 get number sh = remote("node4.anna.nssctf.cn",28597) sh.recvuntil(b"> mod =") p = int(sh.recvline().strip().decode()) r = [] d = [] for i inrange(2): sh.sendline(b"1") sh.recvuntil(b"> r =") r.append(int(sh.recvline().strip().decode())) sh.recvuntil(b"> d =") d.append(int(sh.recvline().strip().decode()))
#part2 bivariate copper defsmall_roots(f, bounds, m=1, d=None): ifnot d: d = f.degree() R = f.base_ring() N = R.cardinality() f /= f.coefficients().pop(0) f = f.change_ring(ZZ) G = Sequence([], f.parent()) for i inrange(m + 1): base = N ^ (m - i) * f ^ i for shifts in itertools.product(range(d), repeat=f.nvariables()): g = base * prod(map(power, f.variables(), shifts)) G.append(g) B, monomials = G.coefficient_matrix() monomials = vector(monomials) factors = [monomial(*bounds) for monomial in monomials] for i, factor inenumerate(factors): B.rescale_col(i, factor) B = B.dense_matrix().LLL() B = B.change_ring(QQ) for i, factor inenumerate(factors): B.rescale_col(i, 1 / factor) H = Sequence([], f.parent().change_ring(QQ)) for h infilter(None, B * monomials): H.append(h) I = H.ideal() if I.dimension() == -1: H.pop() elif I.dimension() == 0: roots = [] for root in I.variety(ring=ZZ): root = tuple(R(root[var]) for var in f.variables()) roots.append(root) return roots return []
#part3 get flag secret = (inverse(int(d[0]+e0),p)-r[0]) % p sh.sendline(b"2") sh.sendline(str(secret).encode()) sh.recvline() sh.recvline() print(sh.recvline())
from Pwn4Sage.pwn import * from Crypto.Util.number import * import itertools
#part1 get number sh = remote("node4.anna.nssctf.cn",28544) sh.recvuntil(b"> mod =") p = int(sh.recvline().strip().decode()) r = [] d = [] enum = 15 for i inrange(enum): sh.sendline(b"1") sh.recvuntil(b"> r =") r.append(int(sh.recvline().strip().decode())) sh.recvuntil(b"> d =") d.append(int(sh.recvline().strip().decode())) A = [r[0]-r[i] for i inrange(1,enum)] B = [d[i]*(r[0]-r[i])+1for i inrange(1,enum)] C = [d[0]*(r[0]-r[i])-1for i inrange(1,enum)] D = [d[0]*d[i]*(r[0]-r[i])+d[0]-d[i] for i inrange(1,enum)]
#part2 use Lattice T = 2^1000 L = Matrix(ZZ,3*enum-1,3*enum-1) for i inrange(enum-1): L[i,i] = 1 L[i,-(enum-1)+i] = A[i]*T
L[2*enum-1,-(enum-1)+i] = D[i]*T L[i+2*enum,-(enum-1)+i] = p*T for i inrange(enum): L[i+enum-1,i+enum-1] = 2^328 L[2*enum-1,2*enum-1] = 2^(328*2)
res = L.LLL()[0] #e0 = int(res[14])//(2^328) e0 = GCD(res[0],res[14])
#part3 get flag secret = (inverse(int(d[0]+e0),p)-r[0]) % p sh.sendline(b"2") sh.sendline(str(secret).encode()) sh.recvline() sh.recvline() print(sh.recvline())
A = [h[i] - 2*xQ[i] for i inrange(1,n)] A0 = [2*(h[0]-xQ[i]) for i inrange(1,n)] B = [2*(h[i]*(h[0]-xQ[i])-2*h[0]*xQ[i]-a-xQ[i]^2) for i inrange(1,n)] B0 = [(h[0]-xQ[i])^2for i inrange(1,n)] C = [h[i]*(h[0]-xQ[i])^2-2*((h[0]^2+a)*xQ[i]+(a+xQ[i]^2)*h[0]+2*b) for i inrange(1,n)]
from Pwn4Sage.pwn import * from Crypto.Util.number import *
#part1 get number sh = remote("node4.anna.nssctf.cn",28005) sh.recvuntil(b"> mod =") p = int(sh.recvline().strip().decode()) sh.recvuntil(b"> a =") a = int(sh.recvline().strip().decode()) sh.recvuntil(b"> b =") b = int(sh.recvline().strip().decode()) sh.recvuntil(b"> R =") R = eval(sh.recvline().strip().decode()) E = EllipticCurve(GF(p), [a, b]) R = E(R)
#part2 send t and -t to construct poly h = [] xQ = [0] #first get error_P sh.sendline(b"1") sh.sendline(b"0") h.append(int(sh.recvline().strip().decode()))
n = 15 for t inrange(1,n): sh.sendline(b"1") sh.sendline(str(t).encode()) h1 = int(sh.recvline().strip().decode()) sh.sendline(b"1") sh.sendline(str(-t).encode()) h2 = int(sh.recvline().strip().decode()) h.append(h1+h2)
xQ.append(int((t*R)[0]))
A = [h[i] - 2*xQ[i] for i inrange(1,n)] A0 = [2*(h[0]-xQ[i]) for i inrange(1,n)] B = [2*(h[i]*(h[0]-xQ[i])-2*h[0]*xQ[i]-a-xQ[i]^2) for i inrange(1,n)] B0 = [(h[0]-xQ[i])^2for i inrange(1,n)] C = [h[i]*(h[0]-xQ[i])^2-2*((h[0]^2+a)*xQ[i]+(a+xQ[i]^2)*h[0]+2*b) for i inrange(1,n)]
#part3 use poly to get Lattice and LLL delta = 2^163 n = n-1 L = Matrix(ZZ,3*n+3,3*n+3)
L[0,0] = delta^3 for i inrange(n+1): L[i+1,i+1] = delta^2 for i inrange(n+1): L[i+n+2,i+n+2] = delta for i inrange(n): L[i+2*n+3,i+2*n+3] = p
for i inrange(n): L[0,i+2*n+3] = C[i] L[1,i+2*n+3] = B[i] L[2+i,i+2*n+3] = B0[i] for i inrange(n): L[n+2,i+2*n+3] = A[i] L[n+2+1+i,i+2*n+3] = A0[i]
res = L.LLL()[0]
#part4 get flag e0 = int(GCD(res[1],res[n+2]) // delta) print(isPrime(e0)) xP = h[0] + e0
from Crypto.Util.number import * import random, os from gmpy2 import * flag = os.getenv('DASFLAG')
mod = random.getrandbits(1024)
# print('> mod =', mod)
defXennyOracle(): m = random.getrandbits(40) e = random.getrandbits(40) c = powmod(m, e, mod) print('> c =', c & 0xffffffff) msg = int(input('Input r:')) if m == msg: returnTrue returnFalse
deftask(): cnt = 0 whileTrue: if XennyOracle(): cnt += 1 if cnt == 100: print('flag: ', flag) task()