虎符-ctf crypto writeup

1.GM

Since the title describes a GM password system at this time, we found the FM password system cracking method on the Internet https://blog.csdn.net/qq_26816591/article/details/82957481

First establish a quadratic equation of one variable according to n, phin to solve the factors p, q of n, and then bring it into the decryption formula to solve

import gmpy2

phi=9433451661749413225919414595243321311762902037908850954799703396083863718641136503053215995576558003171249192969972864840795298784730553210417983714593764557582927434784915177639731998310891168685999240937407871771369971713515313634198744616074610866924094854671900334810353127446778607137157751925680243990711180904598841255660443214091848674376245163953774717113246203928244509033734184913005865837620134831142880711832256634797590773413831659733615722574830257496801417760337073484838170554497953033487131634973371143357507027731899402777169516770264218656483487045393156894832885628843858316679793205572348688820
n=9433451661749413225919414595243321311762902037908850954799703396083863718641136503053215995576558003171249192969972864840795298784730553210417983714593764557582927434784915177639731998310891168685999240937407871771369971713515313634198744616074610866924094854671900334810353127446778607137157751925680243990905528141072864168544519279897224494849206184262202130305820187569148057247731243651084258194009459936702909655448969693589800987266378249891157940262898554047247605049549997783511107373248462587318323152524969684724690316918761387154882496367769626921299091688377118938693074486325995308403232228282839975697
cc=密文
a=1
b=phi-n-1
c=n
delat=gmpy2.iroot(pow(b,2)-4*a*c,2)
assert delat[1]
p=(delat[0]-b)/(2*a)
print p
assert n%p==0
q=n//p
print c%p
m=""
for i in cc:
	if gmpy2.jacobi(i,p)==1 and gmpy2.jacobi(i,q)==1:
		m=m+"0"
	elif gmpy2.jacobi(i,p)==-1 and gmpy2.jacobi(i,q)==-1:
		m=m+"1"
	else:
		m=m+"k"
print m

2.pell

We found a Pell's Equation problem at this time, we found the solution on the Internet https://brilliant.org/wiki/quadratic-diophantine-equations-pells-equation/

So we only need to blast out the first one, and then we can find x ^ {2} -ny ^ {2} = 1the solutions of the following 149 groups according to the formula

from pwn import *
import hashlib
import math
import gmpy2

def proof(a,b):
	ss="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
	print b
	for i in ss:
		for j in ss:
			for k in ss:
				for o in ss:
					jie=i+j+k+o+a
					ll=hashlib.sha256(jie).hexdigest()
					if ll==b:
						return jie
io=remote("39.97.210.182",61235)
io.recvuntil("+")
tian=io.recvuntil(")")[:-1]
io.recvuntil("== ")
sh256=io.recvuntil("\n")[:-1]
print tian.encode("hex")
print sh256.encode("hex")
io.recvuntil(":")
haha=proof(tian,sh256)
print haha
io.sendline(haha[:4])
io.recvuntil("Where a = ")
a=io.recvuntil(",")[:-1]
io.recvuntil("b = ")[:-1]
b=io.recvuntil("\n")
aint=int(a)
bint=int(b)
print aint,bint
if bint==2:
	print "bintwrong"
	io.interactive()
if gmpy2.iroot(aint,2)[1]:
	print "aintwrong"
	io.interactive()
xjie=[]
yjie=[]
for i in xrange(2,1000000):
	xx=gmpy2.iroot(1+aint*pow(i,2),2)
	if xx[1]:
		xjie.append(xx[0])
		yjie.append(i)
		break
print xjie[0],yjie[0]
io.sendline(str(xjie[0]))
io.sendline(str(yjie[0]))
for i in xrange(1,150):
	sleep(1)
	print i
	xk=xjie[-1]*xjie[0]+aint*yjie[-1]*yjie[0]
	yk=xjie[-1]*yjie[0]+yjie[-1]*xjie[0]
	io.sendline(str(xk))
	io.sendline(str(yk))
	xjie.append(xk)
	yjie.append(yk)
io.interactive()

 

Published 43 original articles · Like 23 · Visits 30,000+

Guess you like

Origin blog.csdn.net/zhang14916/article/details/105639269