from Crypto.Util.number import * from random import *
flag=b"xxx"
p = 54652877966498174351366784061030941540826109780679767812638885938702023311073348172671269559746951554247092305850067866703779230619519047541036253311250412466443049505699771781919268578313412843196495008809132067113297376693264085286413313110640544744871885363521805202464017885850100288070851518716948912763 a = randint(1, p) b = randint(1, p) s = randint(1, p) n = bytes_to_long(flag) w=s
for i inrange(n): w=(a*w+b)%p
print("w =", w) print("a =", a) print("b =", b) print("s =", s)
# w = 45260711485867413779000039767671034310702875593332662748555320224906545365554170110949539036859843730784214686503912104285690508613273585625200010294558934989365549837830937722020828604189890519693227340880703315693705242316707543693923876818002374458929661657478657015556029058691793765311471853536999728095 # a = 48971444119769808567235868690448906239564969042250773366602052091584947357482111194551805793917339304979214302903321173854777686459558987828324146105464727334291096913040974329068646352169555875473254644341551758096967146463887075885459513015418793985654070835966250474489149215017332659976431908899883075451 # b = 41431597971174895116159534256023499341950550092310958530514478990317523065020737452786308814902366495344759036892471030280016939718362179920001980632686064327381708613306810839684102705939569434751140239433742455076828057785026744706949885961770693799072605278910197649006648865172715746263760943017995064476 # s = 40591085096279567372215447069043586704185332353065048038060554508569817563633133729571174498948047412713943422549121053099203747227568766567100905468380508721248880476131212092521246665670757585737204962567688062946799557019445118729948361267403108325647850072311709196263096458304871305953385062547211896996
p = 54652877966498174351366784061030941540826109780679767812638885938702023311073348172671269559746951554247092305850067866703779230619519047541036253311250412466443049505699771781919268578313412843196495008809132067113297376693264085286413313110640544744871885363521805202464017885850100288070851518716948912763 w = 45260711485867413779000039767671034310702875593332662748555320224906545365554170110949539036859843730784214686503912104285690508613273585625200010294558934989365549837830937722020828604189890519693227340880703315693705242316707543693923876818002374458929661657478657015556029058691793765311471853536999728095 a = 48971444119769808567235868690448906239564969042250773366602052091584947357482111194551805793917339304979214302903321173854777686459558987828324146105464727334291096913040974329068646352169555875473254644341551758096967146463887075885459513015418793985654070835966250474489149215017332659976431908899883075451 b = 41431597971174895116159534256023499341950550092310958530514478990317523065020737452786308814902366495344759036892471030280016939718362179920001980632686064327381708613306810839684102705939569434751140239433742455076828057785026744706949885961770693799072605278910197649006648865172715746263760943017995064476 s = 40591085096279567372215447069043586704185332353065048038060554508569817563633133729571174498948047412713943422549121053099203747227568766567100905468380508721248880476131212092521246665670757585737204962567688062946799557019445118729948361267403108325647850072311709196263096458304871305953385062547211896996
k = b*inverse(1-a,p) an = (w - k)*inverse(s-k,p) n = discrete_log(mod(an,p),mod(a,p)) print(long_to_bytes(int(n)))
#!/usr/bin/env python # -*- coding: utf-8 -*- from Crypto.Util.number import getPrime, bytes_to_long from secret import flag
m = bytes_to_long(flag) p = getPrime(512) q = getPrime(512) n = p * q e = 65537 c = pow(m, e, n) print("n = %s" % str(n)) print("c = %s" % str(c))
g = 23 G = 11 x = 403638639297424424030427248437292930901426348835802538164151760802964623822045794500939051975836868693929980721020252046582234954683741983106400414344716052437631852316722239725845579202592686299335379402696402426374492134051721258686792647746078809606686805108337953402269205238590881513586553277287068954382 y = 2788042276518515357450960102700140012515982479659057104181173132696595727177169896340844620440016059927182507197941000790257318837349294378321194646380886927861234824905576296807661416166077156207249648372928455517246584917020679149653861285226961170842021388854323284132579306053835679130563246044941718724356142 assertpow(g, pow(G, x), n)==pow(g, pow(G, y), n)
# n = 97556864962789127824701383242111068245641430412088446049554987171631796271268013457675328750740703181455093780021347024032292309660522866063731237500797701206940725352099428514342659100771939441946971272319151463476877708240447532346101326328206390117414020245045680514443093718216781806739876823259237682041 # c = 97506619181657974835472296128714598728435343734591865967781506389027493446492338931895061849567882930303570448811849167940548090710640607320028639112831504753106905840969732206394654206682020482058950978096138227623007427344924113713884121137383236661810418984400003458312561214376484805408760046782907944992
from Crypto.Util.number import * from tqdm import * from gmpy2 import powmod from factordb.factordb import FactorDB
e = 65537 g = 23 G = 11 x = 403638639297424424030427248437292930901426348835802538164151760802964623822045794500939051975836868693929980721020252046582234954683741983106400414344716052437631852316722239725845579202592686299335379402696402426374492134051721258686792647746078809606686805108337953402269205238590881513586553277287068954382 y = 2788042276518515357450960102700140012515982479659057104181173132696595727177169896340844620440016059927182507197941000790257318837349294378321194646380886927861234824905576296807661416166077156207249648372928455517246584917020679149653861285226961170842021388854323284132579306053835679130563246044941718724356142
n = 97556864962789127824701383242111068245641430412088446049554987171631796271268013457675328750740703181455093780021347024032292309660522866063731237500797701206940725352099428514342659100771939441946971272319151463476877708240447532346101326328206390117414020245045680514443093718216781806739876823259237682041 c = 97506619181657974835472296128714598728435343734591865967781506389027493446492338931895061849567882930303570448811849167940548090710640607320028639112831504753106905840969732206394654206682020482058950978096138227623007427344924113713884121137383236661810418984400003458312561214376484805408760046782907944992
f = FactorDB(y-x) f.connect() fs = f.get_factor_list() print(fs)
t = 1 for i in trange(2**len(fs)): prime = 1 binlist = bin(i)[2:].zfill(len(fs)) choice = [int(j) for j in binlist] for j inrange(len(fs)): if(choice[j] == 1): prime *= fs[j] prime += 1 if(isPrime(prime)): t *= prime
#try to factor p temp = powmod(2,2**10*t,n)-2 print(GCD(temp,n))
#try to get kphi(n) d = inverse(e, 2**10*t) print(long_to_bytes(powmod(c,d,n)))
from Crypto.Util.number import * from tqdm import * from gmpy2 import invert,powmod
e = 65537 g = 23 G = 11 x = 403638639297424424030427248437292930901426348835802538164151760802964623822045794500939051975836868693929980721020252046582234954683741983106400414344716052437631852316722239725845579202592686299335379402696402426374492134051721258686792647746078809606686805108337953402269205238590881513586553277287068954382 y = 2788042276518515357450960102700140012515982479659057104181173132696595727177169896340844620440016059927182507197941000790257318837349294378321194646380886927861234824905576296807661416166077156207249648372928455517246584917020679149653861285226961170842021388854323284132579306053835679130563246044941718724356142
n = 97556864962789127824701383242111068245641430412088446049554987171631796271268013457675328750740703181455093780021347024032292309660522866063731237500797701206940725352099428514342659100771939441946971272319151463476877708240447532346101326328206390117414020245045680514443093718216781806739876823259237682041 c = 97506619181657974835472296128714598728435343734591865967781506389027493446492338931895061849567882930303570448811849167940548090710640607320028639112831504753106905840969732206394654206682020482058950978096138227623007427344924113713884121137383236661810418984400003458312561214376484805408760046782907944992
#print(len(bin(y-x))) #print(len(bin(n))) kphiphin = y-x for k in trange(1,2**16): phiphin = kphiphin // k phin = 2*(n + phiphin - 3) try: d = invert(e,phin) flag = long_to_bytes(powmod(c,d,n)) if(b"flag"in flag): print(flag) except: pass
#flag{90ccb0c9-8700-41cb-9d62-b630cf8f1821}
EzLog
题目来源:广东省大学生攻防大赛 2022
题目:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
from secret import flag assertlen(flag)<118 from Crypto.Util.number import long_to_bytes,bytes_to_long from Crypto.Util.Padding import pad
print("I believe you have no way to catch me even though Pohlig has smooth weapons.") print(f"y={pow(g,x,p)}")
# I believe you have no way to catch me even though Pohlig has smooth weapons. # y=96684738736980459903034929785324785968796025930893469779531286222406396988966715592949333235326832011076688325476630562163362584667393368651336925308324274452289994386658111183814813840211779123227496106401048680166365937882835154692663834966767665274167877263256747696012785293060701554746392300871850636481
#part1 G = Zmod(p) order = p - 1 m = G(g) c = G(y) dlogs = [] for i in factors: t = order // i y = c ^ t g = m ^ t dlog = discrete_log(c ^ t, m ^ t) dlogs.append(int(dlog)) c=crt(dlogs, factors) print("Pohlig-Hellman Done!")
#假设开头是flag{uuid},则爆破前5个十六进制字符 if(0): table = "0123456789abcdef" dic = [ord(i) for i in table] result_list = [] for char1 in table: for char2 in table: for char3 in table: for char4 in table: for char5 in table: current_string = char1 + char2 + char3 + char4 + char5 result_list.append(current_string) #爆破填充 for j in tqdm(result_list): for i inrange(11,16): # delete padding padding = long_to_bytes(i)*i padding = bytes_to_long(padding) temp = (c - padding)*inverse(256**i,modd) % modd
#check final = long_to_bytes(temp)[:10] error = 0 for tt in final: if(tt notin dic): error = 1 break if(error == 0): print(long_to_bytes(temp))
#假设开头是flag{+可见字符,则爆破4个可见字符 if(1): table = string.printable dic = [ord(i) for i in table] for char1 in tqdm(table): for char2 in table: for char3 in table: for char4 in table: j = char1 + char2 + char3 + char4 for i inrange(11,16): # delete padding padding = long_to_bytes(i)*i padding = bytes_to_long(padding) temp = (c - padding)*inverse(256**i,modd) % modd
p = 100380180012669637378744942171261398091918624065560475592116442008723831000724625143134783707140522784290998397673597179788440926203643287774297527809892664834392514365222771089497090006645985087685142898313371176199974996077656302299931624478967894041880873282005346940525877863969908284953093553124147377177 g = 5 y = 96684738736980459903034929785324785968796025930893469779531286222406396988966715592949333235326832011076688325476630562163362584667393368651336925308324274452289994386658111183814813840211779123227496106401048680166365937882835154692663834966767665274167877263256747696012785293060701554746392300871850636481 factors = [3^2,56989,60217,538687139,560945999,571334087,610502371,631183649,632950873,635821279,650856469,655219333,656624429,681519161,718737731,731233123,733484177,763003931,789196883,819494821,819518603,844402217,857626969,895870279,907446997,908829937,950563309,972564941,1030070381,1048221233,1063554559] phi = prod(factors) h = (p-1) // phi
#part1 use Pohlig-hellman to get first-step m0(use sage) m0 = discrete_log(Mod(pow(y,h,p),p),Mod(pow(g,h,p),p),ord = phi)
#part2 guess the suffix is "}."" and padlen is 13 padlen = 13 suffix = bytes_to_long(b"}." + long_to_bytes(padlen)*padlen) length = padlen + 2 m1 = (m0-suffix)*inverse(256^length,phi) % phi
#part3 bsgs(use sage) y1 = y * pow(g,-(256^length*m1+suffix),p) % p g1 = pow(g,256^length*phi,p) k = discrete_log(Mod(y1,p),Mod(g1,p),ord = p-1,bounds = (0,2^(1024-860-length*8)))
#part4 get flag flag = 256^length*(k*phi+m1)+suffix print(long_to_bytes(flag))
#b'You are a master of the dlp algos! Here is your flag: flag{S0_Smooth_ord3r_pr1me_dlp!_pohlig_hellman_with_padding}.\r\r\r\r\r\r\r\r\r\r\r\r\r'
import socketserver from Crypto.Util.number import * import os import signal import string import random from hashlib import sha256 from secret import flag
classTask(socketserver.BaseRequestHandler): def_recvall(self): BUFF_SIZE = 2048 data = b'' whileTrue: part = self.request.recv(BUFF_SIZE) data += part iflen(part) < BUFF_SIZE: break return data.strip()
p = 144622268328968993341365710278894755118767129325286994164661347213200068288320713151689155598130690763440455157929587751885813242814750422828312072382119518429040602281694119210475772654999865828418886175678335978908269120940864300610431302161143383386149363868608635140950451657400233892787130315426229955639 s = [getPrime(100)] for i inrange(1023): s.append(s[i]*s[i]%(p-1)) whileTrue: self.send(b'>', newline=False) choice = self.recv() if choice == b"1": try: self.send(b"Give me m:") m = int(self.recv()) % p self.send(b"Give me k:") k = int(self.recv()) if k notinrange(1024): break c = pow(m, s[k], p) self.send("c = {}".format(c).encode()) except: break elif choice == b"2": self.send(b"Give me the secret:") secret = int(self.recv()) % p ifpow(secret, s[0], p) == 0xdeadbeef: self.send(flag) else: self.send(b'flag') break self.request.close()
from Pwn4Sage.pwn import * from Crypto.Util.number import * from tqdm import * from hashlib import * import string
sh = remote("node5.anna.nssctf.cn",28301)
#part1 proof defproof_of_work(): table = string.digits + string.ascii_letters temp = sh.recvuntil(b"sha256(XXXX+") temp = sh.recvline() suffix = temp[:16].decode() hex1 = temp[20:].strip().decode() for i in tqdm(table): for j in table: for k in table: for m in table: temp1 = i+j+k+m if(sha256((temp1+suffix).encode()).hexdigest() == hex1): sh.sendline(temp1.encode()) return proof_of_work()
#part2 p = 144622268328968993341365710278894755118767129325286994164661347213200068288320713151689155598130690763440455157929587751885813242814750422828312072382119518429040602281694119210475772654999865828418886175678335978908269120940864300610431302161143383386149363868608635140950451657400233892787130315426229955639 q = (p-1) // 2 m = 0xdeadbeef k = q-2
for i in trange(1024): if(k % 2): sh.recvuntil(b'>') sh.sendline(b'1') sh.recvuntil(b"Give me m:") sh.sendline(str(m).encode()) sh.recvuntil(b"Give me k:") sh.sendline(str(i).encode()) sh.recvuntil(b'c = ') m = int(sh.recvline().strip().decode()) k = k // 2
sh.recvuntil(b'>') sh.sendline(b'2') sh.recvuntil(b"Give me the secret:") sh.sendline(str(m).encode()) sh.recvline() print(sh.recvline())