forked from integgroll/buildvpn
-
Notifications
You must be signed in to change notification settings - Fork 6
/
BuildVPN.sh
executable file
·370 lines (332 loc) · 11.9 KB
/
BuildVPN.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
#!/bin/bash
# Global Variables
openvpn_dir='/etc/openvpn'
easyrsa_dir='/etc/openvpn/easy-rsa'
ovpnkey_dir='/etc/openvpn/easy-rsa/keys'
ovpnsvr_cnf='/etc/openvpn/server.conf'
ovpncrt_dir='/etc/openvpn/certs'
os_distro=$(cat /etc/issue|head -n1|cut -d" " -f1)
# Title Function
func_title(){
# Clear (For Prettyness)
clear
# Print Title
echo '====================================================================='
echo ' BuildVPN.sh | [Version]: 1.9.0 | [Updated]: 02.09.2015'
echo '====================================================================='
}
# OpenVPN Install Function
func_install(){
func_title
echo '[*] Updating Package Lists'
apt-get update
# Install Packages Based on OS Distribution
echo '[*] Installing Packages'
if [ ${os_distro} == 'Debian' ]; then
apt-get -y install openvpn openssl
elif [ ${os_distro} == 'Ubuntu' ]; then
apt-get -y install openvpn openssl easy-rsa
else
func_title
echo
echo '[!] Error: BuildVPN.sh is only supported on Debian 5+ and Ubuntu 12+.'
echo
exit 1
fi
}
# Server Buildout Function
func_build_server(){
# Locate RSA Example Directory
echo '[*] Discovering easy-rsa Directory...'
easyrsa_fnd=$(find / -name vars|grep easy-rsa|head -n1|sed 's:/vars::')
# RSA Validation
if [[ ${easyrsa_fnd} == '' ]]; then
func_title
echo
echo '[!] Error: easy-rsa not installed, re-run with -i option.'
echo
exit 1
fi
# Get Server Configuration User Input
func_title
echo
read -p 'Enter Server Hostname......................: ' host
echo
echo '+----------------------+'
echo '| Available Interfaces |'
echo '+----------------------+'
for if in $(netstat -i|awk 'FNR >= 3 { print $1 }'); do
ifconfig ${if}|awk '/Link |inet /'|tr -s '[:space:]'|sed -e 's/ Link.*//g' -e ':a;N;$!ba;s/\n inet//g' -e 's/addr://g'|cut -d" " -f1,2|sed 's/ /\t/'
done
echo
read -p 'Enter IP OpenVPN Server Will Bind To........: ' vpnip
read -p 'Enter Subnet For VPN (ex: 192.168.100.0)....: ' vpnnet
read -p 'Enter Subnet Netmask (ex: 255.255.255.0)....: ' netmask
read -p 'Enter Preferred DNS Server (ex: 8.8.8.8)....: ' dns
read -p 'Enter Max Client Connection Threshold.......: ' maxconn
read -p 'Increase Encryption Key Size (y/n)..........: ' incbits
# Get User Increased Key Size
if [[ ${incbits} == [yY] ]]; then
read -p 'Enter Encryption Key Size (ex: 2048)........: ' keysize
fi
read -p 'Route All Traffic Through This VPN (y/n)....: ' routeall
read -p 'Allow Certificates With Same Subject (y/n)..: ' unique
read -p 'Enter Interface For Masquerading (ex: eth0).: ' natif
# Build Certificate Authority
func_title
echo
echo '[*] Preparing Directories'
# Copy Easy-RSA Sample Directory
cp -R ${easyrsa_fnd} ${easyrsa_dir}
cd ${easyrsa_dir}
# Update vars With User Specified Key Size
if [[ ${incbits} == [yY] ]]; then
sed -i "s/KEY_SIZE=.*$/KEY_SIZE=${keysize}/g" vars
fi
# Create OpenSSL Configuration if Non-Existant
if [ ! -f openssl.cnf ]; then
echo '[*] Preparing OpenSSL Configuration'
cp openssl-1.0.0.cnf openssl.cnf
fi
echo '[*] Resetting Variables'
. ./vars >> /dev/null
echo '[*] Preparing Build Configurations'
./clean-all >> /dev/null
echo '[*] Building Certificate Authority'
./build-ca
func_title
echo
echo '[*] Building Key Server'
./build-key-server ${host}
func_title
echo
echo '[*] Generating Diffie Hellman Key'
./build-dh
func_title
echo
cd ${ovpnkey_dir}
echo '[*] Generating TLS-Auth Key'
openvpn --genkey --secret ta.key
# Build Server Configuration
echo "[*] Creating server.conf In ${openvpn_dir}"
echo "local ${vpnip}" > ${ovpnsvr_cnf}
echo 'port 1194' >> ${ovpnsvr_cnf}
echo 'proto udp' >> ${ovpnsvr_cnf}
echo 'dev tun' >> ${ovpnsvr_cnf}
echo "ca ${ovpnkey_dir}/ca.crt" >> ${ovpnsvr_cnf}
echo "cert ${ovpnkey_dir}/${host}.crt" >> ${ovpnsvr_cnf}
echo "key ${ovpnkey_dir}/${host}.key" >> ${ovpnsvr_cnf}
# Get DH Key
dhkey=$(find /etc/openvpn/easy-rsa -name "*dh*.pem")
echo "dh ${dhkey}" >> ${ovpnsvr_cnf}
echo "server ${vpnnet} ${netmask}" >> ${ovpnsvr_cnf}
echo 'ifconfig-pool-persist ipp.txt' >> ${ovpnsvr_cnf}
# Determine If Route All Traffic Option Was Chosen
if [[ ${routeall} == [yY] ]]; then
echo 'push "redirect-gateway def1"' >> ${ovpnsvr_cnf}
fi
# Determine If Unique Subjects Option Was Chosen
if [[ ${unique} == [yY] ]]; then
echo 'unique_subject = no' >> ${openvpn_dir}/easy-rsa/keys/index.txt.attr
fi
echo "push "dhcp-option DNS ${dns}"" >> ${ovpnsvr_cnf}
echo 'keepalive 10 120' >> ${ovpnsvr_cnf}
echo "tls-auth ${ovpnkey_dir}/ta.key 0" >> ${ovpnsvr_cnf}
echo 'comp-lzo' >> ${ovpnsvr_cnf}
echo "max-clients ${maxconn}" >> ${ovpnsvr_cnf}
echo 'user nobody' >> ${ovpnsvr_cnf}
echo 'group nogroup' >> ${ovpnsvr_cnf}
echo 'persist-key' >> ${ovpnsvr_cnf}
echo 'persist-tun' >> ${ovpnsvr_cnf}
echo "status ${openvpn_dir}/status.log" >> ${ovpnsvr_cnf}
echo "log ${openvpn_dir}/openvpn.log" >> ${ovpnsvr_cnf}
echo 'verb 3' >> ${ovpnsvr_cnf}
echo 'mute 20' >> ${ovpnsvr_cnf}
echo '[*] Enabling IPv4 Forwarding In /etc/sysctl.conf'
sed -i 's/#net.ipv4.ip_forward=1/net.ipv4.ip_forward=1/' /etc/sysctl.conf
sed -i 's/net.ipv4.ip_forward=0/net.ipv4.ip_forward=1/' /etc/sysctl.conf
echo '[*] Reloading sysctl Configuration'
/sbin/sysctl -p >> /dev/null 2>&1
echo '[*] Loading IPTables Masquerading Rule Into Current Ruleset'
/sbin/iptables -t nat -A POSTROUTING -s ${vpnnet}/${netmask} -o ${natif} -j MASQUERADE
echo '[*] Adding IPTables Masquerading Rule To /etc/rc.local'
sed -i "s:^exit 0:/sbin/iptables -t nat -A POSTROUTING -s ${vpnnet}/${netmask} -o ${natif} -j MASQUERADE:" /etc/rc.local
echo "exit 0" >> /etc/rc.local
# Finish Message
echo '[*] Server Buildout Complete'
echo
exit 0
}
# Build Client Certificates Function
func_build_client(){
# Create Certificate Directory If Non-Existant
if [ ! -d ${ovpncrt_dir} ]; then
mkdir ${ovpncrt_dir}
fi
read -p 'Enter Username (No Spaces)......................: ' user
read -p 'Enter Name For Configuration File (No Spaces)...: ' confname
echo
echo '+------------------------+'
echo '| Available IP Addresses |'
echo '+------------------------+'
for if in $(netstat -i|awk 'FNR >= 3 { print $1 }'); do
ifconfig ${if}|awk '/Link |inet /'|tr -s '[:space:]'|sed -e 's/ Link.*//g' -e ':a;N;$!ba;s/\n inet//g' -e 's/addr://g'|cut -d" " -f1,2|sed 's/ /\t/'
done
echo
read -p 'Enter IP/Hostname OpenVPN Server Binds To.......: ' vpnip
# Build Certificate
func_title
echo
echo "[*] Generating Client Certificate For: ${user}"
cd ${easyrsa_dir}
. ./vars
./build-key ${user}
# Prepare Client Build Directory
cd ${openvpn_dir} && mkdir ${user}
# Build Client Configuration
func_title
echo
echo '[*] Creating Client Configuration'
echo 'client' > ${user}/${confname}.ovpn
echo 'dev tun' >> ${user}/${confname}.ovpn
echo 'proto udp' >> ${user}/${confname}.ovpn
echo "remote ${vpnip} 1194" >> ${user}/${confname}.ovpn
echo 'resolv-retry infinite' >> ${user}/${confname}.ovpn
echo 'nobind' >> ${user}/${confname}.ovpn
echo 'persist-key' >> ${user}/${confname}.ovpn
echo 'persist-tun' >> ${user}/${confname}.ovpn
echo 'mute-replay-warnings' >> ${user}/${confname}.ovpn
echo '<ca>' >> ${user}/${confname}.ovpn
cat ${ovpnkey_dir}/ca.crt >> ${user}/${confname}.ovpn
cp ${ovpnkey_dir}/ca.crt ${user}/ca.crt
echo '</ca>' >> ${user}/${confname}.ovpn
echo '<cert>' >> ${user}/${confname}.ovpn
cat ${ovpnkey_dir}/${user}.crt|awk '!/^ |Certificate:/'|sed '/^$/d' >> ${user}/${confname}.ovpn
cp ${ovpnkey_dir}/${user}.crt ${user}/${user}.crt
echo '</cert>' >> ${user}/${confname}.ovpn
echo '<key>' >> ${user}/${confname}.ovpn
cat ${ovpnkey_dir}/${user}.key >> ${user}/${confname}.ovpn
cp ${ovpnkey_dir}/${user}.key ${user}/${user}.key
echo '</key>' >> ${user}/${confname}.ovpn
echo 'ns-cert-type server' >> ${user}/${confname}.ovpn
echo 'key-direction 1' >> ${user}/${confname}.ovpn
echo '<tls-auth>' >> ${user}/${confname}.ovpn
cat ${ovpnkey_dir}/ta.key|awk '!/#/' >> ${user}/${confname}.ovpn
cp ${ovpnkey_dir}/ta.key ${user}/ta.key
echo '</tls-auth>' >> ${user}/${confname}.ovpn
echo 'comp-lzo' >> ${user}/${confname}.ovpn
echo 'verb 3' >> ${user}/${confname}.ovpn
echo 'mute 20' >> ${user}/${confname}.ovpn
echo 'BuildVPN File Information' >> ${user}/README
echo >> ${user}/README
echo 'Certificate Authority Certificate.: ca.crt' >> ${user}/README
echo 'TLS-Auth Shared Secret Key........: ta.key' >> ${user}/README
echo "User Certificate..................: ${user}.crt" >> ${user}/README
echo "User Private Key..................: ${user}.key" >> ${user}/README
echo "User OpenVPN Profile Bundle.......: ${confname}.ovpn" >> ${user}/README
# Build Client Tarball
echo "[*] Creating ${user}-${confname}.tar Configuration Package In: ${ovpncrt_dir}"
tar -cf ${ovpncrt_dir}/${user}-${confname}.tar ${user}
# Clean Up Temp Files
echo '[*] Removing Temporary Files'
rm -rf ${user}
# Finish Message
echo "[*] Client ${user} Buildout Complete"
echo
exit 0
}
# Build External-to-Internal Tunnel
# Thanks to Johnny-Nohandle (https://github.com/johnny-nohandle)
func_build_tunnel(){
# Get Tunnel Configuration User Input
echo '+----------------------+'
echo '| Available Interfaces |'
echo '+----------------------+'
for if in $(netstat -i|awk 'FNR >= 3 { print $1 }'); do
ifconfig ${if}|awk '/Link |inet /'|tr -s '[:space:]'|sed -e 's/ Link.*//g' -e ':a;N;$!ba;s/\n inet//g' -e 's/addr://g'|cut -d" " -f1,2|sed 's/ /\t/'
done
echo
read -p 'Enter External IP The Tunnel Binds To.......: ' etunip
read -p 'Enter External Port The Tunnel Binds To.....: ' etunport
read -p 'Enter Internal IP The Tunnel Forwards To....: ' itunip
read -p 'Enter Internal Port The Tunnel Forwards To..: ' itunport
read -p 'Enter Protocol The Tunnel Will Use..........: ' tunproto
# Reconfigure Current IPTables and Save Configuration to Startup
echo
echo '[*] Loading IPTables Prerouting Rule Into Current Ruleset'
/sbin/iptables -t nat -A PREROUTING -p ${tunproto} -d ${etunip} --dport ${etunport} -j DNAT --to-destination ${itunip}:${itunport}
echo '[*] Adding IPTables Prerouting Rule To /etc/rc.local'
sed -i "s,^exit 0,/sbin/iptables -t nat -A PREROUTING -p ${tunproto} -d ${etunip} --dport ${etunport} -j DNAT --to-destination ${itunip}:${itunport}," /etc/rc.local
echo "exit 0" >> /etc/rc.local
echo
read -p 'Do You Have Another Tunnel To Add? (y/n)....: ' moretun
if [[ ${moretun} == [yY] ]]; then
func_build_tunnel
else
echo '[*] Tunnel Buildout Complete'
echo
exit 0
fi
}
# Check Permissions
if [ $(whoami) != 'root' ]; then
func_title
echo
echo '[!] Error: You must run this script with root privileges.'
echo
exit 1
fi
# Check OS Version
if [ ${os_distro} == 'Debian' ]; then
debian_ver=$(cat /etc/debian_version|cut -d"." -f1)
if [[ ! ${debian_ver} -ge '5' ]]; then
func_title
echo
echo '[!] Error: BuildVPN.sh is only supported on Debian version 5+.'
echo
exit 1
fi
elif [ ${os_distro} == 'Ubuntu' ]; then
ubuntu_ver=$(cat /etc/issue|head -n1|cut -d" " -f2|cut -d"." -f1)
if [[ ! ${ubuntu_ver} -ge '12' ]]; then
func_title
echo
echo '[!] Error: BuildVPN.sh is only supported on Ubuntu version 12+.'
echo
exit 1
fi
else
func_title
echo
echo '[!] Error: BuildVPN.sh is only supported on Debian 5+ and Ubuntu 12+.'
echo
exit 1
fi
# Select Function and Menu Statement
func_title
case ${1} in
-i)
echo
func_install
;;
-s)
echo
func_build_server
;;
-c)
echo
func_build_client
;;
-t)
echo
func_build_tunnel
;;
*)
echo
echo "[Usage]...: ${0} [OPTION]"
echo '[Options].: -i = Install OpenVPN Packages'
echo ' -s = Build Server Configuration'
echo ' -c = Build Client Configuration'
echo ' -t = Build External-to-Internal Tunnel'
echo
esac