ipsec.sh 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #!/bin/bash
  2. # Debian 9 & 10 64bit
  3. # Ubuntu 18.04 & 20.04 bit
  4. # Centos 7 & 8 64bit
  5. # ==================================================
  6. VPN_IPSEC_PSK='myvpn'
  7. NET_IFACE=$(ip -o $NET_IFACE -4 route show to default | awk '{print $5}');
  8. export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
  9. source /etc/os-release
  10. OS=$ID
  11. ver=$VERSION_ID
  12. bigecho() { echo; echo "## $1"; echo; }
  13. bigecho "VPN setup in progress... Please be patient."
  14. # Create and change to working dir
  15. mkdir -p /opt/src
  16. cd /opt/src
  17. bigecho "Trying to auto discover IP of this server..."
  18. PUBLIC_IP=$(wget -qO- ipinfo.io/ip);
  19. bigecho "Installing packages required for the VPN..."
  20. if [[ ${OS} == "centos" ]]; then
  21. epel_url="https://dl.fedoraproject.org/pub/epel/epel-release-latest-$(rpm -E '%{rhel}').noarch.rpm"
  22. yum -y install epel-release || yum -y install "$epel_url"
  23. bigecho "Installing packages required for the VPN..."
  24. REPO1='--enablerepo=epel'
  25. REPO2='--enablerepo=*server-*optional*'
  26. REPO3='--enablerepo=*releases-optional*'
  27. REPO4='--enablerepo=PowerTools'
  28. yum -y install nss-devel nspr-devel pkgconfig pam-devel \
  29. libcap-ng-devel libselinux-devel curl-devel nss-tools \
  30. flex bison gcc make ppp
  31. yum "$REPO1" -y install xl2tpd
  32. if [[ $ver == '7' ]]; then
  33. yum -y install systemd-devel iptables-services
  34. yum "$REPO2" "$REPO3" -y install libevent-devel fipscheck-devel
  35. elif [[ $ver == '8' ]]; then
  36. yum "$REPO4" -y install systemd-devel libevent-devel fipscheck-devel
  37. fi
  38. else
  39. apt install openssl iptables iptables-persistent -y
  40. apt-get -y install libnss3-dev libnspr4-dev pkg-config \
  41. libpam0g-dev libcap-ng-dev libcap-ng-utils libselinux1-dev \
  42. libcurl4-nss-dev flex bison gcc make libnss3-tools \
  43. libevent-dev ppp xl2tpd pptpd
  44. fi
  45. bigecho "Compiling and installing Libreswan..."
  46. SWAN_VER=3.32
  47. swan_file="libreswan-$SWAN_VER.tar.gz"
  48. swan_url1="https://github.com/libreswan/libreswan/archive/v$SWAN_VER.tar.gz"
  49. swan_url2="https://download.libreswan.org/$swan_file"
  50. if ! { wget -t 3 -T 30 -nv -O "$swan_file" "$swan_url1" || wget -t 3 -T 30 -nv -O "$swan_file" "$swan_url2"; }; then
  51. exit 1
  52. fi
  53. /bin/rm -rf "/opt/src/libreswan-$SWAN_VER"
  54. tar xzf "$swan_file" && /bin/rm -f "$swan_file"
  55. cd "libreswan-$SWAN_VER" || exit 1
  56. cat > Makefile.inc.local <<'EOF'
  57. WERROR_CFLAGS = -w
  58. USE_DNSSEC = false
  59. USE_DH2 = true
  60. USE_DH31 = false
  61. USE_NSS_AVA_COPY = true
  62. USE_NSS_IPSEC_PROFILE = false
  63. USE_GLIBC_KERN_FLIP_HEADERS = true
  64. EOF
  65. if ! grep -qs IFLA_XFRM_LINK /usr/include/linux/if_link.h; then
  66. echo "USE_XFRM_INTERFACE_IFLA_HEADER = true" >> Makefile.inc.local
  67. fi
  68. if [[ ${OS} == "debian" ]]; then
  69. if [ "$(packaging/utils/lswan_detect.sh init)" = "systemd" ]; then
  70. apt-get -y install libsystemd-dev
  71. fi
  72. elif [[ ${OS} == "ubuntu" ]]; then
  73. if [ "$(packaging/utils/lswan_detect.sh init)" = "systemd" ]; then
  74. apt-get -y install libsystemd-dev
  75. fi
  76. fi
  77. NPROCS=$(grep -c ^processor /proc/cpuinfo)
  78. [ -z "$NPROCS" ] && NPROCS=1
  79. make "-j$((NPROCS+1))" -s base && make -s install-base
  80. cd /opt/src || exit 1
  81. /bin/rm -rf "/opt/src/libreswan-$SWAN_VER"
  82. if ! /usr/local/sbin/ipsec --version 2>/dev/null | grep -qF "$SWAN_VER"; then
  83. exiterr "Libreswan $SWAN_VER failed to build."
  84. fi
  85. bigecho "Creating VPN configuration..."
  86. L2TP_NET=192.168.42.0/24
  87. L2TP_LOCAL=192.168.42.1
  88. L2TP_POOL=192.168.42.10-192.168.42.250
  89. XAUTH_NET=192.168.43.0/24
  90. XAUTH_POOL=192.168.43.10-192.168.43.250
  91. DNS_SRV1=8.8.8.8
  92. DNS_SRV2=8.8.4.4
  93. DNS_SRVS="\"$DNS_SRV1 $DNS_SRV2\""
  94. [ -n "$VPN_DNS_SRV1" ] && [ -z "$VPN_DNS_SRV2" ] && DNS_SRVS="$DNS_SRV1"
  95. # Create IPsec config
  96. cat > /etc/ipsec.conf <<EOF
  97. version 2.0
  98. config setup
  99. 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
  100. protostack=netkey
  101. interfaces=�faultroute
  102. uniqueids=no
  103. conn shared
  104. left=�faultroute
  105. leftid=$PUBLIC_IP
  106. right=%any
  107. encapsulation=yes
  108. authby=secret
  109. pfs=no
  110. rekey=no
  111. keyingtries=5
  112. dpddelay=30
  113. dpdtimeout=120
  114. dpdaction=clear
  115. ikev2=never
  116. ike=aes256-sha2,aes128-sha2,aes256-sha1,aes128-sha1,aes256-sha2;modp1024,aes128-sha1;modp1024
  117. phase2alg=aes_gcm-null,aes128-sha1,aes256-sha1,aes256-sha2_512,aes128-sha2,aes256-sha2
  118. sha2-truncbug=no
  119. conn l2tp-psk
  120. auto=add
  121. leftprotoport=17/1701
  122. rightprotoport=17/%any
  123. type=transport
  124. phase2=esp
  125. also=shared
  126. conn xauth-psk
  127. auto=add
  128. leftsubnet=0.0.0.0/0
  129. rightaddresspool=$XAUTH_POOL
  130. modecfgdns=$DNS_SRVS
  131. leftxauthserver=yes
  132. rightxauthclient=yes
  133. leftmodecfgserver=yes
  134. rightmodecfgclient=yes
  135. modecfgpull=yes
  136. xauthby=file
  137. ike-frag=yes
  138. cisco-unity=yes
  139. also=shared
  140. include /etc/ipsec.d/*.conf
  141. EOF
  142. if uname -m | grep -qi '^arm'; then
  143. if ! modprobe -q sha512; then
  144. sed -i '/phase2alg/s/,aes256-sha2_512//' /etc/ipsec.conf
  145. fi
  146. fi
  147. # Specify IPsec PSK
  148. cat > /etc/ipsec.secrets <<EOF
  149. %any %any : PSK "$VPN_IPSEC_PSK"
  150. EOF
  151. # Create xl2tpd config
  152. cat > /etc/xl2tpd/xl2tpd.conf <<EOF
  153. [global]
  154. port = 1701
  155. [lns default]
  156. ip range = $L2TP_POOL
  157. local ip = $L2TP_LOCAL
  158. require chap = yes
  159. refuse pap = yes
  160. require authentication = yes
  161. name = l2tpd
  162. pppoptfile = /etc/ppp/options.xl2tpd
  163. length bit = yes
  164. EOF
  165. # Set xl2tpd options
  166. cat > /etc/ppp/options.xl2tpd <<EOF
  167. +mschap-v2
  168. ipcp-accept-local
  169. ipcp-accept-remote
  170. noccp
  171. auth
  172. mtu 1280
  173. mru 1280
  174. proxyarp
  175. lcp-echo-failure 4
  176. lcp-echo-interval 30
  177. connect-delay 5000
  178. ms-dns $DNS_SRV1
  179. EOF
  180. if [ -z "$VPN_DNS_SRV1" ] || [ -n "$VPN_DNS_SRV2" ]; then
  181. cat >> /etc/ppp/options.xl2tpd <<EOF
  182. ms-dns $DNS_SRV2
  183. EOF
  184. fi
  185. # Create VPN credentials
  186. cat > /etc/ppp/chap-secrets <<EOF
  187. "$VPN_USER" l2tpd "$VPN_PASSWORD" *
  188. EOF
  189. VPN_PASSWORD_ENC=$(openssl passwd -1 "$VPN_PASSWORD")
  190. cat > /etc/ipsec.d/passwd <<EOF
  191. $VPN_USER:$VPN_PASSWORD_ENC:xauth-psk
  192. EOF
  193. # Create PPTP config
  194. cat >/etc/pptpd.conf <<END
  195. option /etc/ppp/options.pptpd
  196. logwtmp
  197. localip 192.168.41.1
  198. remoteip 192.168.41.10-100
  199. END
  200. cat >/etc/ppp/options.pptpd <<END
  201. name pptpd
  202. refuse-pap
  203. refuse-chap
  204. refuse-mschap
  205. require-mschap-v2
  206. require-mppe-128
  207. ms-dns 8.8.8.8
  208. ms-dns 8.8.4.4
  209. proxyarp
  210. lock
  211. nobsdcomp
  212. novj
  213. novjccomp
  214. nologfd
  215. END
  216. bigecho "Updating IPTables rules..."
  217. service fail2ban stop >/dev/null 2>&1
  218. iptables -t nat -I POSTROUTING -s 192.168.43.0/24 -o $NET_IFACE -j MASQUERADE
  219. iptables -t nat -I POSTROUTING -s 192.168.42.0/24 -o $NET_IFACE -j MASQUERADE
  220. iptables -t nat -I POSTROUTING -s 192.168.41.0/24 -o $NET_IFACE -j MASQUERADE
  221. if [[ ${OS} == "centos" ]]; then
  222. service iptables save
  223. iptables-restore < /etc/sysconfig/iptables
  224. else
  225. iptables-save > /etc/iptables.up.rules
  226. iptables-restore -t < /etc/iptables.up.rules
  227. netfilter-persistent save
  228. netfilter-persistent reload
  229. fi
  230. bigecho "Enabling services on boot..."
  231. systemctl enable xl2tpd
  232. systemctl enable ipsec
  233. systemctl enable pptpd
  234. for svc in fail2ban ipsec xl2tpd; do
  235. update-rc.d "$svc" enable >/dev/null 2>&1
  236. systemctl enable "$svc" 2>/dev/null
  237. done
  238. bigecho "Starting services..."
  239. sysctl -e -q -p
  240. chmod 600 /etc/ipsec.secrets* /etc/ppp/chap-secrets* /etc/ipsec.d/passwd*
  241. mkdir -p /run/pluto
  242. service fail2ban restart 2>/dev/null
  243. service ipsec restart 2>/dev/null
  244. service xl2tpd restart 2>/dev/null
  245. wget -O /usr/bin/add-l2tp https://raw.githubusercontent.com/alifnurmareta/bc/main/add-l2tp.sh && chmod +x /usr/bin/add-l2tp
  246. wget -O /usr/bin/del-l2tp https://raw.githubusercontent.com/alifnurmareta/bc/main/del-l2tp.sh && chmod +x /usr/bin/del-l2tp
  247. wget -O /usr/bin/add-pptp https://raw.githubusercontent.com/alifnurmareta/bc/main/add-pptp.sh && chmod +x /usr/bin/add-pptp
  248. wget -O /usr/bin/del-pptp https://raw.githubusercontent.com/alifnurmareta/bc/main/del-pptp.sh && chmod +x /usr/bin/del-pptp
  249. wget -O /usr/bin/renew-pptp https://raw.githubusercontent.com/alifnurmareta/bc/main/renew-pptp.sh && chmod +x /usr/bin/renew-pptp
  250. wget -O /usr/bin/renew-l2tp https://raw.githubusercontent.com/alifnurmareta/bc/main/renew-l2tp.sh && chmod +x /usr/bin/renew-l2tp
  251. touch /var/lib/premium-script/data-user-l2tp
  252. touch /var/lib/premium-script/data-user-pptp
  253. rm -f /root/ipsec.sh