Javascript & Obfuscation
Strings
You can escape any character not in an escape sequence
Use a backslash to continue a line
Template strings (Backtick) support multiple lines
Template strings allow executing JavaScript expressions in placeholders.
Tagged template strings
Obfuscation
Hexadecimal
Hexadecimal encoding works only within strings. If you attempt to use it as an identifier, it will fail.
Prefix: \x
Unicode
Unicode escapes also work in strings but are also allowed in identifiers, but you cannot encode parentheses or other characters.
First form: \u
(you must specify four hexadecimal characters)
Second form: \u{}
Unlike standard unicode escapes you are not restricted tofour hexadecimal characters.
Octal
Can only be used strings. Using a number outside the octal range returns the number itself in JavaScript.
Prefix: only \
Eval and escapes
Since eval()
operates on strings, it attempts to decode the input provided to it. As a result, when the JavaScript is executed, the engine processes the decoded string. This behavior allows us to bypass some of the previously defined rules.
With unicode you can do the same and you can also double encode backslash
When using eval()
and can mix and match the encodings
Javascript eval() + atob()
atob()
decode a base-64 encoded string.
This can be useful to bypass char/string blocked.
eval() - DOM XSS
Consider
eval('var searchResultsObj = ' + this.responseText);
If you can manipulate the
this.responseText
string you can execute an alert.(The response is taken with ajax)
If the response is
{"results":[],"searchTerm":"XSS"}
and you are able to changeXSS
keyword into\"-alert(1)}//
the result will be{"results":[],"searchTerm":"\\"-alert(1)}//"}
and an alert will appear
Note:
Notice that JSON automatically escape the double quote
"
(standard feature of javascript string) so we need to use\"
We add
//
to comment the restThis specific example with JSON works because the site didn't use
JSON.parse(this.responseText)
This specific example is a case of Reflected DOM XSS
replace()
The replace()
method returns a new string with matches of a pattern replaced by a replacement, which can be a string or a function. The pattern can be a string or RegExp.
If pattern is a string, only the first occurrence will be replaced. The original string is left unchanged.
You can easy bypass this with <><img src=1 onerror=alert(1)>
document.location
In JavaScript, the location
variable (document.location
) represents the URL of the current document. Assigning a value to it redirects the page to that URL.
Javascript in innerHTML
HTML specifies that a <script>
tag inserted with innerHTML should not execute
In this case you can use const name = "<img src='x' onerror='alert(1)'>";
Javascript in href attribute
Possible values:
An absolute URL - points to another web site.
href="http://www.example.com/default.htm"
A relative URL - points to a file within a web site.
href="default.htm"
Link to an element with a specified id within the page.
href="#section2"
Other protocols (like
https://
,ftp://
,mailto://
,file://
, etc..)A script.
href="javascript:alert('Hello');"
Last updated