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 穿透。
相关推荐
鱼人2 分钟前
响应式三巨头:rem / vw / em 深度对比,移动端到底该选谁?
后端
小强19886 分钟前
Grid 网格布局实战:快速实现复杂网页排版
后端
胡志辉6 分钟前
深入浅出 call、apply、bind
前端·javascript·后端
长大19886 分钟前
Flex 布局完整教程:告别浮动,拥抱万能弹性布局
后端
iccb101341 分钟前
5年,一个程序员是如何把私有化在线客服系统做到第一名的
前端·后端·github
Rust研习社1 小时前
这 8 个 Rust 学习资源值得每个新手收藏起来
后端·rust·编程语言
Nturmoils1 小时前
ksql 里这些命令不用加分号,但日常查库少不了
后端
Dilee1 小时前
Spring AI 接 RAG 最小 Demo:DeepSeek、Ollama、SimpleVectorStore 一次跑通
后端
ClouGence3 小时前
Oracle CDC 架构优化:从主库直连到 DataGuard 备库同步
数据库·后端·oracle
Gopher_HBo3 小时前
Go语言学习笔记(十二)Tcp实现Rpc
后端