Ipsec server multiple p2p Ubuntu 22.04 con StrongsWan
Requerimientos previos
- Tener IP fija tanto en el servidor como en los clientes
Instalar en el server y clientes
sudo apt install strongswan strongswan-pki libcharon-extra-plugins libcharon-extauth-plugins libstrongswan-extra-plugins libtss2-tcti-tabrmd0
Crear certificados (server solamente)
Carpetas y permisos
mkdir -p ~/pki/{cacerts,certs,private}
chmod 700 ~/pki
Generar CA Certificate Authority
pki --gen --type rsa --size 4096 --outform pem > ~/pki/private/ca-key.pem
pki --self --ca --lifetime 3650 --in ~/pki/private/ca-key.pem --type rsa --dn "CN=VPN root CA" --outform pem > ~/pki/cacerts/ca-cert.pem
Generar certificado
pki --gen --type rsa --size 4096 --outform pem > ~/pki/private/server-key.pem
pki --pub --in ~/pki/private/server-key.pem --type rsa \
| pki --issue --lifetime 1825 \
--cacert ~/pki/cacerts/ca-cert.pem \
--cakey ~/pki/private/ca-key.pem \
--dn "CN=XXX.XXX.XXX.XXX" --san XXX.XXX.XXX.XXX \
--flag serverAuth --flag ikeIntermediate --outform pem \
> ~/pki/certs/server-cert.pem
XXX.XXX.XXX.XXX Reemplazar por la IP del servidor de Ipsec o bien el dominio completo FQDN
Mover TLS/SSL certificados a StrongSwan
sudo cp -r ~/pki/* /etc/ipsec.d/
Configurar StrongSwan (Servidor)
Mover archivo original
sudo mv /etc/ipsec.conf{,.original}
Crear archivo de configuracion nuevo
sudo nano /etc/ipsec.conf
Contenido
config setup
charondebug="ike 1, knl 1, cfg 0"
uniqueids=no
conn ikev2-vpn
auto=add
compress=no
type=tunnel
keyexchange=ikev2
fragmentation=yes
forceencaps=yes
dpdaction=clear
dpddelay=300s
rekey=no
left=%any
leftid=COMPLETAR_IP_PUBLICA
leftcert=server-cert.pem
leftsendcert=always
leftsubnet=0.0.0.0/0
right=%any
rightid=%any
rightauth=eap-mschapv2
leftsourceip=10.10.10.1
rightsourceip=10.10.10.10-10.10.10.204
rightdns=8.8.8.8,8.8.4.4
rightsendcert=never
eap_identity=%identity
ike=chacha20poly1305-sha512-curve25519-prfsha512,aes256gcm16-sha384-prfsha384-ecp384,aes256-sha1-modp1024,aes128-sha1-modp1024,3des-sha1-modp1024!
esp=chacha20poly1305-sha512,aes256gcm16-ecp384,aes256-sha256,aes256-sha1,3des-sha1!
rightsourceip = Rango de IP's que se le va a dar a los clientes
Configurar autenticacion del servidor
Crear el archivo /etc/ipsec.secrets
sudo nano /etc/ipsec.secrets
Contenido del archivo /etc/ipsec.secrets
: RSA "server-key.pem"
cliente_ipsec_a : EAP "GENERAR_UNA_PASS_SEGURA"
cliente_ipsec_b : EAP "GENERAR_UNA_PASS_SEGURA"
Reiniciar strongswan
sudo systemctl restart strongswan-starter
Instalar cliente (Solo Clientes)
Instalar con apt
sudo apt install strongswan libcharon-extra-plugins
En caso de no querer que el tunel se levante automaticamente, se puede deshabilitar con: sudo systemctl disable --now strongswan-starter
y despues se levanta son start
y stop
bajo el comando systemctl
Copiar llave del servidor al cliente
scp root@servidor:/etc/ipsec.d/cacerts/ca-cert.pem /tmp/ca-cert.pem
sudo cp /tmp/ca-cert.pem /etc/ipsec.d/cacerts
Este paso explico como hacerlo por scp, pero tranquilamente se puede hacer con cat
y creando un archivo a mano el el cliente sin tener que pasar un archivo, solo con mantener el contenido de /etc/ipsec.d/cacerts/ca-cert.pem
Configurar secretos (Clientes)
Editar el archivo /etc/ipsec.secrets
y completar con los datos que cargamos en el paso del servidor
cliente_ipsec_a : EAP "GENERAR_UNA_PASS_SEGURA"
Configurar IPsec (Clientes)
Editar el archivo /etc/ipsec.conf
config setup
conn ikev2-rw
right=IP_PUBLICA_SERVIDOR
# This should match the `leftid` value on your server's configuration
rightid=IP_PUBLICA_SERVIDOR
rightsubnet=0.0.0.0/0
rightauth=pubkey
leftsourceip=%config
leftid=cliente_ipsec_a
leftauth=eap-mschapv2
eap_identity=%identity
auto=start
Completar 3 campos:
1️⃣ right : IP Publica del Servidor
2️⃣ rightid IP Publica del Servidor
3️⃣ leftid Nombre de usuario dela archivo /etc/ipsec.secrets
en esta caso cliente_ipsec_a
Configurar pluigin strongswan bypas lan (solo clientes)
Editar el archivo /etc/strongswan.d/charon/bypass-lan.conf
y cambiar a yes
el valor de load =
vim /etc/strongswan.d/charon/bypass-lan.conf
bypass-lan {
## A comma-separated list of network interfaces for which connected subnets
## should be ignored, if interfaces_use is specified this option has no
## effect.
## interfaces_ignore =
## A comma-separated list of network interfaces for which connected subnets
## should be considered. All other interfaces are ignored.
## interfaces_use =
## Whether to load the plugin. Can also be an integer to increase the
## priority of this plugin.
load = yes
}
Configurar ipv4
Abriri el archivo /etc/ufw/sysctl.conf
y setear o gregar estos valores
net/ipv4/ip_forward=1
net/ipv4/conf/all/accept_redirects=0
net/ipv4/conf/all/send_redirects=0
net/ipv4/ip_no_pmtu_disc=1