XSS
CS 161 Spring 2025 - Lecture 15
Computer Science 161
Last Time: Cookies
2
Computer Science 161
Last Time: Session Authentication
3
Computer Science 161
Last Time: CSRF
4
Computer Science 161
Today: XSS
5
Computer Science 161
Cross-Site Scripting (XSS)
6
Textbook Chapter 22
Computer Science 161
Top 25 Most Dangerous Software Weaknesses (2020)
7
Rank | ID | Name | Score |
[1] | Improper Neutralization of Input During Web Page Generation (’Cross-site Scripting’) | 46.82 | |
[2] | Out-of-bounds Write | 46.17 | |
[3] | Improper Input Validation | 33.47 | |
[4] | Out-of-bounds Read | 26.50 | |
[5] | Improper Restriction of Operations within the Bounds of a Memory Buffer | 23.73 | |
[6] | Improper Neutralization of Special Elements used in an SQL Command (’SQL Injection’) | 20.69 | |
[7] | Exposure of Sensitive Information to an Unauthorized Actor | 19.16 | |
[8] | Use After Free | 18.87 | |
[9] | Cross-Site Request Forgery (CSRF) | 17.29 | |
[10] | Improper Neutralization of Special Elements used in an OS Command (’OS Command Injection’) | 16.44 | |
[11] | Integer Overflow or Wraparound | 15.81 | |
[12] | Improper Limitation of a Pathname to a Restricted Directory (’Path Traversal’) | 13.67 | |
[13] | NULL Pointer Dereference | 8.35 | |
[14] | Improper Authentication | 8.17 | |
[15] | Unrestricted Upload of File with Dangerous Type | 7.38 | |
[16] | Incorrect Permission Assignment for Critical Resource | 6.95 | |
[17] | Improper Control of Generation of Code (’Code Injection’) | 6.53 |
Computer Science 161
Review: Same-Origin Policy
8
Computer Science 161
Review: JavaScript
9
Computer Science 161
Review: JavaScript
10
<script>alert("Happy Birthday!")</script>
HTML (with embedded JavaScript)
Webpage
Happy Birthday!
OK
When the browser loads this HTML, it will run the embedded JavaScript and cause a pop-up to appear.
Computer Science 161
A Go HTTP Handler
11
func handleSayHello(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query()["name"][0]
fmt.Fprintf(w, "<html><body>Hello %s!</body></html>", name)
}
<html><body>Hello EvanBot!</body></html>
https://vulnerable.com/hello?name=EvanBot
Handler
URL
Response
Computer Science 161
A Go HTTP Handler
12
func handleSayHello(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query()["name"][0]
fmt.Fprintf(w, "<html><body>Hello %s!</body></html>", name)
}
https://vulnerable.com/hello?name=<b>EvanBot</b>
<html><body>Hello <b>EvanBot</b>!</body></html>
Handler
URL
Response
Computer Science 161
A Go HTTP Handler
13
func handleSayHello(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query()["name"][0]
fmt.Fprintf(w, "<html><body>Hello %s!</body></html>", name)
}
https://vulnerable.com/hello?name=<script>alert(1)</script>
<html><body>Hello <script>alert(1)</script>!</body></html>
Handler
URL
Response
Problem: This input represents control data (HTML), not just text!
Computer Science 161
A Go HTTP Handler
14
func handleSayHello(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query()["name"][0]
content := "<html><body>Hello "+name+"!</body></html>"
fmt.Fprint(w, content)
}
https://vulnerable.com/hello?name=<script>alert(1)</script>
<html><body>Hello <script>alert(1)</script>!</body></html>
Handler
URL
Response
Not just %s: It can happen with any string manipulation
Computer Science 161
Cross-Site Scripting (XSS)
15
Computer Science 161
Stored XSS
16
Computer Science 161
Stored XSS
17
bank.com
Attacker
Victim
1. Inject malicious script
2. Request content
3. Receive malicious script
4. Victim browser executes malicious script
5a. Steal valuable data (e.g. session token)
5b. Make malicious requests
Computer Science 161
Reflected XSS
18
Computer Science 161
Reflected XSS
19
bank.com
Attacker
Victim
2. Request URL under attacker’s control
3. Reflect malicious script
5a. Steal valuable data (e.g. session token)
5b. Make malicious requests
4. Victim browser executes malicious script
1. Cause malicious request (e.g. click on link)
Computer Science 161
Reflected XSS: Making a Request
20
Computer Science 161
Reflected XSS is not CSRF
21
Computer Science 161
XSS in the Wild… On CalNet?!
22
Computer Science 161
XSS in the Wild… On CalNet?!
23
Garbage execution value in HTML
Computer Science 161
Constructing an Attack on CalNet
<html>� <head>� <script>� // When the malicious page finishes loading, automatically submit the form!� document.addEventListener('DOMContentLoaded', () => {� document.getElementById('form').submit();� });� </script>� </head>� <body>� <!-- Malicious form containing our malicious execution data. -->� <form id="form" action="https://auth.berkeley.edu/cas/login" method="POST">� <input name="username" type="text" value="evanbot" />� <input name="password" type="text" value="obviously-not-the-real-password" />� <input name="execution" type="text" value="<script>alert('XSS!')</script>" />� </form>� </body>�</html>
24
Attack: Force a POST request to CalNet!
Computer Science 161
So What Happened?
25
Computer Science 161
XSS Defenses
26
<html>
<body>
Hello <script>alert(1)</script>!
</body>
</html>
Computer Science 161
XSS Defenses: Escaping
27
func handleSayHello(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query()["name"][0]
fmt.Fprintf(w, "<html><body>Hello %s!</body></html>", html.EscapeString(name))
}
https://vulnerable.com/hello?name=<script>alert(1)</script>
<html><body>Hello <script>alert(1)</script>!</body></html>
Handler
URL
Response
Computer Science 161
XSS Defenses: Escaping
28
<html>
<body>
Hello {{.name}}!
</body>
</html>
Example: Golang HTML template
Computer Science 161
XSS Defenses: CSP
29
Computer Science 161
UI Attacks
30
Textbook Chapter 23
Computer Science 161
User Interface (UI) Attacks
31
Computer Science 161
Clickjacking
32
Computer Science 161
Clickjacking: Download buttons
33
Computer Science 161
Clickjacking
34
Navigate to berkeley.edu. Notice the URL when hovering over the image.
Computer Science 161
Clickjacking
35
<iframe style="opacity: 1.0" src="https://www.berkeley.edu/"></iframe>
Load berkeley.edu in an iframe
We can’t generate clicks ourselves because of SOP, but the user can still click…
Computer Science 161
Clickjacking
36
<iframe style="opacity: 1.0" src="https://www.berkeley.edu/"></iframe>
<p style="margin-top: 210pt"><em>You <b>Know</b> You Want To Click Here!</em></p>
Place some enticing content underneath
Computer Science 161
Clickjacking
37
<iframe style="opacity: 0.8" src="https://www.berkeley.edu/"></iframe>
<p style="margin-top: 210pt"><em>You <b>Know</b> You Want To Click Here!</em></p>
Make the iframe slightly transparent…
Computer Science 161
Clickjacking
38
<iframe style="opacity: 0.1" src="https://www.berkeley.edu/"></iframe>
<p style="margin-top: 210pt"><em>You <b>Know</b> You Want To Click Here!</em></p>
Make it more transparent
Computer Science 161
Clickjacking
39
<iframe style="opacity: 0" src="https://www.berkeley.edu/"></iframe>
<p style="margin-top: 210pt"><em>You <b>Know</b> You Want To Click Here!</em></p>
Make it entirely transparent
But the user still clicks on the iframe!
Computer Science 161
Clickjacking: Invisible iframes
40
Computer Science 161
Clickjacking: Invisible iframes
41
Download .exe
Computer Science 161
Clickjacking: Invisible iframes
42
$0.15
$0.15
Computer Science 161
Clickjacking: Temporal Attack
43
Computer Science 161
Clickjacking: Temporal Attack
44
Instructions:
Please double-click on the button below to continue to your content
Click here
Computer Science 161
Clickjacking: Temporal Attack
45
Instructions:
Please double-click on the button below to continue to your content
Click here
Computer Science 161
Clickjacking: Cursorjacking
46
Fake cursor, created with CSS and/or JavaScript
Real cursor, hidden or less visible with CSS
Computer Science 161
Clickjacking: Cursorjacking
47
Download .exe
PLAY NOW!
What do you think you’re clicking on?
Computer Science 161
Clickjacking: Defenses
48
Computer Science 161
Clickjacking: Defenses
49
Computer Science 161
Clickjacking: Defenses
50
Computer Science 161
Phishing
51
Computer Science 161
Phishing
52
Computer Science 161
Phishing
53
Computer Science 161
Phishing
54
evanbot@berkeley.edu
Computer Science 161
Phishing
55
Computer Science 161
Phishing
56
Evan
Bot
2019-08-21
hive12
cs.berkeley.edu
Caltopia
01100101011101100110000101101110
Computer Science 161
Phishing
57
Computer Science 161
Phishing
58
Computer Science 161
Phishing
59
Computer Science 161
Phishing
60
Computer Science 161
Phishing: Check the URL?
61
www.pnc.com/webapp/unsec/homepage.var.cn is actually an entire domain!
The attacker can still register an HTTPS certificate for the perfectly valid domain
Is this real?
Computer Science 161
Phishing: Check the URL?
62
Is this real?
These letters come from the Cyrillic alphabet, not the Latin alphabet! They’re rendered the same but have completely different bytes!
Computer Science 161
Phishing: Homograph Attacks
63
Computer Science 161
Phishing: Check Everything
64
Is this real?
Extended Validation: Certificate authority verified the identity of the site (not just the domain)
Computer Science 161
Phishing: Check Everything
65
Oops, never mind
Computer Science 161
Phishing: Browser-in-browser Attacks
66
Computer Science 161
Phishing: Don’t Blame the Users
67
Computer Science 161
Two-Factor Authentication
68
Computer Science 161
Two-Factor Authentication
69
Computer Science 161
Subverting 2FA: Relay Attacks
70
Computer Science 161
Subverting 2FA: Relay Attacks
71
“Welcome to Google.�Please login”
Victim
Attacker
“User: victim�Password: password123”
“User: victim�Password: password123”
“Your 2FA code is 382924”
Attacker
“Enter the security code.”
“382924”
“382924”
Computer Science 161
Subverting 2FA: Social Engineering
72
Computer Science 161
2FA Example: Authentication Tokens
73
Computer Science 161
2FA Example: Security Keys
74
Computer Science 161
Summary: XSS
75
Computer Science 161
Summary: XSS Defenses
76
Computer Science 161
Summary: Clickjacking
77
Computer Science 161
Summary: Phishing
78
Computer Science 161