XSS
most reliable method of detecting XSS vulnerabilities is manual code review, which should cover both back-end and front-end code.
If we understand precisely how our input is being handled all the way until it reaches the web browser, we can write a custom payload that should work with high confidence.
XSS vulnerabilities are mainly linked to two parts of the web application: A Source like a user input field and a Sink that displays the input data. These are the main two points that we should focus on securing, both in the front-end and in the back-end.
The most important aspect of preventing XSS vulnerabilities is proper input sanitization and validation on both the front and back end. In addition to that, other security measures can be taken to help prevent XSS attacks.
Input sanitization https://github.com/cure53/DOMPurify n addition to input validation, we should always ensure that we do not allow any input with JavaScript code in it, by escaping any special characters. For this, we can utilize the DOMPurify JavaScript library, as follows:
// DOMPurify
<script type="text/javascript" src="dist/purify.min.js"></script>
let clean = DOMPurify.sanitize( dirty );
This will escape any special characters with a backslash \, which should help ensure that a user does not send any input with special characters (like JavaScript code), which should prevent vulnerabilities like DOM XSS.
When it comes to input sanitization, then the back-end plays a vital role, as front-end input sanitization can be easily bypassed by sending custom GET or POST requests. Luckily, there are very strong libraries for various back-end languages that can properly sanitize any user input, such that we ensure that no injection can occur.
For a NodeJS back-end, we can also use the DOMPurify library as we did with the front-end, as follows:
// DOMPurify
import DOMPurify from 'dompurify';
var clean = DOMPurify.sanitize(dirty);
Another important aspect to pay attention to in the back-end is Output Encoding. This means that we have to encode any special characters into their HTML codes, which is helpful if we need to display the entire user input without introducing an XSS vulnerability. For a PHP back-end, we can use the htmlspecialchars or the htmlentities functions, which would encode certain special characters into their HTML codes (e.g. < into <), so the browser will display them correctly, but they will not cause any injection of any sort:
Using HTTPS across the entire domain.
Using XSS prevention headers.
Using the appropriate Content-Type for the page, like
X-Content-Type-Options=nosniff.Using
Content-Security-Policyoptions, likescript-src 'self', which only allows locally hosted scripts.Using the
HttpOnlyandSecurecookie flags to prevent JavaScript from reading cookies and only transport them over HTTPS.In addition to the above, having a good
WAFcan significantly reduce the chances of XSS exploitation,

Xenotix is a XSS tester by OWASP
The True-Client-IP header is similar to the X-Forwarded-For header, both tell the server or proxy what the IP of the client is. Due to there being no sanitation in the header we are able to perform an XSS attack.
True-Client-IP: <iframe src="javascript:alert(`xss`)">

Note that we create a JS that loads a non existant page on our Kali box and we start Netcat listener on port 80 : nc -nlvp 80
XSS inside a INPUT HTML Tag
in title
aspen.eccouncil.org/BTC/BTCChallenges/LaunchExam/Beginner
Session highjacking via XSS One of the most common method used to mount a session hijacking attack is to exploit an XSS vulnerability in the web app.
Can perform the attack when all of the following conditions are met:
XSS Vulnerability exists and you can execute your own payload through it Session ID is sent through cookies in each HTTP request (this was an assumption) Cookies are readable by JS
Using the following script we will be able to steal the users cookies. Once we collect them, we just need to change our current cookies, refresh our browser and navigate the web app with victim session
Session Fixation Not a vulnerability per say but can turn into Session Fixation if: The session identifier remains the same after a successfully reserved operation ( for example a login) The session identifier can be propogated (for example via URL or Javascript)
Session fixation is possible if the attacker is also a member of the vulnerable website
Most common protection mechanism against CSRF exploit is the token.
The token is a nonce (a number used once and discarded) and makes part of the request required to perform a given action unpredictable for an attacker
Note; the token becomes useless when the app is also vulnerable to XSS when in Var
DOM Based xss

Last updated