Reflected XSS into a JavaScript string with angle brackets HTML encoded
Description
This lab contains a reflected cross-site scripting vulnerability in the search query tracking functionality where angle brackets are encoded. The reflection occurs inside a JavaScript string. To solve this lab, perform a cross-site scripting attack that breaks out of the JavaScript string and calls the alert function.
Approach
After accessing the lab, I intercepted the search request:
In the response, I noticed my input was embedded within a script tag:
<script> var searchTerms ='ichyaboy'; document.write('<img src="/resources/images/tracker.gif?searchTerms='+encodeURIComponent(searchTerms)+'">');</script>
To exploit this, I crafted a payload to break out of the JavaScript context, inject my malicious script, and comment out the rest to prevent errors. Here’s the payload:
';alert(1)//
This payload works as follows:
'; closes the existing string.
alert(1) injects a script that triggers an alert.
// comments out the remainder of the JavaScript code to avoid syntax errors.
After injecting this payload, an alert box popped up, confirming the XSS vulnerability and solving the lab.