Lec07 XSS Clickjacking 1.5m
Lec07 XSS Clickjacking 1.5m
http://wikipedia.org:8080/editor
Same Origin Policy
A B DOM
A
A
DOM
B
Same Origin Policy
https://bank.com
evil.com
bank.com
Same Origin Policy
bank.com
Victim
bank.com
Attacker
Inject into HTML elements
• HTML template:
<p>Search result for USER_DATA_HERE</p>
• Injected code
<script>MALICIOUS_CODE</script>
• Resulting page:
<p>Search result for
<script> MALICIOUS_CODE </script></p>
• How to fix?
• Change all < to <
• Change all & to &
• Resulting page (escaping)
<p>Search result for
<script> MALICIOUS_CODE</script></p>
Inject into HTML attributes
• HTML template:
<img src='avatar.png' alt='USER_DATA_HERE'/>
• Injected code:
Evil' onload='MALICIOUS_CODE
• Resulting page (no escaping):
<img src='avatar.png'
alt= 'Evil' onload='MALICIOUS_CODE' />
• How to fix?
• Change all ‘ to ' and “ to "
• Change all & to &
• Resulting page (escaping)
<img src='avatar.png'
alt= 'Evil' onload='MALICIOUS_CODE' />
Inject into HTML attributes
• HTML template:
<div onmouseover='handleHover(USER_DATA_HERE)'>
• User input: ); MALICIOUS_CODE
• Resulting page :
<div onmouseover='handleHover(); MALICIOUS_CODE’>
• Escaping just ' and " is not enough here!
Inject into <div>
• HTML template:
<div id='USER_DATA_HERE'>Some text</div>
• Injected code :
username’>Some text </div>
<script>
if (typeof username !== 'undefined')
MALICIOUS_CODE
}
</script>
Insert into script elements
• HTML template:
<script>
let username = ‘USER_DATA_HERE’
alert(‘Hi there, ${username}’)
</script>
• Injected code:
Evil’; MALICIOUS_CODE; //
• Resulting page
<script>
let username = ‘Evil’; MALICIOUS_CODE; //’ alert(`Hi
there, ${username}`)
</script>
• How to fix?
• Change all ‘ to ' and “ to "
• Change all & to &
Insert into script elements
• Injected code:
</script><script> MALICIOUS_CODE </script><script>
• Resulting page
<script>
let username = '</script><script>
MALICIOUS_CODE </script><script>’
alert(`Hi there, ${username}`)
</script>
• How to fix?
• Change all < to <
• Change all & to &
Insert into script elements
• How to fix?
<template id=‘username’>
HTML_ENCODED_USER_INPUT</template>
<script>
let content = document.getElementById('username').content
let username = content.textContent
alert(`Hi there, ${username}`)
</script>
Contexts which are never safe
<div NEVER_PUT_UNTRUSTED_DATA_HERE></div>
<body onload!#$%&()*~+-
_.,:;?@[/|\]^`=alert(document.cookie)>
<img """><script>alert(document.cookie)</script>">
<iframe
src=https://attacker.com/path/to/some/file/xss.js <
2. XSS Defense
XSS Defense
• NodeJS
response.setHeader("Content-Security-Policy", "<directive>
<value>; <directive> <value>; … ")
<directive>
Content-Security-Policy:
script-src 'strict-dynamic' 'nonce-RANDOM_HERE'
* 'unsafe-inline';
Navigate to soict.hust.edu.vn.
Notice the URL when hovering
over the image.
Clickjacking
Load soic.hust.edu.vn in an
iframe
GET / HTTP/1.1
Host: bank.com
Client
attacker.com HTTP/1.1 200 OK
X-Frame-Options:…
<!DOCTYPE html>
Server
bank.com
Embed allow?
X-Frame-Options:sameorigin