๐ด Cross-Site Scripting (XSS)
๐ง Description
What is XSS?
Cross-Site Scripting (XSS) is a client-side code injection attack. The attacker aims to execute malicious scripts in the victim's web browser by injecting malicious code into a trusted website. XSS occurs when web applications take untrusted data and render it without proper validation or encoding.
- Steal session cookies and hijack user sessions
- Deface websites or inject malicious content
- Redirect users to malicious websites
- Perform keylogging and form hijacking
- Access sensitive information in the DOM
- Deliver malware to users
XSS Types:
- Reflected XSS: Malicious script is part of the request sent to the server and is reflected back
- Stored XSS: Malicious script is permanently stored on the target server
- DOM-based XSS: Vulnerability exists in client-side code rather than server-side
- Blind XSS: XSS payload is stored and executed in a different part of the application
๐ท๏ธ Classification
- Vulnerability Type: Client-Side Injection (CWE-79)
- OWASP Category: A03:2021 - Injection
- CAPEC:
- CAPEC-63: Cross-Site Scripting
- CAPEC-209: Cross-Site Scripting using Flash
- CAPEC-242: Cross-Site Scripting in Error Messages
- XSS Subtypes: Reflected, Stored, DOM-based, Blind
๐ฏ Attack Surface
XSS can occur in various input points:
- โ Search Fields: Query parameters
- โ Comment Sections: Blog comments, forum posts
- โ User Profiles: Display names, bio fields
- โ URL Parameters: GET parameters reflected in page
- โ HTTP Headers: User-Agent, Referer
- โ File Names: Uploaded file names displayed
- โ Chat/Message Boards: User-submitted content
- โ Product Reviews: E-commerce reviews
โ ๏ธ Preconditions
- User Input: Application accepts user input and displays it without encoding
- No Sanitization: Input is not sanitized or validated
- No Output Encoding: Output is not properly encoded for HTML context
- JavaScript Context: Input can break out of JavaScript strings
๐ Detection
Manual Testing:
- Inject basic payload:
<script>alert(1)</script> - Test with image onerror:
<img src=x onerror=alert(1)> - Test SVG:
<svg onload=alert(1)> - Test event handlers:
<body onload=alert(1)>
Automated Tools:
- Burp Suite: Active/passive scanning
- OWASP ZAP: Spider and scan
- nuclei: XSS templates
- XSStrike: Advanced XSS detection
๐ง Burp Suite Workflow
- Browse target application normally
- Review Proxy history for reflection points
- Send reflected parameter to Repeater
- Test with basic XSS payloads
- Check if payload is reflected without encoding
- Escalate to exploit - steal cookies, perform actions
# In Repeater, test: GET /search?q= HTTP/1.1 Host: target.com # If response contains the script tag unencoded - VULNERABLE
โ๏ธ Tool Automation
๐ซ XSStrike
Advanced XSS detection and exploitation
๐ก๏ธ Burp Suite
Manual testing with Intruder
โก Nuclei
Template-based XSS scanning
๐ Dalfox
Go-based XSS scanner
# XSStrike python3 xsstrike.py -u "http://target.com/search?q=test" # nuclei nuclei -u "http://target.com" -t templates/xss.yaml # dalfox dalfox url "http://target.com/search?q=test"
๐ฃ Basic Payloads
๐ Advanced Payloads
๐ค AI-Generated Payloads
Context-aware payloads for specific scenarios:
๐จ Context-Aware Payloads
๐ Proof of Concept
Vulnerable Code Example:
// PHP - Vulnerable code $name = $_GET['name']; echo "Welcome, " . $name; // Attacker requests: // http://target.com/welcome.php?name= // Result: Script executes in user's browser, stealing cookies
Cookie Theft PoC:
// Payload to steal cookies // When victim visits, their cookies are sent to attacker
๐จ Request / Response
Attacker Request:
GET /search?q= HTTP/1.1 Host: target.com User-Agent: Mozilla/5.0
Server Response (Vulnerable):
HTTP/1.1 200 OK Content-Type: text/html Search results for: ...
๐ฅ Impact Analysis
Session Hijacking:
- Steal session cookies
- Session fixation attacks
- Access user accounts
- Keylogging user input
- Phishing via injected forms
- Credential harvesting
- Redirect to malicious sites
- Drive-by downloads
- Exploit kit delivery
โก Advanced Exploitation
1. Session Hijacking:
// Steal session // Session fixation
2. Keylogging:
// Record keystrokes
3. Form Hijacking:
// Modify form action
4. DOM Modification:
// Modify page content
๐ Attack Chains
โ Test Cases
| ID | Test Case | Payload | Expected |
|---|---|---|---|
| 1 | Basic Script Tag | <script>alert(1)</script> | Alert popup |
| 2 | Image Onerror | <img src=x onerror=alert(1)> | Alert popup |
| 3 | SVG Onload | <svg onload=alert(1)> | Alert popup |
| 4 | Event Handler | <body onload=alert(1)> | Alert popup |
| 5 | DOM-based | <img src=x onerror=eval(atob(...))> | Alert popup |
๐ก๏ธ Mitigation
โ Content Security Policy (CSP): Implement strict CSP headers to prevent inline script execution
โ Input Validation: Validate and sanitize all user inputs
โ HTTPOnly Cookies: Set HttpOnly flag on session cookies to prevent JavaScript access
๐ฐ Advanced Mitigation
1. Content Security Policy:
# Strict CSP Header Content-Security-Policy: default-src 'self'; script-src 'self'; object-src 'none'; frame-ancestors 'none' # Allow specific sources Content-Security-Policy: script-src 'self' https://trusted.cdn.com;
2. Output Encoding Libraries:
# Python - html.escape() import html safe_output = html.escape(user_input) # JavaScript - DOMPurify const clean = DOMPurify.sanitize(dirty);
3. Cookie Security:
# Set secure, HttpOnly, SameSite cookies Set-Cookie: session=abc123; Secure; HttpOnly; SameSite=Strict
๐ Monitoring & Detection
WAF Rules:
# Detect XSS patterns in requests SecRule REQUEST_URI "@rx (