-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrongswanvpn.setup.sh
582 lines (476 loc) · 17.7 KB
/
strongswanvpn.setup.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
#!/bin/bash
# strongSwan VPN server installer for Debian
#
# Copyright (C) 2022 Artem Yakovlev <[email protected]>
echo ""
echo "******************************************************************"
echo "*"
echo "* strongSwan VPN server installer for Debian *"
echo "*"
echo "* Copyright (C) 2022 Artem Yakovlev <[email protected]>"
echo "*"
echo "******************************************************************"
echo ""
if [ `id -u` -ne 0 ]
then
echo "Please start this script with root privileges!"
echo "Try again with sudo."
exit 0
fi
# *** Variables ***
CERT_CA=ca
SERVER_IP_ADDRESS=$(ip addr show eth0 | awk '$1 == "inet" {gsub(/\/.*$/, "", $2); print $2}')
SERVER_NAME=$(hostname | sed s/' '//g)
USER_NAME=user
USER_PASSWORD=12345
MOBILECONFIG_PATH=/etc/ipsec.d
MOBILECONFIG_SH=mobileconfig.sh
MOBILECONFIG_CONF=iphone.mobileconfig
SHELLWINDOWS_PATH=/etc/ipsec.d
SHELLWINDOWS_PS1=scriptwindows.ps1
# *****************
installPackagesVPNServer() {
apt-get update
apt-get upgrade
apt-get install wget
apt-get install strongswan
apt-get install libstrongswan-standard-plugins
apt-get install strongswan-pki
apt-get install libcharon-extra-plugins
apt-get install libcharon-extauth-plugins
apt-get install zsh
apt-get install iptables-persistent
wget -P /root https://raw.githubusercontent.com/artemyakovlev94/vpnstrongswan/main/mobileconfig.sh
# скачать файл скрипта powershell в /root
echo "StrongSwan VPN Server packages have been installed"
}
removePackagesVPNServer() {
apt-get remove wget
apt-get remove strongswan
apt-get remove libstrongswan-standard-plugins
apt-get remove strongswan-pki
apt-get remove libcharon-extra-plugins
apt-get remove libcharon-extauth-plugins
apt-get remove zsh
apt-get remove iptables-persistent
echo "StrongSwan VPN Server packages have been removed"
}
# Create certificate root CA
createCertificateCA() {
if [ -f "/etc/ipsec.d/private/$CERT_CA.pem" ]; then
cp -p -f /etc/ipsec.d/private/$CERT_CA.pem /etc/ipsec.d/private/$CERT_CA.pem.backup
rm /etc/ipsec.d/private/$CERT_CA.pem
fi
if [ -f "/etc/ipsec.d/cacerts/$CERT_CA.pem" ]; then
cp -p -f /etc/ipsec.d/cacerts/$CERT_CA.pem /etc/ipsec.d/cacerts/$CERT_CA.pem.backup
rm /etc/ipsec.d/cacerts/$CERT_CA.pem
fi
ipsec pki --gen --type rsa --size 4096 --outform pem > /etc/ipsec.d/private/$CERT_CA.pem
ipsec pki --self --ca --lifetime 3650 --in /etc/ipsec.d/private/$CERT_CA.pem \
--type rsa --digest sha256 \
--dn "CN=$SERVER_IP_ADDRESS" \
--outform pem > /etc/ipsec.d/cacerts/$CERT_CA.pem
echo "StrongSwan VPN server CA root certificate has been created"
}
# Create certificate strongSwan VPN server
createCertificateServer() {
if [ -f "/etc/ipsec.d/private/$SERVER_NAME.pem" ]; then
cp -p -f /etc/ipsec.d/private/$SERVER_NAME.pem /etc/ipsec.d/private/$SERVER_NAME.pem.backup
rm /etc/ipsec.d/private/$SERVER_NAME.pem
fi
if [ -f "/etc/ipsec.d/certs/$SERVER_NAME.pem" ]; then
cp -p -f /etc/ipsec.d/certs/$SERVER_NAME.pem /etc/ipsec.d/certs/$SERVER_NAME.pem.backup
rm /etc/ipsec.d/certs/$SERVER_NAME.pem
fi
ipsec pki --gen --type rsa --size 4096 --outform pem > /etc/ipsec.d/private/$SERVER_NAME.pem
ipsec pki --pub --in /etc/ipsec.d/private/$SERVER_NAME.pem --type rsa |
ipsec pki --issue --lifetime 3650 --digest sha256 \
--cacert /etc/ipsec.d/cacerts/$CERT_CA.pem \
--cakey /etc/ipsec.d/private/$CERT_CA.pem \
--dn "CN=$SERVER_IP_ADDRESS" \
--san $SERVER_IP_ADDRESS \
--flag serverAuth \
--outform pem > /etc/ipsec.d/certs/$SERVER_NAME.pem
echo "The strongSwan VPN server certificate has been created"
}
# Create user certificate strongSwan VPN server
createCertificateUser() {
if [ -f "/etc/ipsec.d/private/$USER_NAME.pem" ]; then
cp -p -f /etc/ipsec.d/private/$USER_NAME.pem /etc/ipsec.d/private/$USER_NAME.pem.backup
rm /etc/ipsec.d/private/$USER_NAME.pem
fi
if [ -f "/etc/ipsec.d/certs/$USER_NAME.pem" ]; then
cp -p -f /etc/ipsec.d/certs/$USER_NAME.pem /etc/ipsec.d/certs/$USER_NAME.pem.backup
rm /etc/ipsec.d/certs/$USER_NAME.pem
fi
ipsec pki --gen --type rsa --size 4096 --outform pem > /etc/ipsec.d/private/$USER_NAME.pem
ipsec pki --pub --in /etc/ipsec.d/private/$USER_NAME.pem --type rsa |
ipsec pki --issue --lifetime 3650 --digest sha256 \
--cacert /etc/ipsec.d/cacerts/$CERT_CA.pem \
--cakey /etc/ipsec.d/private/$CERT_CA.pem \
--dn "CN=$USER_NAME" \
--san $USER_NAME \
--flag clientAuth \
--outform pem > /etc/ipsec.d/certs/$USER_NAME.pem
echo "StrongSwan VPN server user certificate created"
}
# Edit ipsec.conf
editIPSecConfig() {
if [ -f "/etc/ipsec.conf" ]; then
cp -p -f /etc/ipsec.conf /etc/ipsec.conf.backup
fi
cat > /etc/ipsec.conf <<EOF
config setup
uniqueids=never
charondebug="ike 2, knl 2, cfg 2, net 2, esp 2, dmn 2, mgr 2"
conn %default
auto=add
keyexchange=ikev2
type=tunnel
ike=aes128gcm16-sha2_256-prfsha256-ecp256,chacha20poly1305-sha512-curve25519-prfsha512,aes256gcm16-sha384-prfsha384-ecp384,aes256-sha1-modp1024,aes128-sha1-modp1024,3des-sha1-modp1024!
esp=aes128gcm16-sha2_256-ecp256,chacha20poly1305-sha512,aes256gcm16-ecp384,aes256-sha256,aes256-sha1,3des-sha1!
fragmentation=yes
rekey=no
dpddelay=300s
compress=yes
dpdaction=clear
left=%any
leftauth=pubkey
leftsourceip=$SERVER_IP_ADDRESS
leftid=$SERVER_IP_ADDRESS
leftcert=$SERVER_NAME.pem
leftsendcert=always
leftsubnet=0.0.0.0/0
right=%any
rightsourceip=10.10.10.0/24
rightdns=8.8.8.8,8.8.4.4
eap_identity=%identity
conn ikev2-pubkey
rightauth=pubkey
conn ikev2-eap-mschapv2
rightauth=eap-mschapv2
EOF
echo "ipsec.conf has been edited"
}
# Edit ipsec.secrets
editIPSecSecrets() {
if [ -f "/etc/ipsec.secrets" ]; then
cp -p -f /etc/ipsec.secrets /etc/ipsec.secrets.backup
fi
cat > /etc/ipsec.secrets <<EOF
# This file holds shared secrets or RSA private keys for authentication.
# RSA private key for this host, authenticating it to any other host
# which knows the public part. Suitable public keys, for ipsec.conf, DNS,
# or configuration of other implementations, can be extracted conveniently
# with "ipsec showhostkey".
#####################################################
: RSA "$SERVER_NAME.pem"
EOF
echo "ipsec.secrets has been edited"
}
# Edit sysctl.conf
editSysctlConf() {
if [ -f "/etc/sysctl.conf" ]; then
cp -p -f /etc/sysctl.conf /etc/sysctl.conf.backup
fi
sed -i "/#net.ipv4.ip_forward=1/d" /etc/sysctl.conf
sed -i "/#net.ipv4.conf.all.accept_redirects = 0/d" /etc/sysctl.conf
sed -i "/#net.ipv4.conf.all.send_redirects = 0/d" /etc/sysctl.conf
sed -i "/#net.ipv4.ip_no_pmtu_disc=1/d" /etc/sysctl.conf
sed -i "/net.ipv4.ip_forward=1/d" /etc/sysctl.conf
sed -i "/net.ipv4.conf.all.accept_redirects = 0/d" /etc/sysctl.conf
sed -i "/net.ipv4.conf.all.send_redirects = 0/d" /etc/sysctl.conf
sed -i "/net.ipv4.ip_no_pmtu_disc=1/d" /etc/sysctl.conf
# включить переадресацию пакетов
echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
# предотвратить MITM-атаки
echo "net.ipv4.conf.all.accept_redirects = 0" >> /etc/sysctl.conf
# запретить отправку ICMP-редиректов
echo "net.ipv4.conf.all.send_redirects = 0" >> /etc/sysctl.conf
# запретить поиск PMTU
echo "net.ipv4.ip_no_pmtu_disc=1" >> /etc/sysctl.conf
# Подгрузим новые значения:
sysctl -p
echo "sysctl.conf has been edited"
}
# Edit Firewall
editFirewall() {
# Очистим все цепочки
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -F
iptables -Z
# Разрешим соединения по SSH на 22 порту, чтобы не потерять доступ к машине:
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# Разрешим соединения на loopback-интерфейсе:
iptables -A INPUT -i lo -j ACCEPT
# Теперь разрешим входящие соединения на UDP-портах 500 и 4500:
iptables -A INPUT -p udp --dport 500 -j ACCEPT
iptables -A INPUT -p udp --dport 4500 -j ACCEPT
# Разрешим переадресацию ESP-трафика:
iptables -A FORWARD --match policy --pol ipsec --dir in --proto esp -s 10.10.10.0/24 -j ACCEPT
iptables -A FORWARD --match policy --pol ipsec --dir out --proto esp -d 10.10.10.0/24 -j ACCEPT
# Настроим маскирование трафика, так как наш VPN-сервер, по сути, выступает как шлюз между Интернетом и VPN-клиентами:
iptables -t nat -A POSTROUTING -s 10.10.10.0/24 -o eth0 -m policy --pol ipsec --dir out -j ACCEPT
iptables -t nat -A POSTROUTING -s 10.10.10.0/24 -o eth0 -j MASQUERADE
# Настроим максимальный размер сегмента пакетов:
iptables -t mangle -A FORWARD --match policy --pol ipsec --dir in -s 10.10.10.0/24 -o eth0 -p tcp -m tcp --tcp-flags SYN,RST SYN -m tcpmss --mss 1361:1536 -j TCPMSS --set-mss 1360
# Запретим все прочие соединения к серверу:
iptables -A INPUT -j DROP
iptables -A FORWARD -j DROP
# Сохраним правила, чтобы они загружались после каждой перезагрузки:
netfilter-persistent save
netfilter-persistent reload
echo "firewall has been edited"
}
setIPAddressThisServer() {
while true; do
read -p "Enter the IP address of this server: " SERVER_IP_ADDRESS
if [ "$SERVER_IP_ADDRESS" != "" ]; then
if [ "$SERVER_IP_ADDRESS" = "exit" ]; then
exit 0;
else
read -p "Confirm the IP address of this server [$SERVER_IP_ADDRESS] [Y/n] " yn
case $yn in
[Yy]* ) break;;
[Nn]* ) echo "Enter the IP address of this server or enter 'exit' to exit the installer";;
* ) echo "Please answer with Yes or No [Y/n].";;
esac
fi
fi
done
}
setHostnameThisServer() {
while true; do
read -p "Enter the hostname of this server: " SERVER_NAME
if [ "$SERVER_NAME" != "" ]; then
if [ "$SERVER_NAME" = "exit" ]; then
exit 0;
else
read -p "Confirm the hostname of this server [$SERVER_NAME] [Y/n] " yn
case $yn in
[Yy]* ) break;;
[Nn]* ) echo "Enter the hostname of this server or enter 'exit' to exit the installer";;
* ) echo "Please answer with Yes or No [Y/n].";;
esac
fi
fi
done
}
installStrongSwanVPNServer() {
# Подтверждение установки strongSwan VPN сервера
read -p "Do you want to install strongSwan VPN server? [Y/n] " yn
case $yn in
[Yy]* ) break;;
[Nn]* ) return;;
* ) echo "Please answer with Yes or No [Y/n]";;
esac
read -p "Confirm the IP address of this server [$SERVER_IP_ADDRESS] [Y/n] " yn
case $yn in
[Yy]* ) echo "IP address of this server $SERVER_IP_ADDRESS";;
[Nn]* ) setIPAddressThisServer;;
* ) echo "Please answer with Yes or No [Y/n]";;
esac
read -p "Confirm the hostname of this server [$SERVER_NAME] [Y/n] " yn
case $yn in
[Yy]* ) echo "Hostname of this server $SERVER_NAME";;
[Nn]* ) setHostnameThisServer;;
* ) echo "Please answer with Yes or No [Y/n]";;
esac
hostnamectl set-hostname $SERVER_NAME
# Установить необходимые пакеты для strongSwan VPN сервера
installPackagesVPNServer
# Создать корневой сертификат центра сертификации strongSwan VPN сервера
createCertificateCA
# Создать сертификат strongSwan VPN сервера
createCertificateServer
# Отредактировать конфигурацию strongSwan VPN сервера (ipsec.conf)
editIPSecConfig
# Отредактировать файл ключей strongSwan VPN сервера (ipsec.secrets)
editIPSecSecrets
# Перезапустить strongSwan
ipsec restart
# Отредактировать сетевые параметры ядра strongSwan VPN сервера (sysctl.conf)
editSysctlConf
# Внесем изменения в Firewall
editFirewall
echo "The strongSwan VPN server has been installed. A server reboot is required to continue."
# Подтверждение перезапуска сервера
read -p "Reboot the server? [Y/n]" yn
case $yn in
[Yy]* ) reboot;;
[Nn]* ) return;;
* ) echo "Please answer with Yes or No [Y/n]";;
esac
}
showCARootCertificate() {
if [ -f "/etc/ipsec.d/cacerts/$CERT_CA.pem" ]; then
cat /etc/ipsec.d/cacerts/$CERT_CA.pem
else
echo "CA root certificate doesn't exist"
fi
}
# Добавить пользователя strongSwan VPN сервера
addVPNUser() {
# Ввод имени нового пользователя
while true; do
read -p "Enter username: " USER_NAME
if [ "$USER_NAME" != "" ]; then
if [ "$USER_NAME" = "exit" ]; then
return
else
if [ -f "/etc/ipsec.d/private/$USER_NAME.pem" ] || [ -f "/etc/ipsec.d/certs/$USER_NAME.pem" ]; then
echo "A user with the same name already exists"
else
read -p "Confirm new user [$USER_NAME] [Y/n] " yn
case $yn in
[Yy]* ) break;;
[Nn]* ) echo "Retype username or enter \"exit\" to cancel";;
* ) echo "Please answer with Yes or No [Y/n].";;
esac
fi
fi
fi
done
# Ввод пароля нового пользователя
while true; do
read -p "Input user password: " USER_PASSWORD
if [ "$USER_PASSWORD" != "" ]; then
if [ "$USER_PASSWORD" = "exit" ]; then
return
else
read -p "Confirm password [$USER_PASSWORD] [Y/n] " yn
case $yn in
[Yy]* ) break;;
[Nn]* ) echo "Retype username or enter \"exit\" to cancel";;
* ) echo "Please answer with Yes or No [Y/n].";;
esac
fi
fi
done
createCertificateUser
echo "$USER_NAME : EAP \"$USER_PASSWORD\"" >> /etc/ipsec.secrets
# Перезапустить strongSwan
ipsec restart
echo "User \"$USER_NAME\" has been created"
}
# Удалить пользователя strongSwan VPN сервера
deleteVPNUser() {
# Ввод имени пользователя
while true; do
read -p "Enter username: " USER_NAME
if [ "$USER_NAME" != "" ]; then
if [ "$USER_NAME" = "exit" ]; then
return
else
read -p "Confirm delete user [$USER_NAME] [Y/n] " yn
case $yn in
[Yy]* ) break;;
[Nn]* ) echo "Retype username or enter \"exit\" to cancel";;
* ) echo "Please answer with Yes or No [Y/n].";;
esac
fi
fi
done
if [ -f "/etc/ipsec.d/private/$USER_NAME.pem" ]; then
cp -p -f /etc/ipsec.d/private/$USER_NAME.pem /etc/ipsec.d/private/$USER_NAME.pem.backup
rm /etc/ipsec.d/private/$USER_NAME.pem
fi
if [ -f "/etc/ipsec.d/certs/$USER_NAME.pem" ]; then
cp -p -f /etc/ipsec.d/certs/$USER_NAME.pem /etc/ipsec.d/certs/$USER_NAME.pem.backup
rm /etc/ipsec.d/certs/$USER_NAME.pem
fi
sed -i "/$USER_NAME/d" /etc/ipsec.secrets
echo "User [$USER_NAME] has been deleted"
}
# Show VPN Users and Passwords
showVPNUsers() {
echo ""
echo "=========== VPN Users ==========="
echo ""
grep -i " : EAP " /etc/ipsec.secrets
echo ""
echo "================================="
echo ""
}
# Сформировать профиль конфигурации VPN для iPhone
getVPNProfileIPhone() {
while true; do
read -p "Enter username: " USER_NAME
if [ "$USER_NAME" != "" ]; then
if [ "$USER_NAME" = "exit" ]; then
return
else
if [ -f "/etc/ipsec.d/private/$USER_NAME.pem" ] && [ -f "/etc/ipsec.d/certs/$USER_NAME.pem" ]; then
read -p "Confirm username [$USER_NAME] [Y/n] " yn
case $yn in
[Yy]* ) break;;
[Nn]* ) echo "Enter username or enter \"exit\" to cancel";;
* ) echo "Please answer with Yes or No [Y/n].";;
esac
else
echo "User with this name does not exist"
fi
fi
fi
done
if [ -f "/root/mobileconfig.sh" ]; then
if [ -f "/root/n.mobileconfig.sh" ]; then
rm /root/n.mobileconfig.sh
fi
cp -p -f /root/mobileconfig.sh /root/n.mobileconfig.sh
sed -i "s/CLIENT=\"client_name\"/CLIENT=\"$USER_NAME\"/" /root/n.mobileconfig.sh
sed -i "s/SERVER=\"server_name\"/SERVER=\"$SERVER_NAME\"/" /root/n.mobileconfig.sh
sed -i "s/FQDN=\"server_ip\"/FQDN=\"$SERVER_IP_ADDRESS\"/" /root/n.mobileconfig.sh
sed -i "s/CA=\"ca\"/CA=\"$CERT_CA\"/" /root/n.mobileconfig.sh
chmod u+x /root/n.mobileconfig.sh
/root/n.mobileconfig.sh > /root/ios.mobileconfig
echo ""
echo "########### [ Скопируйте блок ниже ] ##########"
echo "############### [ BEGIN BLOCK ] ###############"
echo ""
echo "###############################################"
echo ""
cat /root/ios.mobileconfig
echo ""
echo "###############################################"
echo ""
echo "################ [ END BLOCK ] ################"
echo ""
if [ -f "/root/n.mobileconfig.sh" ]; then
rm /root/n.mobileconfig.sh
fi
if [ -f "/root/ios.mobileconfig" ]; then
rm /root/ios.mobileconfig
fi
else
echo "Не удалось создать профиль для iPhone"
fi
}
while true; do
echo ""
echo "*******************************************"
echo "* 1 - Install strongSwan VPN server"
echo "* 2 - Restart strongSwan"
echo "* 3 - Show VPN Server CA Root Certificate"
echo "* 4 - Add strongSwan VPN server user"
echo "* 5 - Delete strongSwan VPN server user"
echo "* 6 - Show VPN Users and Passwords"
echo "* 7 - Get VPN Configuration Profile for iPhone"
echo "* 0 - Exit setup"
echo "*******************************************"
read -p "" yn
case $yn in
[1]* ) installStrongSwanVPNServer;;
[2]* ) ipsec restart;;
[3]* ) showCARootCertificate;;
[4]* ) addVPNUser;;
[5]* ) deleteVPNUser;;
[6]* ) showVPNUsers;;
[7]* ) getVPNProfileIPhone;;
[0]* ) break;;
* ) echo "Select a menu item.";;
esac
done
echo "strongSwan VPN server installer for Debian has been closed"