XSS Attacks Explained
Cross-Site Scripting for Beginners: Types, Examples & Complete Protection Guide 2026
What is Cross-Site Scripting (XSS)?
Cross-Site Scripting (XSS) is a code injection attack that allows attackers to execute malicious JavaScript in the browsers of other users. When a vulnerable web page renders user input without proper sanitization, attackers can inject scripts that steal session cookies, redirect users, or modify page content.
XSS ranks #3 in the OWASP Top 10 web application security risks. According to research, approximately 40% of all web applications have some form of XSS vulnerability. Attackers exploit these vulnerabilities to hijack user sessions, deface websites, or redirect users to malicious sites.
XSS Attack Flow
Why XSS Matters
A successful XSS attack can compromise user accounts, inject malware, or allow attackers to perform actions on behalf of users without their knowledge.
Three Types of XSS Attacks
Understanding the different XSS types helps you identify and prevent each variant.
Script is injected via URL parameters and reflected off the server without storage. Non-persistent.
- - URL-based attacks
- - Social engineering needed
- - Quick delivery via links
Malicious script is permanently stored on the target server. Most dangerous type.
- - Persistent attack
- - Affects all visitors
- - Database injection
Client-side JavaScript processes user input and dynamicall modifies the DOM.
- - Pure client-side
- - Server-side blind
- - Angular/React targets
Type Comparison Table
| Type | Persistence | Severity | Detection |
|---|---|---|---|
| Reflected | Non-persistent | Medium | Easy - URL inspection |
| Stored | Permanent | Critical | Harder - requires code review |
| DOM-based | Client-side | Medium-High | Requires JS analysis |
Real XSS Attack Examples
Understanding how XSS works with practical examples.
1. Cookie Stealing
When executed, this script sends the victim's cookies to the attacker's server, allowing session hijacking.
2. Keylogger Injection
Every keystroke is sent to the attacker's server, capturing passwords, credit card numbers, and sensitive data.
3. Phishing via Fake Forms
Creates a fake login form that captures credentials and sends them to the attacker.
Impact of XSS Attacks
Steal session cookies to take over user accounts without knowing passwords
Capture usernames and passwords through fake login forms or keyloggers
Inject drive-by-download scripts that install malware on visitors computers
Modify visible content to display misleading information or propaganda
Create fake overlays that look legitimate to steal sensitive information
Perform unauthorized actions on behalf of logged-in users
How to Find XSS Vulnerabilities
Manual Testing
Test every input field and URL parameter with XSS payloads:
"><script>alert('XSS')</script>
<img src=x onerror=alert('XSS')>
<svg onload=alert('XSS')>
' onmouseover=alert('XSS') x='
Automated Tools
Burp Suite
Professional web security scanner with active and passive scanning capabilities
OWASP ZAP
Free open-source scanner with comprehensive XSS detection
XSStrike
Specialized XSS scanner with fuzzing and DOM analysis
Nuclei
Fast template-based vulnerability scanner with XSS templates
XSS Prevention Techniques
Validate all user input on server-side. Reject or sanitize potentially dangerous characters.
Encode output based on context (HTML, JavaScript, URL, CSS) to prevent script execution.
Implement CSP headers to control which scripts can execute on your pages.
Mark session cookies with HttpOnly flag to prevent JavaScript access.
Leverage React, Angular, or Vue built-in sanitization features.
Conduct penetration testing and code reviews to find vulnerabilities early.
Example: Safe Output Encoding in JavaScript
element.textContent = userInput;
// For HTML context, encode special characters
function encodeHTML(str) {
return str.replace(/[&<>"']/g, c => {
'&': '&', '<': '<', '>': '>',
'"': '"', ''': ''' }[c]);
}
XSS in Modern Web Applications
Modern frameworks have built-in protections, but vulnerabilities still exist.
Risk: DangerouslySetInnerHTML bypasses protection
Risk: Bypass with bypassSecurityTrust methods
Risk: v-html directive can introduce XSS
Bug Bounty Tips for XSS
Where to look and what to test when hunting for XSS.
Learn Web Application Security at Cyber Defence
Master XSS, SQL injection, and other web vulnerabilities in our hands-on ethical hacking course. Learn attack techniques and defense strategies from certified professionals.
