VPN – PPP over SSH sous Debian / Ubuntu

Nous avons configuré notre firewall et Openssh de manière à ouvrir des sessions ssh par échange de clés.
Comme je l’ai déjà mentionné, le wifi n’est pas très sécurisé. Notre DMZ wifi n’a pas plus de droits que les machines provenant d’Internet. Pour travailler avec plus de confort, nous allons établir un VPN entre le portable et notre firewall. Le portable pourra ainsi communiquer avec le réseau local en toute sécurité.
Comme ssh est déjà bien configuré, c’est un jeu d’enfant de réaliser une connexion ppp over ssh comme nous allons le voir.

La configuration de notre VPN va se dérouler en 3 temps.
Configuration du firewall pour qu’il ait une nouvelle interface ppp0, et que cette interface puisse communiquer librement avec notre LAN sur eth1
Configuration de pppd sur le client et sur le serveur
Script de lancement de la connexion ppp over ssh et routage sur le client (pour que les applications utilisent bien ppp0 pour aller sur le LAN, et non pas un autre chemin.

Configuration du firewall :

Les lignes qui ont été ajoutées pour l’occasion sont en rouge.
La connexion ppp qui s’établit entre le portable Horace et le firewall Pollux reçoit le plan d’adressage 192.168.3.1 côté client et 192.168.3.2 côté serveur.

#!/bin/sh
# /usr/local/bin/vpn-pppssh
#
# This script initiates a ppp-ssh vpn connection.
# see the VPN PPP-SSH HOWTO on http://www.linuxdoc.org for more information.
#
# revision history:
# 1.6 11-Nov-1996 miquels@cistron.nl
# 1.7 20-Dec-1999 bart@jukie.net
# 2.0 16-May-2001 bronson@trestle.com
# 2.1 23-jan-2006 anthony@nonsenz.org

#
# You will need to change these variables…
#

# The host name or IP address of the SSH server that we are
# sending the connection request to:
SERVER_HOSTNAME=pollux.nonsenz.org

# The TCP port used by sshd (usually 22)
SERVER_PORT=443

# The username on the VPN server that will run the tunnel.
# For security reasons, this should NOT be root. (Any user
# that can use PPP can intitiate the connection on the client)
SERVER_USERNAME=nonsenz

# The VPN network interface on the server should use this address:
SERVER_IFIPADDR=192.168.3.2

# …and on the client, this address:
CLIENT_IFIPADDR=192.168.3.1

# Lan behind vpn server uses this addresses:
LAN_NETWORK=192.168.0.0
LAN_NETMASK=255.255.255.0

# This tells ssh to use unprivileged high ports, even though it’s
# running as root. This way, you don’t have to punch custom holes
# through your firewall.
LOCAL_SSH_OPTS= »-P »

#
# The rest of this file should not need to be changed.
#

PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/bin/X11/:

#
# required commands…
#

PPPD=/usr/sbin/pppd
SSH=/usr/bin/ssh

if ! test -f $PPPD ; then echo « can’t find $PPPD »; exit 3; fi
if ! test -f $SSH ; then echo « can’t find $SSH »; exit 4; fi

case « $1 » in
start)
echo -n « Starting vpn to $SERVER_HOSTNAME:  »
${PPPD} updetach noauth passive pty « sudo -u ${SERVER_USERNAME} ${SSH} ${LOCAL_SSH_OPTS} ${SERVER_HOSTNAME} -p ${SERVER_PORT} -l ${SERVER_USERNAME} -o Batchmode=yes sudo ${PPPD} nodetach notty noauth » ${CLIENT_IFIPADDR}:${SERVER_IFIPADDR}
route add -net ${LAN_NETWORK} netmask ${LAN_NETMASK} gw ${SERVER_IFIPADDR} dev ppp0
echo  » vpn connected. »
;;

stop)
echo -n « Stopping vpn to $SERVER_HOSTNAME:  »
PID=`ps ax | grep « ${SSH} ${LOCAL_SSH_OPTS} ${SERVER_HOSTNAME} -p ${SERVER_PORT} -l ${SERVER_USERNAME} -o » | grep -v ‘ passive ‘ | grep -v ‘grep ‘ | awk ‘{print $1}’`
if [ « ${PID} » != «  » ]; then
kill $PID
echo « disconnected. »
else
echo « Failed to find PID for the connection »
fi
;;

config)
echo « SERVER_HOSTNAME=$SERVER_HOSTNAME »
echo « SERVER_USERNAME=$SERVER_USERNAME »
echo « SERVER_IFIPADDR=$SERVER_IFIPADDR »
echo « CLIENT_IFIPADDR=$CLIENT_IFIPADDR »
;;

*)
echo « Usage: vpn {start|stop|config} »
exit 1
;;
esac

exit 0

Pour lancer le script, sudo vpn-pppssh start et pour l’arrêter sudo vpn-pppssh stop.
Normalement ça fonctionne.

nonsenz@horace:~$ sudo vpn-pppssh start
Password:
Starting vpn to pollux.nonsenz.org: Using interface ppp0
Connect: ppp0 <–> /dev/pts/2
Deflate (15) compression enabled
local IP address 192.168.3.1
remote IP address 192.168.3.2
vpn connected.
nonsenz@horace:~$ route -n
Table de routage IP du noyau
Destination Passerelle Genmask Indic Metric Ref Use Iface
192.168.3.2 0.0.0.0 255.255.255.255 UH 0 0 0 ppp0
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
192.168.0.0 192.168.3.2 255.255.255.0 UG 0 0 0 ppp0
0.0.0.0 192.168.1.5 0.0.0.0 UG 0 0 0 eth1
nonsenz@horace:~$ ping 192.168.0.1
PING 192.168.0.1 (192.168.0.1) 56(84) bytes of data.
64 bytes from 192.168.0.1: icmp_seq=1 ttl=63 time=25.8 ms
64 bytes from 192.168.0.1: icmp_seq=2 ttl=63 time=8.35 ms
64 bytes from 192.168.0.1: icmp_seq=3 ttl=63 time=1.93 ms

— 192.168.0.1 ping statistics —
3 packets transmitted, 3 received, 0% packet loss, time 2001ms
rtt min/avg/max/mdev = 1.937/12.049/25.855/10.108 ms

Maintenant, un petit hack pour lancer votre vpn depuis un réseau local qui ne laisse sortir que les protocoles courants http, https et ce via un proxy http.

  • Sur le serveur, on lance openssh sur le port 443 réservé habituellement à https (on peut lancer openssh-server sur deux ports simultanément, 22 et 443 par exemple)
  • Dans le script ci-dessus, on utilise le SERVER_PORT=443
  • On installe ensuite le logiciel corkscrew sur le client
  • On ajoute une ligne au fichier ~/.ssh/config du client pour que ssh utilise corkscrew (voir README de corkscrew).

Laisser un commentaire