{"id":313,"date":"2015-11-11T21:56:04","date_gmt":"2015-11-11T21:56:04","guid":{"rendered":"http:\/\/tuxlabs.com\/?p=313"},"modified":"2015-11-11T21:57:13","modified_gmt":"2015-11-11T21:57:13","slug":"preventing-bind9-dns-naughty-ness-named-conf-iptablesufw-on-ubuntu","status":"publish","type":"post","link":"https:\/\/tuxlabs.com\/?p=313","title":{"rendered":"Preventing (bind9) DNS Naughty-ness (named.conf &#038; iptables\/ufw) on Ubuntu"},"content":{"rendered":"<p>If you run a DNS server on the Internet with a default configuration many people\/robots will take advantage of you. The same is true for Mail, but that is another article. Needless to say if you are running a service on the Internet, the naughty goblins will find you. To thwart these dirty criminals all that&#8217;s necessary is to configure your named.conf properly. However, since these robotos are being naughty there is a high degree of certainty they are infected endpoints, and as such I really don&#8217;t want them coming anywhere near me or my machines. After all for humanity sake we don&#8217;t want to be infected by the deadly plague ! This article is short and sweet, here is how to protect your DNS server &amp; your server in one article using named.conf &amp; ufw (iptables).<\/p>\n<p>&nbsp;<\/p>\n<h2>Named.conf.options<\/h2>\n<p>Now a days named.conf is really just a file that inherits 3 other files, named.conf.local, named.conf.options, and named.conf.default-zones. The one we are going to fix is named.conf.options. The configuration below should only be applied in a scenario where you want to run an authorative nameserver, and a caching name server, but the key is you only want to allow people to query the cache that &#8216;you know personally or are you&#8217; vs. allowing the entire internet, because then bad things happen. If this is not the setup you are going for, don&#8217;t do this \ud83d\ude42 But if it is follow along.<\/p>\n<p>Add the following section with the proper IP&#8217;s to the top fo the file<\/p>\n<pre class=\"lang:default decode:true \">acl \"trusted\" {\r\n192.241.206.98;\r\nlocalhost;\r\nlocalnets;\r\n};<\/pre>\n<p>Note you can also add a CIDR for a subnet\u00a0like 192.168.0.0\/16<\/p>\n<p>After that&#8217;s done under the options {} section&#8230; make it look like this<\/p>\n<pre class=\"lang:default decode:true \">        allow-query { any; };\r\n        allow-recursion { trusted; };\r\n        allow-query-cache { trusted; };\r\n        allow-transfer { 202.157.182.142; };<\/pre>\n<p>Note, allow transfer is necessary if you have a secondary nameserver that needs to receive updates. Now restart bind9<\/p>\n<pre class=\"lang:default decode:true \">tuxninja@tlprod1:\/etc\/bind$ sudo service bind9 restart<\/pre>\n<p>Ok now <strong>all querying<\/strong> including\u00a0behavior from non-trusted people will not be allowed. If it is working check your \/var\/log\/syslog and you will see some denies like this<\/p>\n<pre class=\"lang:default decode:true\">Nov 11 16:00:31 tlprod1 named[952]: client 192.163.221.224#80 (hehehey.ru): query (cache) 'hehehey.ru\/ANY\/IN' denied\r\nNov 11 16:00:31 tlprod1 named[952]: client 192.163.221.224#80 (hehehey.ru): query (cache) 'hehehey.ru\/ANY\/IN' denied\r\nNov 11 16:00:31 tlprod1 named[952]: client 104.37.29.110#4761 (hehehey.ru): query (cache) 'hehehey.ru\/ANY\/IN' denied<\/pre>\n<p>Now the above is from my actual log file. I was quite annoyed that clients are basically abusing the hell out of hehehey.ru&#8230; so I decided <strong>I don&#8217;t want to talk to those people at all<\/strong>. To those people I should be a blackhole. To do this I used UFW which is short for uncomplicated firewall, which essentially makes dealing with Iptables much much nicer. It&#8217;s only my 2nd time using UFW, but I&#8217;ve been using Iptables for well over a decade. Anyway, here is my simple setup with UFW that I came up with.<\/p>\n<pre class=\"lang:default decode:true\">tuxninja@tlprod1:\/etc\/bind$ sudo ufw default deny incoming\r\nDefault incoming policy changed to 'deny'\r\n(be sure to update your rules accordingly)\r\n\r\ntuxninja@tlprod1:\/etc\/bind$ sudo ufw default allow outgoing\r\nDefault outgoing policy changed to 'allow'\r\n(be sure to update your rules accordingly)\r\n\r\ntuxninja@tlprod1:\/etc\/bind$ sudo ufw allow ssh\r\nRules updated\r\nRules updated (v6)\r\n\r\ntuxninja@tlprod1:\/etc\/bind$ sudo ufw allow 80\r\nRules updated\r\nRules updated (v6)<\/pre>\n<p>So we are configuring the default policy to deny all incoming traffic, allow outgoing, and then allow SSH &amp; Apache\/Web traffic basically. Next I created a script called block.sh to add ufw deny rules for bad actors I parsed out of my log, here&#8217;s what block.sh looks like<\/p>\n<pre class=\"lang:default decode:true\"># cat block.sh \r\n#!\/bin\/bash\r\n\r\nwhile read line; do\r\n\tufw deny from $line\r\ndone<\/pre>\n<p>Don&#8217;t forget to chmod +x your shell script. Then I did this&#8230; blocking all bad actors&#8230;<\/p>\n<pre class=\"lang:default decode:true \">root@tlprod1:~# cat \/var\/log\/syslog | grep hehehey.ru | grep -v repeated | awk -F ' ' '{print $7}' | cut -d '#' -f 1 | .\/block.sh<\/pre>\n<p>Note, use sudo if you don&#8217;t run this as root. This will go through my log and find all these bad requests, and block the requestor. It&#8217;s quite aggresive, so be careful, make sure you thoroughly limit your parsing with grep to only block things you really don&#8217;t want talking to your server, because this blocks ALL traffic from this requestor to your service, not just DNS.<\/p>\n<p>Once that is complete you need to finally permit good DNS requests by running<\/p>\n<pre class=\"lang:default decode:true \">ufw allow 53<\/pre>\n<p>And then finally enable your firewall<\/p>\n<pre class=\"lang:default decode:true \">ufw enable<\/pre>\n<p>If you are successful you should see entries in your log that look like this<\/p>\n<pre class=\"lang:default decode:true \">Nov 11 15:10:35 tlprod1 kernel: [1652178.544292] [UFW BLOCK] IN=eth0 OUT= MAC=04:01:63:57:8a:01:3c:8a:b0:0d:3f:f0:08:00 SRC=65.60.18.103 DST=192.241.206.198 LEN=72 TOS=0x00 PREC=0x00 TTL=247 ID=31303 PROTO=UDP SPT=20225 DPT=53 LEN=52<\/pre>\n<p>You can also view all your firewall rules by running<\/p>\n<pre class=\"lang:default decode:true  \">sudo ufw status numbered<\/pre>\n<p>Happy Blocking !<\/p>\n<h2><\/h2>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<a href=\"https:\/\/tuxlabs.com\/?p=313\" rel=\"bookmark\" title=\"Permalink to Preventing (bind9) DNS Naughty-ness (named.conf &#038; iptables\/ufw) on Ubuntu\"><p>If you run a DNS server on the Internet with a default configuration many people\/robots will take advantage of you. The same is true for Mail, but that is another article. Needless to say if you are running a service on the Internet, the naughty goblins will find you. To thwart these dirty criminals all [&hellip;]<\/p>\n<\/a>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,78,12],"tags":[116,112,113,115,117,111,114,110],"class_list":{"0":"post-313","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-howtos","7":"category-security","8":"category-systems-administration","9":"tag-authorative-zones","10":"tag-bind9","11":"tag-dns","12":"tag-dns-caching","13":"tag-firewall","14":"tag-iptables","15":"tag-named-conf","16":"tag-ufw","17":"h-entry","18":"hentry"},"_links":{"self":[{"href":"https:\/\/tuxlabs.com\/index.php?rest_route=\/wp\/v2\/posts\/313","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tuxlabs.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tuxlabs.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tuxlabs.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tuxlabs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=313"}],"version-history":[{"count":2,"href":"https:\/\/tuxlabs.com\/index.php?rest_route=\/wp\/v2\/posts\/313\/revisions"}],"predecessor-version":[{"id":315,"href":"https:\/\/tuxlabs.com\/index.php?rest_route=\/wp\/v2\/posts\/313\/revisions\/315"}],"wp:attachment":[{"href":"https:\/\/tuxlabs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tuxlabs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tuxlabs.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}