<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by michbsd on Medium]]></title>
        <description><![CDATA[Stories by michbsd on Medium]]></description>
        <link>https://medium.com/@michbsd?source=rss-4078e74ab002------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*QUMabPzRON_EPPz_.jpeg</url>
            <title>Stories by michbsd on Medium</title>
            <link>https://medium.com/@michbsd?source=rss-4078e74ab002------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 13:12:59 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@michbsd/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Varnish Security Firewall (VSF) heatmap with the Elastic Stack]]></title>
            <link>https://medium.com/@michbsd/varnish-security-firewall-vsf-heatmap-with-the-elastic-stack-f890836a5131?source=rss-4078e74ab002------2</link>
            <guid isPermaLink="false">https://medium.com/p/f890836a5131</guid>
            <category><![CDATA[varnish]]></category>
            <category><![CDATA[vsf]]></category>
            <category><![CDATA[freebsd]]></category>
            <category><![CDATA[logstash]]></category>
            <category><![CDATA[devops]]></category>
            <dc:creator><![CDATA[michbsd]]></dc:creator>
            <pubDate>Wed, 26 Oct 2016 15:05:03 GMT</pubDate>
            <atom:updated>2016-10-27T09:30:17.333Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/640/0*eKLYMCAE3sxvN3dW.png" /></figure><p>I am using <a href="https://www.varnish-cache.org/">Varnish</a> reverse-proxy in order to increase overall performance to my website, by caching objects in memory and serving them amazingly fast; thus reducing requests (load) to my backend web-servers.</p><p>Recently I discovered <a href="https://github.com/comotion/VSF">VSF</a> (Varnish Security Firewall) which is basically a set of rules that is written in VCL (Varnish Configuration Language) that you include in your own VCL. These rules will make a bunch of checks (you can modify them, or add your own custom rules if needed) for a variety of things; such as file extensions, empty user-agent strings, cross site scripting and much more). If a malicious request is sent to your website, you can display an error and block the request.</p><p>With <a href="https://www.varnish-cache.org/">Varnish</a> — I am also using <em>varnishncsa</em> to log all requests, response times and so on to a log file on my server. I use a <a href="https://github.com/elastic/beats/tree/master/filebeat"><em>filebeat</em></a> (part of <a href="https://github.com/elastic/beats">beats</a>) from the good people at <a href="https://www.elastic.co/">Elastic</a> to ship in (near) real-time all entries, as they come, to logstash (and subsequently into elasticsearch).</p><p>This allows me to produce the nice dashboard pictured above with kibana. Since the dashboard in linked to the indexed log files, I am able to dynamically change the heatmap. I can make a rectangular selection on the map (geo coordinates) and the heatmap will show blocked requests for this region only. As seen on the screenshot below :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/0*-RQnCrpwLnNPw4o8.png" /></figure><p>So how does it all work?</p><p>I assume you have a working <a href="https://www.varnish-cache.org/">Varnish</a> instance, and a working logstash, elasticsearch and kibana environment running; if not, there are plenty of tutorials available that can help you. You should also install <a href="https://github.com/comotion/VSF">VSF</a> and <a href="https://github.com/elastic/beats/tree/master/filebeat">filebeat</a> by yourself.</p><p>I run <a href="https://www.freebsd.org/">FreeBSD</a> on my systems, you might choose to run something else.</p><p>Start by including the VSF rules before vcl_recv() in your default VCL.</p><p><strong>/usr/local/etc/varnish/default.vcl:</strong></p><p>[…]<br><em>include “/usr/local/etc/varnish/security/vsf.vcl”;<br>sub vcl_recv {</em><br>[…]</p><p>and reload your vcl:<br><em># varnishadm vcl.load vsftest default.vcl &amp;&amp; varnishadm vcl.use vsftest</em></p><p>if everything works, you should see:<br><em>VCL compiled.</em></p><p><em>VCL ‘vsftest’ now active</em></p><p>Next, make sure that the rule description is being logged in <em>varnishncsa</em>. On FreeBSD, the <em>varnishncsa</em> format is defined in /etc/rc.conf — so you need to add this the right place on your system:</p><p><strong>/etc/rc.conf:</strong><br><em>varnishd_enable=”YES”<br>varnishd_config=”/usr/local/etc/varnish/default.vcl”<br>varnishd_storage=”malloc,52G”<br>varnishd_admin=”:81&quot;<br>varnishncsa_enable=”YES”<br>varnishncsa_pidfile=”/var/run/varnishncsa.pid”<br>varnishncsa_file=”/var/log/varnishncsa.log”<br>varnishncsa_logformat=”%{X-Forwarded-For}i %u %t %m ‘%{Host}i’ ‘%U’ ‘%q’ %s %b ‘%{Referer}i’ ‘%{User-agent}i’ %{Varnish:time_firstbyte}x %{Varnish:handling}x ‘%{X-VSF-RuleName}i’”</em></p><p>You can find an overview of the different formatters for the logformat <a href="https://www.varnish-cache.org/docs/4.1/reference/varnishncsa.html">here</a>.<br>The one we are interested in right now is “%{X-VSF-RuleName}i” — which is set by VSF whenever a request is caught by a rule.<br>Here’s an example:</p><p><em>123.123.255.248 — [23/Sep/2016:00:26:36 +0200] PROPFIND ‘5.5.5.5’ ‘/webdav/’ ‘’ 403 279 ‘-’ ‘WEBDAV Client’ 0.000060 synth ‘-’ ‘Method Not Allowed’</em></p><p>We need to ship this to logstash. As mentioned earlier, I use <a href="https://github.com/elastic/beats/tree/master/filebeat">filebeat</a>:</p><p><strong>filebeat.yml</strong><br><em>filebeat:<br> prospectors:<br> -<br> paths:<br> — /var/log/varnishncsa.log<br> input_type: log<br> document_type: varnish</em></p><p><em>output.redis:<br> hosts: [“172.16.0.1”]<br> port: 6379<br> password: “xxxxmysecurestringxxx”<br> index: “filebeat”<br> db: 0<br> timeout: 5</em></p><p>I ship all my entries to a redis instance (broker) that my different logstash instances will poll continuously.</p><p>So far so good. We now have to configure logstash to match and manipulate the log entries before inserting them into elasticsearch</p><p>Here is the relevant section from my configuration.</p><p><strong>/usr/local/etc/logstash/logstash.conf</strong><br><em> if [type] == “varnish” {<br> grok {<br> patterns_dir =&gt; “/usr/local/etc/logstash/patterns”<br> match =&gt; [<br> “message”, “%{VARNISH}”<br> ]<br> named_captures_only =&gt; true<br> }<br> geoip {<br> source =&gt; “ip1”<br> target =&gt; “geoip”<br> database =&gt; “/usr/local/etc/logstash/GeoLiteCity.dat”<br> add_field =&gt; [ “[geoip][coordinates]”, “%{[geoip][longitude]}” ]<br> add_field =&gt; [ “[geoip][coordinates]”, “%{[geoip][latitude]}” ]<br> }<br> mutate {<br> convert =&gt; [ “[geoip][coordinates]”, “float”]<br> convert =&gt; [ “bytes”, “integer” ]<br> convert =&gt; [ “berespms”, “integer” ]<br> }<br> }</em></p><p>The <em>geoip</em> section, takes the ip address in the field “ip1” and makes a lookup in GeoLiteCity.dat — that will return geo coordinates into the field geoip.coordinates.</p><p>In the <em>mutate</em> section, I “typecast” certain fields — so I can make some calculations and aggregrations for my graphs. The default type for a field is a string, which is not so great if you want to calculate the value.</p><p>Here is my grok pattern for the Varnish log:</p><p><em>VARNISH %{IP:ip1} — \[%{HTTPDATE:timestamp}\] %{WORD:method} ‘%{NOTSPACE:host}’ ‘%{NOTSPACE:path}’ ‘(?:%{URIPARAM:param}|)’ %{NUMBER:http_status} (?:%{NUMBER:bytes}|-’) ‘(?:%{NOTSPACE:referrer}|-)’ %{QS:agent} %{BASE10NUM:berespms} %{WORD:cache_handling} (?:%{QS:vsfvuln}|-)</em></p><p>Once this is in place, and you have restarted / reloaded all the different new configurations, you should be able to see vsfvuln and the GeoIP information in the indexed data:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/0*0gC1NNDd73C6Ebw4.png" /></figure><p>I hope this was helpful, feel free to ask questions in the comment section below.</p><p><em>Thanks to all the people at Varnish Cache for providing this incredibly versatile and powerful software.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f890836a5131" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>