strongswan psk 使用

bash 复制代码
# https://www.strongswan.org/testing/testresults/ikev2/net2net-psk/sun.swanctl.conf

connections {

   gw-gw {
      local_addrs  = 192.168.0.2
      remote_addrs = 192.168.0.1

      local {
         auth = psk
         id = sun.strongswan.org
      }
      remote {
         auth = psk
         id = moon.strongswan.org
      }
      children {
         net-net {
            local_ts  = 10.2.0.0/16
            remote_ts = 10.1.0.0/16

            updown = /usr/local/libexec/ipsec/_updown iptables
            rekey_time = 5400
            rekey_bytes = 500000000
            rekey_packets = 1000000
            esp_proposals = aes128gcm128-x25519
         }
      }
      version = 2
      mobike = no
      reauth_time = 10800
      proposals = aes128-sha256-x25519
   }
}

secrets {
   ike-1 {
      id-moon = moon.strongswan.org
      id-sun =sun.strongswan.org
      secret = 0sv+NkxY9LLZvwj4qCC2o/gGrWDF2d21jL
   }
}

# https://www.strongswan.org/testing/testresults/ikev2/net2net-psk/moon.swanctl.conf

connections {

   gw-gw {
      local_addrs  = 192.168.0.1
      remote_addrs = 192.168.0.2

      local {
         auth = psk
         id = moon.strongswan.org
      }
      remote {
         auth = psk
         id = sun.strongswan.org
      }
      children {
         net-net {
            local_ts  = 10.1.0.0/16
            remote_ts = 10.2.0.0/16

            updown = /usr/local/libexec/ipsec/_updown iptables
            rekey_time = 5400
            rekey_bytes = 500000000
            rekey_packets = 1000000
            esp_proposals = aes128gcm128-x25519
         }
      }
      version = 2
      mobike = no
      reauth_time = 10800
      proposals = aes128-sha256-x25519
   }
}

secrets {
   ike-1 {
      id-1 = moon.strongswan.org
      secret = 0x45a30759df97dc26a15b88ff
   }
   ike-2 {
      id-2 = sun.strongswan.org
      secret = "This is a strong password"
   }
   ike-3 {
      id-3a = moon.strongswan.org
      id-3b =sun.strongswan.org
      secret = 0sv+NkxY9LLZvwj4qCC2o/gGrWDF2d21jL
   }
   ike-4 {
      secret = 'My "home" is my "castle"!'
   }
   ike-5 {
     id-5 = 192.168.0.1
     secret = "Andi's home"
   }
}

好的,我来解释一下这对 strongSwan IPsec 配置文件的含义。 这两个配置文件分别用于 VPN 连接的两端,我们称之为 "sun" 和 "moon"。 它们定义了一个网关到网关 (gateway-to-gateway) 的 IPsec VPN 连接,使用预共享密钥 (PSK) 进行身份验证。

总体结构:

两个配置文件都包含两个主要部分:

  • connections: 定义了 VPN 连接的参数,例如本地/远程地址、身份验证方式、流量选择器等。
  • secrets: 存储了用于身份验证的预共享密钥。

详细解释 (以 sun.swanctl.conf 为例,moon.swanctl.conf 类似,但参数相反):

sun.swanctl.conf:

ini 复制代码
connections {

   gw-gw {
      local_addrs  = 192.168.0.2
      remote_addrs = 192.168.0.1

      local {
         auth = psk
         id = sun.strongswan.org
      }
      remote {
         auth = psk
         id = moon.strongswan.org
      }
      children {
         net-net {
            local_ts  = 10.2.0.0/16
            remote_ts = 10.1.0.0/16

            updown = /usr/local/libexec/ipsec/_updown iptables
            rekey_time = 5400
            rekey_bytes = 500000000
            rekey_packets = 1000000
            esp_proposals = aes128gcm128-x25519
         }
      }
      version = 2
      mobike = no
      reauth_time = 10800
      proposals = aes128-sha256-x25519
   }
}

secrets {
   ike-1 {
      id-moon = moon.strongswan.org
      id-sun = sun.strongswan.org
      secret = 0sv+NkxY9LLZvwj4qCC2o/gGrWDF2d21jL
   }
}
  • connections: 定义了一个名为 gw-gw (gateway-to-gateway) 的连接。
    • local_addrs = 192.168.0.2: 本地 VPN 网关的 IP 地址是 192.168.0.2 (sun)。
    • remote_addrs = 192.168.0.1: 远程 VPN 网关的 IP 地址是 192.168.0.1 (moon)。
    • local { auth = psk; id = sun.strongswan.org }: 本地身份验证配置: 使用预共享密钥 (psk) 认证,本地身份标识是 sun.strongswan.org
    • remote { auth = psk; id = moon.strongswan.org }: 远程身份验证配置: 使用预共享密钥 (psk) 认证,远程身份标识是 moon.strongswan.org
    • children { net-net { ... } }: 定义了 SA (Security Association,安全联盟) 的子配置,用于保护实际的数据流量。
      • local_ts = 10.2.0.0/16: 本地流量选择器 (Traffic Selector),指定了本地网络是 10.2.0.0/16 网段。 这意味着从 10.2.0.0/16 发出的流量会被加密并通过 VPN 隧道传输。
      • remote_ts = 10.1.0.0/16: 远程流量选择器,指定了远程网络是 10.1.0.0/16 网段。 这意味着去往 10.1.0.0/16 的流量会被加密并通过 VPN 隧道传输。
      • updown = /usr/local/libexec/ipsec/_updown iptables: 指定了在 SA 建立或断开时执行的脚本。 这个脚本通常用于配置防火墙规则,允许或阻止通过 VPN 隧道的流量。
      • rekey_time = 5400: SA 重新密钥的时间间隔 (秒)。
      • rekey_bytes = 500000000: SA 重新密钥的数据量 (字节)。
      • rekey_packets = 1000000: SA 重新密钥的数据包数量。
      • esp_proposals = aes128gcm128-x25519: 指定了用于 ESP (Encapsulating Security Payload) 的加密算法。 这里使用的是 AES-128-GCM 搭配 X25519 密钥交换算法。
    • version = 2: 指定使用 IKEv2 协议。
    • mobike = no: 禁用 MOBIKE (Mobility and Multihoming Protocol)。
    • reauth_time = 10800: 重新身份验证的时间间隔 (秒)。
    • proposals = aes128-sha256-x25519: 指定了用于 IKE (Internet Key Exchange) 协商的加密算法。 这里使用的是 AES-128 加密,SHA-256 哈希算法,搭配 X25519 密钥交换算法。
  • secrets: 定义了预共享密钥。
    • ike-1 { id-moon = moon.strongswan.org; id-sun = sun.strongswan.org; secret = 0sv+NkxY9LLZvwj4qCC2o/gGrWDF2d21jL }: 指定了用于 moon.strongswan.orgsun.strongswan.org 之间身份验证的预共享密钥。 注意,这个密钥必须在两个配置文件中完全相同。

moon.swanctl.conf:

sun.swanctl.conf 类似,但是 local_addrsremote_addrs 以及 local_tsremote_ts 的值是相反的。 重要的是,secrets 部分中 ike-1secret 值必须与 sun.swanctl.conf 中相同。

总结:

这个配置定义了一个简单的 IKEv2 IPsec VPN,使用预共享密钥进行身份验证,并保护 10.2.0.0/16 和 10.1.0.0/16 两个网段之间的流量。

关键点:

  • IP 地址: 确保 local_addrsremote_addrs 设置正确,并且两个 VPN 网关可以互相访问。
  • 身份标识: 确保 id (例如 sun.strongswan.orgmoon.strongswan.org) 在两个配置文件中匹配。
  • 预共享密钥: 确保预共享密钥在两个配置文件中完全相同。
  • 流量选择器: 确保 local_tsremote_ts 正确定义了需要保护的流量。
  • 防火墙规则: 需要配置防火墙规则,允许 UDP 500 和 4500 端口的流量通过,以及 ESP 协议的流量通过。
  • NAT 穿透: 如果 VPN 网关位于 NAT 设备之后,可能需要配置 NAT 穿透。
相关推荐
牛肉汤3 分钟前
联邦式架构中的协议抉择:从 Mastodon 的设计看分布式系统的技术权衡
后端
吹风的坚果6 分钟前
HTTPS的那些事
后端·https
五行星辰10 分钟前
Redisson:Redis界的变形金刚,会变身还会唱跳Rap!
java·后端
BUG研究员_11 分钟前
Spring Boot自动装配原理
java·spring boot·后端
L7ink12 分钟前
FinBot - WeChatFerry + DeepSeek打造 个人智能记账助手
后端·deepseek
五行星辰13 分钟前
Jedis大战Redis:码农版《星球大战》开演
java·后端
uhakadotcom21 分钟前
Supabase数据库性能优化全攻略:从慢查询到闪电响应的实战技巧
后端·面试·github
YGGP43 分钟前
【每日八股】Golang篇(二):关键字(上)
开发语言·后端·golang
无奈何杨1 小时前
阿里TTL+Log4j2+MDC实现轻量级日志链路追踪
后端
伯牙碎琴1 小时前
十一、Spring Boot:使用JWT实现用户认证深度解析
java·spring boot·后端