Informations :
Dates
- Publish : : Thursday 18 september 2014
- Modification : Thursday 15 december 2016
Share :
Je vais décrire comment configurer un client VPN par les protocoles IPSec et XL2TP.
Installation des paquets IPSec
Il faut installer un xSecureWAN ;) il y quelques temps çà s'appellait FreeSWAN puis OpenSWAN ;)
De nôs jours c'est :strongSWAN
!
Script avec 1 ligne
001aptitude install strongswan xl2tpd
Configuration d'IPSec
Nous allons configurer IPSec : vim /etc/ipsec.conf
.
Script avec 29 lignes
001version 2.0 002config setup 003 nat_traversal=yes 004 virtual_private=%v4:10.0.0.0/8 005 oe=off 006 protostack=netkey 007 uniqueids=no 008# plutodebug=all 009 klipsdebug=none 010 plutoopts="--interface=vmbr1" 011 012conn net.zw3b.fr 013 authby=secret 014 pfs=no 015 auto=add 016 keyingtries=3 017 dpddelay=30 018 dpdtimeout=120 019 dpdaction=clear 020 rekey=no 021 keylife=24h 022 ikelifetime=6h 023 type=transport 024 left=%defaultroute 025 leftprotoport=17/1701 026 # 027 right=IP_DU_SERVER_VPN 028 rightid=@net.zw3b.vpn 029 rightprotoport=17/1701
Puis on ajoute la passphrase dans /etc/ipsec.secrets
Script avec 1 ligne
001%any @net.zw3b.vpn : PSK "ma clef a partager"
Configuration de XL2TP
Nous allons configurer XL2TP : vim /etc/xl2tpd/xl2tpd.conf
.
Script avec 25 lignes
001[global] 002;auth file = /etc/xl2tpd/xl2tpd-secrets 003ipsec saref = yes 004;saref refinfo = 30 005;listen-addr = 0.0.0.0 006;port = 1701 007; utilse pour la conf par default(a sup si radius) 008access control = no 009rand source = dev 010; 011;debug avp = yes 012;debug network = yes 013;debug packet = no 014;debug state = yes 015;debug tunnel = yes 016; 017[lac net] 018lns = IP_DU_SERVER_VPN 019;require authentication = yes 020;require chap = yes 021;refuse pap = yes 022;length bit = yes 023;name = p2.zw3b.vpn 024ppp debug = yes 025pppoptfile = /etc/ppp/options.net.client.xl2tpd
Configuration de PPP
Nous allons configurer PPP : vim /etc/ppp/options.net.client.xl2tpd
.
Script avec 47 lignes
001# Do not support BSD compression. 002nobsdcomp 003passive 004lock 005 006# Allow all usernames to connect. 007#name * 008#proxyarp 009#ipcp-accept-local 010#ipcp-accept-remote 011#lcp-echo-failure 10 012lcp-echo-interval 30 013nodeflate 014 015# Do not authenticate incoming connections. This is handled by IPsec. 016#noauth 017#refuse-chap 018#refuse-mschap 019#refuse-mschap-v2 020# Authentifition 021name pc_1.zw3b.vpn 022password mot2passe4client_1 023 024# Authentification 025noauth 026#require-chap 027#require-mschap 028#require-mschap-v2 029#hide-password 030#modem 031 032# Set the DNS servers the PPP clients will use. 033#ms-dns 8.8.8.8 034#ms-dns 213.186.33.99 035 036mtu 1400 037mru 1400 038 039# Options 040refuse-eap 041noccp 042crtscts 043idle 1800 044#defaultroute 045nodefaultroute 046 047logfile /var/log/ppp.log
Script de connexion au serveur VPN : IPSEC+XL2TP
Ce script démare les services ipsec
et xl2tpd
. Puis lance la connexion ipsec auto --up net.zw3b.fr
puis une lance la connexion xl2tp
au LAC net
pour enfin ajouter au client les routes que le serveur pourait distribuer.
vi /root/vpn-client-zw3b.sh
Script avec 57 lignes
001#!/bin/bash 002 003#####---------------------------- INFORMATIONS --------------------------------##### 004# 005# Name : vpn-client-zw3b.sh 006# Desc : Script de connexion au serveur VPN : IPSEC+XL2TP 007# Auteur : O.Romain Jaillet-ramey alias Kss* 008# Mail : orj AT lab3w DOT fr 009# Date : 2014-05-05 010# 011#####---------------------------- INFORMATIONS --------------------------------##### 012 013 014#####--------------------------------------------------------------------------##### 015 016function ipsec_l2tp() 017{ 018 /etc/init.d/ipsec 019 /etc/init.d/xl2tpd 020} 021 022#####--------------------------------------------------------------------------##### 023 024 025#####--------------------------------------------------------------------------##### 026case "" in 027 028start|restart) 029{content_text} stop 030echo "{content_text} Starting" 031 ipsec_l2tp start 032 sleep 2 033 /usr/sbin/ipsec auto --up net.zw3b.fr 034 sleep 2 035 /bin/echo "c net" >> /var/run/xl2tpd/l2tp-control 036 sleep 10 037 /sbin/route add -net 172.16.5.0 netmask 255.255.255.0 dev ppp0 038 /bin/echo "VPN ZW3B CONNECTION UP" 039;; 040 041stop) 042echo "{content_text} Stop" 043 /sbin/route del -net 172.16.5.0 netmask 255.255.255.0 dev ppp0 044 sleep 2 045 /bin/echo "d net" > /var/run/xl2tpd/l2tp-control 046 sleep 2 047 /usr/sbin/ipsec auto --down net.zw3b.fr 048 ipsec_l2tp stop 049 /bin/echo "VPN ZW3B CONNECTION DOWN" 050;; 051 052*) 053/bin/echo "usage : {content_text} (start|stop|restart)" 054;; 055 056esac 057#####--------------------------------------------------------------------------#####
Script qui check si la connexion VPN est active.
Ce script vérifie que la connexion PPP est active vérifiant que l'adresse IP sur l'interface ppp0
existe. En cas d'erreur on lance le script /repertoire/vpn-net-client.sh restart
de connexion au serveur VPN IPSec + XL2TP
vi /root/vpn-client-zw3b-check.sh
Script avec 30 lignes
001#!/bin/bash 002 003#####---------------------------- INFORMATIONS --------------------------------##### 004# 005# Name : vpn-client-zw3b-check.sh 006# Desc : Script de connexion au serveur VPN : IPSEC+XL2TP 007# Auteur : O.Romain Jaillet-ramey alias Kss* 008# Mail : orj AT lab3w DOT fr 009# Date : 2014-10-08 010# 011#####---------------------------- INFORMATIONS --------------------------------##### 012 013HOSTNAME=`/bin/hostname` 014NOW=$(date +"%b %d %H:%M:%S") 015 016LOCAL_IP="172.16.5.253" 017 018LOCAL_SEARCH_IP=`/sbin/ifconfig |/bin/grep "${LOCAL_IP}"|/usr/bin/cut -d":" -f2|/usr/bin/cut -d" " -f1` 019LOCAL_SEARCH_IF=`/sbin/ifconfig |/bin/grep -B 1 "${LOCAL_IP}" |/usr/bin/cut -d" " -f1 |/bin/sed '/^$/d'` 020 021 022if [ "${LOCAL_SEARCH_IP}" = ${LOCAL_IP} ] 023then 024 /bin/echo $NOW $HOSTNAME VPN STD: Iface $LOCAL_SEARCH_IP sur $LOCAL_SEARCH_IF OK 025 exit 2 026else 027 /bin/echo $NOW $HOSTNAME VPN STD: On relance la connexion.... 028 /root/vpn-net-client.sh restart 029 /bin/echo $NOW $HOSTNAME VPN STD: Connexion OK 030fi
NdM : Pour le cas où vous ayez une deconnexion ;)
Puis on met le script vvpn-client-zw3b-check.sh
en crontab -e
.
Script avec 1 ligne
001*/5 * * * * sh /root/vpn-client-zw3b-check.sh 1>>/var/log/syslog 2>/dev/null 2>&1
Autre Script qui check si la connexion IPSEC/XLTP est active.
Ce script vérifie que la connexion IPSEC est active vérifiant avec la comande ipsec status
que la connexion est bien établie, puis on vérifie que l'interface ppp0
existe en vérifiant l'adresse IP du serveur existe et est associée à celle de notre client. En cas d'erreur on lance le script de démarrage du client VPN IPSec + XL2TP.
Script avec 97 lignes
001#!/bin/bash 002 003#####---------------------------- INFORMATIONS --------------------------------##### 004# 005# Name : vpn-client-zw3b-check-connection.sh 006# Desc : Script qui verifie que la connection (IPSEC/XL2TP) au serveur existe 007# Auteur : O.Romain Jaillet-ramey alias Kss* 008# Mail : orj AT lab3w DOT fr 009# Date creation : 2016-07-23 010# Date modification : 2016-08-03 011# 012#####---------------------------- INFORMATIONS --------------------------------##### 013 014#####---------------------------- CONFIG VPN ZWB ------------------------------##### 015 016HOSTNAME=`/bin/hostname` 017NOW=$(date +"%b %d %H:%M:%S") 018NOW=$(date) 019 020ZWB_NAME="ZW3B" 021ZWB_HOST="zwb.lab3w.fr" 022ZWB_IP="172.16.5.199" 023 024CLIENT_IP="172.16.5.254" 025CLIENT_MAIL="admins@domain.tld" 026 027#####---------------------------- CONFIG VPN ZWB ------------------------------##### 028 029#####---------------------------- SCRIPT --------------------------------------##### 030 031function check_zwb_l2tp() 032{ 033 ZWB_SEARCH_IP=`/sbin/ifconfig |/bin/grep "${ZWB_IP}"|/usr/bin/cut -d":" -f2|/usr/bin/cut -d" " -f1` 034 #ZWB_REPONSE=`ping -c1 ${ZWB_IP} |/bin/grep "received" |/usr/bin/cut -d"," -f2 |/usr/bin/cut -d" " -f2` 035 036 #if [ "${ZWB_REPONSE}" -eq "1" ] 037 if [ "${ZWB_SEARCH_IP}" = "${CLIENT_IP}" ] 038 then 039 echo "1"; 040 else 041 echo "0"; 042 fi 043 044 exit 1; 045} 046 047function check_zwb_ipsec() 048{ 049 COMMAND="/usr/sbin/ipsec status ${ZWB_HOST}" 050 XL2TP_CHECK=$(check_zwb_l2tp) 051 052 if [[ -n "`${COMMAND} |/bin/grep "ESTABLISHED"`" ]] && [[ "${XL2TP_CHECK}" = "1" ]] 053 then 054 055 REQ=`$COMMAND |/bin/grep "ESTABLISHED" | cut -d" " -f3-4` 056 RES="$NOW $HOSTNAME VPN $ZWB_NAME: Connexion OK depuis $REQ" 057 058 echo $RES 059 060 else 061 MSG=" -> Restart du client VPN" 062 063 if [ "${XL2TP_CHECK}" = "0" ]; then 064 065 ERR="$NOW $HOSTNAME VPN $ZWB_NAME: Connexion DOWN -> Erreur XL2TP" 066 /root/vpn-client-zw3b.sh restart && echo "$ERR $MSG" | mail -s "[ERR]VPNClient" ${CLIENT_MAIL} 067 068 elif [ -z "`${COMMAND} |/bin/grep "ESTABLISHED"`" ]; then 069 070 ERR="$NOW $HOSTNAME VPN $ZWB_NAME: Connexion DOWN -> Erreur IPSEC" 071 /root/vpn-client.sh restart && echo "$ERR $MSG" | mail -s "[ERR]VPNClient" ${CLIENT_MAIL} 072 fi 073 074 echo "$ERR $MSG"; 075 fi 076 077 078 exit 1; 079 080 081 082} 083 084#####---------------------------- SCRIPT --------------------------------------##### 085 086 087#####---------------------------- USAGE ---------------------------------------##### 088case in 089 check) 090 091 check_zwb_ipsec 092;; 093*) 094 echo "Use : {content_text} check" 095;; 096esac 097#####---------------------------- USAGE --------------------------------------#####
Puis on met le script vvpn-client-zw3b-check-connection.sh
en tâches planifiées toutes les N minutes : crontab -e
.
Script avec 1 ligne
001*/5 * * * * /root/vpn-client-zw3b-check-connection.sh check 1>>/var/log/syslog 2>/dev/null 2>&1
qui devrait vous ressortir en syslog quelque chose comme çà :
Script avec 1 ligne
001Aug 14, 11:19:01 CLIENT VPN ZW3B: Connexion OK depuis 50 minutes.
Bonne connexion :)