📌
Software Security
  • README
  • Prerequisites
    • Prerequisites
  • Introduction
    • Cyber security principles
    • Basic web concepts
      • HTTP
      • JavaScript
      • Cookies
      • SQL
      • DOM
      • APIs and the multitier architecture
    • Basic browser security concepts
      • Same-Origin Policy (SOP)
      • Cross-Origin Resource Sharing (CORS)
      • Cookies
      • Tracking
    • Basic security concepts
      • Hashing
  • Access Control: Basics
    • Authentication
      • Passwords
      • Password managers
      • Attacking passwords - online
      • Attacking passwords - offline
    • Authorization
      • Insecure direct object references
    • Session Management
    • CSRF
      • CSRF: why & how it works
      • Protecting against CSRF attacks
    • SSRF
      • SSRF: how it works and how we can protect against it
  • Access Control: Advanced
    • Authentication
      • Federation
      • Alternative authentication mechanisms
      • FIDO2 and WebAuthn
  • Injection attacks
    • Injection attacks
    • SQL Injection
    • Command Injection
    • Cross-site scripting
      • Input validation
      • Context sensitive output encoding
      • About the HttpOnly flag
      • Content Security Policy
    • Subresource integrity
    • Sandboxing
  • HTTPS
    • HTTPS
    • Introduction to cryptography
    • PKI
    • Setting up HTTPS
    • References
  • HTTP Headers for security
    • HTTP Headers
  • Threat Modeling
    • Threat modeling introduction
    • Threat modeling basics
    • Inspiration for threats
  • Bringing it all together
    • A comprehensive overview of controls
Powered by GitBook
On this page
  • Cross-site scripting (XSS)
  • Impact of XSS
  • Types of XSS
  • Stored XSS (type I)
  • Reflected XSS (type II)
  • DOM-based XSS (type 0)
  • Attack scenario
  • Preventing XSS
  • Source attribution

Was this helpful?

  1. Injection attacks

Cross-site scripting

PreviousCommand InjectionNextInput validation

Last updated 2 years ago

Was this helpful?

Cross-site scripting (XSS)

Cross-Site Scripting (XSS) attacks are a type of injection attack, in which malicious scripts are injected into otherwise benign and trusted websites. XSS attacks are successful when these malicious scripts are then shown in victim users' browsers. Since the victim user’s browser has no way to know that the script should not be trusted, it will be executed. Moreover, because it runs on the same origin as the trusted application, the malicious script can access any cookies, session tokens, or other sensitive information retained by the browser and used with that site. These scripts can even rewrite the content of the HTML page.

The following video explains quite well how XSS works and how it was used to hack Twitter by .

Impact of XSS

Successful XSS attacks often lead to a serious compromise of your application. Attackers may steal cookies, they can initiate forged requests (leading to CSRF), they can manipulate the DOM, steal security tokens, etc. keeps a list of potentially useful XSS payloads.

Types of XSS

Stored XSS (type I)

Stored XSS generally occurs when user input is stored on the target server, such as in a database, in a message forum, visitor log, comment field, etc. A victim will then retrieve the stored data from the web application without that data being made safe to render in the browser. With the advent of HTML5 and other browser technologies, we can envision the attack payload being permanently stored in the victim’s browser, such as an HTML5 database, and never being sent to the server at all.

A typical example is a profile page, where people are able to enter their own personal information which is then stored in a database. Should a malicious user enter <script>alert('xss')</script> instead of Michael for example, this may lead to a stored XSS vulnerability unless precautions are taken.

Reflected XSS (type II)

Reflected XSS occurs when user input is immediately returned by a web application in an error message, search result, or any other response that includes some or all of the input provided by the user as part of the request, without that data being made safe to render in the browser, and without permanently storing the user provided data.

An example may be when an attacker tries to send the victim to the following page: https://www.example.org/home?display=<script>alert('xss')</script> . In case the application uses the data from the display querystring to construct the page, this may lead to an XSS vulnerability.

DOM-based XSS (type 0)

As defined by Amit Klein, who published the first article about this issue, DOM Based XSS is a form of XSS where the entire XSS flow takes place in the browser (the DOM). It is very similar to reflected XSS, but an important difference is that the injected script is never sent to the application's server.

For example, an attacker could inject script using a URL fragment instead of a query string: https://www.example.org/home#display=<script>alert('xss')</script>. In case other scripts in the browser use the display fragment to alter the page, this may lead to an XSS vulnerability.

Attack scenario

The following is a step-by-step description of a stored XSS attack:

  • Attacker injects script in an otherwise benevolent application;

  • Victim visits the benevolent application;

  • The attacker's script is sent to the victim's browser;

  • The attacker's script is executed in the victim's browser.

Preventing XSS

The recommendations to protect against XSS are the following:

  • validate user input (something which should not only be done to protect against XSS!)

  • deploy context-sensitive output encoding

  • deploy a strict content security policy (CSP)

These defenses will be discussed in the next pages.

Other measures for specific circumstances exist:

  • sanitize user input (in case you want to render pure HTML in your application)

  • sandboxing (in case you iframe other websites such as Youtube) => this won't be covered in this course

Source attribution

Some parts of this page are based on and , which are licensed under .

creating a self-retweeting tweet
This website
Cross Site Scripting (XSS)
Types of XSS
FLOSS