-
Notifications
You must be signed in to change notification settings - Fork 0
/
ufw_geo_blocks.sh
207 lines (176 loc) · 4.55 KB
/
ufw_geo_blocks.sh
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
201
202
203
204
205
206
207
#!/bin/bash
#set -x
# Function to add firewall rule
function add_ufw_rule() {
local ip=$1
local port=$2
local rule=$3
local protocol=$4
# Check rule
if [ "$rule" == "deny" ]; then
# Add deny rule with protocol
sudo ufw deny from "$ip" to any port "$port" proto "$protocol"
elif [ "$rule" == "allow" ]; then
# Add allow rule with protocol
sudo ufw allow from "$ip" to any port "$port" proto "$protocol"
elif [ "$rule" == "delete" ]; then
# Delete IP rule
sudo ufw delete deny from "$ip" to any port "$port" proto "$protocol"
sudo ufw delete allow from "$ip" to any port "$port" proto "$protocol"
else
echo "Invalid rule: $rule"
return 1
fi
return 0
}
# Parse arguments
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-d|--deny)
rule="deny"
shift
;;
-a|--allow)
rule="allow"
shift
;;
-D|--delete)
rule="delete"
shift
;;
-p|--port)
port="$2"
shift
shift
;;
-c|--country)
country_code="$2"
shift
shift
;;
*)
echo "Unknown parameter $key"
exit 1
;;
esac
done
# Validate required arguments
if [ -z "$rule" ]; then
echo "Missing required rule"
exit 1
fi
if [ -z "$country_code" ]; then
echo "Missing country code"
exit 1
fi
# Determine protocol
protocol="tcp"
if [[ $port =~ .*/udp$ ]]; then
protocol="udp"
fi
# Parse port range
if [[ $port =~ ^([0-9]+):([0-9]+)/(tcp|udp)$ ]]; then
start_port=${BASH_REMATCH[1]}
end_port=${BASH_REMATCH[2]}
# Validate format
if ! [[ $port =~ ^[0-9]+:[0-9]+/(tcp|udp)$ ]]; then
echo "Invalid port format"
exit 1
fi
else
# Single port
start_port="$port"
end_port="$port"
fi
# Add or delete firewall rules
while read ip; do
if [ -z "$start_port" ]; then
# No port specified, apply rules based on IP address
if [ "$rule" == "delete" ]; then
sudo ufw delete deny from "$ip"
sudo ufw delete allow from "$ip"
elif [ "$rule" == "deny" ]; then
sudo ufw deny from "$ip"
elif [ "$rule" == "allow" ]; then
sudo ufw allow from "$ip"
fi
else
# Port(s) specified, apply rules based on IP address and port
for p in $(seq "$start_port" "$end_port"); do
if [ "$rule" == "delete" ]; then
sudo ufw delete deny from "$ip" to any port "$p" proto "$protocol"
sudo ufw delete allow from "$ip" to any port "$p" proto "$protocol"
elif [ "$rule" == "deny" ]; then
sudo ufw deny from "$ip" to any port "$p" proto "$protocol"
elif [ "$rule" == "allow" ]; then
sudo ufw allow from "$ip" to any port "$p" proto "$protocol"
fi
done
fi
done < "$ip_file"
# Generate IP file path
ip_file="$country_code.zone"
# Download IP file
wget -O "$ip_file" "http://www.ipdeny.com/ipblocks/data/countries/$ip_file"
# Check if download failed
if [ ! -s "$ip_file" ]; then
echo "Failed to download IP file"
exit 1
fi
# Determine protocol
protocol="tcp"
if [[ $port =~ .*/udp$ ]]; then
protocol="udp"
fi
if [ -z "$port" ]; then
# No port specified, just apply rules based on IP address
start_port=""
end_port=""
else
# Parse port range
if [[ $port =~ ^([0-9]+):([0-9]+)/(tcp|udp)$ ]]; then
start_port=${BASH_REMATCH[1]}
end_port=${BASH_REMATCH[2]}
# Validate format
if ! [[ $port =~ ^[0-9]+:[0-9]+/(tcp|udp)$ ]]; then
echo "Invalid port format"
exit 1
fi
else
# Single port
start_port="$port"
end_port="$port"
fi
fi
# Add or delete firewall rules
while read ip; do
if [ -z "$start_port" ]; then
# No port specified, apply rules based on IP address
if [ "$rule" == "delete" ]; then
sudo ufw delete deny from "$ip"
sudo ufw delete allow from "$ip"
elif [ "$rule" == "deny" ]; then
sudo ufw deny from "$ip"
elif [ "$rule" == "allow" ]; then
sudo ufw allow from "$ip"
fi
else
# Port(s) specified, apply rules based on IP address and port
if [ -n "$start_port" ] && [ -n "$end_port" ]; then
for p in $(seq "$start_port" "$end_port"); do
if [ "$rule" == "delete" ]; then
sudo ufw delete deny from "$ip" to any port "$p" proto "$protocol"
sudo ufw delete allow from "$ip" to any port "$p" proto "$protocol"
elif [ "$rule" == "deny" ]; then
sudo ufw deny from "$ip" to any port "$p" proto "$protocol"
elif [ "$rule" == "allow" ]; then
sudo ufw allow from "$ip" to any port "$p" proto "$protocol"
fi
done
fi
fi
done < "$ip_file"
# Clean up temp files
rm "$ip_file"
echo "Done!"