#!/bin/bash # Purpose: Block all traffic from AFGHANISTAN (af) and CHINA (CN). Use ISO code. # # See url for more info - http://www.cyberciti.biz/faq/?p=3402 # Author: nixCraft <www.cyberciti.biz> under GPL v.2.0+ #…
SKSohaib Khan//2 min read
Block Country using Iptables
Shell/68 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/bin/bash# Purpose: Block all traffic from AFGHANISTAN (af) and CHINA (CN). Use ISO code. ## See url for more info - http://www.cyberciti.biz/faq/?p=3402# Author: nixCraft <www.cyberciti.biz> under GPL v.2.0+# -------------------------------------------------------------------------------
ISO="af cn"### Set PATH ###
IPT=/sbin/iptables
WGET=/usr/bin/wget
EGREP=/bin/egrep
### No editing below ###
SPAMLIST="countrydrop"
ZONEROOT="/root/iptables"
DLROOT="http://www.ipdeny.com/ipblocks/data/countries"cleanOldRules(){
$IPT -F
$IPT -X
$IPT -t nat -F
$IPT -t nat -X
$IPT -t mangle -F
$IPT -t mangle -X
$IPT -P INPUT ACCEPT
$IPT -P OUTPUT ACCEPT
$IPT -P FORWARD ACCEPT
}
# create a dir
[ ! -d $ZONEROOT ] && /bin/mkdir -p $ZONEROOT# clean old rules
cleanOldRules
# create a new iptables list$IPT -N $SPAMLISTfor c in$ISOdo# local zone file
tDB=$ZONEROOT/$c.zone
# get fresh zone file$WGET -O $tDB$DLROOT/$c.zone
# country specific log message
SPAMDROPMSG="$c Country Drop"# get
BADIPS=$(egrep -v "^#|^$"$tDB)
for ipblock in$BADIPSdo$IPT -A $SPAMLIST -s $ipblock -j LOG --log-prefix "$SPAMDROPMSG"$IPT -A $SPAMLIST -s $ipblock -j DROP
donedone# Drop everything$IPT -I INPUT -j $SPAMLIST$IPT -I OUTPUT -j $SPAMLIST$IPT -I FORWARD -j $SPAMLIST# call your other iptable script# /path/to/other/iptables.shexit
Shell/5 lines
1
2
3
4
5
Setup Crontab to run weekly I've setup to run every sat.
* * * * 6 /root/block.sh
To make sure your settings are saved on every reboot, install iptables-persistent by using
Shell/1 line
sudo apt-get install iptables-persistent
During the installation, you will be asked if you want to save the iptable rules to both the IPv4 rules and the IPv6 rules. Say yes to both. Your rules will then be saved in /etc/iptables/rules.v4 and /etc/iptables/rules.v6. Once the installation is complete, start iptables-persistent running:
I spent the last few months building the Azure and Microsoft 365 foundation for a large enterprise. The shape of the work is the useful part anyway. The brief was not "set up Azure." The brief was "build the thing…
Creating virtual machines in Proxmox manually can be time-consuming, especially when deploying multiple instances or standardizing configurations across environments. This comprehensive guide walks you through…
Introduction Securing your server is a critical aspect of maintaining a robust and reliable online presence. Bitninja, a comprehensive security solution, provides an effective way to protect your server from various…