I'm trying to bypass some XSS filtering. Every time I insert a single quote I get a backslash, so I wrote this payload:


In the source code it looks like this:

a = '\';alert(1);\''

but I still can't get an alert box. What is wrong?

  • 1
    You can just pass alert(1);// . Also, i think more detailis needed on how this input is being executed in code – Sravan Aug 18 '16 at 8:58
  • it dosn't work .. – Xozu Aug 18 '16 at 9:08
  • 1
    Whats the js output for that? – Sravan Aug 18 '16 at 9:14
  • i dont get anything i just dont get the alert box – Xozu Aug 18 '16 at 10:58
  • To be clear: you're saying that the generated script (inside the HTML script element) is the same whether your payload includes the backslashes or not, right? Also, I'm guessing not, but just in case: are you able to include an un-escaped </script> in your payload? – CBHacking Dec 4 '19 at 22:29

It is impossible to know for sure without seeing the code that the server runs, but we can make some educated guesses:

  • Looks like there is a template looking something like this:

    a = '{escape(input)}';
  • You say ' gets replaced by \'.

  • From your example, it seems that \' being unaffected is an exception to the above rule (probably to prevent the very attack you were trying). This is a bit surprising - the normal thing to do would be to replace the \ with \\, so you would get \\\'.
  • There might be multiple other defences built in that you just haven't noticed yet because you haven't managed to bypass the first.

It is important to understand the context you want to escape from. In this case it is a JavaScript string literal (or Rule #3 in the OWASP cheat sheet). To get out of it, you have two options:

  1. Switch to a general JavaScript context. I think this can only be done by getting an unescaped ' in there. If \' is always unaffected no matter context, try \\'. If you are lucky, nothing is done about the first backslash. While you are at it, try different numbers of backslashes, just in case.

  2. End the script block to get into an HTML context. Try something like this:

    </script><script> alert(1); </script><script>

    Most likely the < will be escaped to &lt;, but it is worth a try.

See also this related question.

| improve this answer | |
  • First method :

To bypass this filter you need to comment the rest of your request at the end by adding //, However, in some XSS challenge they add '\' before '/' so you cannot bypass the filter using this method ("http://sudo.co.il/xss/level5-2.php?p=test") :


To get the following :

<SCRIPT>var a="\\";alert(1);//";</SCRIPT>"

But sadly it didn't work for me and I got the following output :

a = '\\';alert(1);\/\/'
  • Second Method :

If the first method didn't work, you need to think about another solution that worked for me by adding the following to the link :


Using the above code you can bypass the filter :

a = '\\';alert(1);<!--'
| improve this answer | |

Bypass escaped double quote

Based on your description this should work:


The " will be escaped as \", thus resulting in \", which escapes the \, but not the ".

To prevent this, you would at the very least have to also escape \ as \.

Bypass escaped double quote and double backslash to single backslash

Based on your comment, I'm assuming the actual relevant parts of the filtering work like this:

" -> \"
\\ -> \

This isn't secure either. An injection might look like this:


" will be escaped as \" in step 1 which leads us to \\", which is then transformed to \" in step 2. You can't use double quotes in the injected string, but that's not a problem as XSS with single quotes or completely without quotes is perfectly possibly.

To secure this the double backslash would need to be escaped, not transformed to a single one, which would give us \\\\\" with the above described injection, which is safe.

| improve this answer | |
  • i got you but still it's not working i inserted this payload:'\\'-alert(1)-\'' as you can see i closed the strings but still don't get any alert :( – Xozu Aug 18 '16 at 11:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.