Cấu hình VPN Nâng cao

Cấu hình VPN Thay thế và Nâng cao Bảo mật

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.

Cấu hình StrongSwan Thay thế

Tại sao chọn StrongSwan?

🔧 Triển khai IPsec Hiện đại

  • Phát triển tích cực và cập nhật bảo mật thường xuyên
  • Hiệu suất và ổn định tốt hơn
  • Hỗ trợ IKEv2 nâng cao
  • Khả năng debug và logging được cải thiện

Cài đặt và Cấu hình StrongSwan

  1. Cài đặt StrongSwan trên Amazon Linux 2:
# 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
  1. Cấu hình IPsec StrongSwan (/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
  1. Cấu hình Pre-shared Keys (/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>"
  1. Khởi động và Kích hoạt StrongSwan:
# 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

Cấu hình Bảo mật Nâng cao

1. Thuật toán Mã hóa Mạnh

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

2. Xác thực dựa trên Certificate (Nâng cao)

# 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

3. Cấu hình DPD Nâng cao

# 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ấu hình BGP Dynamic Routing

Điều kiện tiên quyết cho BGP

  1. Tạo VPN Connection với BGP:
    • Trong AWS Console, chọn Dynamic routing thay vì Static
    • Cấu hình BGP ASN (Autonomous System Number)
    • Customer Gateway ASN: 65000 (ví dụ)
    • AWS ASN: 64512 (mặc định)

2. Cấu hình BGP với StrongSwan

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

3. Lệnh Giám sát BGP

# 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"

Giám sát và Troubleshooting Nâng cao

1. Tích hợp CloudWatch

Kích hoạt VPN Logs trong AWS Console:

  • Điều hướng đến VPN Connection
  • Actions → Modify VPN tunnel options
  • Kích hoạt Tunnel activity log
  • Chọn CloudWatch Log Group
  • Đặt output format thành json cho structured logging

2. Custom CloudWatch Metrics

# 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 -

3. Hướng dẫn Troubleshooting Toàn diện

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"

4. Tối ưu Hiệu suất

# 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

Checklist Triển khai Production

Cứng hóa Bảo mật

  • Sử dụng thuật toán mã hóa mạnh (AES-256-GCM, SHA-384)
  • Kích hoạt Perfect Forward Secrecy
  • Triển khai xác thực dựa trên certificate cho môi trường bảo mật cao
  • Lịch trình xoay vòng PSK thường xuyên
  • Phân đoạn mạng và quy tắc firewall

Giám sát và Cảnh báo

  • CloudWatch VPN tunnel metrics
  • Custom application metrics
  • Giám sát BGP session (nếu sử dụng dynamic routing)
  • Cảnh báo tự động cho tunnel failures
  • Tập hợp và phân tích log

High Availability

  • Cả hai tunnels được cấu hình và hoạt động
  • BGP cho automatic failover (khuyến nghị)
  • Health checks và automated recovery
  • Quy trình disaster recovery
  • Kiểm thử failover thường xuyên

Tài liệu và Quy trình

  • Sơ đồ mạng và IP addressing
  • Runbooks troubleshooting
  • Quy trình quản lý thay đổi
  • Kế hoạch ứng phó sự cố bảo mật
  • Đánh giá bảo mật thường xuyên

💡 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.