前言
RSA是在CTF中經常出現的一類題目。一般難度不高,並且有一定的套路。(10.1補:我錯了,我不配!我不配密碼學)在此我寫篇文章進行總結。本文不過多贅述RSA的加解密, 僅從做題角度提供方法。雖然說不贅述加解密,但是我們還是需要清楚在RSA里面的幾個基本參數。
N:大整數N,我們稱之為模數(modulus)
p 和 q :大整數N的兩個因子(factor)
e 和 d:互為模反數的兩個指數(exponent)
c 和 m:分別是密文和明文
而{N,e}稱為公鑰,{N,d}稱為私鑰。總的來說,明文m(一般為flag)就像是一個鎖,而私鑰就是打開這個鎖的鑰匙。我們要做的就是根 據公鑰來生成這把鑰匙來打開鎖。而私鑰中的N又是可以從公鑰中獲得的,所以關鍵就是在d的獲取,d的值和p,q,e有關。p,q又是N的 兩個因子,所以RSA題目關鍵便是對N的分解,分解出N的兩個因子題目便解決了。這便是RSA題目的思路。
具體類型
已知p,q,e,獲取d
這種題目一般不難,是RSA里面的入門題目。通常可以使用python腳本解題。
import gmpy2 p =gmpy2.mpz(336771668019607304680919844592337860739) q =gmpy2.mpz(296173636181072725338746212384476813557) e =gmpy2.mpz(65537) phi_n= (p - 1) * (q - 1) d = gmpy2.invert(e, phi_n) print("d is:") print (d)
也可以通過RSA-Tool解出d.
已知e,d,N,求p,q
python代碼如下:
# coding=utf-8 import random import libnum d = 79636639378326691339908122673730404813380296570362148297604910660437221154417 e = 65537 n = 99742889480132178464693625265991467727088330702125690789109469022100733238623 k = e * d - 1 r = k t = 0 while True: r = r / 2 t += 1 if r % 2 == 1: break success = False for i in range(1, 101): g = random.randint(0, n) y = pow(g, r, n) if y == 1 or y == n - 1: continue for j in range(1, t): x = pow(y, 2, n) if x == 1: success = True break elif x == n - 1: continue else: y = x if success: break else: continue if success: p = libnum.gcd(y - 1, n) q = n / p print 'P: ' + '%s' % p print 'Q: ' + '%s' % q else: print 'Cannot compute P and Q'
已知N,e,c,求m
這種題目要先分解出p,q。之后的python代碼如下:
#!/usr/bin/env python # -*- coding: utf-8 -*- import gmpy2 p = 336771668019607304680919844592337860739 q = 296173636181072725338746212384476813557 e = 65537 c = 55907434463693004339309251502084272273011794908408891123020287672115136392494 n = p * q fn = (p - 1) * (q - 1) d = gmpy2.invert(e, fn) h = hex(gmpy2.powmod(c, d, n))[2:] if len(h) % 2 == 1: h = '0' + h s = h.decode('hex') print s
給出公鑰文件和密文文件獲取明文
出題人會給你一個公鑰文件(通常是以.pem或.pub結尾的文件)和密文(通常叫做flag.enc之類的),你需要分析公鑰,提取出(N,e),通過各種攻擊手段恢復私鑰,然后去解密密文得到flag。 EG:一般先用openssl提取公鑰文件中的N和e。
root@kali:~/桌面/RSA# openssl rsa -pubin -text -modulus -in public.pem RSA Public-Key: (256 bit) Modulus: 00:dc:84:79:8f:78:6d:6d:ab:33:14:46:3e:2c:5f: 27:cd:0d:c4:8a:0f:97:13:da:fc:f9:18:02:eb:bc: b7:1d:5f Exponent: 65537 (0x10001) Modulus=DC84798F786D6DAB3314463E2C5F27CD0DC48A0F9713DAFCF91802EBBCB71D5F writing RSA key -----BEGIN PUBLIC KEY----- MDwwDQYJKoZIhvcNAQEBBQADKwAwKAIhANyEeY94bW2rMxRGPixfJ80NxIoPlxPa /PkYAuu8tx1fAgMBAAE= -----END PUBLIC KEY-----
公鑰:65537 (0x10001)
模數:DC84798F786D6DAB3314463E2C5F27CD0DC48A0F9713DAFCF91802EBBCB71D5F
轉化為十進制: 99742889480132178464693625265991467727088330702125690789109469022100733238623
分解N得到p:336771668019607304680919844592337860739
q:296173636181072725338746212384476813557
寫個python腳本解出flag
import gmpy2 p = 336771668019607304680919844592337860739 q = 296173636181072725338746212384476813557 e = 65537 f = int(open('flag.enc', 'rb').read().encode('hex'), 16) print f n = p * q fn = (p - 1) * (q - 1) d = gmpy2.invert(e, fn) h = hex(gmpy2.powmod(f, d, n))[2:] if len(h) % 2 == 1: h = '0' + h s = h.decode('hex') print s
分解N
我們在上面的類型題目中經常提到分解N。但只是一筆概括,下面具體講講怎么來分解N。
嘗試在線網站分解N:
Yafu分解N:
在RSA中,當p、q的取值差異過大或過於相近的時候,使用yafu可以快速的把n值分解出p、q值,原理是使用Fermat方法與Pollard rho方法等。
利用公約數分解N:
識別此類題目,通常會發現題目給了多個n,均不相同,並且都是2048bit,4096bit級別,無法正面硬杠,並且明文都沒什么聯系,e也一般取65537。可以直接gcd(n1,n2)求出一個因數。 代碼如下:
import gmpy2 n1 = 9051013965404084482870087864821455535159008696042953021965631089095795348830954383127323853272528967729311045179605407693592665683311660581204886571146327720288455874927281128121117323579691204792399913106627543274457036172455814805715668293705603675386878220947722186914112990452722174363713630297685159669328951520891938403452797650685849523658191947411429068829734053745180460758604283051344339641429819373112365211739216160420494167071996438506850526168389386850499796102003625404245645796271690310748804327 n2 = 13225948396179603816062046418717214792668512413625091569997524364243995991961018894150059207824093837420451375240550310050209398964506318518991620142575926623780411532257230701985821629425722030608722035570690474171259238153947095310303522831971664666067542649034461621725656234869005501293423975184701929729170077280251436216167293058560030089006140224375425679571181787206982712477261432579537981278055755344573767076951793312062480275004564657590263719816033564139497109942073701755011873153205366238585665743 p = gmpy2.gcd(n1, n2) print 'gcd(n1, n2):\n', p q1 = n1 / p q2 = n2 / p print 'q1 is:\n', q1 print 'q2 is:\n', q2
低加密指數攻擊:
在 RSA 中 e 也稱為加密指數。由於 e 是可以隨意選取的,選取小一點的 e 可以縮短加密時間(比如 3),但是選取不當的話,就會造成安全問題。(題目特征:e=3)
這種題目又具體分以下兩種情況:
明文m極小(m的三次方小於N):
此時利用代碼如下:
# -*- coding: cp936 -*- import gmpy2 e = 3 # 讀入 n, 密文 n= 22885480907469109159947272333565375109310485067211461543881386718201442106967914852474989176175269612229966461160065872310916096148216253429849921988412342732706875998100337754561586600637594798877898552625378551427864501926224989873772743227733285336042475675299391051376624685754547818835551263597996620383338263448888107691240136257201191331617560711786674975909597833383395574686942099700631002290836152972352041024137872983284691831292216787307841877839674258086005814225532597955826353796634417780156185485054141684249037538570742860026295194559710972266059844824388916869414355952432189722465103299013237588737 c= 15685364647213619014219110070569189770745535885901269792039052046431067708991036961644224230125219358149236447900927116989931929305133870392430610563331490276096858863490412102016758082433435355613099047001069687409209484751075897343335693872741 print 'n=', n print 'c=', c print '[+]Detecting m...' result = gmpy2.iroot(c, 3) print ' [-]The c has cubic root?', result[1] if result[1]: print ' [-]The m is:', '{:x}'.format(result[0]).decode('hex') print '[!]All Done!'
m的 3 次方比N大,但不足夠大:
此時利用代碼如下:
# -*- coding: cp936 -*- import gmpy2, time e = 3 # 讀入 n, 密文 n = 114976915747243387792157708464120735018971336213935438953074748276198282761939060395482051056351068439137722626185590043024556656813730840050547350912425438364703854627760482842307943026011880815011654341047422453012558617703411700393668892701036222135444420377515575624398723436532681305293727164639582093389 c = 5828813410620741112500628876643872258919868379601617907887884191584237969605489971465692568848339200057188383649365078832766143513766368216471491824042974016773526107276856706832404477882581400769791378958901067683158857990261489285951805740071223765359992165262854641069674603160977034446644199945940251030 i = 239000000 # i 應該是未知的。這里縮短一下距離, 防止跑得太久 print 'n=', n print 'c=', c print '[!]Done!\n' print '[+]Detecting m...' s = time.clock() while 1: m, b = gmpy2.iroot(c + i * n, 3) if b: print ' [-]m is: ' + '{:x}'.format(int(m)).decode('hex') break #print ' [-]i = %d\r' % i, i += 1 print '[!]Timer:', round(time.clock() - s, 2), 's'
低加密指數廣播攻擊
如果選取的加密指數較低,並且使用了相同的加密指數給一個接受者的群發送相同的信息,那么可以進行廣播攻擊得到明文。
這個識別起來比較簡單,一般來說都是給了三組加密的參數和明密文,其中題目很明確地能告訴你這三組的明文都是一樣的,並且e都取了一個較小的數字。 利用代碼如下:
# -*- coding: cp936 -*- import gmpy2 import time def CRT(items): N = reduce(lambda x, y: x * y, (i[1] for i in items)) result = 0 for a, n in items: m = N / n d, r, s = gmpy2.gcdext(n, m) if d != 1: raise Exception("Input not pairwise co-prime") result += a * s * m return result % N, N # 讀入 e, n, c e = 9 n = [142782424368849674771976671955176187834932417027468006479038058385550042422280158726561712259205616626939123504489410624745195777853423961104590708231562726165590769610040722589287393102301338152085670464005026301781192671834390892019478189768725018303217559795377795540494239283891894830166363576205812991157L, 153610425077816156109768509904751446801233412970601397035720458311275245730833227428213917577405780162151444202393431444812010569489900435979730559895340377469612234558042643742219128033827948585534761030527275423811282367831985007507137144308704413007806012914286105842311420933479771294576841956749281552971L, 152540067782701001222493009941492423063369171831039847414320547494725020441901272486665728360741395415762864872737675660423920609681185809510355937534756399208661762715484879562585724584849261266873624875852300611683382543315580370484972470694466195837255994159609193239840228218925381488410059939975556977947L, 125842716702134814646356078531900645012495638692517778270527426844383063904041812273637776798591687732598509470005151551320457132061693618473039437320011446697406190781306264437609046721508738109650829547010385875425097336266103994639126319889016342284747700714199556143378526590058467791687837422897022829661L, 116144389285266462769913139639175922392318396923181100785008570884082681963637784423143843845816350379438789947802939701820129805341796427821894273985551331666719808355412080909245720551238149511778060242720419584504473490216670437024863860559347959698828131475160058721701582089480924088773887932997353631767L, 127833907448946785858374094953899556339175475846831397383049660262333005992005484987913355932559627279178940862787593749842796469355336182379062826441222705075178971785791223706944120681105575965622931327112817747065200324610697178273898956820957640413744954233327851461318200323486469677469950386824833536523L, 130561613227079478921314550968562766645507834694262831586725464124109153306162445639759476845681271537955934718244296904503168256991962908095007040044300188572466395275317838178325500238288302672390013747102961340256309124310478931896245221622317302428447389760864327859640573452084295225059466376349115703119L, 115953389401040751013569404909249958538962411171147823610874077094621794755967854844224923689925397631692572916641171075740839099217316101334941033937183815345038898177087515909675028366437302462022970987947264115373697445950951595479758872029099661065186221250394358255523574834723958546450323357472451930993L, 143437107845384843564651522639125300763388830136500260725097766445883003928355325003575359566631064630487365774344508496878731109174874449170057678821440711511966073934025028100604234445470976333825866939923998344367645612128590820812489407412175198698290167077116185959180877334222693344630253253476594907313L] c = [85033868418784308573673709960700777350314426427677627319697346811123742342359072170220428874952996988431950989321281905284522596263957356289624365171732095210045916218066135140320107686084053271623461104022705353814233772164502775939590711842361956121603943483040254727995655776263673058788416722141673409688L, 66065963470666895005407449599703926269325406456711861190876894466341571726360462706664546294453572319565476664348345756905411939632955966517708138047546806602828064213238537646393524578984547577761559965654539771172357089802682793169968961304179886652390277814477825753096636750388350662980872556701402397564L, 116011740820520887443111656288411611070614127688662643257265381793048354928820176624229624692124188995846076431510548507016903260774215950803926107831505634778278712070141663189086436127990584944132764896694777031370995058271038329228336417590284517922855284619653301817355115583540545182119702335431334401666L, 97640420284096094887471273365295984332267897927392169402918423863919914002451127544715668846623138003564829254309568918651163254043205129883843425179687841236818720463784828905460885026290909768599562386370732119591181513319548915478512030197629196018254041500662654260834562708620760373487652389789200792120L, 8112507653841374573057048967617108909055624101437903775740427861003476480616929517639719198652146909660899632120639789106782550275648578142883715280547602249589837441805676364041484345030575130408744621981440093280624046635769338568542048839419939250444929802135605724150484414516536378791500915047844188300L, 36792148360808115566234645242678223867680969786675055638670907933041180936164293809961667801099516457636164692292891528415720085345494773373966277807505798679784807614784581861287048096977968620964436947452527540958289441390882589051225367658014709290392321808926567572528170531844664734909469690750971883323L, 53043093283305492238903255767698153246673671181809989362223466090875767705978690531154079519999671834688647277179370374802495005937892824566602423646978168777735383632928274082669949750078161820002768640908750005814934158829006019656592134357897586040866207754535586785064545866404380204728594863102313407789L, 88499407133762624445946519155722583633934260410706930537441122463087556094734626189377091740335667052378955691250910459790202385799502439716173363179773811920751410726795431402796346647688144853156900427797933862087074385441977254140336390678022955770879265490567987868532251217565094093318626424653599450992L, 138337520305048557335599940473834485492131424901034295018189264168040969172072024612859307499682986987325414798210700710891033749119834960687318156171051379643844580970963540418974136891389303624057726575516576726845229494107327508855516437230240365759885913142671816868762838801720492804671259709458388192984L] print '[+]Detecting m...' data = zip(c, n) x, n = CRT(data) realnum = gmpy2.iroot(gmpy2.mpz(x), e)[0].digits() print ' [-]m is: ' + '{:x}'.format(int(realnum)).decode('hex') print '[!]All Done!'
低解密指數攻擊
與低加密指數相同,低解密指數可以加快解密的過程,但是也帶來了安全問題。種基於連分數(一個數論當中的問題)的特殊攻擊類型就可以危害 RSA 的安全。此時需要滿足:q<p<2q。如果滿足上述條件,通過Wiener Attack可以在多項式時間中分解N。 識別特征:E特別大。 利用代碼如下:
# -*- coding: cp936 -*- import gmpy2 import time # 展開為連分數 def continuedFra(x, y): cF = [] while y: cF += [x / y] x, y = y, x % y return cF def Simplify(ctnf): numerator = 0 denominator = 1 for x in ctnf[::-1]: numerator, denominator = denominator, x * denominator + numerator return (numerator, denominator) # 連分數化簡 def calculateFrac(x, y): cF = continuedFra(x, y) cF = map(Simplify, (cF[0:i] for i in xrange(1, len(cF)))) return cF # 解韋達定理 def solve_pq(a, b, c): par = gmpy2.isqrt(b * b - 4 * a * c) return (-b + par) / (2 * a), (-b - par) / (2 * a) def wienerAttack(e, n): for (d, k) in calculateFrac(e, n): if k == 0: continue if (e * d - 1) % k != 0: continue phi = (e * d - 1) / k p, q = solve_pq(1, n - phi + 1, n) if p * q == n: return abs(int(p)), abs(int(q)) print 'not find!' time.clock() n = 12238605063252292170613110607692779326628090745751955692266649177882959231822580682548279800443278979485092243645806337103841086023159482786712759291169541633901936290854044069486201989034158882661270017305064348254800318759062921744741432214818915527537124001063995865927527037625277330117588414586505635959411443039463168463608235165929831344586283875119363703480280602514451713723663297066810128769907278246434745483846869482536367912810637275405943566734099622063142293421936734750356828712268385319217225803602442033960930413469179550331907541244416573641309943913383658451409219852933526106735587605884499707827 e = 11850552481503020257392808424743510851763548184936536180317707155841959788151862976445957810691568475609821000653594584717037528429828330763571556164988619635320288125983463358648887090031957900011546300841211712664477474767941406651977784177969001025954167441377912326806132232375497798238928464025466905201977180541053129691501120197010080001677260814313906843670652972019631997467352264392296894192998971542816081534808106792758008676039929763345402657578681818891775091140555977382868531202964486261123748663752490909455324860302967636149379567988941803701512680099398021640317868259975961261408500449965277690517 c = 9472193174575536616954091686751964873836697237500198884451530469300324470671555310791335185133679697207007374620225900775502162690848135615431624557389304657410880981454777737587420426091879654002644281066474715074536611611252677882396384453641127487515845176069574754606670518031472235144795376526854484442135299818868525539923568705203042265537204111153151119105287648912908771710419648445826883069030285651763726003413418764301988228077415599665616637501056116290476861280240577145515875430665394216054222788697052979429015400411487342877096677666406389711074591330476335174211990429870900468249946600544116793793 p, q = wienerAttack(e, n) print '[+]Found!' print ' [-]p =',p print ' [-]q =',q print ' [-]n =',p*q d = gmpy2.invert(e,(p-1)*(q-1)) print ' [-]d =', d print ' [-]m is:' + '{:x}'.format(pow(c,d,n)).decode('hex') print '\n[!]Timer:', round(time.clock(),2), 's' print '[!]All Done!'
共模攻擊
如果在RSA的使用中使用了相同的模N對相同的明文m進行了加密,那么就可以在不分解n的情況下還原出明文m的值。
識別特征:若干次加密,e不同,N相同,m相同。
利用代碼如下:
# -*- coding: cp936 -*- import time import gmpy2 n = 158052722013789461456896900244510199169216575693048895162538548356466884311543740968048825149608833390255268602486435690724338965409521812963337715301197225841194835534751041470231293288252951274190599189716955573428884560130364021535005115652592074445852835422027406556727605302404510264249211145063332337043 e = [665213, 368273] c = [16698617641888248664694980135332125531792692516788088682722832061393117609508765284473236240256421599515450690670639565968165473479697383505401285976148490839526672808730165847471005704945978274496508928460578173068717106075169723401049489389383596761956301440156581021583368058047939083755488885694261340425L, 59192887933967939708054321952273893559113509451228797382728687616356609407020086787061368452871936378934964292805289941535766263083244529814852043063188312786173717046316177403357053871483983775362121186037776932260378728059531236711960979620603784044468207000654149190295060179235411429700710154759043236436L] print '[+]Detecting m...' time.clock() c1 = c[0] c2 = c[1] e1 = e[0] e2 = e[1] s = gmpy2.gcdext(e1, e2) s1 = s[1] s2 = s[2] # 求模反元素 if s1 < 0: s1 = -s1 c1 = gmpy2.invert(c1, n) elif s2 < 0: s2 = -s2 c2 = gmpy2.invert(c2, n) m = pow(c1, s1, n) * pow(c2, s2, n) % n print ' [-]m is:' + '{:x}'.format(int(m)).decode('hex') print '\n[!]Timer:', round(time.clock(),2), 's' print '[!]All Done!'
Rabin攻擊
當e=2時可以采取Rabin攻擊。
利用代碼如下:
#!/usr/bin/python # coding=utf-8 # 適合e=2 import gmpy import string from Crypto.PublicKey import RSA # 讀取公鑰參數 with open('./tmp/pubkey.pem', 'r') as f: key = RSA.importKey(f) N = key.n e = key.e p = 275127860351348928173285174381581152299 q = 319576316814478949870590164193048041239 with open('./tmp/flag.enc', 'r') as f: cipher = f.read().encode('hex') cipher = string.atoi(cipher, base=16) # print cipher # 計算yp和yq yp = gmpy.invert(p,q) yq = gmpy.invert(q,p) # 計算mp和mq mp = pow(cipher, (p + 1) / 4, p) mq = pow(cipher, (q + 1) / 4, q) # 計算a,b,c,d a = (yp * p * mq + yq * q * mp) % N b = N - int(a) c = (yp * p * mq - yq * q * mp) % N d = N - int(c) for i in (a,b,c,d): s = '%x' % i if len(s) % 2 != 0: s = '0' + s print s.decode('hex')
e=1
當e=1時,攻擊代碼可以轉化為:
#/usr/bin/env python3 #coding:utf-8 import binascii import gmpy2 N_hex=0x180be86dc898a3c3a710e52b31de460f8f350610bf63e6b2203c08fddad44601d96eb454a34dab7684589bc32b19eb27cffff8c07179e349ddb62898ae896f8c681796052ae1598bd41f35491175c9b60ae2260d0d4ebac05b4b6f2677a7609c2fe6194fe7b63841cec632e3a2f55d0cb09df08eacea34394ad473577dea5131552b0b30efac31c59087bfe603d2b13bed7d14967bfd489157aa01b14b4e1bd08d9b92ec0c319aeb8fedd535c56770aac95247d116d59cae2f99c3b51f43093fd39c10f93830c1ece75ee37e5fcdc5b174052eccadcadeda2f1b3a4a87184041d5c1a6a0b2eeaa3c3a1227bc27e130e67ac397b375ffe7c873e9b1c649812edcd e_hex=0x1 c_hex=0x4963654354467b66616c6c735f61706172745f736f5f656173696c795f616e645f7265617373656d626c65645f736f5f63727564656c797d c_hex = gmpy2.mpz(c_hex) N_hex = gmpy2.mpz(N_hex) i = 0 while i<10: m_hex = hex(c_hex + gmpy2.mpz(hex(i))*N_hex) print(m_hex[2:]) try: print(binascii.a2b_hex(m_hex[2:]).decode("utf8")) except binascii.Error as e: print("位數非偶數,跳過...") i += 1
以上便是常見的CTF種分解N的辦法。在實際應用中,我總結出了以下的判斷順序:
1.先網站在線查詢
2.使用Yafu
3.如果所給為多個N,使用公因數攻擊
4.當E為1,2或特別大時,使用Rabin攻擊或低解密指數攻擊
5.當E為3時,如果只給出一組明密文。使用低加密指數攻擊,如果給出多組明密文,使用低加密指數廣播攻擊
6.如果所給為多次加密,使用同個N,使用共模攻擊
超級大殺器
上面有一堆讓人頭大的算法,比如分解一個大整數可能就有十來種算法,當然不是每個算法都能成功分解我們題目給的大整數的,如果我們挨個嘗試,不僅浪費精力還浪費時間,等你找到正確的算法,已經與一血,二血,三血無緣了。所以我們需要一個自動化的工具。
https://github.com/3summer/CTF-RSA-tool
該項目便是一個超級大殺器,它能根據題目給的參數類型,自動判斷應該采用哪種攻擊方法,並嘗試得到私鑰或者明文,從而幫助CTFer快速拿到flag或解決其中的RSA考點。 EG:
僅需一行命令便可以解出我們本來需要openssl提取N,e.分解N生成私鑰的繁瑣操作,可以大大提高效率。
不過,話雖如此,我們還是需要對上面那些常見的解法及N的分解方法有所了解並掌握。
CTF中RSA的題目大概就有這么幾樣,至於N的分解方法我也概括的差不多了,只有一類比較特殊Coppersmith相關尚未提及,這個就等以后有時間再另開篇章講了。(咕咕咕)
參考文章: https://www.tr0y.wang/2017/11/06/CTFRSA/index.html
https://err0rzz.github.io/2017/11/14/CTF%E4%B8%ADRSA%E5%A5%97%E8%B7%AF/
https://blog.csdn.net/huanghelouzi/article/details/82974741
https://www.freebuf.com/articles/others-articles/161475.html