Serveur OpenVPN

De Le Wiki du Forum-Debian.fr
Aller à la navigation Aller à la recherche

Nous détaillerons dans ce tuto le déploiement d'un serveur OpenVPN pour une utilisation en road warrior - utilisateur(s) isolé(s) vers un serveur OpenVPN. Le mode "bridged" sera abordé plus tard.

OpenVPN, c'est quoi, ça sert à quoi ?

OpenVPN est un système de réseau privé virtuel développé par James Yonan (jim@yonan.net) sur le protocole SSL et non sur le protocole IPSEC comme la plupart des VPN.

VPN est l'acronyme de Virtual Private Network. Il s'agit d'un tube virtuel qui crée un réseau privé au travers d'un réseau public.Openvpn est utilisé pour relier deux ordinateurs (ou deux réseaux) de façon sécurisée au travers d'un tunnel crypté.

Son rôle est de "tunneliser", de manière sécurisée, des données sur un seul port TCP/UDP à travers un réseau non sûr comme Internet et ainsi établir des VPNs.

Il existe deux méthodes pour faire tourner OpenVPN:

- Le mode "routed" plus simple, est généralement utilisé pour connecter des utilisateurs depuis l'extérieur d'un réseau (les roadwarriors");

- Le mode "Bridged" plus compliqué à installer, mais un peu plus rapide. Il est généralement utilisé pour interconnecter deux réseaux entre eux.

Voici un exemple de VPN site à site:

Les clients se connectant à partir d'une machine isolée à un tunnel VPN sont nommées "Road Warriors"...

Voici un exemple de VPN "nomade" à site:



Installation du programme

Sur le serveur Debian:

# apt-get install openvpn openssl

Sur le client Debian:

# apt-get install openvpn

Mise en oeuvre de la PKI (Génération des certificats et clefs avec Openvpn)

PKI ( "Public Key Infrastructure"=Infrastructure de clé publique )

Nous utiliserons évidemment les utilitaires fournis par openvpn qui simplifient grandement le travail...

# cp -R /usr/share/doc/openvpn/examples/easy-rsa/ /etc/openvpn
# cd /etc/openvpn/easy-rsa/2.0

Quelques modifications pour coller à nos besoins:

# nano vars
export KEY_COUNTRY="MG"
export KEY_PROVINCE="IH"export KEY_CITY="Quelquepart"
export KEY_ORG="serveur.domaine.org"
export KEY_EMAIL="admin@domaine.org"
# . ./vars NOTE: If you run ./clean-all, I will be doing a rm -rf on /etc/openvpn/easy-rsa/2.0/keys
# ./clean-all
# ./build-ca
Generating a 1024 bit RSA private key
...........................................++++++
................++++++ writing new private key to 'ca.key'
----- You are about to be asked to enter information that will be incorporated
into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [MG]:
State or Province Name (full name) [IH]:
Locality Name (eg, city) [Quelquepart]:
Organization Name (eg, company) [serveur.domaine.org]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) [serveur.domaine.org CA]:
Name []:
Email Address [admin@domaine.org]:

Génération du certificat et des clefs pour le Serveur

# ./build-key-server server
Generating a 1024 bit RSA private key
...........................................++++++
................++++++ writing new private key to 'ca.key'
----- You are about to be asked to enter information that will be incorporated
into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [MG]:
State or Province Name (full name) [IH]:
Locality Name (eg, city) [Quelquepart]:
Organization Name (eg, company) [serveur.domaine.org]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) [server]:
Name []:Email Address [admin@domaine.org]:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
Using configuration from /usr/share/doc/openvpn/examples/easy-rsa/2.0/openssl.cnf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
countryName           :PRINTABLE:'MG'
stateOrProvinceName   :PRINTABLE:'IH'
localityName          :PRINTABLE:'Quelquepart'
organizationName      :PRINTABLE:'serveur.domaine.org'
commonName            :PRINTABLE:'server'
emailAddress          :IA5STRING:'admin@domaine.org'
Certificate is to be certified until May  5 14:27:03 2021 GMT (3650 days)
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entriesData Base Updated

Attention, il est important de répondre Y (oui) aux deux question suivantes:

Sign the certificate? [y/n]:y
commit? [y/n]y

Génération des certificats et des clefs pour le Client

# ./build-key client1
Generating a 1024 bit RSA private key
...........................................++++++
................++++++ writing new private key to 'ca.key'
----- You are about to be asked to enter information that will be incorporated
into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [MG]:
State or Province Name (full name) [IH]:
Locality Name (eg, city) [Quelquepart]:
Organization Name (eg, company) [serveur.domaine.org]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) [client1]:
Name []:
Email Address [admin@domaine.org]:
Please enter the following 'extra' attributesto be sent with your certificate request
A challenge password []:
An optional company name []:
Using configuration from /usr/share/doc/openvpn/examples/easy-rsa/2.0/openssl.cnf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
countryName           :PRINTABLE:'MG'
stateOrProvinceName   :PRINTABLE:'IH'
localityName          :PRINTABLE:'Quelquepart'
organizationName      :PRINTABLE:'serveur.domaine.org'
commonName            :PRINTABLE:'client1'
emailAddress          :IA5STRING:'admin@domaine.org'
Certificate is to be certified until May  5 14:28:36 2021 GMT (3650 days)
Sign the certificate? [y/n]:y 1 out of 1 certificate requests certified, commit? [y/n]y Write out database with 1 new entries Data Base Updated

Si vous avez d'autres clients, c'est simple...

# ./build-key client2
# ./build-key client3

Generate Diffie Hellman parameters

Generating DH parameters, 1024 bit long safe prime, generator 2 This is going to take a long time ....................................................................................+................................................................................................................+...........................................................+...........................+.......+.........................+...............................................................................................................................+........+.+...........................................................................+...............+..............+....................................................+................................+..............................................................................+...................+..........+........................................+..........................+.......................................................................................+..................+.....................+........................+................................................................+.........................................................+.....................................................................+.......................+.......+.........................................................................................................................................................................................+............+......................+..............+..........................+...................................................+......+.+....+................................+...............................................................................................+..............................+...+..............................................................+...................+...............................+.............................................................................................................................+..............................................................................................+................+...............+.................................................................................................................................................................................................+........................................................++*++*++*

Résultats

# vdir
total 72
-rw-r--r-- 1 root root 3827 10 mai   10:17 01.pem
-rw-r--r-- 1 root root 3698 10 mai   10:18 02.pem
-rw-r--r-- 1 root root 1184 10 mai   10:17 ca.crt (Root CA certificate - server + all clients)
-rw------- 1 root root  887 10 mai   10:17 ca.key (Root CA key - SECRET - key signing machine only)
-rw-r--r-- 1 root root  245 10 mai   10:18 dh1024.pem (Diffie Hellman parameters - server only)
-rw-r--r-- 1 root root 3698 10 mai   10:18 client1.crt (Client1 Certificate - client1 only)
-rw-r--r-- 1 root root  664 10 mai   10:18 client1.csr (Client1 Key - SECRET - client1 only)
-rw------- 1 root root  887 10 mai   10:18 client1.key
-rw-r--r-- 1 root root  203 10 mai   10:18 index.txt
-rw-r--r-- 1 root root   21 10 mai   10:18 index.txt.attr
-rw-r--r-- 1 root root   21 10 mai   10:17 index.txt.attr.old
-rw-r--r-- 1 root root  103 10 mai   10:17 index.txt.old
-rw-r--r-- 1 root root    3 10 mai   10:18 serial
-rw-r--r-- 1 root root    3 10 mai   10:17 serial.old
-rw-r--r-- 1 root root 3827 10 mai   10:17 server.crt (Server Certificate - server only)
-rw-r--r-- 1 root root  668 10 mai   10:17 server.csr
-rw------- 1 root root  887 10 mai   10:17 server.key (Server Key - SECRET - server only)
-rw------- 1 root root  636 16 mai   17:37 ta.key

Configuration du serveur

Configuration de openvpn

Créez un lien symbolique ou copiez le répertoire contenant les clefs:

# ln -s /etc/openvpn/easy-rsa/2.0/keys/ /etc/openvpn

ou

# cp -r /etc/openvpn/easy-rsa/2.0/keys/ /etc/openvpn

Création du fichier de configuration du serveur:

# nano /etc/openvpn/server.conf
# Port protocole et interface
port 1194
proto udp
dev tun

# Chemin vers les fichiers ssl
ca keys/ca.crt
cert keys/server.crt
key keys/server.key
dh keys/dh1024.pem

# IP désirée pour le serveur
server 10.8.0.0 255.255.255.0
ifconfig-pool-persist ipp.txt
keepalive 10 120
comp-lzo
user nobody
group nogroup
persist-key
persist-tun
status openvpn-status.log
verb 3

# On définit le serveur VPN comme passerelle par défaut pour les clients.
push "redirect-gateway def1"
push "route 10.9.8.0 255.255.252.0"
# On définit le serveur VPN comme DNS par défaut
push "dhcp-option DNS 10.8.0.1"

Configuration sur le serveur d’IPTABLES en mode "routed"

Règles nécessaires au bon fonctionnement d’OpenVPN en full-tunneling :

iptables -A FORWARD -i tun0 -o eth0 -j ACCEPT
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE

Activer le forwarding :

echo 1 > /proc/sys/net/ipv4/ip_forward

Attention, l'ip_forwarding ne sera pas conservé au prochain démarrage, il faudra éditer le fichier et changer manuellement le 0 en 1

# nano /proc/sys/net/ipv4/ip_forward 1

Configuration du client

Avec une clef usb récupérez les fichiers suivants sur le serveur:

  1. ca.crt
  2. client1.crt# client1.key

Que vous copierez directement dans le dossier /etc/openvpn/

Puis vous créez un fichier de configuration:

# cat /etc/openvpn/client1.conf
client
dev tun proto udp remote ip_du_serveur 1194 resolv-retry infinite nobind persist-key persist-tun ca ca.crt cert eee.crt key eee.key comp-lzo verb 3 pull

Tests

Lancer le serveur:

# cd /etc/openvpn
# openvpn server.conf
Wed May 18 15:32:49 2011 OpenVPN 2.1.3 x86_64-pc-linux-gnu [SSL] [LZO2] [EPOLL] [PKCS11] [MH] [PF_INET6] [eurephia] built on Oct 22 2010 Wed May 18 15:32:49 2011 NOTE: OpenVPN 2.1 requires '--script-security 2' or higher to call user-defined scripts or executables Wed May 18 15:32:49 2011 Diffie-Hellman initialized with 1024 bit key Wed May 18 15:32:49 2011 /usr/bin/openssl-vulnkey -q -b 1024 -m <modulus omitted> Wed May 18 15:32:50 2011 TLS-Auth MTU parms [ L:1542 D:138 EF:38 EB:0 ET:0 EL:0 ] Wed May 18 15:32:50 2011 Socket Buffers: R=[124928->131072] S=[124928->131072] Wed May 18 15:32:50 2011 ROUTE default_gateway=10.9.8.254 Wed May 18 15:32:50 2011 TUN/TAP device tun0 opened Wed May 18 15:32:50 2011 TUN/TAP TX queue length set to 100 Wed May 18 15:32:50 2011 /sbin/ifconfig tun0 10.8.0.1 pointopoint 10.8.0.2 mtu 1500 Wed May 18 15:32:50 2011 /sbin/route add -net 10.8.0.0 netmask 255.255.255.0 gw 10.8.0.2 Wed May 18 15:32:50 2011 Data Channel MTU parms [ L:1542 D:1450 EF:42 EB:135 ET:0 EL:0 AF:3/1 ] Wed May 18 15:32:50 2011 GID set to nogroup Wed May 18 15:32:50 2011 UID set to nobody Wed May 18 15:32:50 2011 UDPv4 link local (bound): [undef] Wed May 18 15:32:50 2011 UDPv4 link remote: [undef] Wed May 18 15:32:50 2011 MULTI: multi_init called, r=256 v=256 Wed May 18 15:32:50 2011 IFCONFIG POOL: base=10.8.0.4 size=62 Wed May 18 15:32:50 2011 IFCONFIG POOL LIST Wed May 18 15:32:50 2011 eee,10.8.0.4 Wed May 18 15:32:50 2011 Initialization Sequence Completed

Sur le client:

# cd /etc/openvpn
# openvpn client1.conf
Wed May 18 15:47:31 2011 OpenVPN 2.1.3 i486-pc-linux-gnu [SSL] [LZO2] [EPOLL] [PKCS11] [MH] [PF_INET6] [eurephia] built on Oct 21 2010 Wed May 18 15:47:31 2011 WARNING: No server certificate verification method has been enabled.  See http://openvpn.net/howto.html#mitm for more info. Wed May 18 15:47:31 2011 NOTE: OpenVPN 2.1 requires '--script-security 2' or higher to call user-defined scripts or executables Wed May 18 15:47:31 2011 /usr/bin/openssl-vulnkey -q -b 1024 -m <modulus omitted> Wed May 18 15:47:31 2011 LZO compression initialized Wed May 18 15:47:31 2011 Control Channel MTU parms [ L:1542 D:138 EF:38 EB:0 ET:0 EL:0 ] Wed May 18 15:47:31 2011 Socket Buffers: R=[112640->131072] S=[112640->131072] Wed May 18 15:47:31 2011 Data Channel MTU parms [ L:1542 D:1450 EF:42 EB:135 ET:0 EL:0 AF:3/1 ] Wed May 18 15:47:31 2011 Local Options hash (VER=V4): '41690919' Wed May 18 15:47:31 2011 Expected Remote Options hash (VER=V4): '530fdded' Wed May 18 15:47:31 2011 UDPv4 link local: [undef] Wed May 18 15:47:31 2011 UDPv4 link remote: [AF_INET]41.188.26.122:1194 Wed May 18 15:47:32 2011 TLS: Initial packet from [AF_INET]41.188.26.122:1194, sid=c1e74ce6 3d6cd1b4 Wed May 18 15:47:34 2011 VERIFY OK: depth=1, /C=MG/ST=IH/L=quelquepart/O=SHI/CN=SHI_CA/emailAddress=admin@domaine.org Wed May 18 15:47:34 2011 VERIFY OK: depth=0, /C=MG/ST=IH/L=quelquepart/O=SHI/CN=server/emailAddress=admin@domaine.org Wed May 18 15:47:39 2011 Data Channel Encrypt: Cipher 'BF-CBC' initialized with 128 bit key Wed May 18 15:47:39 2011 Data Channel Encrypt: Using 160 bit message hash 'SHA1' for HMAC authentication Wed May 18 15:47:39 2011 Data Channel Decrypt: Cipher 'BF-CBC' initialized with 128 bit key Wed May 18 15:47:39 2011 Data Channel Decrypt: Using 160 bit message hash 'SHA1' for HMAC authentication Wed May 18 15:47:39 2011 Control Channel: TLSv1, cipher TLSv1/SSLv3 DHE-RSA-AES256-SHA, 1024 bit RSA Wed May 18 15:47:39 2011 [server] Peer Connection Initiated with [AF_INET]41.188.26.122:1194 Wed May 18 15:47:41 2011 SENT CONTROL [server]: 'PUSH_REQUEST' (status=1) Wed May 18 15:47:42 2011 PUSH: Received control message: 'PUSH_REPLY,redirect-gateway def1,route 10.9.8.0 255.255.252.0,dhcp-option DNS 10.8.0.1,route 10.8.0.1,topology net30,ping 10,ping-restart 120,ifconfig 10.8.0.6 10.8.0.5' Wed May 18 15:47:42 2011 OPTIONS IMPORT: timers and/or timeouts modified Wed May 18 15:47:42 2011 OPTIONS IMPORT: --ifconfig/up options modified Wed May 18 15:47:42 2011 OPTIONS IMPORT: route options modified Wed May 18 15:47:42 2011 OPTIONS IMPORT: --ip-win32 and/or --dhcp-option options modified Wed May 18 15:47:42 2011 ROUTE: default_gateway=UNDEF Wed May 18 15:47:42 2011 TUN/TAP device tun0 opened Wed May 18 15:47:42 2011 TUN/TAP TX queue length set to 100 Wed May 18 15:47:42 2011 /sbin/ifconfig tun0 10.8.0.6 pointopoint 10.8.0.5 mtu 1500 Wed May 18 15:47:42 2011 NOTE: unable to redirect default gateway -- Cannot read current default gateway from system Wed May 18 15:47:42 2011 /sbin/route add -net 10.9.8.0 netmask 255.255.252.0 gw 10.8.0.5 Wed May 18 15:47:42 2011 /sbin/route add -net 10.8.0.1 netmask 255.255.255.255 gw 10.8.0.5 Wed May 18 15:47:42 2011 Initialization Sequence Completed

A ce moment là vous devez être capable, depuis le serveur de "pinguer" le client, et depuis le client de pinguer le serveur et les autres machines du sous-réseau.

Voilà votre serveur OpenVPN est prêt!

Il ne reste plus qu'a le lancer:

# service openvpn start

Au prochain démarrage des machines (serveur et clients) le service sera automatiquement lancé avec tous les fichiers de configurations qui se trouvent dans le répertoire /etc/openvpn. Vous pouvez ainsi lancer plusieurs instances de serveurs ou de clients.


A faire

Serveur OpenVPN en mode bridged

Références

Tuto de mattotop (mode bridged) sur forum.debian-fr.org

site officiel de OpenVPN

Howto en Français



Lol 18 mai 2011 à 08:23 (CDT)