LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by...

62
LECTURE 23: MORE SECURITY CSE 442 – Software Engineering

Transcript of LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by...

Page 1: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

LECTURE23:MORESECURITYCSE442–SoftwareEngineering

Page 2: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

SeriousHackingAttempts

Page 3: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

CrossSiteRequestForgery

Page 4: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

Cross-SiteRequests

¨  Same-siterequestiflocalpagemakesHTTPrequest¨  Requestsenttoothersitecalledcross-siterequest¨  ManydifferentreasonsforincludingHTTPrequest

¤ Embedimages¤ Loadframes¤ Showads¤ Sendlink

¨  Sendscookiesonsame-siteANDcross-siterequests

Page 5: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

Browser

CrossSiteRequestExample

Page 6: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

PagefromFacebook

Browser

CrossSiteRequestExample

Page 7: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

PagefromFacebook

Browser

Same-Site Request

CrossSiteRequestExample

Page 8: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

PagefromGoogle

PagefromFacebook

Browser

CrossSiteRequestExample

Same-Site Request

Page 9: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

PagefromGoogle

PagefromFacebook

Browser

Same-Site Request

Same-Site Request

CrossSiteRequestExample

Page 10: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

PagefromGoogle

PagefromFacebook

Browser

Same-Site Request

PagewithAds&Likebutton

Cross-Site Requests

Same-Site Request

CrossSiteRequestExample

Page 11: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

Cross-SiteKnowledge

¨  Browsersknowifrequestcross-siterequestornot¤ ButtypeofrequestNOTsharedwithserver

¨  Whencookiesstorestateproblemscanarise¤ Tocheckforauthorization,serverretrievescurrentstate¤ Butcookiessentonallrequests,evenifitiscross-site

¨  Createsopportunityformischiefbyforgingrequest¤ Cross-SiteRequestForgery(CSRF)nameforthisattack

Page 12: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

PageonNotEvilHacker.com <img src= “facebook.com?

action=post& content=HertzStuff”>

Browser

CSRFExample

Page 13: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

CSRFBankExample

¨  Toidentifyuser,setssessioncookiewhenloggedin¤ Serveralwayscheckscookie,sohacknotpossible

¨  Oncecomplete,victimleavesWITHOUTloggingout¤ HTTPstateless,soserverassumesauthorizationvalid

¨  Eachtimevictimloadssite,attackersteals$500www.vic.com/transfer.php?to=250&amt=500 ¤ Sitesecuredonserver-side&attackerlacksaccess¤ Butvictimstillhasbrowsercookie,sorequestlooksvalid

¨  Justneedrequest,noclicksrequired¤ Willconnectiflinkissrcforimg,iframe,orscript

Page 14: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

GetandPost

¨  GETrequestssimpler,butforgingPOSTpossible¤ Usuallyneedsbuttonclick,butthatcanbedoneinJS

<body onload="document.forms[0].submit();"> <form action="http://vic.com/transfer.do" method="POST">

<input type="hidden" name="to" value="250"/> <input type="hidden" name="amt" value="500"/> <input type="submit" value="Dank Memes"/>

</form>

Page 15: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

Socialengineering

¨  Musttrickvictimintoloadingpage¨  Todothis,manytechniquesexist¨  BrowsernotneededifemailinHTML

¤ Aside:Weoftendisableimagesinemail

¨  Verylowchancethatschemeworks¤ Handlethisbysendingtomanypeople¤ Commonlyusedwebsitebestforthis¤  Iftargetsknown,lesserusedsiteokay

Page 16: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

CSRFCountermeasures

¨  referrer(optional)fieldinHTTPheaderstatessender¤ Field(hasto)exposehistory,soleak’sinfoaboutuser¤ Toprotectprivacy,somebrowsersneverincludefield

¨  HTTPanopenprotocol;couldwriteownprogram¤ Spoofreferrertomatchneedsratherthanreality

¨  Couldaddfieldspecifyingifrequestcross-siteornot

Page 17: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

CSRFCountermeasures

¨  referrer(optional)fieldinHTTPheaderstatessender¤ Field(hasto)exposehistory,soleak’sinfoaboutuser¤ Toprotectprivacy,somebrowsersneverincludefield

¨  HTTPanopenprotocol;couldwriteownprogram¤ Spoofreferrertomatchneedsratherthanreality

¨  Couldaddfieldspecifyingifrequestcross-siteornot

Page 18: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

CSRFCountermeasures

¨  Randomsecretvalue("SecretToken")oneachpage¨  Includesecretvaluewhenpageinformationsent

¤ Otherpageslackaccess,duetosameoriginpolicy:

ScriptscanonlyaccessDOMonpageswithsameorigin

¨  Couldalsousecookietostoresecretvalue¤ Whenrequestsubmitted,read&addcookietorequest¤ Sameoriginpolicypreventsattackingpagefromcookie

Page 19: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

CrossSiteScripting

Page 20: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

CrossSiteScripting(XSS)

¨  Attacksbyprovidingcoderatherthandata¤ Typeof“injection”attackthatisverycommononweb¤ Workswhenbrowserinterpretsasscriptinglanguage

¨  AnyinputchannelpotentialattackvectorforXSS¤ Attackcanoccurimmediatelyifreal-timeinputsused¤ Withpersistentstorage,delayeduntildataread&used

Page 21: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

CrossSiteScripting(XSS)

Page 22: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

CrossSiteScripting(XSS)

Page 23: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

WhoCares?

¨  CanprogramuseractionsbyembeddingJavascript¤ Browserwouldactasifactionsperformedbyuser¤ ComputershaveIQof0;donotknowwhatuserwanted

¨  UsingXSSmaliciousactorcan:¤ Stealcookies¤ Defacewebsites(fakenews;embarrassingimages)¤ Createwiretapofkeystrokes¤ Stealpersonalinformation¤ Runexploits(SAMYworm)

Page 24: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

DefenseAgainstXSS

¨  Replacewithencoding&haveHTMLrender:¤ <becomes&lt; ¤ >becomes&gt; ¤ &becomes&amp; ¤ ␠becomes&nbsp; ¤ ¶becomes<br/> Manyothersalsopossible

Filtering Encoding

¨  ForHTML,mustreject:¤ <script> ¤ <iframe> ¤  <div style=

“background:url( ‘javascript:alert(1)’)”>

¤  <IMG src= j&#X41vascript:alert(1)>

¤  eval( ‘xmlhttp.onread’+ ‘ydstatechang’+ ‘e=callback’);

Page 25: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

DefenseAgainstXSS

¨  Replacewithencoding&haveHTMLrender:¤ <becomes&lt; ¤ >becomes&gt; ¤ &becomes&amp; ¤ ␠becomes&nbsp; ¤ ¶becomes<br/> Manyothersalsopossible

Filtering Encoding

¨  ForHTML,mustreject:¤ <script> ¤ <iframe> ¤  <div style=

“background:url( ‘javascript:alert(1)’)”>

¤  <IMG src= j&#X41vascript:alert(1)>

¤  eval( ‘xmlhttp.onread’+ ‘ydstatechang’+ ‘e=callback’);

Page 26: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

Filtering&EncodingSupport

¨  Manylibrariescreatedtohelpdeveloperswithwork¤ JSoupexistsforJava(jsoup.org/)¤ AntiXSSusablein.Net(www.nuget.org/packages/AntiXSS)

¤ OWASPEnterpriseSecurityhasmulti-lingualsupport(www.owasp.org/index.php/Category:OWASP_Enterprise_Security_API#tab=Home)

¨  Canalsofindhelpinlanguagesprovidingfunctions¤ htmlspecialchars()definedforPHP¤ ValidateRequest()methoddefinedbyASP.net

Page 27: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

SQLInjectionAttacks

Page 28: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

SQLInjectionAttack

¨  LikeXSS,attacksbyprovidingcoderatherthandata¤ Muchmoredangerousbecauseitattacksdataservers¤ Damagegreater,too;allofserver'sdatacompromised

Page 29: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

SQLInjectionAttackKey

¨  Attacktakesadvantageofmostwebapps'structure¤ Browserattacksbycraftinginputsenttoappserver¤ Butthisattackisnotonappserver,butbyappserver¤ Databaseleftdefenselesssinceittrustsappserver

Browser

WebApplication

Server Database

Page 30: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

SampleDatabaseTable

UID NAMEGENDER AGE EMAIL PASSWD

1 Alice F 22 [email protected] az34Fn892 Bob M 30 [email protected] Ff4323sa93 Carol F 26 [email protected] Ra234d024 Douglas M 44 [email protected] 22as95asdF

TableName:USERS_TABLE

¨  Basicoperations:select,insert,update,&delete ¤ Nameis”cooler”CRUDforcreate,read,update,delete

Page 31: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

InsertStatement

UID NAMEGENDER AGE EMAIL PASSWD

1 Alice F 22 [email protected] az34Fn892 Bob M 30 [email protected] Ff4323sa93 Carol F 26 [email protected] Ra234d024 Douglas M 44 [email protected] 22as95asdF

TableName:USERS_TABLE

INSERT INTO USERS_TABLE VALUES (‘5’,‘Edgar’, ‘M’, ‘30’, ‘[email protected]’, ‘45adr56y’)

Page 32: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

UpdateStatement

UID NAMEGENDER AGE EMAIL PASSWD

1 Alice F 22 [email protected] az34Fn892 Bob M 30 [email protected] Ff4323sa93 Carol F 26 [email protected] Ra234d024 Douglas M 44 [email protected] 22as95asdF

TableName:USERS_TABLE

UPDATE USERS_TABLE SET EMAIL=‘[email protected]’ WHERE NAME=‘Alice’

UPDATE USERS_TABLE SET AGE=43

Page 33: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

DeleteStatement

UID NAMEGENDER AGE EMAIL PASSWD

1 Alice F 22 [email protected] az34Fn892 Bob M 30 [email protected] Ff4323sa93 Carol F 26 [email protected] Ra234d024 Douglas M 44 [email protected] 22as95asdF

TableName:USERS_TABLE

DELETE FROM USERS_TABLE WHERE NAME=‘Alice’

DELETE FROM USERS_TABLE WHERE Age < 25

DELETE FROM USERS_TABLE

Page 34: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

SQLCOMMENTS

¨  SQLsupportssingleandmultilinecomments¤ Startwith--forsinglelinecomments ¤ Addtextbetween/* */formultilinecomment

SELECT * FROM Customers -- WHERE City='Berlin'; /*Select all the columns of all the recordsin the Customers table:*/ SELECT * FROM Customers; SELECT CustomerName /*, City, Country*/ FROM Customers;

Page 35: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

WebAppFlowofData

Browser Web Application Server Database

<?php $sql = “SELECT id, name, salary FROM credential WHERE eid= ‘$eid’ AND password=‘$passwd’”; $result = $conn->query($sql); ?>

SELECT id, name, salary FROM credential WHERE eid= ‘9999’ AND passwd= ‘secret’;

Page 36: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

HackAttack!

Whatinput(s)

willretrievemorethanweshould?

<?php $sql = “SELECT id, name, salary FROM credential WHERE eid= ‘$eid’ AND password=‘$passwd’”; $result = $conn->query($sql); ?>

Page 37: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

SQLInjectionAttack(1)

¨  If$eidis:x’ OR 1=1 –-

¨  Querywouldbesenttodatabaseandexecutedas:

SELECT * FROM credential

WHERE eid = ‘x’ OR 1=1 -- ’

Page 38: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

MultipleSQLstatements

¨  Alsousesemicolon(;)tocreate1+statements¨  Makespossibleworsehacksthanjustretrieval

SELECT * FROM credential WHERE eid = ‘x’;

DELETE FROM credential -- ’

¨  Countermeasuresmayexistifscripttriggersaction

Page 39: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

MoreHackAttack!

Whatinput(s)

willgivesomeoneBIGraise?

<?php $sql = “UPDATE credential SET NickName = ‘$nname’, PhoneNumber=‘$phone’ WHERE eid= ‘$eid’; $result = $conn->query($sql); ?>

Page 40: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

SQLInjectionAttack(2)

¨  When$nnameis:A’, Salary=1000000, Email=‘

¨  Querybecomes: UPDATE credential

SET NickName = ‘A’, Salary=1000000,

Email=‘ ’, PhoneNumber=‘ ’ WHERE eid = ‘20000’

¨  CommentsnotalwaysneededforSQLinjection

Page 41: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

InputValidationtoBlock

¨  Couldvalidateinput(checkforspecialcharacters)&¤  Inputwithspecialcharactersrejected¤ Removespecialcharactersfromtheinput¤ Playitsafeandescapespecialcharacters

¨  ButrequiresknowingALLspecialcharacters¤ Mustbeupdatedasnewcharacterscreated

¨  Betterapproachisusinglibraryforthis¤  InPHP,havemysql_real_escape_string()¤ Stillcreatesgame–whatifMySQLupdatedfirst?

Page 42: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

UnderlyingCause

¨  Problemcomesfrommixingdata&codeinprogram¨  Userinput(data)providedtoparserforitswork¨  Shouldbedata,butaddedtostringexecutedascode

¤ Userinjectscodewhichisthenexecutedasnormal

¨  Bestsolution:alwaysseparatecode&data

Page 43: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

Don’tmakeitagame

PreparedStatementsalwaysbetter

thanfiltersorsanitizing

SQLInjectionKeyConcept

Page 44: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

PHPPreparedStatement

$stmt = $conn->prepare( “SELECT * FROM credential WHERE user = ? AND age = ? AND height = ?”); $stmt->bind_param(“sid”, $user, $age, $meters); $stmt->execute();

Page 45: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

PHPPreparedStatement

Step1:Sendcodethatwillbeexecuted

$stmt = $conn->prepare( “SELECT * FROM credential WHERE user = ? AND age = ? AND height = ?”); $stmt->bind_param(“sid”, $user, $age, $meters); $stmt->execute();

Page 46: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

PHPPreparedStatement

Step2:Senddatatofillinvariables

$stmt = $conn->prepare( “SELECT * FROM credential WHERE user = ? AND age = ? AND height = ?”); $stmt->bind_param(“sid”, $user, $age, $meters); $stmt->execute();

Page 47: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

PHPPreparedStatement

Step3:Profit

$stmt = $conn->prepare( “SELECT * FROM credential WHERE user = ? AND age = ? AND height = ?”); $stmt->bind_param(“sid”, $user, $age, $meters); $stmt->execute();

Page 48: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

BufferOverflows

Page 49: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

¨  FormusedbymajorityofInternetattacks¤ 50%ofCERTadvisoriesdealwithbufferoverflows¤ Veryquick&easywaytoinfectlotsofmachines

BufferOverflows

Page 50: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

¨  Morriswormoverflowedfingerd¤  Infected10%oftheexistingInternet

¨  CodeRedusedoverflowinMS-IISserver¤  Infected300,000machinesinabout14hours

¨  SQLSlammerhackedthroughMS-SQLserver¤ Neededjust10minutestoinfect75,000machines

MajorBufferOverflows

Page 51: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

¨  Bufferoverflowattacksareeasytostop¤ Javadoesnotallowthisexploittowork,infact¤ VerycommoninCcode

WhatCanWeDo?

Page 52: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

¨  Memorysetasideforquickaccessbyprogram¤ Usuallyfoundonprogramstackorintheheap¤ Pre-definedsizeusedtoimproveaccessspeed¤ Butwhathappensifmoredatastuffedintoit

WhatisaBuffer?

Page 53: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

¨  Shouldcheckforspacebeforestoringdata¤ Justcommonsensetostoreonlywherepermitted¤ Buffersnormallyhugesotheycanholdalldata¤ Butthere’scost:addedchecksslowprogramdown

¨  Languagessplitinhandlingofarrayaccess¤ AutomaticarrayboundschecksdonebyJava,C#¤ NotinmanyolderlanguageslikePascal&C++

HowtoHandleBuffer

Page 54: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

¨  Whencallingfunction,programcreatesframe¤ Valueofparametersstoredinthisframe¤ Containsspaceforallofthelocalvariables¤ Addresstoreturntowhenfunctioncompletes

¨  Sinceautomatic,systemassumesvaluesvalid¤ Programmercannotadjustorcontrolthisdata¤ Assoonascomplete,blindlyjumpstoreturnaddress

ProgramStack

Page 55: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

HighAddress Parameters i à 456 (4 bytes)

ReturnAddress 0xFEEDFACE (4 bytes)

CallingFP0xA0029482 (4 bytes)

LowAddress

LocalVariables x à 34 (4 bytes) y à 34 (4 bytes) buffer (100 bytes)

void foo(int i) { int x; int y; char buffer[100]; // Code here… }

StackframeExample

Page 56: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

¨  Whenweoverflowthebuffer…¤ Localvariablesoverwritteninitiallybythisextradata¤ Thencreatebrandnewaddressforframepointer¤ Assignreturnaddressnexttowhateverisininput

SmashingTheStack

Page 57: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

¨  Whenweoverflowthebuffer…¤ Localvariablesoverwritteninitiallybythisextradata¤ Thencreatebrandnewaddressforframepointer¤ Assignreturnaddressnexttowhateverisininput

¨  Oops.¤  Importantthatreturnaddressshouldbeaccurate¤ Randomvalueusedasresultofouroverflow¤ Codethatwillbeexecuteddecidedbywhom?

SmashingTheStack

Page 58: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

¨  Systemwillnormallycrashasresultofoverrun¤ Moreoftenthannot,datawillberandom¤ Rarelyproductivetojumptorandomaddress¤ Woo-hoo!Ourprogramisnotunsafe,itjustsucks.

UsuallyJustCrashes

Page 59: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

¨  Mustfirstwaitforhackertofindbug¤ Solongasnobodyusesprogramthisisnotaproblem¤ Onceprogramused,countonshortestwaitever

Neverask“Howcoulditbeworse?”

WhatisLeft?

Page 60: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

¨  Mustfirstwaitforhackertofindbug¤ Solongasnobodyusesprogramthisisnotaproblem¤ Onceprogramused,countonshortestwaitever

¨  Neverask“Howcoulditbeworse?”¤ SomelanguageshaveStringsasprimitivetype¤ Manyothersusenullterminatedarrayofchar ¤ Functionsprocessarrayuntilnullcharacterfound¤ Createsanentirelynewsourceofpossiblehacks

WhatisLeft?

Page 61: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

¨  Avoidfunctionsusingunlimitednumberofbytes¤ Canalwaysfind&uselimitedmemoryversions

¨  Restricttoactualsizetopreventoverflow¤ Requiresyoubeabletoknowarray’sactualsize¤ Updateeverywherewhenchangingcode¤ Usingmagicnumbersmakesverydifficult

PossibleSolutions

Page 62: LECTURE 23: MORE SECURITY - CSE442 · 2020. 10. 21. · Cross Site Scripting (XSS) ¨ Attacks by providing code rather than data ¤ Type of “injection” attack that is very common

¨  Commerciallibrariesinjectcheckswhereitcan¤ Asinmodernlanguages,butlesscapableoruseful¤ Checksaddtime:programruns2-3%slower

¨  Legacycodetoodifficulttofixcanuselibraries¤  Mostbosseswouldbeangryaddingtonewcode

OtherSolutions