{"id":1163,"date":"2013-11-01T03:57:08","date_gmt":"2013-11-01T09:57:08","guid":{"rendered":"http:\/\/blog.supportpro.com\/?p=1163"},"modified":"2018-10-04T00:32:07","modified_gmt":"2018-10-04T06:32:07","slug":"country-wise-ip-block","status":"publish","type":"post","link":"https:\/\/www.supportpro.com\/blog\/country-wise-ip-block\/","title":{"rendered":"Block IP in iptables based on country code"},"content":{"rendered":"<p>It is very easy to block IP (country wise) with the help of CSF the default firewall from Cpanel.But it is not the case when we try with IPTables.<\/p>\n<p>In this blog I will try to demonstrate how to Block IP from a certain country with the help of IPtables.For example purpose I choose Afghanistan and China.<\/p>\n<p><!--more--><\/p>\n<p>I will give step by step instruction to install and automate the scrtipt given below.<\/p>\n<blockquote><p>#!\/bin\/bash<br \/>\n# Purpose: Block all traffic from AFGHANISTAN (af) and CHINA (CN).<\/p>\n<p>Use ISO code. #<br \/>\n# &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\nISO=&#8221;af cn&#8221;<br \/>\n### Set PATH ###<br \/>\nIPT=\/sbin\/iptables<br \/>\nWGET=\/usr\/bin\/wget<br \/>\nEGREP=\/bin\/egrep<br \/>\n### No editing below ###<br \/>\nSPAMLIST=&#8221;countrydrop&#8221;<br \/>\nZONEROOT=&#8221;\/root\/iptables&#8221;<br \/>\nDLROOT=&#8221;http:\/\/www.ipdeny.com\/ipblocks\/data\/countries&#8221;<br \/>\ncleanOldRules(){<br \/>\n$IPT -F<br \/>\n$IPT -X<br \/>\n$IPT -t nat -F<br \/>\n$IPT -t nat -X<br \/>\n$IPT -t mangle -F<br \/>\n$IPT -t mangle -X<br \/>\n$IPT -P INPUT ACCEPT<br \/>\n$IPT -P OUTPUT ACCEPT<br \/>\n$IPT -P FORWARD ACCEPT<br \/>\n}<br \/>\n# create a dir<br \/>\n[ ! -d $ZONEROOT ] &amp;&amp; \/bin\/mkdir -p $ZONEROOT<br \/>\n# clean old rules<br \/>\ncleanOldRules<br \/>\n# create a new iptables list<br \/>\n$IPT -N $SPAMLIST<br \/>\nfor c in $ISO<br \/>\ndo<br \/>\n# local zone file<br \/>\ntDB=$ZONEROOT\/$c.zone<br \/>\n# get fresh zone file<br \/>\n$WGET -O $tDB $DLROOT\/$c.zone<br \/>\n# country specific log message<br \/>\nSPAMDROPMSG=&#8221;$c Country Drop&#8221;<br \/>\n# get<br \/>\nBADIPS=$(egrep -v &#8220;^#|^$&#8221; $tDB)<br \/>\nfor ipblock in $BADIPS<br \/>\ndo<br \/>\n$IPT -A $SPAMLIST -s $ipblock -j LOG &#8211;log-prefix<br \/>\n&#8220;$SPAMDROPMSG&#8221;<br \/>\n$IPT -A $SPAMLIST -s $ipblock -j DROP<br \/>\ndone<br \/>\ndone<br \/>\n# Drop everything<br \/>\n$IPT -I INPUT -j $SPAMLIST<br \/>\n$IPT -I OUTPUT -j $SPAMLIST<br \/>\n$IPT -I FORWARD -j $SPAMLIST<br \/>\n# call your other iptable script<br \/>\n# \/path\/to\/other\/iptables.sh<br \/>\nexit 0<\/p><\/blockquote>\n<p>Step 1<\/p>\n<p>Save above script as root user and customize ISO variable to point out country name using ISO country names. Once done install the script as follows using crontab<\/p>\n<blockquote><p># @weekly \/path\/to\/country.block.iptables.sh<\/p><\/blockquote>\n<p>To start blocking immediately type:<\/p>\n<blockquote><p># \/path\/to\/country.block.iptables.sh<\/p><\/blockquote>\n<p>Step 2<\/p>\n<p>Another, alternative to above shell script is to use geoip iptables patch. This is not standard iptables modules. You need to download patch and compile Linux kernel.<\/p>\n<p>If you require help, <a href=\"https:\/\/www.supportpro.com\/requestquote.php\">contact SupportPRO Server Admin<\/a><\/p>\n<p style=\"text-align: center;\"><!--HubSpot Call-to-Action Code --><span id=\"hs-cta-wrapper-9d590242-d641-4383-94b4-8cfd62f0af6b\" class=\"hs-cta-wrapper\"><span id=\"hs-cta-9d590242-d641-4383-94b4-8cfd62f0af6b\" class=\"hs-cta-node hs-cta-9d590242-d641-4383-94b4-8cfd62f0af6b\"><!-- [if lte IE 8]><\/p>\n\n\n\n\n\n<div id=\"hs-cta-ie-element\"><\/div>\n\n\n<![endif]--><a href=\"https:\/\/cta-redirect.hubspot.com\/cta\/redirect\/2725694\/9d590242-d641-4383-94b4-8cfd62f0af6b\"><img decoding=\"async\" id=\"hs-cta-img-9d590242-d641-4383-94b4-8cfd62f0af6b\" class=\"hs-cta-img\" style=\"border-width: 0px;\" src=\"https:\/\/no-cache.hubspot.com\/cta\/default\/2725694\/9d590242-d641-4383-94b4-8cfd62f0af6b.png\" alt=\"Server not running properly? Get A FREE Server Checkup By Expert Server Admins - $125 Value\" \/><\/a><\/span><script charset=\"utf-8\" src=\"https:\/\/js.hscta.net\/cta\/current.js\"><\/script><script type=\"text\/javascript\"> hbspt.cta.load(2725694, '9d590242-d641-4383-94b4-8cfd62f0af6b', {}); <\/script><\/span><!-- end HubSpot Call-to-Action Code --><\/p>\n","protected":false},"excerpt":{"rendered":"<p>It is very easy to block IP (country wise) with the help of CSF the default firewall from Cpanel.But it is not the case when we try with IPTables. In&hellip;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[6],"tags":[],"class_list":["post-1163","post","type-post","status-publish","format-standard","hentry","category-linux-basics"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts\/1163","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/comments?post=1163"}],"version-history":[{"count":5,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts\/1163\/revisions"}],"predecessor-version":[{"id":3134,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts\/1163\/revisions\/3134"}],"wp:attachment":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/media?parent=1163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/categories?post=1163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/tags?post=1163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}