#脚本1-小规模 #Sage from sage.modules.free_module_integer import IntegerLattice
row = column = prime =
ma = res =
W = matrix(ZZ, ma) cc = vector(ZZ, res)
# Babai's Nearest Plane algorithm defBabai_closest_vector(M, G, target): small = target for _ inrange(5): for i inreversed(range(M.nrows())): c = ((small * G[i]) / (G[i] * G[i])).round() small -= M[i] * c return target - small
A1 = matrix.identity(column) Ap = matrix.identity(row) * prime B = block_matrix([[Ap], [W]]) lattice = IntegerLattice(B, lll_reduce=True) print("LLL done") gram = lattice.reduced_basis.gram_schmidt()[0] target = vector(ZZ, res) re = Babai_closest_vector(lattice.reduced_basis, gram, target) print("Closest Vector: {}".format(re))
R = IntegerModRing(prime) M = Matrix(R, ma) M = M.transpose()
#脚本2-大规模 #Sage from sage.modules.free_module_integer import IntegerLattice from random import randint import sys from itertools import starmap from operator import mul
# Babai's Nearest Plane algorithm # from: http://mslc.ctf.su/wp/plaidctf-2016-sexec-crypto-300/ defBabai_closest_vector(M, G, target): small = target for _ inrange(1): for i inreversed(range(M.nrows())): c = ((small * G[i]) / (G[i] * G[i])).round() small -= M[i] * c return target - small
m = n = q =
A_values = b_values =
A = matrix(ZZ, m + n, m) for i inrange(m): A[i, i] = q for x inrange(m): for y inrange(n): A[m + y, x] = A_values[x][y] lattice = IntegerLattice(A, lll_reduce=True) print("LLL done") gram = lattice.reduced_basis.gram_schmidt()[0] target = vector(ZZ, b_values) res = Babai_closest_vector(lattice.reduced_basis, gram, target) print("Closest Vector: {}".format(res))
R = IntegerModRing(q) M = Matrix(R, A_values) ingredients = M.solve_right(res)
print("Ingredients: {}".format(ingredients))
for row, b inzip(A_values, b_values): effect = sum(starmap(mul, zip(map(int, ingredients), row))) % q assert(abs(b - effect) < 2 ** 37)
defallpmones(v): returnlen([vj for vj in v if vj in [-1, 0, 1]]) == len(v)
# We generate the lattice of vectors orthogonal to b modulo x0 deforthoLattice(b, x0): m = b.length() M = Matrix(ZZ, m, m)
for i inrange(1, m): M[i, i] = 1 M[1:m, 0] = -b[1:m] * inverse_mod(b[0], x0) M[0, 0] = x0
for i inrange(1, m): M[i, 0] = mod(M[i, 0], x0)
return M
defallones(v): iflen([vj for vj in v if vj in [0, 1]]) == len(v): return v iflen([vj for vj in v if vj in [0, -1]]) == len(v): return -v returnNone
defrecoverBinary(M5): lv = [allones(vi) for vi in M5 if allones(vi)] n = M5.nrows() for v in lv: for i inrange(n): nv = allones(M5[i] - v) if nv and nv notin lv: lv.append(nv) nv = allones(M5[i] + v) if nv and nv notin lv: lv.append(nv) return Matrix(lv)
defkernelLLL(M): n = M.nrows() m = M.ncols() if m < 2 * n: return M.right_kernel().matrix() K = 2 ^ (m // 2) * M.height()
MB = Matrix(ZZ, m + n, m) MB[:n] = K * M MB[n:] = identity_matrix(m)
MB2 = MB.T.LLL().T
assert MB2[:n, : m - n] == 0 Ke = MB2[n:, : m - n].T
return Ke
defattack(m, n, p, h): # This is the Nguyen-Stern attack, based on BKZ in the second step print("n =", n, "m =", m)
iota = 0.035 nx0 = int(2 * iota * n ^ 2 + n * log(n, 2)) print("nx0 =", nx0)
from sage.modules.free_module_integer import IntegerLattice from Crypto.Cipher import AES from base64 import b64encode from hashlib import * from secret import flag import signal
n = 75 m = 150 r = 10 N = 126633165554229521438977290762059361297987250739820462036000284719563379254544315991201997343356439034674007770120263341747898897565056619503383631412169301973302667340133958109
defgen(n, m, r, N): t1 = [ZZ.random_element(-2^15, 2^15) for _ inrange(n*m)] t2 = [ZZ.random_element(N) for _ inrange(r*n)] B = matrix(ZZ, n, m, t1) L = IntegerLattice(B) A = matrix(ZZ, r, n, t2) C = (A * B) % N return L, C
from pwn import * import requests import json import os import gmpy2 from pwnlib.tubes.tube import * from hashlib import * from Crypto.Util.number import * from tqdm import tqdm, trange import random import math from Crypto.Hash import SHA256 from Crypto.Cipher import AES from factordb.factordb import FactorDB from sage.modules.free_module_integer import IntegerLattice import itertools from fastecdsa.curve import Curve from random import getrandbits, shuffle, randint defresultant(p1, p2, var): p1 = p1.change_ring(QQ) p2 = p2.change_ring(QQ) var = var.change_ring(QQ) r = p1.resultant(p2, var) return r.change_ring(F) # r = remote('123.56.87.28', '19962') # context(log_level='debug') # ALPHABET = string.ascii_letters + string.digits # rec = r.recvline().decode() # print(rec) # suffix = rec[rec.find('+'):rec.find(')')][1:].strip() # digest = rec[rec.find('==')+3:-1].strip() # print(f"suffix: {suffix} \ndigest: {digest}")# for i in itertools.product(ALPHABET, repeat=4): # prefix = ''.join(i) # guess = prefix + suffix # if sha256(guess.encode()).hexdigest() == digest: # # log.info(f"Find XXXX: {prefix}") # print((f"Find XXXX: {prefix}")) # break # r.sendline(prefix.encode()) n = 75 m = 150 r = 10 N=126633165554229521438977290762059361297987250739820462036000284719563379254544315991201997343356439034674007770120263341747898897565056619503383631412169301973302667340133958109 withopen('output.txt', 'r') as f: data = f.readlines() for i inrange(len(data)): data[i] = data[i].replace('[', '').replace(']', '').split(' ') tmp = [] for x in data[i]: if x != '': tmp.append(int(x))
data[i] = tmp print(len(tmp)) C = matrix(ZZ, data) A = matrix(ZZ,m+r,m+r) for i inrange(m): A[i,i] = 1 for i inrange(r): for j inrange(m): A[j,i+m] = C[i,j]<<200
A[i+m,i+m] = N<<200
ans = A.LLL() B = matrix(ZZ,n,m) for i inrange(n): assertlist(ans[i][m:]) == [0]*r B[i] = ans[i][:m] # print(B) ans = B.right_kernel().basis() D = matrix(ZZ,ans) # print(D) print('result=') from base64 import b64decode res = D.BKZ(block_size=12)[0] key1 = sha256(str(res).encode()).digest() key2 = sha256(str(-res).encode()).digest() c = 'rX4K8nZnib5PN13ct6AMwTos99Vdnu7gxsdLMZekKu7gEKx862hL9voPRJS+GzGm' c = b64decode(c) aes = AES.new(key1, AES.MODE_ECB) print(aes.decrypt(c)) aes = AES.new(key2, AES.MODE_ECB) print(aes.decrypt(c))