Web Application Penetration Testing Tutorial For Beginners

What is Cross-Site Scripting (XSS) Attack & Vulnerability? Meaning, Types, Methodology, Countermeasures

What is Cross Site Scripting (XSS) Attack?

Cross-site Scripting is a kind of injection where hackers write and execute malicious scripts into websites. This can be done through browsers. The injections become successful when the vulnerabilities in the website or web app take the input without any validation and show the output. 

Using XSS attacks, the hackers can send the malicious code to other end users of the source and gain access to cookies, session tokens, as well as other confidential details in the browser.

Types of Cross Site Scripting (XSS) Attacks

Three different types of cross-site scripting attacks are there:

1. Stored XSS

Also called Persistent or Type I attack, the Stored XSS is caused when the inputs by the users are stored on the server. 

2. Reflected XSS

Reflected XSS, also known as Non-persistent or Type II, is caused when the web app or website shows the errors to user input instantly. 

3. DOM-Based XSS

DOM-based XSS or Type 0 injections occur when the source of data, sink, as well as the data flow, all take place in the DOM. 

Test your knowledge with a quick quiz!

What are two primary types of XSS vulnerabilities?

Select the correct answer

XSS Methodology

For launching an XSS attack, the hackers execute a malicious code into the input entered by the user. This can also be done by making changes to the request. In case the web app or site is found vulnerable to cross-site scripting attacks, the user input will be injected as a code. 

There are multiple methods to trigger cross-site scripting attacks. For instance, it can be triggered when the page is loading or when the user is browsing and moving over the elements on a web page. 

Once the attack is launched, the hackers can capture the keystrokes of the users, redirect them to a malicious site, run exploits based on browsers, access the cookie information of the users who are logged into the site, etc. 

In the worst cases, the hackers can gain access to the accounts of the victims and steal credentials.

XSS Attack Countermeasures

The level of effort required to prevent XSS attacks depends on the complexity of the app and the attack. However, the following are some countermeasures for the prevention of cross-site scripting attacks. 

  • When the site or app receives user input, it should be filtered and validated.

  • When the data controlled by users is output in HTTP responses, the output should be encoded.

  • Implement HTML, URL, JavaScript, and CSS encoding on the basis of the context of the output. 

  • Use relevant response headers for prevention of such attacks in HTTP responses which are not supposed to include HTML or JS.

  • Have a content security policy in place to minimize the severity of cross-site scripting flaws.



Test your knowledge with a quick quiz!

The exploit where the hacker enters malicious code into links that look like from a trusted source?

Select the correct answer

Did you find this article helpful?