XSS - How to prevent this Monster !!

There are two main things when it comes to preventing XSS !!

  • Input Validation
  • Output Sanitization

Let's discuss each in detail:

Input Validation is simply validating every user-controllable input coming to the system and rejecting it if it doesn't meet certain rules (Golden Rule to prevent attacks !!)

So it works at the outer boundary of the system

In the case of XSS, it translates to :

  • Http Query parameters
  • HTTP header Values
  • Anything that gets executed on the web page with user-controllable input (e:g placeholders for form actions, CSS style sheets, href attributes, <script> tags )

So don't allow anything that can result in executing JavaScript and rendering HTML on the web page that is user-controllable.

On the other hand, Output Sanitization is sanitizing the content that is displayed back to the user on the web page

Let's understand it with an example and dive into the PortsSwigger lab

Landing Page

Let's put our hacking hats on !! And hack in the search feature

Try payload: ORION"> BREAKOUT in the search box and hit enter. The same strategy we have been using in Part 1, Part 2 and Part 3 of the article series

After we get the results and page back, see carefully what gets rendered on the developer tools

> sign is HTML esacped

Can we see what has just happened?

ORION"> BREAKOUT payload gets translated to ORION" &gt; breakout

So we're not able to break out of value="" tag because > sign is replaced with &gt;

This is what is called as HTML escaping or HTML encoding and this is one of the ways we can achieve output sanitization

So even if an attacker sends crafted/malicious input into the system, it is sanitized when it is reflected back to the user on the web page.

As a result, for browsers, this is not standard HTML markup and it can't execute that but it is treated as uninterpreted literals

Below are some of the characters and their corresponding character references :

& : @amp;

< : &lt;

> : &gt;

In this particular case , > is treated as &gt;

All standard languages provide HTML escaping functions

But where do we use this HTML escaping?

The answer is: any string that is coming from untrusted data and is inserted into an HTML document

That's all for this article !!

Now, you must be thinking if output sanitization is so powerful, why are hackers still able to attack applications using XSS? (Hint: nothing is 100% full proof)

Atul Joshi

Helping Software Techies And IT Companies to Upskill In Application Security | Secure Code Mentor | Software Engineer At Heart | eJPT2, eWPT, CCNA, CAP Certified

11 个月

Is there any way we can have a system that does input validation automatically for us? Without even knowing us it is being done !! Any guess what is this system called operating at application level ?

回复

要查看或添加评论,请登录

Atul Joshi的更多文章

  • Reading This Month

    Reading This Month

    Reading this book for this month !! It is not an easy read as it talks about the threats in CyberSecurity in a detailed…

  • Security Is Next Six Sigma

    Security Is Next Six Sigma

    I was talking to one of my neighbours who is in the Software Industry for the last 9/10 years. He is an expert React…

  • God Always Have a Better Plan For us !!

    God Always Have a Better Plan For us !!

    I am writing in continuation with Part 1 and Part 2 of the series. In Part 2, we had come to a point where I decided to…

  • My Journey into Cyber Security Field - Part 2

    My Journey into Cyber Security Field - Part 2

    Continuing from where we left in the last article My Journey into Cyber Security Field - Part 1, I enrolled into an…

  • My Journey into Cyber Security Field - Part 1

    My Journey into Cyber Security Field - Part 1

    First of all , I initially thought why would anybody interested in my Journey into Cyber Security Field ? Will that be…

    2 条评论
  • Lets Hack SameSite : Strict

    Lets Hack SameSite : Strict

    This is Day 17 of #cybertechdave100daysofcyberchallenge In the last article Let's Hack SameSite: Lax, we have seen how…

  • Lets Hack SameSite : Lax

    Lets Hack SameSite : Lax

    This is Day 16 of #cybertechdave100daysofcyberchallenge In the last article Same Site Cookies, we have seen that What…

  • Another Gem - Is AI The Future Of Penetration Testing?

    Another Gem - Is AI The Future Of Penetration Testing?

    This is Day 15 of #cybertechdave100daysofcyberchallenge I happen to hit this amazing discussion - Is AI The Future Of…

  • Same Site Cookies

    Same Site Cookies

    In the CSRF article series, we saw what is CSRF (Lets go Cross Site - CSRF) and we attacked an application using CSRF…

  • Penetration Testing Phases

    Penetration Testing Phases

    This is Day 13 of #cybertechdave100daysofcyberchallenge In the last article Pen Testing - Detailed Overview, we have…

社区洞察

其他会员也浏览了