forked from simonbouchard/geoip-blocking-w-firewalld
-
Notifications
You must be signed in to change notification settings - Fork 0
/
firewalld
200 lines (173 loc) · 7.2 KB
/
firewalld
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/bin/bash
#|--------------------------------------------------------------------------
# Firewall GeoIP script for firewalld
#
# Written by Simon Bouchard <[email protected]>
# Refactored and inspired from https://gist.github.com/Pandry/21fc0e30abbfd0579ec69c491b99a446
#
# Created: Mars 2022
#
# You may use, modify, and redistribute this script freely
#|--------------------------------------------------------------------------
#|--------------------------------------------------------------------------
#| Options
#|--------------------------------------------------------------------------
IPSET="ipset"
if [[ -f /etc/default/firewalld-geoip ]]; then
source /etc/default/firewalld-geoip
fi
BLACKLIST="geoip-blacklist"
TMPDIR="/tmp/geoip"
#|--------------------------------------------------------------------------
#| Colors
#|--------------------------------------------------------------------------
if [[ -t 1 ]]; then
RED='\033[0;91m'
GREEN='\033[0;92m'
CYAN='\033[0;96m'
YELLOW='\033[0;93m'
PURPLE='\033[0;95m'
NC='\033[0m'
fi
if [[ -z $ZONES ]]; then
echo "${RED}No zones to ban. Exiting${NC}"
fi
#|--------------------------------------------------------------------------
#| Global variables
#|--------------------------------------------------------------------------
FIREWALLD="$(which firewalld)"
YUM="$(which yum)"
APT="$(which apt)"
#|--------------------------------------------------------------------------
#| Main
#|--------------------------------------------------------------------------
if [[ -x "$YUM" ]]; then
OS="RHEL"
echo -e "[${PURPLE}i${NC}] Detected operating system as RHEL/CentOS"
elif [[ -x "$APT" ]]; then
OS="DEBIAN"
echo -e "[${PURPLE}i${NC}] Detected operating system as Debian/Ubuntu"
else
echo -e "[${RED}✗${NC}] Couldn't determine the current OS. Aborting!"
exit 1
fi
echo -e "[${PURPLE}i${NC}] Verifying if firewalld is available"
if ! [[ -x "$FIREWALLD" ]]; then
echo -e "[${PURPLE}i${NC}] firewalld not available, installing"
if [[ "$OS" == "RHEL" ]]; then
if yum -y install firewalld > /dev/null 2>&1; then
echo -e "[${GREEN}✓${NC}] firewalld is installed"
systemctl enable --now firewalld > /dev/null 2> /dev/null
else
echo -e "[${RED}✗${NC}] Couldn't install firewalld. Aborting!"
exit 1
fi
elif [[ "$OS" == "DEBIAN" ]]; then
if apt-get -y install firewalld > /dev/null 2>&1; then
echo -e "[${GREEN}✓${NC}] firewalld is installed"
systemctl enable --now firewalld > /dev/null 2> /dev/null
else
echo -e "[${RED}✗${NC}] Couldn't install firewalld. Aborting!"
exit 1
fi
fi
else
echo -e "[${GREEN}✓${NC}] firewalld is installed"
fi
echo ""
echo -e "[${PURPLE}i${NC}] Checking for existing ipset for ${BLACKLIST}-ip4"
if firewall-cmd --permanent --get-ipsets | grep -q "${BLACKLIST}-ip4"; then
echo -e "[${PURPLE}i${NC}] Deleting ipset ${BLACKLIST}-ip4"
firewall-cmd --permanent --zone=drop --remove-source=ipset:"${BLACKLIST}-ip4" &> /dev/null
firewall-cmd --reload &> /dev/null
firewall-cmd --permanent --delete-ipset="${BLACKLIST}-ip4" &> /dev/null
fi
echo -e "[${PURPLE}i${NC}] Creating new ipset for ${BLACKLIST}-ip4"
if firewall-cmd --permanent --new-ipset="${BLACKLIST}-ip4" --type=hash:net --option=family=inet --option=hashsize=4096 --option=maxelem=200000 --zone=drop > /dev/null 2>&1;then
echo -e "[${GREEN}✓${NC}] ipset for ${BLACKLIST}-ip4 successfully created"
else
echo -e "[${RED}✗${NC}] Couldn't create the blacklist ${BLACKLIST}-ip4. Aborting!"
exit 1
fi
echo ""
echo -e "[${PURPLE}i${NC}] Checking for existing ipset for ${BLACKLIST}-ip6"
if firewall-cmd --permanent --get-ipsets | grep -q "${BLACKLIST}-ip6"; then
echo -e "[${PURPLE}i${NC}] Deleting ipset ${BLACKLIST}-ip6"
firewall-cmd --permanent --zone=drop --remove-source=ipset:"${BLACKLIST}-ip6" &> /dev/null
firewall-cmd --reload &> /dev/null
firewall-cmd --permanent --delete-ipset="${BLACKLIST}-ip6" &> /dev/null
fi
echo -e "[${PURPLE}i${NC}] Creating new ipset for ${BLACKLIST}-ip6"
if firewall-cmd --permanent --new-ipset="${BLACKLIST}-ip6" --type=hash:net --option=family=inet6 --option=hashsize=4096 --option=maxelem=200000 --zone=drop > /dev/null 2>&1;then
echo -e "[${GREEN}✓${NC}] ipset for ${BLACKLIST}-ip6 successfully created"
else
echo -e "[${RED}✗${NC}] Couldn't create the blacklist ${BLACKLIST}-ip6. Aborting!"
exit 1
fi
echo ""
if mkdir -p $TMPDIR;then
echo -e "[${PURPLE}i${NC}] Retrieving the requested zones to be blacklisted"
echo ""
overall_status=0
for z in $ZONES; do
echo -e "[${PURPLE}i${NC}] Downloading zone ${YELLOW}$z${NC}"
curl -s -L -o "$TMPDIR/${z}-ip4.zone" "https://www.ipdeny.com/ipblocks/data/countries/${z,,}.zone"
zone_status="$?"
curl -s -L -o "$TMPDIR/${z}-ip6.zone" "https://www.ipdeny.com/ipv6/ipaddresses/blocks/${z,,}.zone"
ip6_status="$?"
zone_status=$((zone_status + ip6_status))
if [[ $zone_status -eq 0 ]];then
echo -e "[${GREEN}✓${NC}] OK"
else
echo -e "[${RED}✗${NC}] Failed"
fi
echo ""
overall_status=$((overall_status + zone_status))
done
if [[ $overall_status -eq 0 ]];then
echo -e "[${GREEN}✓${NC}] All zones were successfully downloaded"
echo ""
else
echo -e "[${RED}✗${NC}] Failed to download all the requested zones. Aborting!"
exit 1
fi
else
echo -e "[${RED}✗${NC}] Couldn't create the $TMPDIR directory. Aborting!"
exit 1
fi
# Load the zone(s) into the blacklist
for f in "$TMPDIR"/*-ip4.zone; do
echo -e "[${PURPLE}i${NC}] Adding ipv4 target ranges from ${YELLOW}$f${NC}"
if firewall-cmd --permanent --ipset="${BLACKLIST}-ip4" --add-entries-from-file="$f" > /dev/null;then
echo -e "[${GREEN}✓${NC}] OK"
else
echo -e "[${RED}✗${NC}] Failed"
fi
echo ""
done
for f in "$TMPDIR"/*-ip6.zone; do
echo -e "[${PURPLE}i${NC}] Adding ipv6 target ranges from ${YELLOW}$f${NC}"
if firewall-cmd --permanent --ipset="${BLACKLIST}-ip6" --add-entries-from-file="$f" > /dev/null;then
echo -e "[${GREEN}✓${NC}] OK"
else
echo -e "[${RED}✗${NC}] Failed"
fi
echo ""
done
# Initialize the firewall
echo -e "[${PURPLE}i${NC}] Initializing firewalld"
firewall-cmd --permanent --zone=drop --add-source="ipset:${BLACKLIST}-ip4" > /dev/null
firewall-cmd --permanent --zone=drop --add-source="ipset:${BLACKLIST}-ip6" > /dev/null
# Reload the firewall
echo -e "[${PURPLE}i${NC}] Reloading firewalld"
firewall-cmd --reload > /dev/null
echo -e "[${PURPLE}i${NC}] Cleaning up..."
rm -rf $TMPDIR
echo ""
if type "$IPSET"; then
echo -e "[${PURPLE}i${NC}] Blocking approx. ${YELLOW}$("$IPSET" list ${BLACKLIST}-ip4 | wc -l)${NC} ipv4 target ranges and approx. ${YELLOW}$("$IPSET" list ${BLACKLIST}-ip6 | wc -l)${NC} ipv6 target ranges."
else
echo -e "${YELLOW}Unable to provide stats, ${IPSET} is not reachable.${NC}"
echo -e "You can provide the correct path with ${CYAN}IPSET=/correct/path${NC} in ${CYAN}/etc/default/firewalld-geoip${NC}."
fi
echo -e "[${GREEN}✓${NC}] Firewall successfully configured!"