{"id":42,"date":"2014-12-24T08:16:49","date_gmt":"2014-12-24T08:16:49","guid":{"rendered":"http:\/\/star.genuinewebhost.com\/~linuxguru\/tutorial\/?p=42"},"modified":"2018-04-18T11:55:43","modified_gmt":"2018-04-18T11:55:43","slug":"creating-just-the-required-iptables-rules-from-scratch","status":"publish","type":"post","link":"http:\/\/shijuvarghese.com\/?p=42","title":{"rendered":"Iptables: Creating just the required rules from scratch"},"content":{"rendered":"<p>It\u00a0is a challenging task to create the perfect iptables rules allowing only the required traffic.<\/p>\n<p><em><strong>*** VERY IMPORTANT* **\u00a0<\/strong><\/em><\/p>\n<p><em>Before making any changes in the iptables ensure that you have console access to the host so that you will not get locked out.<\/em><\/p>\n<p>In this exercise we are going to use a <strong>CentOS 7<\/strong> system. Since\u00a0<strong>firewalld<\/strong> is the firewall that comes by default, it will be good to uninstall firewalld to avoid any confusion. The focus of this tutorial will be to guide how to view logs and make adjustments in firewall accordingly.<\/p>\n<p><em><strong>[root@host1 ~]#\u00a0yum remove firewalld -y<\/strong><\/em><\/p>\n<p>Now install iptables:<br \/>\n<em><strong>[root@host1 ~]# yum install iptables iptables-services -y<\/strong><\/em><\/p>\n<p>Start IPTABLES service in the server host<br \/>\n<em><strong>[root@host1 ~]# systemctl start iptables<\/strong><\/em><\/p>\n<p>List all entries in an Iptables<br \/>\n<em><strong>[root@host1 ~]# iptables -L<\/strong><\/em><\/p>\n<p>Flush all iptables rules<br \/>\n<em><strong>[root@host1 ~]# iptables -F<\/strong><\/em><\/p>\n<p>====== ===== =====<\/p>\n<p>Now let us start the actual lab<\/p>\n<p><strong>Resetting all chains to DROP all traffic:<\/strong><\/p>\n<ul>\n<li>iptables -P OUTPUT DROP<\/li>\n<li>iptables -P INPUT DROP<\/li>\n<li>iptables -P FORWARD DROP<\/li>\n<\/ul>\n<p><strong>Enable logging:<\/strong><\/p>\n<ul>\n<li>iptables -N LOGGING<\/li>\n<li>iptables -A INPUT -j LOGGING<\/li>\n<li>iptables -A OUTPUT -j LOGGING<\/li>\n<li>iptables -A LOGGING -m limit &#8211;limit 2\/min -j LOG &#8211;log-prefix &#8220;IPTables-Dropped: &#8221; &#8211;log-level 4<\/li>\n<li>iptables -A LOGGING -j DROP<\/li>\n<\/ul>\n<p><strong>Save all entries:<\/strong><\/p>\n<ul>\n<li>service iptables save<\/li>\n<li><em>You may get <strong>blocked<\/strong> from SSH since there are no rules allowing SSH connections now. You will require\u00a0<strong>console access\u00a0<\/strong>now<\/em><\/li>\n<\/ul>\n<p><strong>Explanation:<\/strong><\/p>\n<ol>\n<li><em>iptables -N LOGGING: This creates a new chain called LOGGING<\/em><\/li>\n<li><em> iptables -A &lt;INPUT,OUTPUT&gt; -j LOGGING: All the remaining incoming packets will jump to the LOGGING chain<\/em><\/li>\n<li><em> line No 4: This will write packets dropped to the default syslog, which is \/var\/log\/messages<\/em><\/li>\n<li><em> iptables -A LOGGING -j DROP: Drop all the packets that came to the LOGGING chain<\/em><\/li>\n<\/ol>\n<p><strong>The logs:<\/strong><\/p>\n<p>The above commands will enable logging, and traffic blocked by iptables will be logged in <strong>\/var\/log\/messages<\/strong> with lines starting with\u00a0<strong>IPTables-Dropped<\/strong>.<\/p>\n<p>Example:<br \/>\n<em>Apr 18 07:35:02 host1 kernel: IPTables-Dropped:IN=ens33 OUT= MAC=00:0c:29:6f:51:93:00:26:82:93:e9:4e:08:00 SRC=192.168.1.7 DST=192.168.1.9 LEN=52 TOS=0x00 PREC=0x00 TTL=64 ID=32353 DF PROTO=TCP SPT=53352 DPT=80 WINDOW=8192 RES=0x00 SYN URGP=0<\/em><\/p>\n<p><strong>Adding rules:<\/strong><\/p>\n<p>Let us add rules to allow SSH to the host.<\/p>\n<p>Let us check the present rules and the sequence numbers associated with them:<\/p>\n<p><em><strong>[root@host1 ~]# iptables -L &#8211;line-numbers<\/strong><\/em><br \/>\nChain INPUT (policy DROP)<br \/>\nnum target prot opt source destination<br \/>\n1 LOGGING all &#8212; anywhere anywhere<\/p>\n<p>Chain FORWARD (policy DROP)<br \/>\nnum target prot opt source destination<\/p>\n<p>Chain OUTPUT (policy DROP)<br \/>\nnum target prot opt source destination<br \/>\n1 LOGGING all &#8212; anywhere anywhere<\/p>\n<p>Chain LOGGING (2 references)<br \/>\nnum target prot opt source destination<br \/>\n1 LOG all &#8212; anywhere anywhere limit: avg 2\/min burst 5 LOG level warning prefix &#8220;IPTables-Dropped:&#8221;<\/p>\n<p><strong>=======<\/strong><\/p>\n<p>We need to insert rules so that they get added before the\u00a0<strong>logging<\/strong> entries.<\/p>\n<p><em><strong>[root@host1 ~]#iptables -I INPUT 1 -p tcp &#8211;dport 22 -j ACCEPT<\/strong><\/em><br \/>\n<em><strong>[root@host1 ~]#iptables -I OUTPUT 1 -p tcp &#8211;sport 22 -j ACCEPT<\/strong><\/em><\/p>\n<p><em><strong>[root@host1 ~]# service iptables save<\/strong><\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>It\u00a0is a challenging task to create the perfect iptables rules allowing only the required traffic. *** VERY IMPORTANT* **\u00a0 Before making any changes in the <a class=\"mh-excerpt-more\" href=\"http:\/\/shijuvarghese.com\/?p=42\" title=\"Iptables: Creating just the required rules from scratch\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":254,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,3,14],"tags":[],"class_list":["post-42","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-firewall","category-linux","category-security"],"_links":{"self":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/42","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=42"}],"version-history":[{"count":26,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/42\/revisions"}],"predecessor-version":[{"id":562,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/42\/revisions\/562"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/media\/254"}],"wp:attachment":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=42"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=42"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=42"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}