{"id":14,"date":"2011-04-15T21:32:00","date_gmt":"2011-04-15T21:32:00","guid":{"rendered":"http:\/\/www.somethingsomethingsecurity.com\/?p=14"},"modified":"2011-04-15T21:32:00","modified_gmt":"2011-04-15T21:32:00","slug":"active-defense-for-web-server-part-4","status":"publish","type":"post","link":"http:\/\/www.somethingsomethingsecurity.com\/?p=14","title":{"rendered":"Active Defense for web server &#8211; Part 4"},"content":{"rendered":"<p>The following is based on my experiences and (limited) knowledge. I am not an expert in anything, nor will I likely ever be one. My hope is that this might help someone, somewhere, sometime. If nothing else, it might be a good start for discussion.<\/p>\n<p><strong>Preamble<\/strong><br \/>This series is for meant for educational use only, and is intended to be used in a lab environment. These have not been tested in the real world and may cause more problems then they help. It will by no means be an in depth discussion.<\/p>\n<p><strong>Background<\/strong><br \/>Today, a very quick introduction to logparser and how we can use it\u00a0to actively defend against offensive attacks. <\/p>\n<p><strong>Process<\/strong><br \/>Logparser is a very versatile tool from Microsoft. It can chew it&#8217;s way through all sorts of data, from text files to event logs, very quickly.<\/p>\n<p>The very wise Professor Windows explains some of the background and usage at <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/bb878032.aspx\">http:\/\/technet.microsoft.com\/en-us\/library\/bb878032.aspx<\/a>.<\/p>\n<p>For this series, we are going to strictly focus on the reading of the IIS logs we setup in part 2. <\/p>\n<p>The general concepts for this series came about from reading <a href=\"http:\/\/www.symantec.com\/connect\/articles\/forensic-log-parsing-microsofts-logparser\">http:\/\/www.symantec.com\/connect\/articles\/forensic-log-parsing-microsofts-logparser<\/a>, and some of the topics covering blue team tactics on pauldotcom.com.<\/p>\n<p>Once you have downloaded and installed the latest version of logparser from Microsoft onto your webserver, do a basic Nikto scan of your test website to generate some entries in\u00a0the logs.<\/p>\n<p>If your using Bactrack simply open a shell and type <br \/>\u00a0\u00a0<span> \/pentest\/scanners\/nikto\/nikto.pl -host http:\/\/yourip\/<\/span><\/p>\n<p>Once that is done, let&#8217;s use loparser to check the logs on the webserver using the following command<br \/>C:\\Program Files\\Log Parser 2.2><span>logparser -o:xml &#8220;SELECT c-ip AS ClientIP, cs(user-agent) AS ClientUserAgent, cs-uri-stem as URIStem, cs-uri-query as URIQuery,COUNT(*) AS [Requests] INTO attacker.xml FROM <span>C:\\WINDOWS\\system32\\LogFiles\\wherever your logs<\/span> are WHERE cs-uri-stem LIKE &#8216;%cmd.exe%&#8217;\u00a0OR cs-uri-stem LIKE &#8216;%&#8230;%&#8217; OR cs-uri-stem LIKE &#8216;%\\%&#8217; OR cs-uri-query LIKE &#8216;%\\%3c%&#8217; <\/span><span>OR cs-uri-query LIKE &#8216;%\\%3e%&#8217; OR cs-uri-query LIKE &#8216;%\\&#8221;%&#8217; OR cs-uri-query LIKE &#8216;%\\&#8217;%&#8217; OR cs-uri-query LIKE &#8216;%#%&#8217; OR cs-uri-query LIKE &#8216;%\\%&#8217; GROUP BY ClientIp, ClientUserAgent, URIStem, URIQuery ORDER BY Requests DESC&#8221;<\/span><br \/>\u00a0 <br \/>\u00a0 <br \/><strong>\u00a0Explanation<\/strong><br \/>\u00a0 -o specifies the output type<br \/>\u00a0\u00a0SELECT is saying which log entries we are interested in. Refer back to part 2 for more details on the logging.<br \/>\u00a0\u00a0AS says we are going to treat each line as a whole<br \/>\u00a0\u00a0WHERE is now going trough each of the fields and looking for matches. For example, cs-uri-stem LIKE &#8216;%cmd.exe%&#8217; is looking in the cs-uri-stem field in each log entry for\u00a0\u00a0 the word cmd.exe in the request.<br \/><span>NOTE: I highly encourage you to look into other types of attack data that can be gleaned from the logs and add searches for them in your logparser query. There are lot&#8217;s of good examples in the Symantec\/Security Focus\u00a0article above, or in the hacker sample script included with the logparser install<\/span>.<br \/>\u00a0 GROUP BY is used to select what we will write in our output<br \/>\u00a0 ORDER BY is used to sort the output. In this case it will be the same order as the logs.<\/p>\n<p>Since we didn&#8217;t specify a location for the output of the file the xml will be in the logparser directory. Open it up and look through the results.<\/p>\n<p>You should see lots of entries like<br \/>\u00a0&#8211; <ROW><br \/>\u00a0 <ClientIP>192.168.100.129<\/ClientIP> <br \/>\u00a0 <ClientUserAgent>Mozilla\/4.75+(Nikto\/2.1.3)+(Evasions:None)+(Test:003295)<\/ClientUserAgent> <br \/>\u00a0 <URIStem>\/cgi-bin\/..%5c..%5c..%5cwinnt\/system32\/cmd.exe<\/URIStem> <br \/>\u00a0 <URIQuery>\/c+dir<\/URIQuery> <br \/>\u00a0 <Requests>1<\/Requests> <br \/>\u00a0 <\/ROW><br \/>&#8211; <ROW><br \/>\u00a0 <ClientIP>192.168.100.129<\/ClientIP> <br \/>\u00a0 <ClientUserAgent>Mozilla\/4.75+(Nikto\/2.1.3)+(Evasions:None)+(Test:003296)<\/ClientUserAgent> <br \/>\u00a0 <URIStem>\/iisadmpwd\/..%5c..%5cwinnt\/system32\/cmd.exe<\/URIStem> <br \/>\u00a0 <URIQuery>\/c+dir<\/URIQuery> <br \/>\u00a0 <Requests>1<\/Requests> <br \/>\u00a0 <\/ROW><\/p>\n<p>So that we know how to get some data out of the log files, what can we do with it?<\/p>\n<p>Well, if we\u00a0create a batch file<br \/>c:\\scripts\\parse.bat<br \/><span>C:\\Program Files\\Log Parser 2.2\\logparserlogparser -i:iisw3c -o:csv -headers off &#8220;SELECT DISTINCT c-ip INTO c:\\block\\badguys.txt FROM %1 WHERE TO_LOWERCASE(cs-uri-stem) LIKE &#8216;%%cmd.exe&#8217; OR TO_LOWERCASE(cs-uri-stem) LIKE &#8216;%%.exe%%&#8217; OR TO_LOWERCASE(cs-uri-stem) = &#8216;%%select&#8217; OR cs-uri-stem LIKE &#8216;%%&#038;%%&#8217; OR cs-uri-stem LIKE &#8216;select&#8217; OR cs-uri-stem LIKE &#8216;%&#8217; OR cs-uri-query LIKE &#8216;%%\\%%3c%%'&#8221;<\/span><br \/><strong>Explanation<\/strong><br \/>\u00a0-i the input is going to be iis logs, -o the output will be csv turn off headers, select only distinct ips. <\/p>\n<p>And\u00a0run it and provide a\u00a0logfile as an argument<br \/><span>c:\\scripts\\parse.bat c:\\windows\\system32\\logfiles\\<\/span><span>specificlogfile<\/span><br \/>A file will be created\u00a0with the\u00a0attackers\u00a0IP\u00a0in it. <br \/>Then if we run\u00a0a script like this one<br \/><span>c:\\scripts\\simplewaf.bat<\/span><br \/><span>@echo <\/span><br \/><span>cd c:\\block<\/span><br \/><span>for \/f %%i in (badguys.txt) do netsh ipsec static add filter filterlist=&#8221;Banned IPs&#8221; srcaddr=%%i dstaddr=yourip description=&#8221;Banned IPs&#8221; protocol=any srcport=0 dstport=0&#8243; <\/span><\/p>\n<p><strong>Explanation<\/strong><br \/>\u00a0We are doing a simple for loop that will iterate through each &#8220;line&#8221; in the file presented in argument. For each\u00a0IP it finds, it is adding the\u00a0IP\u00a0to the list of IPs in the IPSec policy that was created in part 3.<\/p>\n<p>So, assuming that the\u00a0IP of your attacker is different then the one you were using for testing in the previous post, or if you removed it, the IP that you ran the nikto scan from should be added to the IPSec rule and the attacker can no longer connect from that IP.<\/p>\n<p>We can do something similar on a Windows 2008 server<br \/>c:\\scripts\\simplewaf.bat<br \/><span>for \/f %i in (c:\\block\\badguys.txt) do netsh advfirewall firewall add rule name=&#8221;Bad IP %i&#8221; protocol=TCP dir=in localport=80 remoteip=%i action=block ENABLE<\/span><br \/><strong>Explanation<\/strong><br \/>\u00a0This one, I&#8217;m not very happy with. It is creating a new firewall rule for each IP instead of just adding IPs to an existing rule. If I find a better way to do this in the future,\u00a0\u00a0I will edit this post.<\/p>\n<p><strong>Next Steps<\/strong><br \/>By scheduling the parse.bat and the simplewaf.bat to run daily\/hourly or even\u00a0more frequently you could have a very simple web application firewall that blocks attacking IPs, but you can\u00a0\u00a0probably see we could run into some major problems. We will save that for next time&#8230;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The following is based on my experiences and (limited) knowledge. I am not an expert in anything, nor will I likely ever be one. My hope is that this might help someone, somewhere, sometime. If nothing else, it might be a good start for discussion. PreambleThis series is for meant for educational use only, and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,9],"tags":[],"class_list":["post-14","post","type-post","status-publish","format-standard","hentry","category-blue","category-iis"],"_links":{"self":[{"href":"http:\/\/www.somethingsomethingsecurity.com\/index.php?rest_route=\/wp\/v2\/posts\/14","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.somethingsomethingsecurity.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.somethingsomethingsecurity.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.somethingsomethingsecurity.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.somethingsomethingsecurity.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=14"}],"version-history":[{"count":0,"href":"http:\/\/www.somethingsomethingsecurity.com\/index.php?rest_route=\/wp\/v2\/posts\/14\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.somethingsomethingsecurity.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=14"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.somethingsomethingsecurity.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=14"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.somethingsomethingsecurity.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=14"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}