flag = "bi0sctf{%s}" % f"{randint(2**39, 2**40):x}"
p = random_prime(2**1024) unknowns = [randint(0, 2**32) for _ inrange(10)] unknowns = [f + i - (i%1000) for i, f inzip(unknowns, search("{(.*)}", flag).group(1).encode())]
output = [] for _ inrange(100): aa = [randint(0, 2**1024) for _ inrange(1000)] bb = [randint(0, 9) for _ inrange(1000)] cc = [randint(0, 9) for _ inrange(1000)] output.append(aa) output.append(bb) output.append(cc) output.append(sum([a + unknowns[b]^2 * unknowns[c]^3for a, b, c inzip(aa, bb, cc)]) % p)
p = 22164857548872153350792863287126662739346790382724883568469825455088689119474784066358095106688985128245096556482593315395509994112330842037731896432716074587916002372658113733392392830971642878227300074751441475110802528284293674049241201403401496426351459462195992798867652944173025546970871261462401766951 withopen(r"D:\CTF_challs\py\crypto\2024\Bi0sCTF 2024\lalala_output1.py","r") as f: output = ast.literal_eval(f.read())
#part1 groebner P = PolynomialRing(ZZ, [f"x{i}"for i inrange(10)]) unknowns = P.gens()
F = [] for i in trange(100): aa,bb,cc,sum1 = output[4*i+0],output[4*i+1],output[4*i+2],output[4*i+3] temp1 = (sum1 - sum(aa)) % p temp1 = temp1 t = 0 for j inrange(1000): t += unknowns[bb[j]]^2*unknowns[cc[j]]^3 F.append(t-temp1)
f1 = unknowns[9] - 957068055 res = Ideal(F).groebner_basis() for i in res: if(len(list(i)) < 4): print(i)
from Pwn4Sage.pwn import * from hashlib import md5 from Crypto.Util.number import *
#part1 guess number(or use get more points for times and use groebner--since a,b is static) p = 0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff a = 0xffffffff00000001000000000000000000000000fffffffffffffffffffffffc b = 0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b
E = EllipticCurve(GF(p), [a,b]) G = E.gens()[0] q = E.order()
#part3 get secret key and get flag d = (s1*hsh2-s2*hsh1)*(inverse(r2*s1-r1*s2,q)) % q e = inverse(d,q) flag = e*E(flag_enc)
print(long_to_bytes(int(flag[0])))
#bi0sctf{https://bit.ly/3I0zwtG}
rr
题目描述:
1
Down the rabbithole...
题目:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
from Crypto.Util.number import * from FLAG import flag from random import randint
flag = bytes_to_long(flag) n = 472993274721871037103726599805149366727531552333249750035977291933239067588481589544777397613192273114354221827196579379954069925604091911249655707080927769808587176515295614018992848517984372306879552247519117116110554431341268358177159108949791969262793325836353834899335531293329721598226413212541536002401507477776699642647348576111445702197483449777741566350285229621935507081895389023444249054515395783080003733803406382744631528246608154546123270319561514117323480441428953306734274538511770278887429407127143049023747710881993279361892937905382946820141513009017756811296722630617325141162244806884220212939955235410280899112731530527048274396186038160728562551536558223235783656985493518204710943916486379681906506757757594165379493317173050550893487151879681122510523721157284728808336110950008840684602353984682117748018347433177541603140491131603068512706893984834735290809952944273565203183330739252949245209529232254867201402656024997949207918675051941911990640248052951780195402390132237903538546705181463959793972284823588987652138458328270662652334799233015314673544813649692428544375538627858921763941533600553536579901589575693816746953261108022490849251974419402753031545629158199093099096735356165044275617408697 rr = 11898141078345200236264081467585899457224809417108457314508072413792599039332439547789237898270544336909458761754683941320649771736625000667170176071314483 ks = [randint(0, rr**(i+1)) for i inrange(20)] c1 = pow(sum(k*flag**i for i, k inenumerate(ks)), (1<<7)-1, n) c2 = pow(flag, (1<<16)+1, n) ks = [pow(69, k, rr**(i+2)) for i, k inenumerate(ks)] print(f"{ks = }") print(f"{c1 = }") print(f"{c2 = }")
from Crypto.Util.number import * from sage.rings.polynomial.multi_polynomial_sequence import PolynomialSequence from tqdm import tqdm import itertools
defsolve_system_with_jacobian(H, f, bounds, iters=100, prec=1000): vs = list(f.variables()) n = f.nvariables() x = f.parent().objgens()[1] x_ = [var(str(vs[i])) for i inrange(n)] for ii in tqdm(Combinations(range(len(H)), k=n)): f = symbolic_expression([H[i](x) for i in ii]).function(x_) jac = jacobian(f, x_) v = vector([t // 2for t in bounds]) for _ inrange(iters): kwargs = {str(vs[i]): v[i] for i inrange(n)} try: tmp = v - jac(**kwargs).inverse() * f(**kwargs) except ZeroDivisionError: returnNone v = vector((numerical_approx(d, prec=prec) for d in tmp)) v = [int(_.round()) for _ in v] if H[0](v) == 0: returntuple(v) returnNone
R = f.base_ring() N = R.cardinality() f_ = (f // f.lc()).change_ring(ZZ) f = f.change_ring(ZZ) l = f.lm()
M = [] for k inrange(m+1): M_k = set() T = set((f ^ (m-k)).monomials()) for mon in (f^m).monomials(): if mon//l^k in T: for extra in itertools.product(range(d), repeat=f.nvariables()): g = mon * prod(map(power, f.variables(), extra)) M_k.add(g) M.append(M_k) M.append(set())
shifts = PolynomialSequence([], f.parent()) for k inrange(m+1): for mon in M[k] - M[k+1]: g = mon//l^k * f_^k * N^(m-k) shifts.append(g)
from Crypto.Util.number import * from random import randint from FLAG import flag
p = getPrime(1024) q = getPrime(1024) e = getPrime(132) n = p*q hint = pow(e, -1, (p-1)*(q-1)) hint %= p-1 hint %= 2**892 c = pow(3, int.from_bytes(flag), n**5) * pow(randint(0, n**5), n**4, n**5) % n**5
""" n = 9722343735487336242847355367175705096672092545117029199851527087227001665095112331406581010290318957921703096325328326862768861459201224096506317060919486835667369908780262880850949861734346363939614200227301344831209845565227637590016962469165064818450385339408084789219460490771570003649248250098125549751883777385917121014908647963900636814694225913533250242569263841750262192296795919177720443516042006972193940464844059718044438878017817432336475087436031866077325402373438547950481634275773767410248698596974769981162966656910136575149455523084473445761780201089182021418781347413453726696240548842411960178397 e = 5323153428600607366474827268153522064873 c = 9128106076400211790302891811252824557365859263295914819806672313027356017879597156259276057232557597614548050742418485365280305524694004426832069896531486671692562462063184624416012268348059935087037828161901243824582067161433586878141008884976330185561348052441637304755454643398179479215116505856245944555306345757777557395022121796068140566220391012921030768420736902592726104037200041403396506760483386523374366225161516294778224985920562226457769686733422726488624795847474711454397538514349555958637417188665977095558680525349235100286527905789576869572972662715040982208185436819557790062517857608731996417066519220133987864808243896151962316613504271341630230274953625158957103434031391582637694278277176886221304131078005240692954168656292222792833722555464070627220306776632641544334188357810067577550784029449834217848676080193960627138929032912578951880151284003878323853182114030012207949896373695734783631698004600675811512726913649141626146115066425891236975554237158682938964099745220780884079884347052906073216530490633243676915134831324804418410566989306886192743687590855529757605789691981493863292029273401139254934543448966341439303948513266699261650278938684067402860913507689842621595391519090227639907684629841162983852454124546030986411283762938101536264676221777904450717178547838152674410566294280937400196290368544481636850750666313771438253636667634601122561235018292316232335633111595474772273810349284893171480302604833719250453453781210093266339454843926482821341993360016434693250661347303203216948599305102121353574445652764255573536572077762409837628479280331295047290459975370026620838169978316921035609492162085052786943829915442906137063599836720584533200385074702683101049336194258783047318183521466098437420153628598968954236332678203275614402446435216223033804260963642393142002417568855964535316709986640977596845897721671783670070696907220894520837335160816494605130683705587464386202643385688263935088026204614056121745160246499509455752793089324629215884008499726564579763845757062068182946721730306128755414268910929410742220199282343421146810430121947827801171056425435942640932150535954546458772114121498557119913825127286832860975814307160175273154886250581960709573672488119996389986116735407178214281982766051391618187878672106737928646489671994503814871652107136752677107398141842179907758909246276653861569864776043204134345135427118784118473462309509988521112691717301811627018054555866015966545532047340607162395739241626423495285835953128906640802690450118128515355353064004001500408400502946613169130088974076348640048144323898309719773358195921400217897006053213222160549929081452233342133235896129215938411225808985658983546168950790935530147276940650250749733176085747359261765601961315474656996860052862883712183817510581189564814317141703276878435707070103680294131643312657511316154324112431403040644741385541670392956841467233434250239028068493523495064777560338358557481051862932373791428839612299758545173203569689546354726917373906408317003812591905738578665930636367780742749804408217333909091324486584514813293 hint = 27203100406560381632094006926903753857553395157680133688133088561775139188704414077278965969307544535945156850786509365882724900390893075998971604081115196824585813017775953048912421386424701714952968924065123981186929525951094688699758239739587719869990140385720389865 """
from Crypto.Util.number import * import itertools from sympy.ntheory.modular import crt
defsmall_roots(f, bounds, m=1, d=None): ifnot d: d = f.degree()
R = f.base_ring() N = R.cardinality() f /= f.coefficients().pop(0) f = f.change_ring(ZZ)
G = Sequence([], f.parent()) for i inrange(m+1): base = N^(m-i) * f^i for shifts in itertools.product(range(d), repeat=f.nvariables()): g = base * prod(map(power, f.variables(), shifts)) G.append(g)
factors = [monomial(*bounds) for monomial in monomials] for i, factor inenumerate(factors): B.rescale_col(i, factor)
B = B.dense_matrix().LLL()
B = B.change_ring(QQ) for i, factor inenumerate(factors): B.rescale_col(i, 1/factor)
H = Sequence([], f.parent().change_ring(QQ)) for h infilter(None, B*monomials): H.append(h) I = H.ideal() if I.dimension() == -1: H.pop() elif I.dimension() == 0: roots = [] for root in I.variety(ring=ZZ): root = tuple(R(root[var]) for var in f.variables()) roots.append(root) return roots
return []
#part1 copper to get dph and k L = 132 n = 9722343735487336242847355367175705096672092545117029199851527087227001665095112331406581010290318957921703096325328326862768861459201224096506317060919486835667369908780262880850949861734346363939614200227301344831209845565227637590016962469165064818450385339408084789219460490771570003649248250098125549751883777385917121014908647963900636814694225913533250242569263841750262192296795919177720443516042006972193940464844059718044438878017817432336475087436031866077325402373438547950481634275773767410248698596974769981162966656910136575149455523084473445761780201089182021418781347413453726696240548842411960178397 e = 5323153428600607366474827268153522064873 c = 9128106076400211790302891811252824557365859263295914819806672313027356017879597156259276057232557597614548050742418485365280305524694004426832069896531486671692562462063184624416012268348059935087037828161901243824582067161433586878141008884976330185561348052441637304755454643398179479215116505856245944555306345757777557395022121796068140566220391012921030768420736902592726104037200041403396506760483386523374366225161516294778224985920562226457769686733422726488624795847474711454397538514349555958637417188665977095558680525349235100286527905789576869572972662715040982208185436819557790062517857608731996417066519220133987864808243896151962316613504271341630230274953625158957103434031391582637694278277176886221304131078005240692954168656292222792833722555464070627220306776632641544334188357810067577550784029449834217848676080193960627138929032912578951880151284003878323853182114030012207949896373695734783631698004600675811512726913649141626146115066425891236975554237158682938964099745220780884079884347052906073216530490633243676915134831324804418410566989306886192743687590855529757605789691981493863292029273401139254934543448966341439303948513266699261650278938684067402860913507689842621595391519090227639907684629841162983852454124546030986411283762938101536264676221777904450717178547838152674410566294280937400196290368544481636850750666313771438253636667634601122561235018292316232335633111595474772273810349284893171480302604833719250453453781210093266339454843926482821341993360016434693250661347303203216948599305102121353574445652764255573536572077762409837628479280331295047290459975370026620838169978316921035609492162085052786943829915442906137063599836720584533200385074702683101049336194258783047318183521466098437420153628598968954236332678203275614402446435216223033804260963642393142002417568855964535316709986640977596845897721671783670070696907220894520837335160816494605130683705587464386202643385688263935088026204614056121745160246499509455752793089324629215884008499726564579763845757062068182946721730306128755414268910929410742220199282343421146810430121947827801171056425435942640932150535954546458772114121498557119913825127286832860975814307160175273154886250581960709573672488119996389986116735407178214281982766051391618187878672106737928646489671994503814871652107136752677107398141842179907758909246276653861569864776043204134345135427118784118473462309509988521112691717301811627018054555866015966545532047340607162395739241626423495285835953128906640802690450118128515355353064004001500408400502946613169130088974076348640048144323898309719773358195921400217897006053213222160549929081452233342133235896129215938411225808985658983546168950790935530147276940650250749733176085747359261765601961315474656996860052862883712183817510581189564814317141703276878435707070103680294131643312657511316154324112431403040644741385541670392956841467233434250239028068493523495064777560338358557481051862932373791428839612299758545173203569689546354726917373906408317003812591905738578665930636367780742749804408217333909091324486584514813293 hint = 27203100406560381632094006926903753857553395157680133688133088561775139188704414077278965969307544535945156850786509365882724900390893075998971604081115196824585813017775953048912421386424701714952968924065123981186929525951094688699758239739587719869990140385720389865
PR.<x,y> = PolynomialRing(Zmod(n)) f = e*(2^(1024-L))*x + y - 1 + e*hint res = small_roots(f,bounds=(2^L,2^L),m=1,d=8)[0] dph,k = res dp = (2^(1024-L))*dph + hint p = int((e*dp-1) // k + 1) q = int(n // p)
#part2 get m on p-adic and q-adic ccp = pow(c,p-1,p**5) R = Zp(p, prec=5) xp = (R(ccp).log() / R(pow(3,p-1,p^5)).log()).lift()
Can you help me test out this PRNG I implemented? I'm inserting a backdoor, but I'm sure you can't find it. Oh btw, I did some optimizing, so version 2 is faster. You can still try out version 1 though. They're the same anyway.