Writeup Log

binary basis

## solve

暗号化のためのコードと暗号化されたファイルが提供されるので、これをデコードする。

bash
❯ ls -la
total 16
drwxr-xr-x 2 nabeen nabeen 4096 Oct 30  2024 .
drwxr-xr-x 7 nabeen nabeen 4096 Feb 17 22:01 ..
-rw-r--r-- 1 nabeen nabeen 2730 Oct 30  2024 output.txt
-rw-r--r-- 1 nabeen nabeen  434 Oct 30  2024 source.py
source.py
from Crypto.Util.number import getPrime, bytes_to_long
from math import prod

FLAG = open('flag.txt', 'rb').read()

primes = [getPrime(128) for _ in range(16)]

n = prod(primes)
e = 0x10001
m = bytes_to_long(FLAG)
c = pow(m, e, n)
treat = sum([primes[i]*2**(0x1337-158*(2*i+1)) for i in range(16)])

with open('output.txt', 'w') as f:
   f.write(f'{n = }\n')
   f.write(f'{e = }\n')
   f.write(f'{c = }\n')
   f.write(f'{treat = }\n')
output.txt
n = 1194931453681347566065245814885173786722354967445085971276396484216856294626496803373056645819857254018920864743  
14689141024498358715132868441978738105268336335778301627806695859205063825847290995872425532374614756539044160447358  
90337516652554347192286848849910566435520680075138102726046207470512264767299438436533326086284580396249514568104026  
46751646956908627370065153150874700407576065171400717983003260221276594972065834928596431802607562851015475226746966  
13261207152550090000611298331939402585491230470733454735317291288404096144773097239721697692471487615973994925335486  
666529576878075473132154318910180009691  
e = 65537  
c = 7843401455306117052983840191772035764590120810135229626988447290097884431930595188476469571737532236327292809373  
78657931131044320237942352294977011426926814156795491475325928027020538233240827331820727707790065237204424907711209  
87890027997070704393610765382574187921750560918055356164458340750383236011537901475045013520557031618820264221766743  
09453612192522679185089525599345304867074560774246993494765314787925989250105093325340039198449922965611296129596949  
24870057868600069612926760908364725272466736993450296282248721955460994486615015616766227202605142263852602715833074  
93991971640825413235694692735476316469  
treat = 410641222101175903098939208509408519644485731973647023122186528475630746584685507760496234360770596479004264  
09109763901345955202703985682630778316836716410976908994802298857481157910324003086887604087560484126319093760348302  
78403195988460869071873993953128913055524167109950355771321619759240124123420873581963118032041376071865687686732340  
65618293161664916961035275000781538371457723107404555099566606948231448302600955513912364031828362147280602912516936  
90579998717099897785954779748569649102911569556210804834065634957865690729391572538723259866564481741517395331813459  
60346626382714127991191970628132828805046072186166199044659742585027136421705293648918681884686387457800668881394414  
36857279311043218107138307598399182569370756458498719417989210336834653999596461366215590498872736820542773306634586  
15787744906360121329675910070999119357809181250170609599790023712764414191551844874675524795782430239120555096062003  
80005869342340535020661774591691411696535763165074000544138840506169279000650876097110774102755631299078072772437146  
70176095546953717981594493610711755995149473074367609400297343136979311995084079650569164792468549445539945339697390  
46153764629534835323207379057477612661669942078609127146916601554609679997006901572586628537881511818938391905350113  
67443283336166074848601903262298731674641263818723345403725236998008022474483738080099635888145163875802428097206153  
6248320550269641404509205922332930938158599125930491114698209185841465863646506532208640

exploit は以下。

python
from math import prod

# output.txt から得た値
treat = 41064122210117590309893920850940851964448573197364702312218652847563074658468550776049623436077059647900426409109763901345955202703985682630778316836716410976908994802298857481157910324003086887604087560484126319093760348302784031959884608690718739939531289130555241671099503557713216197592401241234208735819631180320413760718656876867323406561829316166491696103527500078153837145772310740455509956660694823144830260095551391236403182836214728060291251693690579998717099897785954779748569649102911569556210804834065634957865690729391572538723259866564481741517395331813459603466263827141279911919706281328288050460721861661990446597425850271364217052936489186818846863874578006688813944143685727931104321810713830759839918256937075645849871941798921033683465399959646136621559049887273682054277330663458615787744906360121329675910070999119357809181250170609599790023712764414191551844874675524795782430239120555096062003800058693423405350206617745916914116965357631650740005441388405061692790006508760971107741027556312990780727724371467017609554695371798159449361071175599514947307436760940029734313697931199508407965056916479246854944553994533969739046153764629534835323207379057477612661669942078609127146916601554609679997006901572586628537881511818938391905350113674432833361660748486019032622987316746412638187233454037252369980080224744837380800996358881451638758024280972061536248320550269641404509205922332930938158599125930491114698209185841465863646506532208640
n     = 119493145368134756606524581488517378672235496744508597127639648421685629462649680337305664581985725401892086474314689141024498358715132868441978738105268336335778301627806695859205063825847290995872425532374614756539044160447358903375166525543471922868488499105664355206800751381027260462074705122647672994384365333260862845803962495145681040264675164695690862737006515315087470040757606517140071798300326022127659497206583492859643180260756285101547522674696613261207152550090000611298331939402585491230470733454735317291288404096144773097239721697692471487615973994925335486666529576878075473132154318910180009691
e     = 65537
c     = 78434014553061170529838401917720357645901208101352296269884472900978844319305951884764695717375322363272928093737865793113104432023794235229497701142692681415679549147532592802702053823324082733182072770779006523720442490771120987890027997070704393610765382574187921750560918055356164458340750383236011537901475045013520557031618820264221766743094536121925226791850895255993453048670745607742469934947653147879259892501050933253400391984499229656112961295969492487005786860006961292676090836472527246673699345029628224872195546099448661501561676622720260514226385260271583307493991971640825413235694692735476316469

primes = []
for i in range(16):
    shift = 0x1337 - 158 * (2 * i + 1)
    p = (treat >> shift) & ((1 << 128) - 1)
    primes.append(p)

phi = prod(p - 1 for p in primes)
d = pow(e, -1, phi)
m = pow(c, d, n)
flag = m.to_bytes((m.bit_length() + 7) // 8, "big")
print(flag.decode())
bash
❯ python exploit.py
HTB{any_leak_related_to_the_primes_can_lead_to_full_factorization}

## flag

  • HTB{any_leak_related_to_the_primes_can_lead_to_full_factorization}