{"id":644,"date":"2021-09-02T17:22:12","date_gmt":"2021-09-02T09:22:12","guid":{"rendered":"https:\/\/play.datalude.com\/blog\/?p=644"},"modified":"2021-09-02T17:27:28","modified_gmt":"2021-09-02T09:27:28","slug":"using-swatchdog-for-monitoring-new-firewall-logs","status":"publish","type":"post","link":"https:\/\/play.datalude.com\/blog\/2021\/09\/using-swatchdog-for-monitoring-new-firewall-logs\/","title":{"rendered":"Using swatchdog for monitoring new firewall logs"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Always interested in trying out a new tool, so I decided to use this when I wanted to monitor a new rule I put in my firewall, to see what it was doing. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The rule was an ipset rule to match against a bunch of addresses and blacklist them. I dumped out a copy of the ruleset using <strong>iptables-save &gt; rules.txt<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The rule in question was<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">-A INPUT -p tcp -m set --match-set blacklist-script src -j DROP<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">So I added a line above it to log any matches with the tag \"BlacklistDrop:\", which would allow me to identify the matches in my syslog<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">-A INPUT -p tcp -m set --match-set blacklist-script src -j LOG --log-prefix \"BlacklistDrop: \"\n-A INPUT -p tcp -m set --match-set blacklist-script src -j DROP\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After editing the file you can make it active by using<strong> iptables-restore rules-edited.txt<\/strong> . So far so good. I didn't want to make the changes permanent so I left it like that. If iptables is restarted, it will forget the new rule. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Next, time to tell swatchdog what to do. In ~\/.swatchdogrc I set up the following. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">watchfor \/BlacklistDrop:\/\n\texec \/root\/scripts\/blacklistlogger.sh \"$_\"\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">So it will watch syslog (the default setting), and when it finds a line in there matching our tag, it will pass it to the blacklistlogger.sh script as a commandline argument. \"$_\" means pass the whole line. In this case, a whole line will look like this:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Sep 2 16:39:39 server kernel: [543547.097604] BlacklistDrop: IN=eth0 OUT= MAC=00:50:56:42:fc:3a:28:99:3a:4d:23:91:08:00 SRC=45.134.26.57 DST=62.171.162.39 LEN=40 TOS=0x00 PREC=0x00 TTL=247 ID=41722 PROTO=TCP SPT=50248 DPT=27741 WINDOW=1024 RES=0x00 SYN URGP=0<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now we just need the script, which you'll need to make executable. Its just a one liner with some sed magic in, to trim the line into the relevant parts. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#!\/bin\/bash\n# In use by swatchdog\n# ps -auxf | grep swatchdog\n\necho \"$1\" | sed -r 's\/.*SRC=([0-9.]+).*DPT=([0-9]+).*\/\\1 \\2\/' &gt;&gt; \/root\/scripts\/blacklist.log\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Lets digest that sed command I only wanted to log the source IP address and what port its attacking. So in the example line above, that would be SRC=45.134.26.57 and DPT=27741<br>sed matches the expression between the first two \/\/ marks on the line. Its looking for <br>any character followed by an exact match of \"SRC=\" + <br>(one or more digits and periods ) + <br>any character followed by an exact match of \"DPT=\" +  <br>(one or more digits)<br>sed will replace that with the expression between the second and third \/\/ marks. In this case its \\1 \\2, which refer to the bracketed terms in the original match &#8230; our two number terms. <br><br>So its fed a long log line, and it spits out the two numbers we're interested in, logging them to blacklist.log. All that remains to do is to start swatchdog with the &#8211;daemon switch so it carries on logging after we log out, and we can come back in the morning to see what we've got. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There don't seem to be a lot of examples of swatchdog usage around. I'd be interested to see more if anyone feels like sharing. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Always interested in trying out a new tool, so I decided to use this when I wanted to monitor a new rule I put in my firewall, to see what it was doing. The rule was an ipset rule to match against a bunch of addresses and blacklist them. I dumped out a copy of &#8230; <a title=\"Using swatchdog for monitoring new firewall logs\" class=\"read-more\" href=\"https:\/\/play.datalude.com\/blog\/2021\/09\/using-swatchdog-for-monitoring-new-firewall-logs\/\" aria-label=\"Read more about Using swatchdog for monitoring new firewall logs\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-644","post","type-post","status-publish","format-standard","hentry","category-it"],"_links":{"self":[{"href":"https:\/\/play.datalude.com\/blog\/wp-json\/wp\/v2\/posts\/644","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/play.datalude.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/play.datalude.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/play.datalude.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/play.datalude.com\/blog\/wp-json\/wp\/v2\/comments?post=644"}],"version-history":[{"count":0,"href":"https:\/\/play.datalude.com\/blog\/wp-json\/wp\/v2\/posts\/644\/revisions"}],"wp:attachment":[{"href":"https:\/\/play.datalude.com\/blog\/wp-json\/wp\/v2\/media?parent=644"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/play.datalude.com\/blog\/wp-json\/wp\/v2\/categories?post=644"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/play.datalude.com\/blog\/wp-json\/wp\/v2\/tags?post=644"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}