#!/usr/bin/env python3.9
# -*- coding: utf-8 -*-
import gmpy2
from Crypto.Util.number import getPrime, isPrime, bytes_to_long
from secret import FLAG, E1, E2, P, Q1, Q2
def next_prime(num: int) -> int:
num = num + 2 if num % 2 else num + 1
while not isPrime(num):
num += 2
return num
p = getPrime(1024)
q = next_prime(13 * p + 52433)
n = p * q
c = pow(E1, 65537, n)
print(f'n = {n}')
print(f'c = {c}')
# n = 259890247443804761118228016219136684924167917522422806784955644506207888351304889509903433463527585637662916733749092132376942374314732903500147772464631303079816368071397816148840032140257741234623942972389330658394009752410215122103809627221978841602901343785314357668760003964914166778126289222020290468903953247175457923739210505739006832916267484268910832870071479071917094394577029957202256240184604809173087617931324803860474150820931470601204798293536446102918790547726389486804819530604731780327035091686886029998993701502986883473430288066966414564995833243607159289786503640253962387954613582807541606942749
# c = 56440807794784873155975554526282639269465966474628218727704063243557056715418468759474870838438499918826893792546655715745526027857276713541238255702873551647125573694638955842404342457788336135938834876470872953749978996773098030211241264236735693391584657468679440790473717213665228349818831369116042003502072082694693222429663702695116191467500411709133178801495474342969042325471712020201636143305265100439673243718028002788717655896374089009854112149946761782391009483256739835432971090640363725440908313218581221815215679758846253486316393841855585127055921605329511990315967829323041426287669986012868743294383
这个首先联想到,用n/13后开平方根,最后往前查找素数,应该能找到,可以求出E1。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
p = gmpy2.iroot(n//13,2)[0]
while 1:
p = sympy.prevprime(p)
q = sympy.nextprime(13 * p + 52433)
if(q == n//p):
break
phi = (p-1)*(q-1)
d = gmpy2.invert(65537, phi)
E1 = pow(c, int(d), n)
print(E1)
assert E2.bit_length() == 68
ns = [getPrime(1024) * getPrime(1024) for _ in range(3)]
cs = [pow(E2, 89, n) for n in ns]
print(f'ns = {ns}')
print(f'cs = {cs}')
# ns = [14768877816717539611731045629979591214811330868162202565624664129912032603118216104003405398241174001521124840138476411692247747689580245229633834241354343663803512914327061439026544199607661340809522611714979050746642474298514984319799503942592454459033529230661408387428307100152240359931113582007834771064836814506679488489884326758202775879875295915804661697151797257882534759047427072076935608038544138918082424231665777438859097733700757281503798641211223630919282623055222757291265697646847664236117770057816055708821059139836629062435882101110861519241590121890490749372041394827106099992940775405355963537093, 19666545323134160617662045141655785833630626383149539459109151988125669512345207825350175718697646952110980014593295830440913639941294759366636907406022093593567085060130487695457293050838339751589656086715810761617034895110908921222238432080750232780959480802362123627948837433700702719795982826451033532941818523986557921585705333167085832750810602154544395282520714531925732606981719148520396524986802623258451137156083339557903763787258971153135598507990986538670962098805822026704693511638643139987774524413005609600478133862229864877539603414684417042041304917739974092129299946247802874434223813699963768959763, 15779368467163552829155887858552042780948412851484383573815791940301167449302309346542230278336893665527037182849622548454420128882466125704119621262910314425821276866328941773658802929134316373368772517587582142657657900715575360467459524840118867966716907874327634603692474154990530124677535030772815052167989006001122992617640466985679436530404398438789489536785124944395405233412010429769188201394928826199790856798037225834203918546557553132305324936899974409559438594918135814322834671435422502955119706510092210390004609239572208028155476305714319440640836664426079307167151133383912378800774498518280438182203]
# cs = [567850780510774217643896612655688253669214849388311742718787427972718595963863911452371929027639644307000739560965604527599743560884685911816686998038866122672741996289320331846282807112765589075393537405844508645186580219935681309551863862838005818102040526577638711724084442111910507832270265866651971011054177521418891278722648745957602559922271453142590901732965224467398990543233797146010537073758529331487664292226361777579207274132983906773032552051857982429265573985911417593730675378625055722840026395255063109465505900223512922055483597989949853680781366266645807749448493048852769413781512590488755978697, 16628689839738879696100069757848926944243292698323350637649584538310327633442975226999529525456259644570646951204781799076562002069453482286619645490158456560690652510541536353482676298819618268940054219537327611925439186331762973027774866201153493437368183695220271473169270116128825534852722159311320980174393841950205399254670211089683984121742072363442124572477542374398931013755766208246311751050272325010037196256536013007563620011493948752002272261333679256550076724906019636720113375326020525053147012787943894857133740100766017749473241441986776405175439625904183358501475568051587218891671961057999591001668, 7423561249867066194530577825751519067130148169119196474389962513177470795034282161787340397726773451715094904236471232577160713265828264263562089259120152501642969714248564032095513372662818544778929805546621303928734411268330553516461133100124009496405027437585651547170169428109291156528057270539229674840678343567274741287724466921000898325604505576001240065875934111729300326280297815704004629408909170232221133608585206749960618344234298494521288440446897752115014786315122220398755923018428341634901663783340643124065995812457742638126684083971978356298818106529467920958965081076230810503321379529620885195317]
第二个有多组n和c,用低加密指数广播攻击
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from functools import reduce
def CRT(mi, ai):
assert(reduce(gmpy2.gcd,mi)==1)
assert (isinstance(mi, list) and isinstance(ai, list))
M = reduce(lambda x, y: x * y, mi)
ai_ti_Mi = [a * (M // m) * gmpy2.invert(M // m, m) for (m, a) in zip(mi, ai)]
return reduce(lambda x, y: x + y, ai_ti_Mi) % M
E2 = gmpy2.iroot(CRT(ns, cs),89)[0]
print(E2)
qq = getPrime(1024)
nn = P * qq
qqq = qq >> 460 << 460
print(f'nn = {nn}')
print(f'qqq = {qqq}')
# nn = 23771565097692613805925074947762277776248175039904858640393515573404187252654078983280079053221178407495942053339065277458665885747681910079114703240305875633863359892288520067708424052894526852549504476844606883063864442920259420950734334491426028395052970793501884544454476339394388681877266173301424526574691645472879318102691079950543768832010711509846115011175415348311434230971982003766118635224304817289494045882942006710195248414232575795509048370893046873209854272475037633188006029149253662072051929871584657148706162114224833446325630812536670801454942386364791171210124128405736320131739365476538566297221
# qqq = 133032422062763199415874751118944045012894101492725125544603031449033342774705002029942162253414972567936917624806763857304579798126496989113467512098796380998642424465450876083699444199323535712213201610265804758481994796046002002888732353517685661856315914283134231856793005918816096507197913746788533141504
mappings = { 0x04:"A", 0x05:"B", 0x06:"C", 0x07:"D", 0x08:"E", 0x09:"F", 0x0A:"G", 0x0B:"H", 0x0C:"I", 0x0D:"J", 0x0E:"K", 0x0F:"L", 0x10:"M", 0x11:"N",0x12:"O", 0x13:"P", 0x14:"Q", 0x15:"R", 0x16:"S", 0x17:"T", 0x18:"U",0x19:"V", 0x1A:"W", 0x1B:"X", 0x1C:"Y", 0x1D:"Z", 0x1E:"1", 0x1F:"2", 0x20:"3", 0x21:"4", 0x22:"5", 0x23:"6", 0x24:"7", 0x25:"8", 0x26:"9", 0x27:"0", 0x28:"\n", 0x2a:"[DEL]", 0X2B:" ", 0x2C:" ", 0x2D:"-", 0x2E:"=", 0x2F:"[", 0x30:"]", 0x31:"\\", 0x32:"~", 0x33:";", 0x34:"'", 0x36:",", 0x37:"." }
nums = []
keys = open('usbdata.txt')
for line in keys:
if line[0]!='0' or line[1]!='0' or line[2]!='0' or line[3]!='0' or line[6]!='0' or line[7]!='0' or line[8]!='0' or line[9]!='0' or line[10]!='0' or line[11]!='0' or line[12]!='0' or line[13]!='0' or line[14]!='0' or line[15]!='0':
continue
nums.append(int(line[4:6],16))
keys.close()
output = ""
for n in nums:
if n == 0 :
continue
if n in mappings:
output += mappings[n]
else:
output += '[unknown]'
print 'output :\n' + output
import base64
dic='0123456789abcdef'
s1='olympics'
s2='in'
s3=b'china'
flag='flag{'
def fun1(x):
if( ord(x)<=47 or ord(x)>57):
if ( ord(x)>96 and ord(x)<=102):
x = chr(ord(x)-87)
else:
x = chr(ord(x)-48)
return x
for i in range(len(s1)):
for x in dic:
for y in dic:
if ((ord(fun1(x))*16)|ord(fun1(y))) == ord(s1[i]):
flag +=x
flag +=y
flag+='_'
for i in range(len(s2)):
if (i&1)!=0:
flag += chr(ord(s2[i])^33)
else:
flag += chr(ord(s2[i])^32)
flag+='_'
encodestr=base64.b64encode(s3)
flag += str(encodestr)[2:-1]
flag += '}'
print(flag)