bookfresh tricky file upload bypass to rce

5
 Hello all today i’m going to write about an interesting vulnerability i’ve found in Square’s Acquisition website bookfresh.com that was escalated to remote code execution. the story started when i saw that Bookfresh became a part of Square bug bounty program at Hackerone .i decided to take a look at and start finding some vulnerabilities . i’ve found that the website is vulnerable to many XSS but i was looking for something bigger like Sql Injection or RCE. so while i was checking for sql injection bugs i navigated to the profile page and found there is a file upload form to upload your profile photo . at the first moment i didn’t expect to find any vulnerability in that upload functionality but i decided to give it a try maybe i could be lucky. i uploaded a jpg image file while intercepting the http request then i changed the filename extension from jpg to php and forwarded the request. i surprised that the image was uploaded with the php extension. i didn’t believe my eyes so i copied the image link and opened it in the browser. it displayed the image binary data as you were opening the image in a text editor which means it was successfully executed as php script and the response content-type was set to text/html so this is a simple and direct file upload bypass, right ? all i have to do is to inject my php code in the jpg file and get fast remote code execution . so i used a simple php code <? phpinfo  (); ?> and injected it into the EXIF headers of jpg image then uploaded the image but when i viewed it again no php code was executed and nothing happened! so i saved the i mage to my computer and exec uted strings command to see if it still have the phpinfo()  code , however the results returned none !!

Upload: api-279898993

Post on 06-Oct-2015

138 views

Category:

Documents


0 download

TRANSCRIPT

  • HelloalltodayimgoingtowriteaboutaninterestingvulnerabilityivefoundinSquaresAcquisitionwebsitebookfresh.comthatwasescalatedtoremotecodeexecution.thestorystartedwhenisawthatBookfreshbecameapartofSquarebugbountyprogramatHackerone.idecidedtotakealookatandstartfindingsomevulnerabilities.ivefoundthatthewebsiteisvulnerabletomanyXSSbutiwaslookingforsomethingbiggerlikeSqlInjectionorRCE.sowhileiwascheckingforsqlinjectionbugsinavigatedtotheprofilepageandfoundthereisafileuploadformtouploadyourprofilephoto.atthefirstmomentididntexpecttofindanyvulnerabilityinthatuploadfunctionalitybutidecidedtogiveitatrymaybeicouldbelucky.iuploadedajpgimagefilewhileinterceptingthehttprequestthenichangedthefilenameextensionfromjpgtophpandforwardedtherequest.isurprisedthattheimagewasuploadedwiththephpextension.ididntbelievemyeyessoicopiedtheimagelinkandopeneditinthebrowser.itdisplayedtheimagebinarydataasyouwereopeningtheimageinatexteditorwhichmeansitwassuccessfullyexecutedasphpscriptandtheresponsecontenttypewassettotext/htmlsothisisasimpleanddirectfileuploadbypass,right ?allihavetodoistoinjectmyphpcodeinthejpgfileandgetfastremotecodeexecution.soiusedasimplephpcodeandinjecteditintotheEXIFheadersofjpgimagethenuploadedtheimagebutwhenivieweditagainnophpcodewasexecutedandnothinghappened!soisavedtheimagetomycomputerandexecutedstringscommandtoseeifitstillhavethephpinfo()code,howevertheresultsreturnednone!!

  • ItturnedoutthatallEXIFmetadatawasdeletedfromtheimageafteruploadingittotheserverandtheimagewasconvertedusingtheGDlibraryinphpusingtheimagecreatefromjpeg()function.sothisseemsnotexploitableusingexifdata,butwhatwillhappenifiinjectedmyphpcodeintotheimagedataitselfnottheEXIFmetadata?ithoughtthatwouldwork!soitriedtoopenthejpgfileandinjectthephpcodeattheendofthefileasthefollowing

    theimagewasstillvalidandworkingonmycomputer,afterthatiuploadedtheimagefile1.jpgbuttheresultswaslikethefollowing:

  • itdisplayederrormessageFilemustbeavalidimage(.gif,.jpg,.jpeg,or.png),iwassurprisedhowitdetectedthattheimagewasntvalidimagewhiletheimageisworkingonmycomputersoitriedwithsomeotherjpgfilesanditturnedoutthatmodifyingasinglecharacterinanyofthosejpgimageswontbeacceptedbyphpgdlibraryasavalidimageandwillnotbeuploaded.afterthatitriedthesamethingwithgifimageanditworkedlikeacharmandtheimagewasuploadedsuccessfullywithoutthrowinganyerrors,butwhenitriedtochecktheimageafteruploadingit.ifoundthatmyphpcodewastotallyremovedfromit itriedagaintoinjectthephpcodeintoothergifimagesandindifferentplacesintheimagebutthephpcodewasgettingremovedafteruploadingit.thatlookstotallyunexploitable,butimonlyonestepawayfromgettingRCE,soishouldfindawaytouploadmyimagewiththeinjectedphpcodeandbypasstheimagecreatefromgif()function.idontknowalotaboutimageprocessingandhowthephpGDworksbutitriedtodothatwithsimpleoldschoolway.icamewithanideatocomparethegifimagesbeforeandafteritgetconvertedusingphpgdandsearchforanysimilaritybetweenthem,soififindasimilarpartintheoriginalfilethatwaskeptalsoafterconvertingusingthephpgdthenicaninjectmyphpcodeinthatpartandgetRCEidecidedtotrythis,soicodedapythonscriptthatwillcomparetheimagesbeforeandafterconvertingandcheckforanysimilaritybetweenthem.thenisearchedinmycomputerforallthegifimagesandcopiedthemallinonefolder,afterwardsiwroteaphpscriptthatwilltakeallthegifimagesinthatfolderandregeneratethemusingthephpgdimagecreatefromgif()functionandsavethemintoanotherfoldertheniusedthepythonscripttocomparethefilesandcheckforanysimilar13byteswhichwillbethelengthofintheoriginalandtheconvertedgifimagefiles,andtheresultswasreallyawesome ,ivefoundgifimagewithabigsimilaritiesafteritwasconvertedusingphpgd.

  • thevalueswererepresentedinhex,soiopenedtheoriginalimagefileusingahexeditorandsearchedforaoneofthosematchedvalues3b45d00ceade0c1a3f0e18aff1andmodifieditto,savedthefileandconverteditwithphpgdthenthencheckedthestringsinthefile.

    andguesswhat?thephpcodewasstillthere iuploadedthegifimagetobookfreshandthatwastheresult

  • phpcodeexecutedsuccessfullyandivegotRCE thetricksuccessfullydefeatedthePHPGDgetimagesize()andimagecreatefromgif()functionsthatareusedbymanywebdevelopersnowdaystovalidateimageuploads.ivereportedthevulnerabilitytosquaresecurityteamtheyreleasedafastfixforthevulnerabilitybutiwasabletobypassitagainsoigavethemmyrecommendationsforacompletefixandtheyapplieditandpaidmeaverynicebountyforthisbug