46
@PhilippeDeRyck Philippe De Ryck C OMMON REST API SECURITY PITFALLS OWASP BeNeLux days 2017

COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API

@PhilippeDeRyck

PhilippeDeRyck

COMMON RESTAPISECURITY PITFALLS

OWASPBeNeLux days2017

Page 2: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API

POST/api/login{“username”:“philippe”,“password”:“Pass1234!”}

Loadtheapplication

Page 3: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API

https://github.com/OWASP/Top10/blob/master/2017/drafts/OWASP%20Top%2010%20-%202017%20RC1-English.pdf

Page 4: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API

ABOUT ME – PHILIPPE DE RYCK

§Mygoalistohelpyoubuildsecurewebapplications−Coursesandtrainingprograms− Talksatvariousdeveloperconferences− Slides,videosandblogpostsonhttps://www.websec.be

§ AuthoroftheWebSecurityFundamentalscourse− FreeonlinecourseontheedX platform−Allinfoonhttps://mooc.websec.be

§ CoursecuratorfortheSecAppDev course− Securitycoursetargetedtowardsdevelopers,architects,…−Week-longcoursetaughtbyinternationalexpertsintheirdomain

secappdev.org

Page 5: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API

HTTPS

Page 6: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API

OFFER YOUR APIOVER HTTPS

§ ThereisnovalidexcusetonotuseHTTPSanymore− Let’sEncryptoffersfreecertificatesforall−Performanceisnolongeranissue

§ APIsareaccesseddirectlyfromwithinanapplication−MakessettingupHTTPSeasier,asyoudonotneedtosupportaredirectfromHTTP− SimplydisableHTTPforyourAPIendpointsaltogether

§Network-basedattackscanstillattemptafallbacktoHTTP−ConfigureHTTPStrictTransportSecurity (HSTS)topreventthisfromhappening−HSTSwilltellthebrowsertouseHTTPSforeveryrequest,regardlessofthescheme

Strict-Transport-Security: max-age=31536000

Page 7: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API

SECURITY PITFALL

AllowingaccesstoyourAPIoverHTTP

APIsareaccessedfromcode,sothereisnoneedtosupportaredirectfromHTTPtoHTTPS.LockyourAPIfurther

downbyenablingHSTS

Page 8: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API

https://motherboard.vice.com/en_us/article/wjx3e4/t-mobile-website-allowed-hackers-to-access-your-account-data-with-just-your-phone-number

Page 9: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API

https://www.codementor.io/olatundegaruba/nodejs-restful-apis-in-10-minutes-q0sgsfhbd

Page 10: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API

INSECURE DIRECT OBJECT REFERENCES

§ Predictableidentifiersenabletheenumerationofresources−Dangerousifresourcesarenotshieldedbystrictauthorizationchecks−ManyAPIsonlycheckauthenticationstatus,butnotwhich userisauthenticated

§ Theonlypropermitigationisimplementingproperauthorizationchecks− E.g.checkingifthecurrentuseristheowneroftheresource

§ Theuseofnon-predictableidentifiersisacomplementarystrategy−UUIDsareagoodexampleofsuchanidentifier− Justbecarefulaboutusingthemasprimarykeysinthedatabase

Page 11: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API

SECURITY PITFALL

Usinginsecuredirectobjectreferences

Alwayscomplementabasicauthenticationcheckwithappropriateauthorizationchecks(e.g.ownershipofaresource)

Page 12: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API

1234

Page 13: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API

1234

Page 14: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API

1

2

3

4

Page 15: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API

THE TRUST LEVELS OF SESSION DATA

§ Server-sidesessionsshareanIDwiththeclientandstoredataontheserver−AttacksonsessionmanagementfocusonguessingorstealingtheID− Thedatastoredintheserver-sidesessionobjectcanbeconsideredtrusted

§ Client-sidesessionsareacompletelydifferentparadigm− Theactualdataisstoredontheclient,soitcanbeeasilyaccessed− Thedatacomesinfromtheclient,andisuntrustedbydefault

§ Client-sidesessionsrequireadditionaldataprotectionmeasures−Mandatoryintegritycheckstodetecttamperingwiththedata−Optionalconfidentialitymechanismstopreventdisclosureofinformation

Page 16: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API

SECURITY PITFALL

Mishandlingclient-sidesessiondata

Client-sidesessiondatacanbereadandmanipulated,soyouneedtoensureconfidentialityandintegrity

Page 17: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API

https://jwt.io/

Page 18: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API

JWTTOKENS IN PRACTICE

§ JWTtokensonlyrepresentclaimstobeexchangedsecurely− Thedataisbase64-encoded,whichoffersnoprotectionatall− TheJWTspecssupportintegrity(signing)andconfidentiality(encryption)

§ ThedefaultmodeofoperationissigningJWTs− Thesignatureispartofthetoken,andcanonlybegeneratedbytheissuer−AvalidsignatureindicatesthatthedataoftheJWTtokenhasnotbeenchanged

§Manylibrariesofferdecodefunctionsthatdonotcheckintegrity− Failingtofullyunderstandtheimportanceofintegritywillcausemisuse−Decodingisalsoaloteasierthanverifyingtheintegrity

Page 19: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API

https://github.com/auth0/java-jwt

Page 20: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API

SECURITY PITFALL

NotverifyingtheintegrityofyourJWTtokens

ManyJWTlibrariesofferfunctionstogetthedatafromatokenwithoutverifyingitsintegrity.Neverusetheminthebackend

Page 21: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API

Payloaddata

Payloaddata

sign verify

Signingwithasharedsecret Signingwithapublic/privatekeypair

Payloaddata

Payloaddata

sign verify

privatekey publickey

Page 22: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API

SIGNATURE SCHEMES FOR JWTTOKENS

§ManydevelopersonlyknowaboutsigningJWTswithasharedsecret− Thisisperfectlyvalidwithinoneapplicationorevenwithinonetrustboundary−Breaksdownwhentokensneedtobeverifiedoutsideofyourtrustboundary

§ Thesharedsecretcanneverleaveyourbackendapplication−Donotshareitwithyourclientapplication,or“friendly”APIs− Ifyouneedverificationinthosecases,signtheJWTwithaprivatekeyinstead

§ Theissuershouldbetheonlyoneknowingtheprivatekey− Thepublickeycanbedistributedtoanyone− Tokensaresignedwiththeprivatekey,andverifiedwiththepublickey

Page 23: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API

SECURITY PITFALL

UsingthewrongsignatureschemeonJWTtokens

SharedsecretsforverifyingJWTtokensareforusewithintheboundariesoftheapplication.Otherwise,useapublic/privatekeypair

Page 24: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API
Page 25: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API
Page 26: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API
Page 27: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API

https://connect2id.com/blog/using-openid-connect-to-make-assertions-about-end-users

Page 28: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API

SECURITY PITFALL

Notpropagatingidentityinformation

Callsareoftendelegatedtointernalsystemsorservices.Ensurethattheseservicespossessallrelevantidentityinformationformaking

authorizationdecisionsandcreatinganaudittrail

Page 29: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API

Cookie:JWT=eyJhbGciOiJIUzI1Ni…

Authorization:BeareryJhbGciOiJIUzI1Ni…

Page 30: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API

THE PROPERTIES OF COOKIES

§ Cookiesareamess,buttheyarecompatiblewiththeweb−Browsersstoreandsendcookiesautomatically−Cookiesarepresentonallrequests,includingthosecomingfromDOMelements−CookiesarecompatiblewithwebmechanismssuchasCORS,SSE,WebSockets,…

§ Securingcookie-basedmechanismsrequiresalotofeffort−Cookiesecurityflagsneedtobeconfiguredcorrectly−Cookieprefixesofferadditionalsecurity,butrequiremodifyingthename−CookiesenableanastyattackcalledCross-SiteRequestForgery(CSRF)

§ Cookiesareanightmaretosupportinnon-webapplications

Page 31: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API

THE PROPERTIES OF CUSTOM HEADERS

§ Customheadersarestraightforward,butcanbehardtouse−Nothandledautomatically,sotheapplicationneedstostoreandsendthevalue− ThebrowserwillnotattachittorequestscomingfromDOMelements− TheuseofmechanismssuchasCORS,SSE,WebSockets,… becomesmoredifficult

§ Securingheader-basedmechanismsisalsosurprisinglydifficult− Youhavetodecidewheretostorethedataintheclientapplication− You’relikelytomessupattachingtheheadertooutgoingrequests−ButthegoodnewsisthatcustomheadersdonotsufferfromCSRF

§ Customheadersareabreezetouseinnon-webapplications

Page 32: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API

https://www.toptal.com/web/cookie-free-authentication-with-json-web-tokens-an-example-in-laravel-and-angularjs

Page 33: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API

SECURITY PITFALL

Minimizingtheimpactofthetransportmechanism

CookiesareoftenfrowneduponinanAPIworld,andcustomheadersarepreferred.Bothhavevastlydifferentsecurityproperties,

somakesureyouunderstandthemfully

Page 34: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API

THE UNDERESTIMATED THREAT OF CSRF

websec.be

anysite.io

loginasPhilippe

Welcomepage

Showmessages

Latestmessages

Showobligatorycatpics

Kittensfromhell

Page 35: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API

https://arstechnica.com/information-technology/2014/03/hackers-hijack-300000-plus-wireless-routers-make-malicious-changes/

Page 36: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API

CROSS-SITE REQUEST FORGERY

§ CSRFexistsbecausethebrowserhandlescookiesveryliberally− Theyareautomaticallyattachedtoanyoutgoingrequest−Bydefault,there’snomechanismtoindicatethesourceorintentofarequest

§ManyAPIsareunawarethatanycontextcansendrequests−GETandPOSTrequestsareeasytotriggerusingDOMelementsorXHR−PUTandDELETErequestsareadifferentstory−DefendingagainstCSRFrequiresexplicitactionbythedeveloper

§ AtraditionalCSRFdefenseisusinghiddenformtokens

Page 37: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API

DEFENDING YOUR APIAGAINST CSRFwebsec.be

anysite.io

loginasPhilippe

Welcome,Philippe

Postmessage

Surething,Philippe

Showobligatorycatpics

Kittensfromhell

POST …Cookie: SID=123, XSRF-TOKEN=abcX-XSRF-TOKEN: abc

CookievalueiscopiedtoaheaderbyJavaScriptcode

Page 38: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API

THE RELATION BETWEEN CSRFAND CORS

§ Cross-originHTTPrequestshavealwaysexistedintheweb− Examplesareloadingimagesfromotherorigins,orsubmittingformsacrossorigins

§ CSRFmattersinanAPIsupporting“traditional”HTTPrequests−GET/POSTrequestswithtraditionalcontenttypesandnocustomheaders− TheserequestscaneasilybeforgedusingtraditionalHTMLelements

§ APIsusing“non-traditional”HTTPrequestsfallundertheprotectionofCORS− SucharequestcanonlybesentfromJavaScriptusingXMLHttpRequest− SucharequesttriggerstheCross-OriginResourceSharing(CORS) securitypolicy− Sucharequestwillonlybeallowediftheserverexplicitlyapprovesit

Page 39: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API

Content-Type:application/json

X-Show-Me:TheMoney

Page 40: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API

SECURITY PITFALL

UnderestimatingtheprevalenceofCSRFCSRFattacksexistwhencookiesareusedforkeepingsessionstate.Verifyifyou’revulnerableandimplementappropriatedefenses.

Ifyoudonotusecookies,youdonotneedtoworryaboutCSRF

Page 41: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API

/users/1’%20OR%20’1’=‘1

statement = conn.prepareStatement("SELECT * FROM BeersWHERE name LIKE ?");

statement.setString(0, parameter);

Page 42: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API

INPUT VALIDATION IS AN IMPORTANT FIRST LINE OF DEFENSE

§ Limitingthenumberofvalidinputsreducestheattacksurface−Untrusteddatashouldbevalidatedbeforeusingit− Therestrictionsthatcanbeimposeddependonthetypeofcontent

§ Bestpracticesforinputvalidation−Onlyacceptcontenttypesthatyouexpect,andrejecteverythingelse−Validateeveryinputagainstitsexpecteddatatype− Imposesensiblelengthrestrictions,andalwayssetastrictupperbound−Alwaysuseasecureparsertoprocessinput

Page 43: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API

BUT INPUT VALIDATION ONLY GETS YOU SO FAR

§ Inputvalidationtargetssymptoms,nottherootcauseoftheissue− Injectionneedstobeaddressedinthecode,notattheinputlevel

§Oncethedataiscomplexenough,validationbypasseswillexist−Validationorsanitizationishardtogetright,sodonotsolelyrelyonthem−AgoodexamplearethehugeXSSfilterevasioncheatsheets

§ Andsometimes,it’sjustnottheAPI’sresponsibility−Cross-sitescriptinginwebapplicationsistheperfectexample− TheAPIhasnoideawherethedatawillbeused,soitcannotrenderitsafe− Theclient-sideapplicationneedstohandlethis,ase.g.Angulardoesoutofthebox

Page 44: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API

SECURITY PITFALL

Overorunderestimatinginputvalidation

Eventhoughinputvalidationisagoodfirstlineofdefense,itwillfailastheonlydefense.Donotrelyoninputvalidationalone

Page 45: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API

QuestionEverythingHowisthisdifferentfromwhatweusedtodo?

Dowereallyunderstandwhatwe’redoing?

Havewevalidatedtheintegrityandformatofthatdata?

Page 46: COMMON REST API SECURITYPITFALLS - OWASP · §And sometimes, it’s just not the API’s responsibility −Cross-site scripting in web applications is the perfect example −The API

NOW IT’S UP TO YOU …

Secure Share@PhilippeDeRyck