You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
4.7 KiB

  1. #!/bin/bash
  2. #
  3. BORDER=">>>>>>>>>>>>>>"
  4. printf "$BORDER Installing openvpn and EasyRSA $BORDER \n\n"
  5. sudo apt-get update -y
  6. sudo apt-get install openvpn easy-rsa -y
  7. printf "$BORDER Setting default openvpn vars $BORDER \n\n"
  8. make-cadir ~/openvpn-ca
  9. cd ~/openvpn-ca
  10. sed -i "s/KEY_PROVINCE=\"CA\"/KEY_PROVINCE=\"NM\"/g" vars
  11. sed -i "s/KEY_CITY=\"SanFrancisco\"/KEY_CITY=\"Albuquerque\"/g" vars
  12. sed -i "s/KEY_ORG=\"Fort-Funston\"/KEY_ORG=\"BreakpointingBad\"/g" vars
  13. sed -i "s/KEY_NAME=\"EasyRSA\"/KEY_NAME=\"server\"/g" vars
  14. sed -i 's/--interact//' build-ca
  15. sed -i 's/--interact//' build-key-server
  16. sed -i 's/--interact//' build-dh
  17. sed -i 's/--interact//' build-key
  18. cd ~/openvpn-ca
  19. source vars
  20. printf "$BORDER Building the certificate authority $BORDER \n\n"
  21. ./clean-all
  22. ./build-ca
  23. printf "$BORDER Creating the server certificate $BORDER \n\n"
  24. ./build-key-server server
  25. printf "$BORDER Generating Diffie-Hellman keys to use during key exchange $BORDER \n\n"
  26. ./build-dh
  27. printf "$BORDER Generating HMAC signature to strengthen the server’s TLS integrity verification"
  28. openvpn --genkey --secret keys/ta.key
  29. printf "$BORDER Generating client certificate and key pair $BORDER \n\n"
  30. cd ~/openvpn-ca
  31. source vars
  32. ./build-key client1
  33. printf "$BORDER Configuring the openvpn service using generated keys + certs $BORDER \n\n"
  34. cd ~/openvpn-ca/keys
  35. sudo cp ca.crt server.crt server.key ta.key dh2048.pem /etc/openvpn
  36. gunzip -c /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz | sudo tee /etc/openvpn/server.conf
  37. sudo sed -i "s/;tls-auth ta.key 0/tls-auth ta.key 0/g" /etc/openvpn/server.conf
  38. sudo sed -i "s/;cipher AES-128-CBC/cipher AES-128-CBC/g" /etc/openvpn/server.conf
  39. sudo sed -i "s/;user nobody/user nobody/g" /etc/openvpn/server.conf
  40. sudo sed -i "s/;group nogroup/group nogroup/g" /etc/openvpn/server.conf
  41. #sudo sed -i "s/;push \"redirect-gateway def1 bypass-dhcp\"/push \"redirect-gateway def1 bypass-dhcp\"/g" /etc/openvpn/server.conf
  42. #sudo sed -i "s/;push \"dhcp-option DNS 208.67.222.222\"/push \"dhcp-option DNS 208.67.222.222\"/g" /etc/openvpn/server.conf
  43. #sudo sed -i "s/;push \"dhcp-option DNS 208.67.220.220\"/push \"dhcp-option DNS 208.67.220.220\"/g" /etc/openvpn/server.conf
  44. sudo bash -c 'cat >> /etc/openvpn/server.conf << EOF
  45. auth SHA256
  46. EOF'
  47. sudo sed -i "s/port 1194/port 443/g" /etc/openvpn/server.conf
  48. sudo sed -i "s/proto udp/proto tcp/g" /etc/openvpn/server.conf
  49. printf "$BORDER Adjusting the servers network config to allow for vpn things $BORDER \n\n"
  50. sudo sed -i "s/#net.ipv4.ip_forward=1/net.ipv4.ip_forward=1/g" /etc/sysctl.conf
  51. sudo sysctl -p
  52. sudo bash -c 'cat >> /etc/ufw/before.rules << EOF
  53. # START OPENVPN RULES
  54. # NAT table rules
  55. *nat
  56. :POSTROUTING ACCEPT [0:0]
  57. # Allow traffic from OpenVPN client to enp0s8
  58. -A POSTROUTING -s 10.8.0.0/8 -o enp0s8 -j MASQUERADE
  59. COMMIT
  60. # END OPENVPN RULES
  61. EOF'
  62. sudo sed -i "s/DEFAULT_FORWARD_POLICY=\"DROP\"/DEFAULT_FORWARD_POLICY=\"ACCEPT\"/g" /etc/default/ufw
  63. sudo ufw allow 443/tcp
  64. sudo ufw allow OpenSSH
  65. sudo ufw disable
  66. sudo ufw --force enable
  67. printf "$BORDER Enabling the openvpn service $BORDER \n\n"
  68. sudo systemctl start openvpn@server
  69. sudo systemctl enable openvpn@server
  70. BORDER=">>>>>>>>>>>>>>>>"
  71. printf "$BORDER Setting up base client config file\n\n"
  72. mkdir -p ~/client-configs/files
  73. chmod 700 ~/client-configs/files
  74. cp /usr/share/doc/openvpn/examples/sample-config-files/client.conf ~/client-configs/base.conf
  75. sed -i "s/my-server-1 1194/192.168.2.2 443/g" ~/client-configs/base.conf
  76. sed -i "s/proto udp/proto tcp/g" ~/client-configs/base.conf
  77. sed -i "s/;user nobody/user nobody/g" ~/client-configs/base.conf
  78. sed -i "s/;group nobody/group nobody/g" ~/client-configs/base.conf
  79. sed -i "s/ca ca.crt/# ca ca.crt/g" ~/client-configs/base.conf
  80. sed -i "s/cert client.crt/# cert client.crt/g" ~/client-configs/base.conf
  81. sed -i "s/key client.key/# key client.key/g" ~/client-configs/base.conf
  82. cat >> ~/client-configs/base.conf << EOF
  83. cipher AES-128-CBC
  84. auth SHA256
  85. key-direction 1
  86. # script-security 2
  87. # up /etc/openvpn/update-resolv-conf
  88. # down /etc/openvpn/update-resolv-conf
  89. EOF
  90. printf "$BORDER Creating make client config script..\n\n"
  91. touch ~/client-configs/make_config.sh
  92. cat >> ~/client-configs/make_config.sh << EOF
  93. # First argument: Client identifier
  94. KEY_DIR=~/openvpn-ca/keys
  95. OUTPUT_DIR=~/client-configs/files
  96. BASE_CONFIG=~/client-configs/base.conf
  97. cat \${BASE_CONFIG} <(echo -e '<ca>') \${KEY_DIR}/ca.crt <(echo -e '</ca>\n<cert>') \${KEY_DIR}/\${1}.crt <(echo -e '</cert>\n<key>') \${KEY_DIR}/\${1}.key <(echo -e '</key>\n<tls-auth>') \${KEY_DIR}/ta.key <(echo -e '</tls-auth>') > \${OUTPUT_DIR}/\${1}.ovpn
  98. EOF
  99. chmod 700 ~/client-configs/make_config.sh
  100. printf "$BORDER Making client config file for client1\n\n"
  101. cd ~/client-configs
  102. ./make_config.sh client1
  103. ls ~/client-configs/files