-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVPN_CF_template.yaml
609 lines (590 loc) · 24.5 KB
/
VPN_CF_template.yaml
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
AWSTemplateFormatVersion: 2010-09-09
Description: CloudFormation template for creating VPN infrastructure
Parameters:
VPCCidrBlock:
Type: String
Default: 10.0.0.0/24
VPNUsername:
Description: VPN username
Type: String
MinLength: 1
MaxLength: 255
AllowedPattern: "[a-zA-Z][a-zA-Z0-9]*"
ConstraintDescription: Must begin with a letter and contain only alphanumeric characters
VPNPassword:
NoEcho: true
Description: VPN Password (Min 8 characters)
Type: String
MinLength: 8
MaxLength: 255
ConstraintDescription: Must contain at least 8 characters
VPNPhrase:
NoEcho: true
Description: Passphrase for IPSEC PSK (Min 8 characters)
Type: String
MinLength: 8
MaxLength: 255
ConstraintDescription: Must contain at least 8 characters
EC2AMIID:
Description: AMI ID for launching the EC2 instance
Type: "AWS::EC2::Image::Id"
EC2InstanceType:
Description: Type of EC2 instance
Type: String
Default: t2.nano
AllowedValues:
- t2.nano
- t2.micro
EC2SSHPort:
Description: Port for SSH
Type: Number
Default: 22022
MinValue: 1025
MaxValue: 49150
ConstraintDescription: Must be any value 1025 - 49150
EC2KeyName:
Description: Name of SSH key for EC2 instance
Type: "AWS::EC2::KeyPair::KeyName"
DNSDomainName:
Description: Name of Route53 Hosted Zone that will be used for adding VPN subdomain
Type: String
Default: ""
DNSSubDomainPrefix:
Description: Subdomain prefix for VPN (e.g. vpn)
Type: String
Default: ""
Mappings:
AWSRegionMapping:
us-east-1:
AZ: us-east-1f
us-east-2:
AZ: us-east-2c
us-west-1:
AZ: us-west-1c
us-west-2:
AZ: us-west-2c
eu-west-1:
AZ: eu-west-1c
eu-west-2:
AZ: eu-west-2b
eu-central-1:
AZ: eu-central-1c
ap-northeast-1:
AZ: ap-northeast-1c
ap-northeast-2:
AZ: ap-northeast-2c
ap-southeast-1:
AZ: ap-southeast-1b
ap-southeast-2:
AZ: ap-southeast-2b
sa-east-1:
AZ: sa-east-1c
ap-south-1:
AZ: ap-south-1b
ca-central-1:
AZ: ca-central-1b
Conditions:
ManageDNS: !Not [ !Equals [ !Ref DNSDomainName, "" ] ]
Resources:
VPC:
Type: "AWS::EC2::VPC"
Properties:
CidrBlock: !Ref VPCCidrBlock
EnableDnsSupport: true
EnableDnsHostnames: true
InternetGateway:
Type: "AWS::EC2::InternetGateway"
IGWAttachment:
Type: "AWS::EC2::VPCGatewayAttachment"
Properties:
InternetGatewayId: !Ref InternetGateway
VpcId: !Ref VPC
VPCSubnet:
Type: "AWS::EC2::Subnet"
Properties:
AvailabilityZone: !FindInMap [ AWSRegionMapping, !Ref "AWS::Region", AZ ]
CidrBlock: !Ref VPCCidrBlock
MapPublicIpOnLaunch: true
VpcId: !Ref VPC
VPCRouteTable:
Type: "AWS::EC2::RouteTable"
Properties:
VpcId: !Ref VPC
VPCRoute:
Type: "AWS::EC2::Route"
Properties:
RouteTableId: !Ref VPCRouteTable
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref InternetGateway
VPCSubnetAssociation:
Type: "AWS::EC2::SubnetRouteTableAssociation"
Properties:
RouteTableId: !Ref VPCRouteTable
SubnetId: !Ref VPCSubnet
VPNSecurityGroup:
Type: 'AWS::EC2::SecurityGroup'
Properties:
GroupDescription: VPN Security Group
SecurityGroupEgress:
- IpProtocol: -1
CidrIp: 0.0.0.0/0
Description: All outgoing traffic
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 500
ToPort: 500
CidrIp: 0.0.0.0/0
Description: Access to IPSEC
- IpProtocol: udp
FromPort: 500
ToPort: 500
CidrIp: 0.0.0.0/0
Description: Access to IPSEC
- IpProtocol: udp
FromPort: 4500
ToPort: 4500
CidrIp: 0.0.0.0/0
Description: Access to IPSEC
- IpProtocol: tcp
FromPort: !Ref EC2SSHPort
ToPort: !Ref EC2SSHPort
CidrIp: 0.0.0.0/0
Description: Access to SSH
VpcId: !Ref VPC
VPNServerInstance:
Type: 'AWS::EC2::Instance'
Properties:
AvailabilityZone: !FindInMap [ AWSRegionMapping, !Ref "AWS::Region", AZ ]
ImageId: !Ref EC2AMIID
InstanceType: !Ref EC2InstanceType
KeyName: !Ref EC2KeyName
SecurityGroupIds:
- !Ref VPNSecurityGroup
SubnetId: !Ref VPCSubnet
Tags:
- Key: Name
Value: vpn_server
UserData:
"Fn::Base64":
!Join [
"",
[
"#!/bin/bash\n",
"#\n",
"#Waiting AWS Elastic IP couple of minutes\n",
"/bin/sleep 180 \n",
"#Passing variables to shell\n",
"YOUR_IPSEC_PSK=",
!Ref VPNPhrase,
"\n",
"YOUR_USERNAME=",
!Ref VPNUsername,
"\n",
"YOUR_PASSWORD=",
!Ref VPNPassword,
"\n",
"SSH_PORT=",
!Ref EC2SSHPort,
"\n",
"echo $YOUR_IPSEC_PSK >> /home/test.txt\n",
"#Updating OS\n",
"apt-get -yq update\n",
"apt-get -yq upgrade\n",
"# VPN 1 - Setup L2TP-IPSEC\n",
"export PATH=\"/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\"\n",
"SYS_DT=\"$(date +%Y-%m-%d-%H:%M:%S)\"; export SYS_DT\n",
"\n",
"exiterr2() { echo \"Error: 'apt-get install' failed.\" >&2; exit 1; }\n",
"conf_bk() { /bin/cp -f \"$1\" \"$1.old-$SYS_DT\" 2>/dev/null; }\n",
"bigecho() { echo; echo \"## $1\"; echo; }\n",
"\n",
"check_ip() {\n",
" IP_REGEX=\"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$\"\n",
" printf %s \"$1\" | tr -d '\\n' | grep -Eq \"$IP_REGEX\"\n",
"}\n",
"\n",
"NET_IFACE=${VPN_NET_IFACE:-'eth0'}\n",
"DEF_IFACE=\"$(route | grep '^default' | grep -o '[^ ]*$')\"\n",
"\n",
"if_state1=$(cat \"/sys/class/net/$DEF_IFACE/operstate\" 2>/dev/null)\n",
"if [ -z \"$VPN_NET_IFACE\" ] && [ -n \"$if_state1\" ] && [ \"$if_state1\" != \"down\" ]; then\n",
" if ! grep -qs raspbian /etc/os-release; then\n",
" case \"$DEF_IFACE\" in\n",
" wlan*)\n",
"echo \"Incorrect network interface\"\n",
" exit 1\n",
" ;;\n",
" esac\n",
" fi\n",
" NET_IFACE=\"$DEF_IFACE\"\n",
"fi\n",
"\n",
"if_state2=$(cat \"/sys/class/net/$NET_IFACE/operstate\" 2>/dev/null)\n",
"if [ -z \"$if_state2\" ] || [ \"$if_state2\" = \"down\" ] || [ \"$NET_IFACE\" = \"lo\" ]; then\n",
" printf \"Error: Network interface '%s' is not available.\\n\\n\" \"$NET_IFACE\" >&2\n",
" if [ -z \"$VPN_NET_IFACE\" ]; then\n",
"echo \"Incorrect network interface\"\n",
" fi\n",
" exit 1\n",
"fi\n",
"\n",
"[ -n \"$YOUR_IPSEC_PSK\" ] && VPN_IPSEC_PSK=\"$YOUR_IPSEC_PSK\"\n",
"[ -n \"$YOUR_USERNAME\" ] && VPN_USER=\"$YOUR_USERNAME\"\n",
"[ -n \"$YOUR_PASSWORD\" ] && VPN_PASSWORD=\"$YOUR_PASSWORD\"\n",
"\n",
"if [ -z \"$VPN_IPSEC_PSK\" ] && [ -z \"$VPN_USER\" ] && [ -z \"$VPN_PASSWORD\" ]; then\n",
" bigecho \"VPN credentials not set by user. Generating random PSK and password...\"\n",
" VPN_IPSEC_PSK=\"$(LC_CTYPE=C tr -dc 'A-HJ-NPR-Za-km-z2-9' < /dev/urandom | head -c 16)\"\n",
" VPN_USER=vpnuser\n",
" VPN_PASSWORD=\"$(LC_CTYPE=C tr -dc 'A-HJ-NPR-Za-km-z2-9' < /dev/urandom | head -c 16)\"\n",
"fi\n",
"\n",
"if [ -z \"$VPN_IPSEC_PSK\" ] || [ -z \"$VPN_USER\" ] || [ -z \"$VPN_PASSWORD\" ]; then\n",
" exiterr \"All VPN credentials must be specified. Edit the script and re-enter them.\"\n",
"fi\n",
"\n",
"if printf %s \"$VPN_IPSEC_PSK $VPN_USER $VPN_PASSWORD\" | LC_ALL=C grep -q '[^ -~]\\+'; then\n",
" exiterr \"VPN credentials must not contain non-ASCII characters.\"\n",
"fi\n",
"\n",
"case \"$VPN_IPSEC_PSK $VPN_USER $VPN_PASSWORD\" in\n",
" *[\\\\\\\"\\']*)\n",
" exiterr \"VPN credentials must not contain the following characters: \\\\ \\\" '\"\n",
" ;;\n",
"esac\n",
"\n",
"# Create and change to working dir\n",
"mkdir -p /opt/src\n",
"cd /opt/src || exiterr \"Cannot enter /opt/src.\"\n",
"export DEBIAN_FRONTEND=noninteractive\n",
"apt-get -yq update || exiterr \"'apt-get update' failed.\"\n",
"apt-get -yq install wget dnsutils openssl || exiterr2\n",
"apt-get -yq install iproute gawk grep sed net-tools || exiterr2\n",
"\n",
"# In case auto IP discovery fails, enter server's public IP here.\n",
"PUBLIC_IP=${VPN_PUBLIC_IP:-''}\n",
"# Try to auto discover IP of this server\n",
"[ -z \"$PUBLIC_IP\" ] && PUBLIC_IP=$(dig @resolver1.opendns.com -t A -4 myip.opendns.com +short)\n",
"# Check IP for correct format\n",
"check_ip \"$PUBLIC_IP\" || PUBLIC_IP=$(wget -t 3 -T 15 -qO- http://ipv4.icanhazip.com)\n",
"check_ip \"$PUBLIC_IP\" || exiterr \"Cannot find valid public IP. Edit the script and manually enter it.\"\n",
"\n",
"apt-get -yq install libnss3-dev libnspr4-dev pkg-config libpam0g-dev \\\n",
" libcap-ng-dev libcap-ng-utils libselinux1-dev \\\n",
" libcurl4-nss-dev flex bison gcc make \\\n",
" libunbound-dev libnss3-tools libevent-dev || exiterr2\n",
"apt-get -yq --no-install-recommends install xmlto || exiterr2\n",
"apt-get -yq install ppp xl2tpd || exiterr2\n",
"apt-get -yq install fail2ban || exiterr2\n",
"\n",
"swan_ver=3.20\n",
"swan_file=\"libreswan-$swan_ver.tar.gz\"\n",
"swan_url1=\"https://github.com/libreswan/libreswan/archive/v$swan_ver.tar.gz\"\n",
"swan_url2=\"https://download.libreswan.org/$swan_file\"\n",
"if ! { wget -t 3 -T 30 -nv -O \"$swan_file\" \"$swan_url1\" || wget -t 3 -T 30 -nv -O \"$swan_file\" \"$swan_url2\"; }; then\n",
" exiterr \"Cannot download Libreswan source.\"\n",
"fi\n",
"/bin/rm -rf \"/opt/src/libreswan-$swan_ver\"\n",
"tar xzf \"$swan_file\" && /bin/rm -f \"$swan_file\"\n",
"cd \"libreswan-$swan_ver\" || exiterr \"Cannot enter Libreswan source dir.\"\n",
"echo \"WERROR_CFLAGS =\" > Makefile.inc.local\n",
"if [ \"$(packaging/utils/lswan_detect.sh init)\" = \"systemd\" ]; then\n",
" apt-get -yq install libsystemd-dev || exiterr2\n",
"fi\n",
"make -s programs && make -s install\n",
"\n",
"# Verify the install and clean up\n",
"cd /opt/src || exiterr \"Cannot enter /opt/src.\"\n",
"/bin/rm -rf \"/opt/src/libreswan-$swan_ver\"\n",
"if ! /usr/local/sbin/ipsec --version 2>/dev/null | grep -qF \"$swan_ver\"; then\n",
" exiterr \"Libreswan $swan_ver failed to build.\"\n",
"fi\n",
"\n",
"L2TP_NET=${VPN_L2TP_NET:-'10.20.30.0/24'}\n",
"L2TP_LOCAL=${VPN_L2TP_LOCAL:-'10.20.30.1'}\n",
"L2TP_POOL=${VPN_L2TP_POOL:-'10.20.30.10-10.20.30.250'}\n",
"XAUTH_NET=${VPN_XAUTH_NET:-'10.20.40.0/24'}\n",
"XAUTH_POOL=${VPN_XAUTH_POOL:-'10.20.40.10-10.20.40.250'}\n",
"DNS_SRV1=${VPN_DNS_SRV1:-'8.8.8.8'}\n",
"DNS_SRV2=${VPN_DNS_SRV2:-'8.8.4.4'}\n",
"\n",
"# Create IPsec (Libreswan) config\n",
"conf_bk \"/etc/ipsec.conf\"\n",
"cat > /etc/ipsec.conf <<EOF\n",
"version 2.0\n",
"\n",
"config setup\n",
" virtual_private=%v4:10.0.0.0/8,%v4:192.168.0.0/16,%v4:172.16.0.0/12,%v4:!$L2TP_NET,%v4:!$XAUTH_NET\n",
" protostack=netkey\n",
" nhelpers=0\n",
" interfaces=%defaultroute\n",
" uniqueids=no\n",
"\n",
"conn shared\n",
" left=%defaultroute\n",
" leftid=$PUBLIC_IP\n",
" right=%any\n",
" encapsulation=yes\n",
" authby=secret\n",
" pfs=no\n",
" rekey=no\n",
" keyingtries=5\n",
" dpddelay=30\n",
" dpdtimeout=120\n",
" dpdaction=clear\n",
" ike=aes256-sha1-modp2048,aes256-sha2-modp1024\n",
" phase2alg=aes256-sha1,aes256-sha2_512\n",
" sha2-truncbug=yes\n",
"\n",
"conn l2tp-psk\n",
" auto=add\n",
" leftprotoport=17/1701\n",
" rightprotoport=17/%any\n",
" type=transport\n",
" phase2=esp\n",
" also=shared\n",
"\n",
"conn xauth-psk\n",
" auto=add\n",
" leftsubnet=0.0.0.0/0\n",
" rightaddresspool=$XAUTH_POOL\n",
" modecfgdns1=$DNS_SRV1\n",
" modecfgdns2=$DNS_SRV2\n",
" leftxauthserver=yes\n",
" rightxauthclient=yes\n",
" leftmodecfgserver=yes\n",
" rightmodecfgclient=yes\n",
" modecfgpull=yes\n",
" xauthby=file\n",
" ike-frag=yes\n",
" ikev2=never\n",
" cisco-unity=yes\n",
" also=shared\n",
"EOF\n",
"\n",
"# Specify IPsec PSK\n",
"conf_bk \"/etc/ipsec.secrets\"\n",
"cat > /etc/ipsec.secrets <<EOF\n",
"%any %any : PSK \"$VPN_IPSEC_PSK\"\n",
"EOF\n",
"\n",
"# Create xl2tpd config\n",
"conf_bk \"/etc/xl2tpd/xl2tpd.conf\"\n",
"cat > /etc/xl2tpd/xl2tpd.conf <<EOF\n",
"[global]\n",
"port = 1701\n",
"\n",
"[lns default]\n",
"ip range = $L2TP_POOL\n",
"local ip = $L2TP_LOCAL\n",
"refuse chap = yes\n",
"refuse pap = yes\n",
"require authentication = yes\n",
"name = l2tpd\n",
"pppoptfile = /etc/ppp/options.xl2tpd\n",
"length bit = yes\n",
"EOF\n",
"\n",
"# Set xl2tpd options\n",
"conf_bk \"/etc/ppp/options.xl2tpd\"\n",
"cat > /etc/ppp/options.xl2tpd <<EOF\n",
"ipcp-accept-local\n",
"ipcp-accept-remote\n",
"ms-dns $DNS_SRV1\n",
"ms-dns $DNS_SRV2\n",
"noccp\n",
"auth\n",
"mtu 1280\n",
"mru 1280\n",
"proxyarp\n",
"lcp-echo-failure 4\n",
"lcp-echo-interval 30\n",
"connect-delay 5000\n",
"refuse-pap\n",
"refuse-chap\n",
"refuse-mschap\n",
"require-mschap-v2\n",
"EOF\n",
"\n",
"# Create VPN credentials\n",
"conf_bk \"/etc/ppp/chap-secrets\"\n",
"cat > /etc/ppp/chap-secrets <<EOF\n",
"# Secrets for authentication using CHAP\n",
"# client server secret IP addresses\n",
"\"$VPN_USER\" l2tpd \"$VPN_PASSWORD\" *\n",
"EOF\n",
"\n",
"conf_bk \"/etc/ipsec.d/passwd\"\n",
"VPN_PASSWORD_ENC=$(openssl passwd -1 \"$VPN_PASSWORD\")\n",
"cat > /etc/ipsec.d/passwd <<EOF\n",
"$VPN_USER:$VPN_PASSWORD_ENC:xauth-psk\n",
"EOF\n",
"\n",
"if ! grep -qs \"hwdsl2 VPN script\" /etc/sysctl.conf; then\n",
" conf_bk \"/etc/sysctl.conf\"\n",
"cat >> /etc/sysctl.conf <<EOF\n",
"\n",
"# Added by hwdsl2 VPN script\n",
"kernel.msgmnb = 65536\n",
"kernel.msgmax = 65536\n",
"kernel.shmmax = 68719476736\n",
"kernel.shmall = 4294967296\n",
"\n",
"net.ipv4.ip_forward = 1\n",
"net.ipv4.tcp_syncookies = 1\n",
"net.ipv4.conf.all.accept_source_route = 0\n",
"net.ipv4.conf.default.accept_source_route = 0\n",
"net.ipv4.conf.all.accept_redirects = 0\n",
"net.ipv4.conf.default.accept_redirects = 0\n",
"net.ipv4.conf.all.send_redirects = 0\n",
"net.ipv4.conf.default.send_redirects = 0\n",
"net.ipv4.conf.lo.send_redirects = 0\n",
"net.ipv4.conf.$NET_IFACE.send_redirects = 0\n",
"net.ipv4.conf.all.rp_filter = 0\n",
"net.ipv4.conf.default.rp_filter = 0\n",
"net.ipv4.conf.lo.rp_filter = 0\n",
"net.ipv4.conf.$NET_IFACE.rp_filter = 0\n",
"net.ipv4.icmp_echo_ignore_broadcasts = 1\n",
"net.ipv4.icmp_ignore_bogus_error_responses = 1\n",
"\n",
"net.core.wmem_max = 12582912\n",
"net.core.rmem_max = 12582912\n",
"net.ipv4.tcp_rmem = 10240 87380 12582912\n",
"net.ipv4.tcp_wmem = 10240 87380 12582912\n",
"EOF\n",
"fi\n",
"\n",
"# Check if IPTables rules need updating\n",
"ipt_flag=0\n",
"IPT_FILE=\"/etc/iptables.rules\"\n",
"if ! grep -qs \"hwdsl2 VPN script\" \"$IPT_FILE\" \\\n",
" || ! iptables -t nat -C POSTROUTING -s \"$L2TP_NET\" -o \"$NET_IFACE\" -j MASQUERADE 2>/dev/null \\\n",
" || ! iptables -t nat -C POSTROUTING -s \"$XAUTH_NET\" -o \"$NET_IFACE\" -m policy --dir out --pol none -j MASQUERADE 2>/dev/null; then\n",
" ipt_flag=1\n",
"fi\n",
"\n",
"# Add IPTables rules for VPN\n",
"if [ \"$ipt_flag\" = \"1\" ]; then\n",
" service fail2ban stop >/dev/null 2>&1\n",
" iptables-save > \"$IPT_FILE.old-$SYS_DT\"\n",
" iptables -I INPUT 1 -p udp --dport 1701 -m policy --dir in --pol none -j DROP\n",
" iptables -I INPUT 2 -m conntrack --ctstate INVALID -j DROP\n",
" iptables -I INPUT 3 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT\n",
" iptables -I INPUT 4 -p udp -m multiport --dports 500,4500 -j ACCEPT\n",
" iptables -I INPUT 5 -p udp --dport 1701 -m policy --dir in --pol ipsec -j ACCEPT\n",
" iptables -I INPUT 6 -p udp --dport 1701 -j DROP\n",
" iptables -I FORWARD 1 -m conntrack --ctstate INVALID -j DROP\n",
" iptables -I FORWARD 2 -i \"$NET_IFACE\" -o ppp+ -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT\n",
" iptables -I FORWARD 3 -i ppp+ -o \"$NET_IFACE\" -j ACCEPT\n",
" iptables -I FORWARD 4 -i ppp+ -o ppp+ -s \"$L2TP_NET\" -d \"$L2TP_NET\" -j ACCEPT\n",
" iptables -I FORWARD 5 -i \"$NET_IFACE\" -d \"$XAUTH_NET\" -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT\n",
" iptables -I FORWARD 6 -s \"$XAUTH_NET\" -o \"$NET_IFACE\" -j ACCEPT\n",
" # Uncomment if you wish to disallow traffic between VPN clients themselves\n",
" iptables -I FORWARD 2 -i ppp+ -o ppp+ -s \"$L2TP_NET\" -d \"$L2TP_NET\" -j DROP\n",
" iptables -I FORWARD 3 -s \"$XAUTH_NET\" -d \"$XAUTH_NET\" -j DROP\n",
" iptables -A FORWARD -j DROP\n",
" iptables -t nat -I POSTROUTING -s \"$XAUTH_NET\" -o \"$NET_IFACE\" -m policy --dir out --pol none -j MASQUERADE\n",
" iptables -t nat -I POSTROUTING -s \"$L2TP_NET\" -o \"$NET_IFACE\" -j MASQUERADE\n",
" echo \"# Modified by hwdsl2 VPN script\" > \"$IPT_FILE\"\n",
" iptables-save >> \"$IPT_FILE\"\n",
"\n",
" # Update rules for iptables-persistent\n",
" IPT_FILE2=\"/etc/iptables/rules.v4\"\n",
" if [ -f \"$IPT_FILE2\" ]; then\n",
" conf_bk \"$IPT_FILE2\"\n",
" /bin/cp -f \"$IPT_FILE\" \"$IPT_FILE2\"\n",
" fi\n",
"fi\n",
"\n",
"mkdir -p /etc/network/if-pre-up.d\n",
"cat > /etc/network/if-pre-up.d/iptablesload <<'EOF'\n",
"#!/bin/sh\n",
"iptables-restore < /etc/iptables.rules\n",
"exit 0\n",
"EOF\n",
"\n",
"for svc in fail2ban ipsec xl2tpd; do\n",
" update-rc.d \"$svc\" enable >/dev/null 2>&1\n",
" systemctl enable \"$svc\" 2>/dev/null\n",
"done\n",
"if ! grep -qs \"hwdsl2 VPN script\" /etc/rc.local; then\n",
" if [ -f /etc/rc.local ]; then\n",
" conf_bk \"/etc/rc.local\"\n",
" sed --follow-symlinks -i '/^exit 0/d' /etc/rc.local\n",
" else\n",
" echo '#!/bin/sh' > /etc/rc.local\n",
" fi\n",
"cat >> /etc/rc.local <<'EOF'\n",
"\n",
"# Added by hwdsl2 VPN script\n",
"service ipsec start\n",
"service xl2tpd start\n",
"echo 1 > /proc/sys/net/ipv4/ip_forward\n",
"exit 0\n",
"EOF\n",
" if grep -qs raspbian /etc/os-release; then\n",
" sed --follow-symlinks -i '/hwdsl2 VPN script/a sleep 15' /etc/rc.local\n",
" fi\n",
"fi\n",
"\n",
"# Reload sysctl.conf\n",
"sysctl -e -q -p\n",
"\n",
"# Update file attributes\n",
"chmod +x /etc/rc.local /etc/network/if-pre-up.d/iptablesload\n",
"chmod 600 /etc/ipsec.secrets* /etc/ppp/chap-secrets* /etc/ipsec.d/passwd*\n",
"\n",
"# Apply new IPTables rules\n",
"iptables-restore < \"$IPT_FILE\"\n",
"\n",
"# Restart services\n",
"service fail2ban restart 2>/dev/null\n",
"service ipsec restart 2>/dev/null\n",
"service xl2tpd restart 2>/dev/null\n",
"\n",
"#VPN 2 - Setup PPTP Server\n",
"apt-get install pptpd -y\n",
"echo \"localip 10.0.0.1\" >> /etc/pptpd.conf\n",
"echo \"remoteip 10.0.0.100-200\" >> /etc/pptpd.conf\n",
"echo \"$VPN_USER pptpd $VPN_PASSWORD *\" >> /etc/ppp/chap-secrets\n",
"echo \"ms-dns 8.8.8.8\" >> /etc/ppp/pptpd-options\n",
"echo \"ms-dns 8.8.4.4\" >> /etc/ppp/pptpd-options\n",
"service pptpd restart\n",
"\n",
"echo \"net.ipv4.ip_forward = 1\" >> /etc/sysctl.conf\n",
"sysctl -p\n",
"iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE && iptables-save\n",
"\n",
"#Change SSH port\n",
"/bin/sed -i \"/Port/ s/22/$SSH_PORT/\" /etc/ssh/sshd_config\n",
"service sshd restart 2>/dev/null\n",
"exit 0"
]
]
VPNElasticIP:
Type: "AWS::EC2::EIP"
Properties:
InstanceId: !Ref VPNServerInstance
Domain: vpc
VpnDnsRecord:
Type: "AWS::Route53::RecordSet"
Condition: ManageDNS
Properties:
Comment: A record for VPN instance
HostedZoneName: !Join [ ".", [ !Ref DNSDomainName, "" ] ]
Name: !Join [ ".", [ !Ref DNSSubDomainPrefix, !Ref DNSDomainName ] ]
ResourceRecords:
- !Ref VPNElasticIP
TTL: 60
Type: A
Outputs:
VPNServerIP:
Description: VPN server IP address
Value: !Ref VPNElasticIP
VPNServerDomain:
Description: VPN server Domain Name
Value: !Join [ ".", [ !Ref DNSSubDomainPrefix, !Ref DNSDomainName ] ]
Condition: ManageDNS
VPNSSHPort:
Description: VPN server SSH port
Value: !Ref EC2SSHPort