How To Prevent XSS Attacks?

One of the largest portals was in news recently when their website was exploited by targeting an XSS vulnerability. This was after the application audit for the website was concluded. This attack showed that injecting XSS is possible using the “Custom XSS attack vector” method.

In this paper, I will be explaining two major aspects of Cross-Site Scripting Attack:

  • Tricky XSS
  • Complete control over User’s browser – BeEF


Cross-Site Scripting (XSS) is an attack in which an attacker exploits a vulnerability in application code and runs his own JavaScript code on the victim’s browser. The impact of an XSS attack is only limited to the potency of the attacker’s JavaScript code.
A quick look into the types of XSS

  • Stored XSS Attacks
  • Reflected XSS Attacks
  • DOM Based XSS

Stored XSS – Stored XSS are the ones where the injected code is permanently stored on the target servers, such as in a database, message forum, visitor log, comment field, etc. The victim retrieves the malicious script from the server when it requests the stored information.

Reflected XSS – Reflected XSS are the ones where the injected code is reflected off the web server, such as 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. Reflected XSS is delivered to victims via another route, such as in an e-mail message, or on some other web server. When a user is tricked into clicking on a malicious link or submitting a specially crafted form, the injected code travels to the vulnerable web server, which reflects the attack back to the user’s browser. The browser then executes the code because it came from a “trusted” server.

DOM-based XSS – DOM Based XSS is an XSS attack wherein the attack payload is executed as a result of modifying the DOM “environment” in the victim’s browser used by the original client-side script so that the client-side code runs in an “unexpected” manner. That is, the page itself (the HTTP response that is) does not change, but the client-side code contained in the page executes differently due to the malicious modifications that have occurred in the DOM environment.

 

XSS attack – more patience, more possibility of an attack

Regular XSS attack strings: –

It has been noticed that XSS is more dependent on our observations of how inputs get stored or get reflected on the web page. In some cases, I have observed that the developer uses user input data in some client-side JavaScript functions. This is where the trap is laid since the developer will only sanitize the USER FACING area (i.e. form), and will not take care of JavaScript functions.

This can be explained by the following examples:-

Custom Attack Vector – I
It has been observed that user input values are being used in client-side javascript functions at the clients’ machine. Generally, developers focus more on the GUI part, hence they will use the best encoding techniques to encode values that are on the GUI but will forget about the function in which input values are being used in plain text.

Style Attribute
It has been observed that a STYLE attribute is ignored by some of the developers. They block all the miscellaneous events like on click, on mouseover, etc. STYLE attribute XSS has a limitation that it gets executed only in IE but that does not mean that it can be ignored.
There are many more victim websites that are available on the internet. It’s just a matter of expertise and you can spot the vulnerabilities. For Reflected XSS, how your input gets reflected on the entire HTML page is a matter of concern.

After successfully exploiting XSS in web applications, let’s see how an attacker can take full control of the victim’s browser using BeEF.

“The Browser Exploitation Framework (BeEF) is a powerful professional security tool. BeEF is a pioneering technique that provides an experienced penetration tester with practical client-side attack vectors. Unlike other security frameworks, BeEF focuses on leveraging browser vulnerabilities to assess the security posture of a target. BeEF hooks one or more web browsers as beachheads for the launching of directed command modules. Each browser is likely to be within a different security context, and each context may provide a set of unique attack vectors.” – http://beefproject.com/

Disclaimer: In this entire example section I have used http://demo.testfire.net as a victim site. This website is handled by a security vendor that contains a number of vulnerabilities by intention only.

BeEF framework code is available athttp://code.google.com/p/beef/downloads/list.Anyone can download and install BeEF, which requires a web server, PHP, and ruby installation as pre-requisites. Backtrack (BT) has an inbuilt setup of BeEF in it. BT users can use it without any installation. In this article, I have used BT to demonstrate.

Let’s start \by narrating a simple XSS example. Below is the search filed to pass the simple XSS vector,

Now, let’s start the attacker machines. And let’s initialize the BeEF on the machine. Below is the screenshot for the BeEF login page.

After logging in into the BeEF framework, below is the first look or we can say a home page of the initialized BeEF.

At the victim side, earlier we have seen the normal XSS on the demo site. Let’s apply the BeEF attack payload on the site.

Simply apply this script code in the search box of the application. The IP address is life, IP address of a machine on which BeEF is configured. Hook.js will hook a browser into BeEF.

There are many other ways that can be possible to force a victim user to click on this payload, for example by sending one image which has a malicious link behind it, or by clickjacking attack, or by email, etc.

When victims execute BeEF payload, they won’t see any changes on their side, but at the attacker’s side, they can see one ZOMBIE created.

Now the entire browser is in the attacker’s hand. He can check the system information for the created information.

An attacker can execute JAVASCRIPTS from their end to the victim’s browser. In the below screenshot attacker is sending the javascript alert box request.

The alert box gets populated at the victim’s end.

BeEF can also detect the social networking site status on the browser. Detect Social networks module will detect if the victim’s browser is authenticated to Gmail, Facebook, or Twitter.

Facebook has been opened in the victim’s browser.

BeEF plugin has detected that Facebook is initiated in the victim’s browser.

BeEF can also be useful to capture the keystrokes of the victim’s browser. One test page has been opened at the victim’s browser which has one textbox field. “Secret password” has been typed as the textbox value.

At the BeEF framework, event Logs will have an entry that on victim browser user typed “secret password” in one textbox.

This was all about some tricky and advanced stuff of XSS. Sometimes one wonders what if attackers hook cyber café’s or libraries or publicly available computers into their BeEF!! All those who access these machines will suffer a lot. One should try their level best to prevent XSS by applying well encoding techniques, also do not allow insertion of any HTML tag or attribute in editable input option (i.e. textbox, list box, text area) and hidden variables. XSS attack may harm a lot, it all depends on how bad the development skills of the application developer is and how good attackers skills are.

Venkatesh Sundar

Venky is an Application Security technologist who built the new age Web application Scanner and Cloud WAF - AppTrana at Indusface as a Founding CTO. Currently, he spends his time on driving Product Roadmap, Customer Success, Growth, and technology adoption for US businesses.

This post was last modified on January 11, 2024 14:52

Share
Venkatesh Sundar
Published by
Venkatesh Sundar

Recent Posts

Managed WAF: A Must-Have to Stop Website Attacks

A Managed WAF is a comprehensive cybersecurity service offered by specialized providers to oversee, optimize,… Read More

4 days ago

Top 10 Best Practices for Attack Surface Reduction

Explore crucial tactics like Asset Inventory, Patch Management, Access Control & Authentication, and additional best… Read More

2 weeks ago

10 Important Data Privacy Questions You Should be Asking Now

Delve into the data privacy questions including consent protocols, data minimization strategies, user rights management,… Read More

2 weeks ago