import gmpy2 from Crypto.Util.number import* def transform(x,y): #使用辗转相处将分数 x/y 转为连分数的形式 res=[] while y: res.append(x//y) x,y=y,x%y return res
def continued_fraction(sub_res): numerator,denominator=1,0 for i in sub_res[::-1]: #从sublist的后面往前循环 denominator,numerator=numerator,i*numerator+denominator return denominator,numerator #得到渐进分数的分母和分子,并返回
#求解每个渐进分数 def sub_fraction(x,y): res=transform(x,y) res=list(map(continued_fraction,(res[0:i] for i in range(1,len(res))))) #将连分数的结果逐一截取以求渐进分数 return res
def wienerAttack(t,k): for (p,sumqr) in sub_fraction(t,k): #用一个for循环来注意试探e/n的连续函数的渐进分数,直到找到一个满足条件的渐进分数 if sumqr==0: #可能会出现连分数的第一个为0的情况,排除 continue if isPrime(p): if n%p==0: print(p) print(sumqr) return p, sumqr t = 2**1024 k = 94541588860584895585135152950569493777168309607384495730944110393788712443252059813470464503558980161423182930915955597122997950103392684040352673659694990925903156093591505153081718027169554019948988048641061593654540898258994671824807628660558123733006209479395447337793897155523508261277918178756662618785 n=780382574657056148524126341547161694121139907409040429176771134165303790043856598163799273195157260505524054034596118923390755532760928964966379457317135940979046201401066257918457068510403020146410174895470232276387032511651496790519359024937958635283547294676457588680828221680705802054780628993173199987362419589945445821005688218540209709368995166794607635504501281131700210990592718388166388793182269128127850804650083811982799377308916540691843310867205397 p,sumqr=wienerAttack(t,k)
from Crypto.Util.number import * from pwn import * from tqdm import tqdm def proof_of_work(): rev = r.recvuntil(b"sha256(XXXX+") suffix = r.recv(16).decode() rev = r.recvuntil(b" == ") tar = r.recv(64).decode() def f(x): hashresult = hashlib.sha256(x.encode()+suffix.encode()).hexdigest() return hashresult == tar prefix = util.iters.mbruteforce(f, string.digits + string.ascii_letters, 4, 'upto') r.recvuntil(b'Give me XXXX: ') r.sendline(prefix.encode()) e = 65537
while True: r = remote("43.129.69.35",10002) r.recvuntil(b'n = ') n = int(r.recvline().strip()) S = {pow(i,-e,n):i for i in tqdm(range(1,2**20))} proof_of_work() secret = '' for i in range(4): c = int(r.recvline().split(b'=')[1].strip()) l = inverse(c,n) for j in range(1,2**16): s = l*pow(j,e,n)%n if s in S: secret += hex(S[s]*j)[2:].zfill(8) break print(len(secret),secret) if len(secret)!=32: r.close() continue r.recvuntil(b"Give me the secret:") r.sendline(secret.encode()) r.interactive()