概述
Optimus Prime是来自于HTB(hackthebox.com)的一个容易级密码学挑战,完成该挑战所需要掌握的知识点在于RSA加密算法。
题目分析
该任务提供了一个在线环境, 使用nc 134.209.186.13 31165连接到远端服务器上,可以得到如下提示
You are going to face justice, and may it be kinder to you than it was to us.
1. View status of the Transformer.
2. View Serial ID's of the Transformer.
3. Register new Transformer.
4. Enter to the access panel.
选择1,2和3都返回一些无用的文字信息,选择4则返回如下内容:
PUBLIC KEY: 556158105590834976431558928325513589105081492652755424074467607151874315636855396122125273325817553796612291848909117644653284981771321131029674811686131187013989064864196404384879788451795938651419563686793351050142562959856882963716443864808039588808249459235975406965140444645383867910008980312118367360517078340175036174601332039664791274773927402370296851615688289571808884278860845489906149783372217128226848310862334179596553922951363126441629316491364930837968039568251399708736609843802545621605524395734415488356308454922527638801209194552358792846849770346101228380941113997038431056263536176936919886745864499606835496983962418763375652668598495791867152858808945622011266551763393984867065078168636677570860666137407919366570703538112245696542273601139605963190931958627975480200833182102370905290478293324966470879497476364727502815626081289739299152417201194933386329363773683669370863087121612415452932920166584826265433897837759005149843012158035548712008040445141751581971956838344879446088088664239419157921124633656067816655030632948205337244381249094012909738141910869640686422395119757931637641944034336335848693385433161156645554789743212704503894060297996319569366957459528459886825717644064858902861269251561
ENCRYPTED PASSWORD: 271702180907267690793918100725911575121586565976380344519978411815623908414926919920145771320358509367026039706311525897631319812724489945222803745552515767583599301259564970362812098971078530575887089961747682525847829315835642428901501905423240136430159690460448822291779760225824089701037146758663375063088868592848274031842687643899444920443077890874427489567370714019013620867314556228347387997428725469534883071914112455539459233275879619909247621361996197629148102879789574781048517996512823693000887116053753070699920472688681749363737786068514199697669595838182221668889395478773008416326040653160614509454481489744924905700168619043692580334514072608523026864387975595214799786307672706006213991097075116544450407962627157551091825377054471674107166525146347970407545256711412053659192898452915272885130478743221479972851570968443648906540500476080483697595822307765451259891684741780787081939405634125300504411172990446065951863202687176500579935285873013261657835088760919460932094939040005615244000299867894505878514955679926787418442820141037223665109184912743861621352237750593392307827445279472867804640415924731119226495749044802783210955613485781579521838723890525137136172267204769692068674567268641585302337928110
The private key has been sent to your email. Please use it to proceed:
解题思路
任务题目中的"Prime"以及输出中的"PUBLIC KEY"等字眼都提示这是一个RSA加密相关的任务。
解题过程
观察以上输出的PUBLIC KEY,我们可以得知该数字不是素数,但也无法找到已知的分解。 再次连接,可以得到另一组不同的数字
PUBLIC KEY: 498712005899102574047090085672537607495675117721787019009683338824961823045375560041604951846282427076543102385990575067869331117121083260174627165032871731279577045598969880971684945789468108848921437266912893421945332606253138984852256381607895290404401528057160741969664262937399959430753515382341552822026924914576658307387993652213246807866391869012653808048725927693715926277118545414630792337856821997245531637761512318221732582323548070285947557983420435176618154379514688737254056486254664556448730935209697614488627962529219292457098968378084484810198625322954971013806082769576861358349091372888210246472041372553033519253998341745422724388353914960958742059871551881650345110981927504719674560538714941172122720895554436068378388498810709466121883624160172609182590942472427532780280635240276072651501588912999933648794610782505814543688909260877614537073015096234528641477478012747879703361724703530002439171204874964775375551225009223274763111664211552362462377489115340026612136316646151716791884369449107175601796761379361375235529334624203103898705673965351568439596715461066226607900467085986062258402558464633102484780119710075816171169823974663312855113451136557829969532897025124111608820103470903024670025902961
ENCRYPTED PASSWORD: 255969462230410974724874222157071907214698658758284129945019852263183265349452974565224431523567435520206840390757808963050048920001328977189604700028220137019834174876893683289876110503827141887722625021066233125120410566741752548662312356551242040149910934276723249204127604800242299144507452541851634516936095503290055126405789006961171998376809675343440099895866785234587394718154935911115242035791253932458474312610345923652546621360104272770090564260456142444548300656588893645791538525020323749718912364346786547618875298785118493472402768557855179315512251225067957136609633235171528186484863746927069205203811405439129387088915927220847395047577631953682214884069470625881706732844000443386937244585986728981629072760184456100508039168486000529601996259697904500990483242205227155175285953916377458780037461385732930536443665210662909285732207572977888627310959529039454477474328933828466470832050592528289704333235068458120443336182330491988704470264401092119563917473478654356824251269935549032216975993562898319602253608213050255932566749926836704860294459425584429526139674999138289704719574309880104027459519928561426614976765506993404471303402866118213391709022911900784132266900265907881488368043823738165532917447149
观察两个不同的PUBLIC KEY,我们可以发现这两个数字并不互质,因此我们可以通过计算它们的最大公约数(gcd)来对其进行分解, 然后使用RSA算法对密文进行解密
import gmpy2
from Crypto.Util.number import bytes_to_long, long_to_bytes
n1 = 556158105590834976431558928325513589105081492652755424074467607151874315636855396122125273325817553796612291848909117644653284981771321131029674811686131187013989064864196404384879788451795938651419563686793351050142562959856882963716443864808039588808249459235975406965140444645383867910008980312118367360517078340175036174601332039664791274773927402370296851615688289571808884278860845489906149783372217128226848310862334179596553922951363126441629316491364930837968039568251399708736609843802545621605524395734415488356308454922527638801209194552358792846849770346101228380941113997038431056263536176936919886745864499606835496983962418763375652668598495791867152858808945622011266551763393984867065078168636677570860666137407919366570703538112245696542273601139605963190931958627975480200833182102370905290478293324966470879497476364727502815626081289739299152417201194933386329363773683669370863087121612415452932920166584826265433897837759005149843012158035548712008040445141751581971956838344879446088088664239419157921124633656067816655030632948205337244381249094012909738141910869640686422395119757931637641944034336335848693385433161156645554789743212704503894060297996319569366957459528459886825717644064858902861269251561
n2 = 498712005899102574047090085672537607495675117721787019009683338824961823045375560041604951846282427076543102385990575067869331117121083260174627165032871731279577045598969880971684945789468108848921437266912893421945332606253138984852256381607895290404401528057160741969664262937399959430753515382341552822026924914576658307387993652213246807866391869012653808048725927693715926277118545414630792337856821997245531637761512318221732582323548070285947557983420435176618154379514688737254056486254664556448730935209697614488627962529219292457098968378084484810198625322954971013806082769576861358349091372888210246472041372553033519253998341745422724388353914960958742059871551881650345110981927504719674560538714941172122720895554436068378388498810709466121883624160172609182590942472427532780280635240276072651501588912999933648794610782505814543688909260877614537073015096234528641477478012747879703361724703530002439171204874964775375551225009223274763111664211552362462377489115340026612136316646151716791884369449107175601796761379361375235529334624203103898705673965351568439596715461066226607900467085986062258402558464633102484780119710075816171169823974663312855113451136557829969532897025124111608820103470903024670025902961
## n1和n2的最大公约数
p = gmpy2.gcd(n1, n2)
## n2 = p * q
q = n2 // p
## 常用的e值
e = 65537
## 解密
phi = (q - 1) * (p - 1)
d = pow(e, -1, phi)
c2 = 255969462230410974724874222157071907214698658758284129945019852263183265349452974565224431523567435520206840390757808963050048920001328977189604700028220137019834174876893683289876110503827141887722625021066233125120410566741752548662312356551242040149910934276723249204127604800242299144507452541851634516936095503290055126405789006961171998376809675343440099895866785234587394718154935911115242035791253932458474312610345923652546621360104272770090564260456142444548300656588893645791538525020323749718912364346786547618875298785118493472402768557855179315512251225067957136609633235171528186484863746927069205203811405439129387088915927220847395047577631953682214884069470625881706732844000443386937244585986728981629072760184456100508039168486000529601996259697904500990483242205227155175285953916377458780037461385732930536443665210662909285732207572977888627310959529039454477474328933828466470832050592528289704333235068458120443336182330491988704470264401092119563917473478654356824251269935549032216975993562898319602253608213050255932566749926836704860294459425584429526139674999138289704719574309880104027459519928561426614976765506993404471303402866118213391709022911900784132266900265907881488368043823738165532917447149
password = pow(c2, d, n2)
print("password:", long_to_bytes(password))
输入密码明文即可得到任务的flag。