Phần này bao gồm các phương án thay thế hiện đại cho OpenSwan và cấu hình bảo mật nâng cao cho môi trường production.
🔧 Triển khai IPsec Hiện đại
# Cập nhật hệ thống
sudo yum update -y
# Cài đặt StrongSwan
sudo amazon-linux-extras install epel -y
sudo yum install strongswan -y
# Kiểm tra cài đặt
strongswan version
/etc/strongswan/ipsec.conf):# /etc/strongswan/ipsec.conf
config setup
charondebug="ike 1, knl 1, cfg 0"
uniqueids=no
conn %default
ikelifetime=28800s
keylife=3600s
rekeymargin=3m
keyingtries=3
keyexchange=ikev2
mobike=no
ike=aes256-sha256-modp2048s256,aes256-sha256-modp1536s256,aes256-sha256-modp1024s256!
esp=aes256-sha256-modp2048s256,aes256-sha256-modp1536s256,aes256-sha256-modp1024s256!
conn aws-tunnel-1
auto=start
left=%defaultroute
leftid=<CUSTOMER_GATEWAY_PUBLIC_IP>
right=<AWS_VPN_TUNNEL_1_IP>
rightid=<AWS_VPN_TUNNEL_1_IP>
leftsubnet=10.11.0.0/16
rightsubnet=10.10.0.0/16
authby=secret
leftauth=psk
rightauth=psk
type=tunnel
dpddelay=10s
dpdtimeout=30s
dpdaction=restart
closeaction=restart
mark_in=100
mark_out=100
conn aws-tunnel-2
auto=start
left=%defaultroute
leftid=<CUSTOMER_GATEWAY_PUBLIC_IP>
right=<AWS_VPN_TUNNEL_2_IP>
rightid=<AWS_VPN_TUNNEL_2_IP>
leftsubnet=10.11.0.0/16
rightsubnet=10.10.0.0/16
authby=secret
leftauth=psk
rightauth=psk
type=tunnel
dpddelay=10s
dpdtimeout=30s
dpdaction=restart
closeaction=restart
mark_in=200
mark_out=200
/etc/strongswan/ipsec.secrets):# /etc/strongswan/ipsec.secrets
<CUSTOMER_GATEWAY_PUBLIC_IP> <AWS_VPN_TUNNEL_1_IP> : PSK "<TUNNEL_1_PSK>"
<CUSTOMER_GATEWAY_PUBLIC_IP> <AWS_VPN_TUNNEL_2_IP> : PSK "<TUNNEL_2_PSK>"
# Kích hoạt IP forwarding
echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
echo 'net.ipv4.conf.all.accept_redirects = 0' >> /etc/sysctl.conf
echo 'net.ipv4.conf.all.send_redirects = 0' >> /etc/sysctl.conf
sysctl -p
# Khởi động StrongSwan
sudo systemctl enable strongswan
sudo systemctl start strongswan
# Kiểm tra trạng thái
sudo systemctl status strongswan
sudo strongswan status
IKEv2 với Bảo mật Nâng cao:
# Thuật toán IKE nâng cao
ike=aes256gcm16-sha384-prfsha384-ecp384,aes256-sha256-modp2048s256!
esp=aes256gcm16-sha384-ecp384,aes256-sha256-modp2048s256!
# Perfect Forward Secrecy
pfs=yes
# Tạo certificates để tăng cường bảo mật
sudo strongswan pki --gen --type rsa --size 4096 --outform pem > ca-key.pem
sudo strongswan pki --self --ca --lifetime 3652 --in ca-key.pem --type rsa --dn "CN=VPN CA" --outform pem > ca-cert.pem
# Certificate cho customer gateway
sudo strongswan pki --gen --type rsa --size 2048 --outform pem > client-key.pem
sudo strongswan pki --issue --lifetime 1826 --cacert ca-cert.pem --cakey ca-key.pem --in client-key.pem --type rsa --dn "CN=customer-gateway" --san customer-gateway --outform pem > client-cert.pem
# Dead Peer Detection tích cực
dpddelay=10s
dpdtimeout=30s
dpdaction=restart
closeaction=restart
# Giám sát kết nối
auto=route
keyingtries=%forever
Cài đặt và Cấu hình FRRouting:
# Cài đặt FRRouting cho BGP
sudo yum install -y frr
# Cấu hình FRRouting
sudo systemctl enable frr
sudo systemctl start frr
Cấu hình BGP (/etc/frr/frr.conf):
# /etc/frr/frr.conf
frr version 7.5
frr defaults traditional
hostname customer-gateway
log syslog informational
no ipv6 forwarding
service integrated-vtysh-config
router bgp 65000
bgp router-id 169.254.12.1
neighbor 169.254.12.2 remote-as 64512
neighbor 169.254.12.2 timers 10 30
neighbor 169.254.12.2 timers connect 10
neighbor 169.254.13.2 remote-as 64512
neighbor 169.254.13.2 timers 10 30
neighbor 169.254.13.2 timers connect 10
address-family ipv4 unicast
network 10.11.0.0/16
neighbor 169.254.12.2 soft-reconfiguration inbound
neighbor 169.254.13.2 soft-reconfiguration inbound
exit-address-family
line vty
# Kiểm tra trạng thái BGP
sudo vtysh -c "show bgp summary"
sudo vtysh -c "show ip route bgp"
sudo vtysh -c "show bgp neighbors"
# Debug BGP
sudo vtysh -c "debug bgp events"
sudo vtysh -c "debug bgp updates"
Kích hoạt VPN Logs trong AWS Console:
# Cài đặt CloudWatch agent
sudo yum install -y amazon-cloudwatch-agent
# Tạo script custom metrics
cat > /opt/vpn-metrics.sh << 'EOF'
#!/bin/bash
# Script giám sát VPN tùy chỉnh
# Kiểm tra trạng thái tunnel
TUNNEL1_STATUS=$(sudo strongswan status | grep "aws-tunnel-1" | grep -c "ESTABLISHED")
TUNNEL2_STATUS=$(sudo strongswan status | grep "aws-tunnel-2" | grep -c "ESTABLISHED")
# Gửi metrics đến CloudWatch
aws cloudwatch put-metric-data \
--namespace "Custom/VPN" \
--metric-data MetricName=Tunnel1Status,Value=$TUNNEL1_STATUS,Unit=Count \
--region ap-southeast-1
aws cloudwatch put-metric-data \
--namespace "Custom/VPN" \
--metric-data MetricName=Tunnel2Status,Value=$TUNNEL2_STATUS,Unit=Count \
--region ap-southeast-1
# Kiểm tra BGP sessions nếu sử dụng dynamic routing
if command -v vtysh &> /dev/null; then
BGP_SESSIONS=$(sudo vtysh -c "show bgp summary" | grep -c "Established")
aws cloudwatch put-metric-data \
--namespace "Custom/VPN" \
--metric-data MetricName=BGPSessions,Value=$BGP_SESSIONS,Unit=Count \
--region ap-southeast-1
fi
EOF
chmod +x /opt/vpn-metrics.sh
# Thêm vào crontab để giám sát thường xuyên
echo "*/5 * * * * /opt/vpn-metrics.sh" | sudo crontab -
Các Vấn đề Thường gặp và Giải pháp:
# 1. Kiểm tra trạng thái tunnel
sudo strongswan status
sudo strongswan statusall
# 2. Kiểm tra logs
sudo journalctl -u strongswan -f
sudo tail -f /var/log/messages | grep charon
# 3. Test kết nối
ping -c 4 169.254.12.2 # AWS tunnel endpoint
ping -c 4 10.10.1.10 # AWS instance
# 4. Kiểm tra routing
ip route show
ip route get 10.10.0.0/16
# 5. Xác minh IPsec SAs
sudo strongswan listall
sudo ip xfrm state
sudo ip xfrm policy
# 6. Network troubleshooting
sudo tcpdump -i any esp
sudo tcpdump -i any host <AWS_TUNNEL_IP>
# 7. BGP troubleshooting (nếu sử dụng dynamic routing)
sudo vtysh -c "show bgp neighbors"
sudo vtysh -c "show ip route"
sudo vtysh -c "show bgp"
# Tối ưu network parameters
echo 'net.core.rmem_default = 262144' >> /etc/sysctl.conf
echo 'net.core.rmem_max = 16777216' >> /etc/sysctl.conf
echo 'net.core.wmem_default = 262144' >> /etc/sysctl.conf
echo 'net.core.wmem_max = 16777216' >> /etc/sysctl.conf
echo 'net.ipv4.tcp_rmem = 4096 65536 16777216' >> /etc/sysctl.conf
echo 'net.ipv4.tcp_wmem = 4096 65536 16777216' >> /etc/sysctl.conf
sysctl -p
# Tối ưu hiệu suất StrongSwan
echo 'charon.threads = 16' >> /etc/strongswan/strongswan.conf
echo 'charon.processor.priority_threads.high = 4' >> /etc/strongswan/strongswan.conf
💡 Mẹo Chuyên nghiệp: Đối với môi trường production, luôn sử dụng BGP dynamic routing để automatic failover và load balancing trên cả hai VPN tunnels. Điều này đảm bảo tính sẵn sàng tối đa và phân phối traffic tối ưu.