Utilizzo di StrongSwan per IPSec VPN su CentOS 7

Cloud Compute

StrongSwan è una soluzione VPN basata su IPsec open source. Supporta entrambi i protocolli di scambio di chiavi IKEv1 e IKEv2 insieme allo stack IPsec NETKEY nativo del kernel Linux. Questo tutorial ti mostrerà come usare strongSwan per configurare un server VPN IPSec su CentOS 7.

Installa strongSwan

I pacchetti strongSwan sono disponibili nel repository Extra Packages for Enterprise Linux (EPEL). Dovremmo prima abilitare EPEL, quindi installare strongSwan.

yum install http://ftp.nluug.nl/pub/os/Linux/distr/fedora-epel/7/x86_64/Packages/e/epel-release-7-11.noarch.rpm
yum install strongswan openssl

Genera certificati

Sia il client che il server VPN hanno bisogno di un certificato per identificarsi e autenticarsi. Ho preparato due script di shell per generare e firmare i certificati. Innanzitutto, scarichiamo questi due script nella cartella /etc/strongswan/ipsec.d.

cd /etc/strongswan/ipsec.d
wget https://raw.githubusercontent.com/michael-loo/strongswan_config/for_vultr/server_key.sh
chmod a+x server_key.sh
wget https://raw.githubusercontent.com/michael-loo/strongswan_config/for_vultr/client_key.sh
chmod a+x client_key.sh

In questi due .shfile, ho impostato il nome dell'organizzazione come VULTR-VPS-CENTOS. Se vuoi cambiarlo, apri i .shfile e sostituiscili O=VULTR-VPS-CENTOScon O=YOUR_ORGANIZATION_NAME.

Quindi, utilizzare server_key.shcon l'indirizzo IP del server per generare la chiave dell'autorità di certificazione (CA) e il certificato per il server. Sostituisci SERVER_IPcon l'indirizzo IP del tuo VPS Vultr.

./server_key.sh SERVER_IP

Generare la chiave client, il certificato e il file P12. Qui, creerò il certificato e il file P12 per l'utente VPN "john".

./client_key.sh john john@gmail.com

Sostituisci "john" e la sua e-mail con la tua prima di eseguire lo script.

Dopo che i certificati per client e server sono stati generati, copia /etc/strongswan/ipsec.d/john.p12e /etc/strongswan/ipsec.d/cacerts/strongswanCert.pemsul tuo computer locale.

Configura strongSwan

Apri il file di configurazione IPSec di strongSwan.

vi /etc/strongswan/ipsec.conf

Sostituisci il suo contenuto con il seguente testo.

config setup
    uniqueids=never
    charondebug="cfg 2, dmn 2, ike 2, net 0"

conn %default
    left=%defaultroute
    leftsubnet=0.0.0.0/0
    leftcert=vpnHostCert.pem
    right=%any
    rightsourceip=172.16.1.100/16

conn CiscoIPSec
    keyexchange=ikev1
    fragmentation=yes
    rightauth=pubkey
    rightauth2=xauth
    leftsendcert=always
    rekey=no
    auto=add

conn XauthPsk
    keyexchange=ikev1
    leftauth=psk
    rightauth=psk
    rightauth2=xauth
    auto=add

conn IpsecIKEv2
    keyexchange=ikev2
    leftauth=pubkey
    rightauth=pubkey
    leftsendcert=always
    auto=add

conn IpsecIKEv2-EAP
    keyexchange=ikev2
    ike=aes256-sha1-modp1024!
    rekey=no
    leftauth=pubkey
    leftsendcert=always
    rightauth=eap-mschapv2
    eap_identity=%any
    auto=add

Modificare il file di configurazione strongSwan, strongswan.conf.

vi /etc/strongswan/strongswan.conf

Elimina tutto e sostituiscilo con il seguente.

charon {
    load_modular = yes
    duplicheck.enable = no
    compress = yes
    plugins {
            include strongswan.d/charon/*.conf
    }
    dns1 = 8.8.8.8
    dns2 = 8.8.4.4
    nbns1 = 8.8.8.8
    nbns2 = 8.8.4.4
}

include strongswan.d/*.conf

Modifica il file segreto IPsec per aggiungere un utente e una password.

vi /etc/strongswan/ipsec.secrets

Aggiungi un account utente "john" in esso.

: RSA vpnHostKey.pem
: PSK "PSK_KEY"
john %any : EAP "John's Password"
john %any : XAUTH "John's Password"

Si noti che entrambi i lati dei due punti ":" necessitano di uno spazio bianco.

Consenti inoltro IPv4

Modifica /etc/sysctl.confper consentire l'inoltro nel kernel Linux.

vi /etc/sysctl.conf

Aggiungi la seguente riga nel file.

net.ipv4.ip_forward=1

Salvare il file, quindi applicare la modifica.

sysctl -p

Configura il firewall

Apri il firewall per la tua VPN sul server.

firewall-cmd --permanent --add-service="ipsec"
firewall-cmd --permanent --add-port=4500/udp
firewall-cmd --permanent --add-masquerade
firewall-cmd --reload

Avvia VPN

systemctl start strongswan
systemctl enable strongswan

StrongSwan ora è in esecuzione sul tuo server. Installa i file strongswanCert.peme dei .p12certificati nel tuo client. Ora potrai unirti alla tua rete privata.

Lascia un commento