Nei server di laboratorio, abbiamo installato vari tool aziendali di planning e gestione del personale, ovviamente raggiungibili soltanto dalla sede stessa. Avendo la necessità di esporre questi tool su internet, per usufruirne da qualsiasi sede, ed anche per eventuali condivisioni con il cliente finale. Per raggiungere questo obiettivo, abbiamo optato per un prodotto Opensource, OpenVPN.
Questo prodotto, permette ad un client opportunamente configurato, di accedere alla rete aziendale da qualsiasi parte del mondo.
Per la nostra VPN, abbiamo utilizzato un server con Centos 6.5 64 bit. Installiamo Openvpn:
yum install openvpn easy-rsa
Il pacchetto di OpenVPN fornisce una serie di script già pronti, per la gestione dei certificati di accesso. Copiamo tali script nella cartella di OpenVPN:
cp -r /usr/share/easy-rsa/2.0/ /etc/openvpn/rsa
Spostiamoci sotto la cartella apeena copiata:
cd /etc/openvpn/rsa
Apriamo il file “vars” e editiamo i campi, questo velocizzerà la creazione dei certificato, è comodo per chi ha la necessità di creare molti certificati. Un esempio del file vars:
export KEY_SIZE=1024
export KEY_COUNTRY="IT"
export KEY_PROVINCE="IT"
export KEY_CITY="Roma"
export KEY_ORG="Converger"
export KEY_EMAIL="info@converger.it
Creiamo la nostra CA (certificate authority)
# source vars
# NOTE: If you run ./clean-all, I will be doing a rm -rf on /etc/openvpn/rsa/keys
# ./clean-all
Ora possiamo generare la nostra Certificate Authority: è necessario richiamare anche lo script “clean-all” per iniziare con un ambiente pulito. A questo punto siamo pronti per generare la nostra CA (certificate authority):
# ./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) [IT]:
State or Province Name (full name) [IT]:
Locality Name (eg, city) [Roma]:
Organization Name (eg, company) [Converger]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) [Converger CA]:
Email Address [info@converger.it]:
I valori di default vengono letti dal file vars sopra customizzato. Ora possiamo creare il certificato per il server VPN:
# ./build-key-server ConvergerVPN
ConvergerVPN è il nome della macchina su cui sto installando il server VPN, per coerenza la coppia chiave/certificato avrà il nome dell’host su cui viene usato.
Per evitare che ad ogni riavvio di OpenVPN sia richiesta una password premere invio senza inserire nulla alla richiesta di password:
Generating a 1024 bit RSA private key..................
++++++.++++++
writing new private key to ConvergerVPN .key'-----You are about to be asked to enter information that will be incorporatedinto 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) [IT]:
State or Province Name (full name) [IT]:
Locality Name (eg, city) [Roma]:
Organization Name (eg, company) [Converger]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) [ConvergerVPN ]:
Email Address [info@converger.it]:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:password
An optional company name []:
Using configuration from /etc/openvpn/rsa/openssl.cnf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
countryName :PRINTABLE:'IT'
stateOrProvinceName :PRINTABLE:'IT'
localityName :PRINTABLE:'Roma'
organizationName :PRINTABLE:'Converger'
commonName :PRINTABLE:' ConvergerVPN '
emailAddress :IA5STRING:'info@converger.it'
Certificate is to be certified until Nov 25 13:50:00 2020 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
Procediamo alla creazione del file Diffie-Hellman, necessario per l’avvio delle connessioni cifrate.
# ./build-dh
Generating DH parameters, 1024 bit long safe prime, generator 2
This is going to take a long time
........+............
Realizziamo l’ultima chiave necessaria per l’instaurazione di una connessione sicura:
# openvpn --genkey --secret keys/ta.key
Generazione dei certificati per i client
La procedura per generare i certificati dei client è identica a quella del server, nell’esempio li creiamo nominali per una semplice identificazione, in caso di grandi numeri è possible usare la matricola aziendale.
# cd /etc/openvpn/rsa
# source vars
# NOTE: If you run ./clean-all, I will be doing a rm -rf on /etc/openvpn/easy-rsa/keys (ignorare il meassaggio)
# ./build-key rmassimi
Generating a 1024 bit RSA private key
..........................................++++++
................................................++++++
writing new private key to 'rmassimi.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) [IT]:
State or Province Name (full name) [RM]:
Locality Name (eg, city) [Roma]:
Organization Name (eg, company) [Converger]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) [rmassimi]:
Name []:Roberto Massimi
Email Address [info@converger.it]:rmassimi@converger.it
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
Using configuration from /etc/openvpn/easy-rsa/openssl.cnf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
countryName :PRINTABLE:'IT'
stateOrProvinceName :PRINTABLE:'RM'
localityName :PRINTABLE:'Roma'
organizationName :PRINTABLE:'Converger'
commonName :PRINTABLE:'rmassimi'
name :PRINTABLE:'Roberto Massimi'
emailAddress :IA5STRING:'rmassimi@converger.it'
Certificate is to be certified until Nov 26 14:29:37 2024 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
Configurazione del server
Ora andiamo a configurare il demone OpenVPN, anche in questo caso il pacchetto dovrebbe portare con se degli esempi.
# cp /usr/share/doc/openvpn-2.3.2/sample/sample-config-files/server.conf /etc/openvpn/
Di seguito un file di configurazione:
# SERVER CONF
port 443
proto tcp
dev tun
ca rsa/keys/ca.crt
cert rsa/keys/rcc001ws.crt
key rsa/keys/rcc001ws.key # This file should be kept secret
dh rsa/keys/dh2048.pem
server 10.1.1.0 255.255.255.0
ifconfig-pool-persist ipp.txt
client-config-dir ccd
route 10.1.1.0 255.255.255.0
keepalive 10 120
cipher AES-256-CBC
comp-lzo
persist-key
persist-tun
status /var/log/openvpn-status.log 5
status-version 2
log-append /var/log/openvpn-status.log
verb 3
keepalive 10 60
push route "10.31.57.0 255.255.255.0"
crl-verify /etc/openvpn/crl.pem
In questo modo, dopo connesso in vpn, tutto funzionerà come prima, ma il nostro PC avrà una nuova interfaccia di rete con IP 10.1.1.x e tutti gli IP della classe 10.31.57.x verranno dirottati sulla VPN.In questa configurazione, rilasciamo IP ai client del tipo 10.1.1.x ed aggiungiamo la rotta 10.31.57.x .
Configurazione di IPTABLES
Come ultimo step sul server, dobbiamo abilitare il fowarding e il MASQUERADE tramite IPTABLES:
sysctl -w net.ipv4.ip_forward=1
iptables -t nat -A POSTROUTING -s 10.1.1.0/24 -o eth0 -j MASQUERADE
Se abbiamo IPTABLES configurato andiamo ad aggiungere anche le policy di ACCEPT:
iptables -A INPUT -i tun0 -j ACCEPT
iptables -A FORWARD -i tun0 -j ACCEPT
Avviare il demone di OpenVPN e configurare i certificati dei client.
Configurazione dei client
Per prima cosa dobbiamo copiarci i certificati:
- La coppia certificato/chiave per il client (i due file .key e .crt)
- Il certificato della CA del server (il file ca.crt)
- La chiave di autenticazione TLS (il file ta.key)
File client.opvn:
client
dev tun
proto tcp
remote IP_SERVER_VPN 443
resolv-retry infinitenobind
persist-key
persist-tun
# THE CSR FILE:
ca ca.crt
cert rmassimi.crt
key rmassimi.key
ns-cert-type server
cipher AES-256-CBC
comp-lzo
verb 3
route-method exe
route-delay 2
Lanciando questa configurazione con openVPN del client, noterete una nuova scheda di rete 10.1.1.x . Se ora provate ad aprire un qualsiasi tool o sistema sulla rete 10.31.57.x, questo risulterà disponibile!
Autore: Roberto Massimi