-
Notifications
You must be signed in to change notification settings - Fork 9
/
setup_iptables.sh
416 lines (325 loc) · 13.6 KB
/
setup_iptables.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#!/usr/bin/env bash
if [ "$EUID" -ne 0 ]; then
echo "Please run the script as root."
exit 1
fi
my_wgl_folder="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. >/dev/null 2>&1 && pwd )"
source "$my_wgl_folder"/doc/functions.sh
# Setting the colours function
colours
############## Determine OS Type ##############
###############################################
ubuntu_os=$(lsb_release -a | grep -i -c Ubuntu)
arch_os=$(lsb_release -a | grep -c Arch)
cent_os=$(hostnamectl | grep -i -c CentOS)
debian_os=$(lsb_release -a | grep -i -c Debian)
fedora_os=$(lsb_release -a | grep -i -c Fedora)
manjaro_os=$(lsb_release -a | grep -i -c Manjaro)
if [[ "$ubuntu_os" -gt 0 ]]; then
distro=ubuntu
elif [[ "$arch_os" -gt 0 ]]; then
distro=arch
elif [[ "$cent_os" -gt 0 ]]; then
distro=centos
elif [[ "$debian_os" -gt 0 ]]; then
distro=debian
elif [[ "$fedora_os" -gt 0 ]]; then
distro=fedora
elif [[ "$manjaro_os" -gt 0 ]]; then
distro=manjaro
fi
###############################################
###############################################
my_separator="--------------------------------------"
check_pub_ip=$(curl -s https://checkip.amazonaws.com)
printf '\e[2J\e[H'
echo -e "
${IWhite}We are going to setup some basic firewwall rules so the server can
function correctly.
Step 1) Please provide the server subnet information to be used.${Color_Off}
${BWhite}Example:${IWhite} If you server IP is ${BRed}10.0.0.1/24${IWhite}, then please type ${BRed}10.0.0.0/24${Color_Off}
"
read -r -p "Server subnet: " server_subnet
printf '\e[2J\e[H'
echo -e "
+--------------------------------------------+
${BWhite}Server subnet = ${BRed}$server_subnet${Color_Off}
+--------------------------------------------+
${IWhite}Step 2) Please also provide the listen port of your server.${Color_Off}
${BWhite}Example: ${BRed}51820${Color_Off}"
read -r -p "Server listen port: " listen_port
printf '\e[2J\e[H'
# Public IP address of the server hosting the WireGuard server
echo -e "
+--------------------------------------------+
${BWhite}Server subnet = ${BRed}$server_subnet${Color_Off}
${BWhite}Server port = ${BRed}$listen_port${Color_Off}
+--------------------------------------------+
${BWhite}Step 3)${Color_Off} ${IWhite}The public IP address of this machine is $check_pub_ip.
Is this the address you would like to use? ${Color_Off}
${BWhite}1 = yes, 2 = no${Color_Off}"
read -r -p "Choice: " public_address
echo -e "$my_separator"
if [[ "$public_address" == 1 ]]; then
server_public_address="$check_pub_ip"
elif [[ "$public_address" == 2 ]]; then
echo -e "
${IWhite}Please specify the public address of the server.${Color_Off}
"
read -r -p "Public IP: " server_public_address
fi
printf '\e[2J\e[H'
echo -e "
+--------------------------------------------+
${BWhite}Server subnet = ${BRed}$server_subnet${Color_Off}
${BWhite}Server port = ${BRed}$listen_port${Color_Off}
${BWhite}Server public address = ${BRed}$server_public_address${Color_Off}
+--------------------------------------------+
${BWhite}Step 4)${IWhite} Please also provide the internet facing interface of the server.
${BWhite}Example: ${BRed}eth0${Color_Off}
Available interfaces are:
+--------------------+
$(ip -br a | awk '{print $1}')
+--------------------+
"
read -r -p "Interface: " local_interface
printf '\e[2J\e[H'
echo -e "
+--------------------------------------------+
${BWhite}Server subnet = ${BRed}$server_subnet${Color_Off}
${BWhite}Server port = ${BRed}$listen_port${Color_Off}
${BWhite}Server public address = ${BRed}$server_public_address${Color_Off}
${BWhite}WAN Interface = ${BRed}$local_interface${Color_Off}
+--------------------------------------------+
${IWhite}Step 5) Specify wireguard server interface name
(will be the same as config name, without .conf)${Color_Off}
"
read -r -p "WireGuard Interface: " wg_serv_iface
printf '\e[2J\e[H'
if [[ "$cent_os" -gt 0 ]]; then
check_if_firewalld_installed=$(yum list installed | grep -i -c firewalld)
if [[ "$check_if_firewalld_installed" == 1 ]]; then
echo -e "
${IWhite}
OS Type: CentOS
Firewalld: installed
The following firewall rules will be configured:${Color_Off}
${IYellow}
firewall-cmd --zone=public --add-port=$listen_port/udp
firewall-cmd --zone=trusted --add-source=$server_subnet
firewall-cmd --permanent --zone=public --add-port=$listen_port/udp
firewall-cmd --permanent --zone=trusted --add-source=$server_subnet
firewall-cmd --direct --add-rule ipv4 nat POSTROUTING 0 -s $server_subnet ! -d $server_subnet -j SNAT --to $server_public_address
firewall-cmd --permanent --direct --add-rule ipv4 nat POSTROUTING 0 -s $server_subnet ! -d $server_subnet -j SNAT --to $server_public_address
${Color_Off}
# Enabling IP forwarding
# In /etc/sysctl.conf, net.ipv4.ip_forward value will be changed to 1:
${IYellow}net.ipv4.ip_forward=1${Color_Off}
#To avoid the need to reboot the server
${IYellow}sysctl -p${Color_Off}
"
read -n 1 -s -r -p "
Review the above.
Press any key to continue
Press r/R to restart the script
Press e/E to exit
" your_choice
case "$your_choice" in
[Rr]*)
sudo bash "$my_wgl_folder"/Scripts/setup_iptables.sh
;;
[Ee]*)
exit
;;
*)
sed -i 's/net.ipv4.ip_forward=0//g' /etc/sysctl.conf
sed -i 's/#net.ipv4.ip_forward=0//g' /etc/sysctl.conf
sed -i 's/#net.ipv4.ip_forward=1//g' /etc/sysctl.conf
echo "net.ipv4.ip_forward=1" >>/etc/sysctl.conf
sysctl -p
firewall-cmd --zone=public --add-port="$listen_port"/udp
firewall-cmd --zone=trusted --add-source="$server_subnet"
firewall-cmd --permanent --zone=public --add-port="$listen_port"/udp
firewall-cmd --permanent --zone=trusted --add-source="$server_subnet"
firewall-cmd --direct --add-rule ipv4 nat POSTROUTING 0 -s "$server_subnet" ! -d "$server_subnet" -j SNAT --to "$server_public_address"
firewall-cmd --permanent --direct --add-rule ipv4 nat POSTROUTING 0 -s "$server_subnet" ! -d "$server_subnet" -j SNAT --to "$server_public_address"
echo -e "${BWhite}Done!${Color_Off}"
;;
esac
elif [[ "$check_if_firewalld_installed" == 0 ]]; then
echo -e "
OS Type: CentOS
Firewalld: NOT installed - using iptables
The following firewall rules will be configured:${Color_Off}"
echo -e "
${IWhite}The following iptables will be configured:${Color_Off}
# Track VPN connection
${IYellow}iptables -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT${Color_Off}
# Allow incoming traffic on a specified port
${IYellow}iptables -A INPUT -p udp -m udp --dport ${BRed}$listen_port ${IYellow}-m conntrack --ctstate NEW -j ACCEPT${Color_Off}
#Forward packets in the VPN tunnel
${IYellow}iptables -A FORWARD -i ${BRed}$wg_serv_iface${IYellow} -o ${BRed}$wg_serv_iface ${IYellow}-m conntrack --ctstate NEW -j ACCEPT${Color_Off}
# Enable NAT
${IYellow}iptables -t nat -A POSTROUTING -s ${BRed}$server_subnet ${IYellow}-o $local_interface -j MASQUERADE${Color_Off}
In addition to setting up iptables, the following commands will be executed:
# Enabling IP forwarding
# In /etc/sysctl.conf, net.ipv4.ip_forward value will be changed to 1:
${IYellow}net.ipv4.ip_forward=1${Color_Off}
#To avoid the need to reboot the server
${IYellow}sysctl -p${Color_Off}
-------------------------------------------
"
read -n 1 -s -r -p "
Review the above.
Press any key to continue
Press r/R to restart the script
Press e/E to exit
" your_choice
case "$your_choice" in
[Rr]*)
sudo bash "$my_wgl_folder"/Scripts/setup_iptables.sh
;;
[Ee]*)
exit
;;
*)
sed -i 's/net.ipv4.ip_forward=0//g' /etc/sysctl.conf
sed -i 's/#net.ipv4.ip_forward=0//g' /etc/sysctl.conf
sed -i 's/#net.ipv4.ip_forward=1//g' /etc/sysctl.conf
echo "net.ipv4.ip_forward=1" >>/etc/sysctl.conf
sysctl -p
iptables -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
iptables -A INPUT -p udp -m udp --dport "$listen_port" -m conntrack --ctstate NEW -j ACCEPT
iptables -A FORWARD -i "$wg_serv_iface" -o "$wg_serv_iface" -m conntrack --ctstate NEW -j ACCEPT
iptables -t nat -A POSTROUTING -s "$server_subnet" -o "$local_interface" -j MASQUERADE
echo -e "${BWhite}Done!${Color_Off}"
;;
esac
fi
else
echo -e "
${IWhite}The following iptables will be configured:${Color_Off}
# Track VPN connection
${IYellow}iptables -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT${Color_Off}
# Allow incoming traffic on a specified port
${IYellow}iptables -A INPUT -p udp -m udp --dport ${BRed}$listen_port ${IYellow}-m conntrack --ctstate NEW -j ACCEPT${Color_Off}
#Forward packets in the VPN tunnel
${IYellow}iptables -A FORWARD -i ${BRed}$wg_serv_iface${IYellow} -o ${BRed}$wg_serv_iface ${IYellow}-m conntrack --ctstate NEW -j ACCEPT${Color_Off}
# Enable NAT
${IYellow}iptables -t nat -A POSTROUTING -s ${BRed}$server_subnet ${IYellow}-o ${BRed}$local_interface ${IYellow}-j MASQUERADE${Color_Off}
In addition to setting up iptables, the following commands will be executed:
# Enabling IP forwarding
# In /etc/sysctl.conf, net.ipv4.ip_forward value will be changed to 1:
${IYellow}net.ipv4.ip_forward=1${Color_Off}
#To avoid the need to reboot the server
${IYellow}sysctl -p${Color_Off}
-------------------------------------------
"
read -n 1 -s -r -p "
Review the above.
Press any key to continue
Press r/R to restart the script
Press e/E to exit
" your_choice
case "$your_choice" in
[Rr]*)
sudo bash "$my_wgl_folder"/Scripts/setup_iptables.sh
;;
[Ee]*)
exit
;;
*)
sed -i 's/net.ipv4.ip_forward=0/net.ipv4.ip_forward=1/g' /etc/sysctl.conf
sed -i 's/#net.ipv4.ip_forward=0/net.ipv4.ip_forward=1/g' /etc/sysctl.conf
sed -i 's/#net.ipv4.ip_forward=1/net.ipv4.ip_forward=1/g' /etc/sysctl.conf
sysctl -p
iptables -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
iptables -A INPUT -p udp -m udp --dport "$listen_port" -m conntrack --ctstate NEW -j ACCEPT
iptables -A FORWARD -i "$wg_serv_iface" -o "$wg_serv_iface" -m conntrack --ctstate NEW -j ACCEPT
iptables -t nat -A POSTROUTING -s "$server_subnet" -o "$local_interface" -j MASQUERADE
echo -e "${BWhite}Done!${Color_Off}"
;;
esac
fi
if [[ "$distro" == "ubuntu" ]] || [[ "$distro" == "debian" ]]; then
echo -e "
${IWhite}In order to make the above iptables rules persistent after system reboot,
${BRed}iptables-persistent ${IWhite} package needs to be installed.
Would you like the script to install iptables-persistent and to enable the service?
${IWhite}Following commands would be used:
${IYellow}apt-get install iptables-persistent
systemctl enable netfilter-persistent
netfilter-persistent save${Color_Off}"
read -n 1 -s -r -p "
Review the above commands.
Press any key to continue or CTRL+C to stop."
apt-get install iptables-persistent
systemctl enable netfilter-persistent
netfilter-persistent save
elif [[ "$distro" == "fedora" ]]; then
echo -e "
${IWhite}In order to make the above iptables rules persistent after system reboot,
netfilter rules will need to be saved.
Would you like the script to save the netfilter rules?
${IWhite}Following commands would be used:
${IYellow}/sbin/service iptables save${Color_Off}"
read -n 1 -s -r -p "
Review the above commands.
Press any key to continue or CTRL+C to stop."
/sbin/service iptables save
elif [[ "$distro" == "arch" ]] || [[ "$distro" == "manjaro" ]]; then
echo -e "
${IWhite}In order to make the above iptables rules persistent after system reboot,
netfilter rules will need to be saved.
Would you like the script to save the netfilter rules?
${IWhite}Following commands would be used:
# Check if iptables.rules file exists and create if needed
${IYellow}check_iptables_rules=\$(ls /etc/iptables/ | grep -c iptables.rules)
if [[ \$check_iptables_rules == 0 ]]; then
touch /etc/iptables/iptables.rules
fi
systemctl enable iptables.service
systemctl start iptables.service
iptables-save > /etc/iptables/iptables.rules
systemctl restart iptables.service
${Color_Off}
"
read -n 1 -s -r -p "
Review the above commands.
Press any key to continue or CTRL+C to stop."
check_iptables_rules=$(ls /etc/iptables/ | grep -c iptables.rules)
if [[ "$check_iptables_rules" == 0 ]]; then
touch /etc/iptables/iptables.rules
fi
systemctl enable iptables.service
systemctl start iptables.service
iptables-save >/etc/iptables/iptables.rules
systemctl restart iptables.service
elif [[ "$distro" == "centos" ]]; then
if [[ "$check_if_firewalld_installed" == 0 ]]; then
echo -e "
${IWhite}In order to make the above iptables rules persistent after system reboot,
netfilter rules will need to be saved.
First, iptables-service needs to be intalled.
Would you like the script to install iptables-service and save the netfilter rules?
${IWhite}Following commands would be used:
${IYellow}
sudo yum install iptables-services
sudo systemctl enable iptables
sudo service iptables save
${Color_Off}
"
read -n 1 -s -r -p "
Review the above commands.
Press any key to continue or CTRL+C to stop."
sudo yum install iptables-services
sudo systemctl enable iptables
sudo service iptables save
fi
fi
echo -e "
Ending the script...."