2022-巅峰极客-网络安全技能挑战赛

point-power

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
from Crypto.Util.number import *
from gmpy2 import *
from random import *
from secrets import flag

assert len(flag)==42
p=getPrime(600)
a=bytes_to_long(flag)
b=randrange(2,p-1)
E=EllipticCurve(GF(p),[a,b])
G=E.random_element()

x1,y1,_=G
G=2*G
x2,y2,_=G

print(f"p = {p}")
print(f"b = {b}")
print(f"x1 = {x1}")
print(f"x2 = {x2}")
'''
p = 3660057339895840489386133099442699911046732928957592389841707990239494988668972633881890332850396642253648817739844121432749159024098337289268574006090698602263783482687565322890623
b = 1515231655397326550194746635613443276271228200149130229724363232017068662367771757907474495021697632810542820366098372870766155947779533427141016826904160784021630942035315049381147
x1 = 2157670468952062330453195482606118809236127827872293893648601570707609637499023981195730090033076249237356704253400517059411180554022652893726903447990650895219926989469443306189740
x2 = 1991876990606943816638852425122739062927245775025232944491452039354255349384430261036766896859410449488871048192397922549895939187691682643754284061389348874990018070631239671589727
'''

椭圆曲线基础知识

一般方程:$ y^2=x^3+ax+b $ (忘记了左边是平方,搞了蛮久还不知道自己错哪了)

因为有$(x2,y2)=2(x1,y1)$,所以可以连立方程

有如下等式
$$
\begin{cases} y_1^2=x_1^2+ax_1+b\\
y_2^2=x_2^2+ax_2+b\\
k=\dfrac{3x_1^2+a}{2y_1}\\
x_2=-2x_1+k^2 \end{cases}
$$
化简有
$$
4(x_1^3+ax_1+b)(2x_1+x_2)-(3x_1^2+a)=0
$$
在$GF(p)$上解该方程即可得到 $a$。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from Crypto.Util.number import *

p = 3660057339895840489386133099442699911046732928957592389841707990239494988668972633881890332850396642253648817739844121432749159024098337289268574006090698602263783482687565322890623
b = 1515231655397326550194746635613443276271228200149130229724363232017068662367771757907474495021697632810542820366098372870766155947779533427141016826904160784021630942035315049381147
x1 = 2157670468952062330453195482606118809236127827872293893648601570707609637499023981195730090033076249237356704253400517059411180554022652893726903447990650895219926989469443306189740
x2 = 1991876990606943816638852425122739062927245775025232944491452039354255349384430261036766896859410449488871048192397922549895939187691682643754284061389348874990018070631239671589727
R.<a> = PolynomialRing(Zmod(p), implementation='NTL')

f = 4*(2*x1+x2)*(x1^3 + a*x1 + b) - (3*x1^2+a)^2
f = f.monic()
root = f.roots()
print(root)
for each in root:
m = int(each[0])
print(long_to_bytes(m))

strange curve

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from Crypto.Util.number import *
from gmpy2 import *
from secrets import flag
import random

def add(P,Q):
(x1,y1)=P
(x2,y2)=Q


x3=(x1+x2)*(1+y1*y2)*invert((1+x1*x2)*(1-y1*y2),p)%p
y3=(y1+y2)*(1+x1*x2)*invert((1-x1*x2)*(1+y1*y2),p)%p

return (x3,y3)

def mul(e,P):
Q=(0,0)
e=e%p
while e:
if e&1:
Q=add(Q,P)
P=add(P,P)
e>>=1
return Q

def Legendre(a,p):
return (pow((a%p+p)%p,(p-1)//2,p))%p

def get_ts(p):
p=p-1
count=0
while p%2==0:
count+=1
p=p//2
return count,p

def get_nonre(p):
a=random.randint(1,p)
while Legendre(a,p)==1:
a=random.randint(1,p)
return a

def amm2(a,p):
t,s=get_ts(p)
ta=pow(get_nonre(p),s,p)
tb=pow(a,s,p)
h=1
for i in range(1,t):
d=pow(tb,2**t-1-i,p)
if d==1:
k=0
else:
k=1
tb=(tb*pow(ta,2*k,p))%p
h=(h*pow(ta,k,p))%p
ta=pow(ta,2,p)
return h*pow(a,(s+1)//2,p)%p

def solve(a,b,c,p):
tmpa=1
tmpb=b*inverse(a,p)%p
tmpc=c*inverse(a,p)%p
assert Legendre(tmpb**2*inverse(4,p)-tmpc,p)==1
res1=(amm2(tmpb**2*inverse(4,p)-tmpc,p)-tmpb*inverse(2,p))%p
res2=(-amm2(tmpb**2*inverse(4,p)-tmpc,p)-tmpb*inverse(2,p))%p
return (res1,res2)

def lift(x,a,b,p):
tmp=b*(x**2-1)*inverse(a*x,p)%p
return solve(1,-tmp,-1,p)[0]

p=9410547699903726871336507117271550134683191140146415131394654141737636910570480327296351841515571767317596027931492843621727002889086193529096531342265353
a=54733430689690725746438325219044741824500093621550218736194675295708808435509
b=75237024593957256761258687646797952793573177095902495908321724558796076392871
x=bytes_to_long(flag)

while True:
try:
y=lift(x,a,b,p)
break
except:
x+=1
continue

assert a*x*(y**2-1)%p == b*y*(x**2-1)%p

P=(x,y)
e=65537

eP=mul(e,P)
print(f"P = {P}")
print(f"eP = {eP}")

'''
P = (56006392793427940134514899557008545913996191831278248640996846111183757392968770895731003245209281149, 5533217632352976155681815016236825302418119286774481415122941272968513081846849158651480192550482691343283818244963282636939305751909505213138032238524899)
eP = (mpz(8694229840573103722999959579565187489450818138005222030156495740841851804943200684116883831426548909867463656993852596745698999492932194245562062558787005), mpz(9279986963919197374405152604360936066932975197577643570458423456304679111057526702737279809805694360981565554506626018364382736924914907001214909905449002))
'''

非预期解。根据下面代码

1
2
3
4
5
6
7
while True:
try:
y=lift(x,a,b,p)
break
except:
x+=1
continue

所以我们只要对 $x$ 往下爆破就行了。

1
2
3
4
5
6
7
8
from Crypto.Util.number import *
m = 56006392793427940134514899557008545913996191831278248640996846111183757392968770895731003245209281149
while m!=0:
flag = long_to_bytes(m)
if b'flag' in flag:
print(flag)
break
m -= 1

learning_with_fault

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from Crypto.Util.number import *
from gmpy2 import *
from secrets import flag
import os

class RSA():
def __init__(self,p,q,e):
self.p=p
self.q=q
self.e=e
self.phi=(p-1)*(q-1)
self.d=invert(self.e,self.phi)
self.dp=self.d%(p-1)
self.dq=self.d%(q-1)
self.n=p*q
self.N=getPrime(512)*getPrime(512)

def sign(self,message):
m=bytes_to_long(message)
sig_p=pow(m,self.dp,self.p)
sig_q=pow(m,self.dq,self.q)
alpha=q*invert(q,p)
beta=p*invert(p,q)
return long_to_bytes((alpha*sig_p+beta*sig_q)%self.n)

def corrupt_sign(self,message):
m=bytes_to_long(message)
sig_p=pow(m,self.dp,self.p)
sig_q=pow(m,self.dq,self.q)
alpha=q*invert(q,p)
beta=p*invert(p,q)
return long_to_bytes((alpha*sig_p+beta*sig_q)%self.N)

def verify(self,message,sign):
return long_to_bytes(pow(bytes_to_long(sign),self.e,self.n)) == message

p=getPrime(512)
q=getPrime(512)
e=65537
rsa=RSA(p,q,e)

with open("sign.txt","w") as f1:
with open("corrupted_sign.txt","w") as f2:
for _ in range(6):
message=os.urandom(64)
sign=rsa.sign(message)
corrupted_sign=rsa.corrupt_sign(message)
assert rsa.verify(message,sign)
f1.write(str(sign)+'\n')
f2.write(str(corrupted_sign)+'\n')

enc=pow(bytes_to_long(flag),rsa.e,rsa.n)
print(f"n = {rsa.n}")
print(f"N = {rsa.N}")
print(f"e = {rsa.e}")
print(f"enc = {enc}")
'''
n = 99670316685463632788041383175090257045961799409733877510733415402955763322569510896091638507050126669571444467488936880059210773298729542608112756526719533574432327269721804307073353651955251188547245641771980139488000798458617636759823027148955008149512692983471670488580994385743789385091027299901520585729
N = 81332992898551792936282861980393365170738006789835182134055801566584228471896473385776004610279937176800796971820133195300006470892468060034368863410462219133248069442508287516929262751427926825122839525496671527936622212986733708071962237633082743396115729744192159064241674410003857168101669882043743570731
e = 65537
enc = 2476965183785968993595493003363618829317072815989584886372189393899395623714779397354978469504773556228655475355703015337932838278988328384587983506790841663233499939173166353582189202860394411808445422387063648198432242875738065748287034529713834303346017134249834382745931627301273142828893469374138264396
'''

踏破铁鞋无觅处,得来全不费功夫。还得是La佬的博客香!😭

RSA | Lazzaro (lazzzaro.github.io)

论文参考:CMBX12 (iacr.org)

看完之后并不是很懂(太菜了)。

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from Crypto.Util.number import *
from tqdm import tqdm
import gmpy2,sys

def orthogonal_lattice(B):
LB = B.transpose().left_kernel(basis="LLL").basis_matrix()
return LB

n = 99670316685463632788041383175090257045961799409733877510733415402955763322569510896091638507050126669571444467488936880059210773298729542608112756526719533574432327269721804307073353651955251188547245641771980139488000798458617636759823027148955008149512692983471670488580994385743789385091027299901520585729
N = 81332992898551792936282861980393365170738006789835182134055801566584228471896473385776004610279937176800796971820133195300006470892468060034368863410462219133248069442508287516929262751427926825122839525496671527936622212986733708071962237633082743396115729744192159064241674410003857168101669882043743570731
e = 65537
enc = 2476965183785968993595493003363618829317072815989584886372189393899395623714779397354978469504773556228655475355703015337932838278988328384587983506790841663233499939173166353582189202860394411808445422387063648198432242875738065748287034529713834303346017134249834382745931627301273142828893469374138264396

cs_ = [
b"\x17\x8bb3\x11\x1b\xb9\xb9\xc6M\xb0\xaa\x07-\x1ar\xff\xfb\xb4&H7!\xb8\xa1\xce\x07\x8b\x84M\x0bw=m\x193Oc\x97w\x8f\xffy4\xa1\x99\xfcW\xf9|\xeb\xa4\x00\x1eD*\xe8-'\xa9\xef\x9d\x13*\xf4\xbe\x9d\x9b&w\xcb\xfd\xb3\xb6\xa3n\xb8\xb4\x97vT\xec@\x86\xd1R\xb0\n\xe1uC\xbc\x14\xeb\xceSu&'{\xb9\x12\x90\x82\xc7,\xdbr\xebP\xe1j\x11E\xd5\x17\xe1\xd0D\xe7z\x94vt\xbf\x1a\xc4+"
,b'\x1dJ\xc5\xb2\xbe\x05\xe6\xc8T\n\xbe"\xbeU\xed\xba\xec\x85\x05\x8b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p\xa5\x9f\x14\xef\'1\x11\x05Gg\xe9\xda\xc9\x18~['
,b':\xefRE\xd7\xa1?\xf3\xb5\xf7\xdd\xe2\xb6~\x85014\xc0\x8a\x80\xe1\xb5#\x94\x10\xb2\xa0\xfe\x87\xd1t\xc3$&\xde8\x195\xcd\xf4@3\x15\xcaK\xcc\xcd\r:\x83*\xd7l\xb6\xf2} \tJ\xb5xKfjh.\xfb\xb5\x91\xc6\xf2x\x8e\x83\xdc\xc3\xef\x8b\x8dW\xa6\xa6\xb0w\xd8\xf2G\xa5-\xc3\x87\x17;\xedH`:\xcd\x08ts\x9eqPE\xd7\xfc\xc4\x98\xb5\xe0\xad\xb7A\x7f\xcb\x01\xbd\x98\xd3Ea\xb9\x07\x80\xf8\x19'
,b"8\xca\x7f!;\\\xde\x1b\x80i\x9b!\x1c??u\x13\x955\xd0xG\xff\xd7\xba\xfe+\x95\x0eu^\x15\x1a\x0e*\xfe\x8a\xafM\xc0\xd1Ty\xd7\xf1\xa7@\xd6\xa6\xee\x0c:It\x1a\xeag\xfc\x0c\xaf\x02<\x03T)\xeb\xb0\x15\x1cz\x85\x992\xa9\xbe\x9bm\xc4D\x83\xf7\xb5T\xdd9?\x94\xd4\x13\xb4\xb3\x8d\xa9\x92\x9dt\x86\xdb\x0b$\x19l\xb1\xb9\x05'o\xf3!\t\x01\x93'z\x15P\x88\xd7iN\n\x8bA\xb5\xd2}\xe8\x10"
]

for i in range(6):
s_[i] = bytes_to_long(s_[i])
cs_[i] = bytes_to_long(cs_[i])

l = 6

v = []
for i in range(len(cs_)):
v.append(int(crt([s_[i], cs_[i]], [n, N])))

v = vector(ZZ, v)
Lv = orthogonal_lattice(Matrix(v))
L1 = orthogonal_lattice(Lv.submatrix(0, 0, l-2, l))
x, y = L1
for a in tqdm(range(333)):
for b in tqdm(range(333)):
z = a*x+b*y
for each in (v-z):
tmp = gcd(each,n)
if tmp>1:
p = tmp
print(p)
q = n//p
assert p*q == n
phi = (p-1)*(q-1)
d = gmpy2.invert(e,phi)
m = int(pow(enc,d,n))
print(long_to_bytes(m))
sys.exit()

2022-巅峰极客-网络安全技能挑战赛
http://example.com/2022/08/20/CTF/2022-巅峰极客-网络安全技能挑战赛/
作者
gla2xy
发布于
2022年8月20日
许可协议