from Crypto.Util.number import * from pwn import *
plain = """**************************"""
flag = 'flag{*********}'
length = len(flag)
block = [plain[i:i + length] for i inrange(0, len(plain), length)] c = [] for i in block: result = ''.join(chr(ord(a) ^ ord(b)) for a, b inzip(flag, i)) c.append(result.encode()) b = [] for i in c: b.append(hex(bytes_to_long(i)))
withopen("c.txt","a") as f: for i in b: f.write(i+"\n")
from Crypto.Util.number import * import Crypto.Util.strxor as xo import numpy as np
defisChr(x): iford('a') <= x and x <= ord('z'): returnTrue iford('A') <= x and x <= ord('Z'): returnTrue returnFalse
definfer(index, pos): if msg[index, pos] != 0: return msg[index, pos] = ord(' ') for x inrange(len(c)): if x != index: msg[x][pos] = xo.strxor(c[x], c[index])[pos] ^ ord(' ')
dat = []
defgetSpace(): for index, x inenumerate(c): res = [xo.strxor(x, y) for y in c if x != y] f = lambda pos: len(list(filter(isChr, [s[pos] for s in res]))) cnt = [f(pos) for pos inrange(len(x))] for pos inrange(len(x)): dat.append((f(pos), index, pos))
b = [] withopen("c.txt","r") as f: for i in f: b.append(i) c = [] for i in b: c.append(long_to_bytes(int(i,16)))
msg = np.zeros([len(c), len(c[0])], dtype=int)
getSpace()
dat = sorted(dat)[::-1] for w, index, pos in dat: infer(index, pos)
print('\n'.join([''.join([chr(c) for c in x]) for x in msg]))# 此时解出来的还会存在部分词汇乱码,需要根据语句情景来恢复
print(''.join(chr(ord(a) ^ ord(b)) for a, b inzip(long_to_bytes(int("0x250314151228131d593026490211373513442c54102a14104e4c18",16)).decode(), "Couriers or other secure me")))
from tqdm import tqdm from hashlib import sha256 from Crypto.Util.number import* from os import urandom from Crypto.Cipher import AES from Crypto.Util.number import long_to_bytes from Crypto.Util.Padding import pad from random import randint
ells=[*primes(5, 250),182] p = 4 * prod(ells) - 1 F = GF(p**2, 'i') E=EllipticCurve(F,[0,0,0,1,0])
P = E(0).division_points(2)[3] phi = E.isogeny(P) E1=phi.codomain() R = E.random_point() W = phi(R) print(W)
R11=(2*R).x() key0=list(R11)[1]
defencrypt(key0, flag): key = sha256(long_to_bytes(key0)).digest()[:16] iv = urandom(16) cipher = AES.new(key, AES.MODE_CBC, iv) ciphertext = cipher.encrypt(pad(flag,16)) data = {"iv":iv.hex(),"encrypt_flag":ciphertext.hex()} return data
# 尝试恢复高位 f = (h1 - x)^4 - h2^3 f = f.monic() ph_candidates = f.small_roots(X=2^312, beta=0.55)
# 尝试恢复低位 g = (h3 - x)^4 - h2^5 g = g.monic() pl_candidates = g.small_roots(X=2^312, beta=0.55)
print("[*] Found ph candidates:", ph_candidates) print("[*] Found pl candidates:", pl_candidates) # 枚举候选组合 for ph in ph_candidates: for pl in pl_candidates: A = ph * 2^712 + pl h = A + x * 2^312 h = h.monic()
pm_candidates = h.small_roots(X=2^400, beta=0.45) print("[*] Found pm candidates:", pm_candidates) for pm in pm_candidates: P = A + pm * 2^312 print("[*] Found P candidates:", P)
这里找到P之后,也能求出来Q,再由常规的RSA解密就能得到M
1 2 3 4 5
phi = (P-1)*(Q-1) d = inverse(65537, phi) m = pow(C, d, N) print(long_to_bytes(m)) # b'Next,L^3&?CRYPTO. BanG+DrF_4m!-AvE:Mujic@'
Message = 'Next,L^3&?CRYPTO. BanG+DrF_4m!-AvE:Mujic@' e = 65537 N = 12346483936467532069844600502119239676313033372787124789065534018638098680694938261365903797794610961755056255088554358681718304314637316017297616701542612135483666829688444092758507973398796527823932009247681477100647417035227935757974942013890173967394365478484132831994661456936201065409486607830868743178437233924223879870543965589241005939785528708048030119016624907724358471034067664557511894461119952527012136178133604221042635783247124296098468177214542758690407206160985686914282921393872770430862822173647405294320391592699481169093059538145145056241039901423589885314472147328817584038411920745568091897017 c = 6877697482375283919771744751381605439921171277761310340017946645022873605953818152308373448204771202951011043367072137908150213472272485958902649248523380292455472558952854638007846191354220903439051097455310398848454348629462926293402240862288285716305468564661881830617053185557555765698822433166931184024709220241641933883025213566214543658561804944511202718544529982995676396084160225664845431235260497011541109681869921720481471267464813367452467525609083650851497606457613461973057487431856642094518513891793247172928632166932816532544130565780015383996148152782665557213716778291735247943795300323455962327962
sth = [ord(ch) for ch in Message] length = len(sth) a = [pow(sth[i] + 0x1145, e, N) for i inrange(length)]
M = matrix(ZZ, length + 2, length + 2)
# First length rows: diagonal 1's and a_i's for i inrange(length): M[i, i] = 1 M[i, length] = a[i]
# Last row: N and c (to handle the modular equation) M[length, length] = N M[length + 1, length] = c M[length + 1, length + 1] = 1 # print(M)
# sagemath from Crypto.Util.number import * f = ''''''# 填入output内容,注意最后不要留空的一行 f = f.split('\n') n = len(f)
defto_vec(num): tmp=[] s=bin(num)[2:].rjust(n,'0') for i inrange(len(s)): if s[i]=='0': tmp+=[-1] else: tmp+=[1] return tmp
vec=[] for i inrange(n): vec.append(to_vec(int(f[i])))
L=Matrix(ZZ,n,n+n) for i inrange(n): for j inrange(n): L[i,j]=2^50*vec[j][i] L[i,i+n]=1 m=[] L=L.LLL() for i inrange(n): tmp=list(L[i]) if tmp[:n].count(0)==n and tmp[n:].count(-1)+tmp[n:].count(1)==n: m=tmp[n:] break
mm='' for i inrange(len(m)): if m[i]==-1: mm+='0' else: mm+='1' print(mm) print(long_to_bytes(int(mm,2)))
import numpy as np from Crypto.Util.number import long_to_bytes
defrecover_flag(ciphertexts): # 确定 flag 的二进制长度(以最长密文为准) m = max(len(bin(c)) - 2for c in ciphertexts) # 去掉 '0b' 前缀 # 构建线性方程组:A * f = b N = len(ciphertexts) A = np.zeros((N, m), dtype=int) b = np.zeros(N, dtype=int) for i, c inenumerate(ciphertexts): # 将密文转为二进制字符串并填充到 m 位 bits = bin(c)[2:].zfill(m) for j inrange(m): A[i, j] = 1 - 2 * int(bits[j]) # 系数矩阵 A[i][j] = 1 - 2*C_i[j] b[i] = m // 2 - sum(int(bit) for bit in bits) # 右侧常数项 f = np.linalg.solve(A, b) flag_bits = [int(round(bit)) for bit in f]
# 将二进制位组合为 flag flag_long = int(''.join(map(str, flag_bits)), 2) return long_to_bytes(flag_long)
if __name__ == "__main__": ciphertexts = [] withopen("output.txt",'r') as f: for line in f: line = line.strip() if line: ciphertexts.append(int(line))
defencrypt(m, p, n): q = n // p m = pad(m) l = len(m) c = b'' gift = [] for i inrange(l// 64): m_i = b2l(m[64 * i: 64 * (i+1)]) gift.append(m_i * q % r) c += l2b((m_i * n % r) ^ p, blocksize=64) return gift, c
q = n // p res=b'' for i in gift: res+=long_to_bytes(i*inverse(q,r)%r)
print(res) # b"\x1d\xa73We're swaying on horseback,The hills are green,And the birdies sing,And roses are pink,Experience I never had,I'm so happy,Happy to just be part of your story,After you I follow,After you I follow,The world you show me broaden my horizon,Forever my hero,Forever my hero,I am your biggest fan,I am your biggest fan,Merry-go-round,In a circle I run,It's so much fun leaving reality behind.\r\nI fall down the horseback,With my crippled legs,And then it starts to rain,Showing me it's all fake,Raindrops wash down the facade,Hills are painted,Birdies are robotic,Roses are made of clay,Excitement that I feel,Excitement that I feel,Return them to the shelf,Cause now I understand,Heroes cannot be real,Heroes cannot be real,I wasn't who I am,I don't know who I am,\xc2\xbfWho am I?,\xc2\xbfWho am I?,\xc2\xbfWho am I?\r\nHere we go, another lap,Prizes to claim,Here's a dream for you,Here's a dream for me,Golden tickets(flag{Stand_up,gaL1op_on,N07h1ng_c4n_Be_dOne_by_f33l1nG_5o_5ORry_for_y0ursE1f}) in my bag stay unexchanged,Don't you love the thrill of the chase?Just let me be your fan,I wanna be your fan,I'm still your biggest fan!\r\nWhy is it that some were given the role of villian,The moment they were released into this system?\r\nHero,On a plastic horse,Fighting like it's real,With a cardboard sword,I know,Successful or not, I am who I am,I am my biggest fan,I am my biggest fan,I am my enemy and my friend.\r\nHero,Gonna prove my version of justice,Is more just than yours,Uno,Remaining on this stage, I am the only one,I am my biggest fan,I am my " # flag{Stand_up,gaL1op_on,N07h1ng_c4n_Be_dOne_by_f33l1nG_5o_5ORry_for_y0ursE1f}
Week3
AL(L IN MY)GO
RanaSoyoAnon
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
from Crypto.Util.number import * p=getPrime(512) q=getPrime(512) n=p*q
for k inrange(-32,32): p = k * b1 + c1 if isPrime(p) and p > 0: for m inrange(-10000, 10000): q = m * a1 + d1 if isPrime(q) and q > 0: if p * q == n: print(f"找到解: p = {p}, q = {q}") print(f"k = {k}, m = {m}") break # k = -11, m = 10
获得p,q之后就是正常的RSA解明文了,没有别的卡点
1 2 3 4 5 6 7 8 9 10 11
n = 146978395433283264897813405718660588088322207100229521002893751180722325695978423785658275468431402472435440059035053335098162775145300245682198712763023558673286879552547337644749558781058327465800671817512926008450966581498366956477375308931684786869145630887681611140511534001970600372671544461336706473151 c = 144091150656791885294463339584727879265256198198991073653628663376156887178012550858130757614613983484915065317350538883976985699134939611573042960790066164023344748678236482688754022510764186756583653133641013319778030947290249842389554899901303923240856508801296856919235057923994680746764691858073309808359
p = 13265752525031277598749193582511402119401436301773279603834276696648524090566525345340535788599653371597390501163172105815650318160883283557455351397733637 q = 11079536962260399440579867577796182350025055784603280643991792116408806832219819306252980113691988363196496955285790340961106337708724374056440534528513523
defl2d(n): if n == 0: qd = [0,] qd = [] while n > 0: n, r = divmod(n, 4) qd.append(str(r)) dna=[] base=["A","T","C","G"] for i inreversed(qd): dna.append(base[int(i)]) d="".join(dna) return d
defdbp(d): bpd=[] bp={"A":"T","T":"A","C":"G","G":"C"} for i in d: bpd.append(bp[i]) pd="".join(bpd) return pd
from Crypto.Util.number import * from Crypto.Cipher import AES from Crypto.Util.Padding import pad from gmpy2 import next_prime from hashlib import sha256 import random
flag = b"flag{******************************}" defget_weak_prime(nbits): whileTrue: p1 = 2 while p1 < 2**nbits: p1 *= int(next_prime(random.getrandbits(10))) p = p1 + 1 if isPrime(p): return p
p = get_weak_prime(512) q = get_weak_prime(512) n = p**11 * q**11
phiN = (p-1)**11 *(q-1)**11
g = 3 x = random.randrange(0, n) X = pow(g, x, n) y = random.randrange(0, n) Y = pow(g, y, n)
defpollard_pm1(nn, B=2048): a=2 for i inrange(2, B): a = pow(a, i, nn) d = gcd(a-1, nn) if d > 1and d < nn: return d nn = n.nth_root(11) p = pollard_pm1(nn) q = nn//p
defberlekamp_massey(s): n = len(s) c,b = n*[0],n*[0] c[0],b[0]=1,1 l,m,d = 0,-1,0 for i inrange(n): d = s[i] for j inrange(1,l+1): d^=c[j]*s[i-j] if d == 1: t = c[:] p = [0]*n for j inrange(n-i+m): p[j+i-m]=b[j] c = [c[j]^p[j] for j inrange(n)]
if2*l <= i: l,m,b = i+1-l,i,t return [i for i ,bit inenumerate(c) if bit]
defxor(state,taps): res = 0 for i in taps: res^=state[i] return res
for i inrange(1337): tmp=seed[0] for i inrange(1,304): seed[i-1]=seed[i] seed[303]=0 result = xor(seed, taps) if result!=tmp: seed[303]=1 seedstr = ''.join(map(str,seed)) print(long_to_bytes(int(seedstr,2)))