{"id":453,"date":"2019-06-21T09:32:34","date_gmt":"2019-06-21T07:32:34","guid":{"rendered":"https:\/\/www.gonscak.sk\/?p=453"},"modified":"2019-06-21T15:20:15","modified_gmt":"2019-06-21T13:20:15","slug":"hardening-iptables-from-accept-all-to-drop-all","status":"publish","type":"post","link":"https:\/\/www.gonscak.sk\/?p=453","title":{"rendered":"Hardening iptables from &#8220;ACCEPT all&#8221; to &#8220;DROP all&#8221;"},"content":{"rendered":"\n<p>Now I write some rules, for hardening iptables. From default policy &#8220;accept&#8221; everything to &#8220;drop&#8221; everything except something I want to accept. This setup was made on Server Ubuntu 18.04.2 LTS.<\/p>\n\n\n\n<p>This post is related to and made from sites: <\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>https:\/\/help.ubuntu.com\/community\/IptablesHowTo<\/p><p>https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-set-up-a-firewall-using-iptables-on-ubuntu-14-0<\/p><\/blockquote>\n\n\n\n<p>By default, we can see, that everything is allowed:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">iptables -L<br> Chain INPUT (policy ACCEPT)<br> target     prot opt source               destination         <br> Chain FORWARD (policy ACCEPT)<br> target     prot opt source               destination         <br> Chain OUTPUT (policy ACCEPT)<br> target     prot opt source               destination  <\/pre>\n\n\n\n<p>So we start with allowing established sessions to receive traffic:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT<\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong>-A INPUT<\/strong>: The <code>-A<\/code> flag <em>appends<\/em> a  rule to the end of a chain.  This is the portion of the command that  tells iptables that we wish to add a new rule, that we want that rule  added to the end of the chain, and that the chain we want to operate on  is the INPUT chain.<\/p><\/blockquote>\n\n\n\n<p>And now, we can allow specific port or service, which we want to allow:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">iptables -A INPUT -p tcp --dport ssh -j ACCEPT<br>iptables -A INPUT -p tcp --dport http -j ACCEPT<br>iptables -A INPUT -p tcp --dport https -j ACCEPT<\/pre>\n\n\n\n<p>And now, we block everything else commint to us:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">iptables -A INPUT -j DROP<\/pre>\n\n\n\n<p>Now we can see our input chain in firewall:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">iptables -L<br> Chain INPUT (policy ACCEPT)<br> target     prot opt source               destination                  <br> ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED<br> ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ssh<br> ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:http<br> ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:https<br> DROP       all  --  anywhere             anywhere            <\/pre>\n\n\n\n<p>Now we must add some rule for loopback. because we block it now. If we add it right now with above command, we add it at the end of chain (after drop all). So all traffic will be blocked. We must add it at the begining of this chain:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">iptables -I INPUT 1 -i lo -j ACCEPT<\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong>-I INPUT 1<\/strong>: The <code>-I<\/code> flag tells iptables to <em>insert<\/em> a rule.  This is different than the <code>-A<\/code> flag which appends a rule to the end.  The <code>-I<\/code> flag takes a chain and the rule position where you want to insert the new rule.<\/p><p><strong>-i lo<\/strong>: This component of the rule matches if the  interface that the packet is using is the &#8220;lo&#8221; interface.  The &#8220;lo&#8221;  interface is another name for the loopback device.  This means that any  packet using that interface to communicate (packets generated on our  server, for our server) should be accepted.<\/p><\/blockquote>\n\n\n\n<p>And now we can see it:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">iptables -L<br>\nChain INPUT (policy ACCEPT)<br>\ntarget     prot opt source               destination         <br>\nACCEPT     all  --  anywhere             anywhere            <br>\nACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED<br>\nACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ssh<br>\nACCEPT     tcp  --  anywhere             anywhere             tcp dpt:http<br>\nACCEPT     tcp  --  anywhere             anywhere             tcp dpt:https<br>\nDROP       all  --  anywhere             anywhere          <\/pre>\n\n\n\n<p>The first and the last lines looks very similar, so use the variable -v (verbose) os -S (list rules). See<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">iptables -L -v<br> Chain INPUT (policy ACCEPT 0 packets, 0 bytes)<br>  pkts bytes target     prot opt in     out     source               destination         <br>     0     0 ACCEPT     all  --  lo     any     anywhere             anywhere            <br>   287 46814 ACCEPT     all  --  any    any     anywhere             anywhere             ctstate RELATED,ESTABLISHED<br>     0     0 ACCEPT     tcp  --  any    any     anywhere             anywhere             tcp dpt:ssh<br>     0     0 ACCEPT     tcp  --  any    any     anywhere             anywhere             tcp dpt:http<br>     0     0 ACCEPT     tcp  --  any    any     anywhere             anywhere             tcp dpt:https<br>   211 45230 DROP       all  --  any    any     anywhere             anywhere    <\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">iptables -S<br>\n-P INPUT ACCEPT<br>\n-P FORWARD ACCEPT<br>\n-P OUTPUT ACCEPT<br>\n-A INPUT -i lo -j ACCEPT<br>\n-A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT<br>\n-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT<br>\n-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT<br>\n-A INPUT -p tcp -m tcp --dport 443 -j ACCEPT<br>\n-A INPUT -j DROP<\/pre>\n\n\n\n<p>Now we have five rules to ACCEPT packets, which we want. The we have the sixth rule for DROP all another packets. <\/p>\n\n\n\n<p>The policy DROP everything can be done by two ways. We have the first way (Default policy of chain is ACCEPT everything. Our five rules catch certain packets and at the end we have the sixth rule to DROP all packet which catch all other remain packets). In case of breaking firewall, or accidentally flush our rules, we still can connect to our server (by default chain policy ACCEPT).<\/p>\n\n\n\n<p>The second way is set default chain policy to DROP,  and set our five rules first. So if packets are catch by one of this rules, is ACCEPTed. Then it is DROPPEd by default. There is a possibility, that if we flush our firewall rules, we never reach our server from network because the default chain policy is DROP. So first, we need the rules like above mentioned except the DROP rule. And then, at the end, change the default chain policy by command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">iptables -P INPUT DROP\n<\/pre>\n\n\n\n<p>And now look at this way of firewall:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">iptables -S<br> -P INPUT DROP<br> -P FORWARD ACCEPT<br> -P OUTPUT ACCEPT<br> -A INPUT -i lo -j ACCEPT<br> -A INPUT -p tcp -m tcp --dport 22 -j ACCEPT<br> -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT<br> -A INPUT -p tcp -m tcp --dport 443 -j ACCEPT<br> -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT<\/pre>\n\n\n\n<p>So we can see, that we DROP all packet, we want and ACCEPT packets we want. It can be done by this two ways. So pick one, which you want. I prefer the second way, because I have another access to server (via console-keyboard connected directly to server). So if something go wrong, I am still be able to connect it.<\/p>\n\n\n\n<p>So if you choose the first way, you must add others rules before the DROP rule, because it will be matched by this rule. Like the loopback rule, you must insert it somewhere before the DROP rules. See the lines:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">iptables -L --line-numbers<br> Chain INPUT (policy ACCEPT)<br> num  target     prot opt source               destination         <br> 1    ACCEPT     all  --  anywhere             anywhere            <br> 2    ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ssh<br> 3    ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:http<br> 4    ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:https<br> 5    ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED<br> 6    DROP       all  --  anywhere             anywhere<\/pre>\n\n\n\n<p>And now we can add another rule somewhere in the middle:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">iptables -I INPUT 6 -p tcp --dport 5666 -j ACCEPT<\/pre>\n\n\n\n<p>And we see it:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">iptables -L --line-numbers<br>\nChain INPUT (policy ACCEPT)<br>\nnum  target     prot opt source               destination         <br>\n1    ACCEPT     all  --  anywhere             anywhere            <br>\n2    ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ssh<br>\n3    ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:http<br>\n4    ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:https<br>\n5    ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED<br>\n6    ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:nrpe<br>\n7    DROP       all  --  anywhere             anywhere<\/pre>\n\n\n\n<p>For save this rules and set it persistant after reboot, I use package:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">apt-get install iptables-persistent<\/pre>\n\n\n\n<p>During installation you will be asked for some questions, like save this rules for permanent use and load next boot. If you haven&#8217;t yet, never mind. You can do it later with this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">iptables-save -c &gt; \/etc\/iptables\/rules.v4<\/pre>\n\n\n\n<p><\/p>\n ","protected":false},"excerpt":{"rendered":"<p>Now I write some rules, for hardening iptables. From default policy &#8220;accept&#8221; everything to &#8220;drop&#8221; everything except something I want to accept. This setup was made on Server Ubuntu 18.04.2 LTS. This post is related to and made from sites: https:\/\/help.ubuntu.com\/community\/IptablesHowTo https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-set-up-a-firewall-using-iptables-on-ubuntu-14-0 By default, we can see, that everything is allowed: iptables -L Chain INPUT &hellip; <a href=\"https:\/\/www.gonscak.sk\/?p=453\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Hardening iptables from &#8220;ACCEPT all&#8221; to &#8220;DROP all&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[107,106,103,104,105,35,60],"class_list":["post-453","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-accept","tag-drop","tag-firewall","tag-iptables","tag-security","tag-server","tag-ubuntu"],"_links":{"self":[{"href":"https:\/\/www.gonscak.sk\/index.php?rest_route=\/wp\/v2\/posts\/453","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gonscak.sk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gonscak.sk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gonscak.sk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gonscak.sk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=453"}],"version-history":[{"count":10,"href":"https:\/\/www.gonscak.sk\/index.php?rest_route=\/wp\/v2\/posts\/453\/revisions"}],"predecessor-version":[{"id":466,"href":"https:\/\/www.gonscak.sk\/index.php?rest_route=\/wp\/v2\/posts\/453\/revisions\/466"}],"wp:attachment":[{"href":"https:\/\/www.gonscak.sk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gonscak.sk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gonscak.sk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}