strongSwan的使用
  Pq37jUF4UeqZ 2023年11月02日 40 0

Author basilguo@163.com

Date Aug. 04, 2023

Description strongSwan的简单使用

1. 背景

有关VPP以及VPP_SSWAN的内容在上一篇博客 说了,这里不再赘述,如有需要可以自行查看。

感觉可能是第二个参考连接给的配置可能有问题,按照第一个连接的配置来。 这里同时也记载一下自己的配置。

  1. StrongSwan配置、运行及测试
  2. IP 安全与 IPsec 协议,实验A :使用Strongswan 建立两台 PC 间安全通信

2. 环境

2.1. 版本

$ sudo vppctl # 或者make run
DBGvpp# show version
vpp v23.06-release built by XXX on XXX at 2023-08-02T07:37:51

$ lsb_release -r
Release:        20.04

2.2. 拓扑

更新了下拓扑。

+--------+   +--------+   +--------+   +--------+   +--------+
|   H1   |---|   H3   |---|   H4   |---|   H5   |---|   H2   |
+--------+   +--------+   +--------+   +--------+   +--------+
# H3
sudo ip addr add 192.168.34.3/24 dev eth2
sudo ip addr add 192.168.13.3/24 dev eth3
sudo ip route add 192.168.45.0/24 via 192.168.34.4 dev eth2
sudo sysctl net.ipv4.ip_forward=1

# H4
## H4->H3
sudo ip addr add 192.168.34.4/24 dev eth3
## H4->H5
sudo ip addr add 192.168.45.4/24 dev eth2
sudo ip route add 192.168.25.0/24 via 192.168.45.5 dev eth2
sudo ip route add 192.168.13.0/24 via 192.168.34.3 dev eth3
sudo sysctl net.ipv4.ip_forward=1

# H5
sudo ip addr add 192.168.45.5/24 dev eth2
sudo ip addr add 192.168.25.5/24 dev eth3
sudo ip route add 192.168.34.0/24 via 192.168.45.4 dev eth2
sudo sysctl net.ipv4.ip_forward=1

# H1
sudo ip addr add 192.168.13.1/24 dev eth2
sudo ip route add 192.168.25.0/24 via 192.168.13.3 dev eth2

# H2
sudo ip addr add 192.168.25.2/24 dev eth2
sudo ip route add 192.168.13.0/24 via 192.168.25.5 dev eth2

现在基本的路由的框架应该都搭起来了。不过如果并不能保证都能ping通,如果不能通,大概就是没有路由,或者地址掉了。总之,需要的是:H3 ping H5以及H1 ping H2都能通即可。后续需要使用到。

还是建议使用FRR-BGP或其他路由协议去搭建,这样自己手动配置有些麻烦。

3. 安装strongSwan

先安装必须的库

sudo apt update
sudo apt install -y libpam0g-dev libssl-dev make gcc

使用二进制包也行,就是用sudo apt install strongswan strongswan-swanctl,还有没有需要安装的我不晓得。

考虑到我以后可能修改代码的需求,所以我得需要从源码安装。

我需要的隧道在H3=H5之间,所以只需要给他们安装strongSwan就行了。H4用于查看抓包,H1-H2用于通信。实质上应该H3\H4\H5三台机器也是可以的。

# 下载的目标目录
cd /usr/local/src
# 进行源码下载
wget --no-check-certificate https://download.strongswan.org/strongswan-5.9.6.tar.gz
# 解压软件
tar -zvxf strongswan-5.9.6.tar.gz

# 编译安装
cd strongswan-5.9.6
#配置安装 --prefix=你的安装目录 其他 --enable 为使能插件
## 不过这里建议不要使用--prefix改变默认安装位置。不过不改的话自己就不知道安装到哪里去了
## /usr/local/etc/下放置了配置文件,也是可以修改的。
## 在配置前,可以使用./configure -h查看下帮助命令
./configure --prefix=/usr/local/strongswan  --enable-eap-identity --enable-eap-md5 --enable-eap-mschapv2 --enable-eap-tls --enable-eap-ttls --enable-eap-peap --enable-eap-tnc --enable-eap-dynamic --enable-eap-radius --enable-xauth-eap --enable-xauth-pam  --enable-dhcp  --enable-openssl  --enable-addrblock --enable-unity --enable-certexpire --enable-radattr --enable-swanctl --enable-openssl --disable-gmp --enable-save-keys
make
sudo make install
# 这个要不要应该都行,不过都是仪式的一部分了
sudo ldconfig

安装完毕,验证下版本:

$ ipsec version
Linux strongSwan U5.9.6/K5.15.0-78-generic
University of Applied Sciences Rapperswil, Switzerland
See 'ipsec --copyright' for copyright information.

4. 配置

按照那两个一开始的参考说的,现在使用的都是swanctl了,而不使用starter的方式了。

  1. 通用配置是指strongSwan的配置
  2. ipsec.conf/ipsec.secret/ipsec.d的配置文件已经不再使用
  3. swanctl的配置文件

** 请注意,如果按照我上面源码安装的,默认的配置文件是在/usr/local/etc中;但是我这里是在使用VPP的vpp_sswan插件安装的,改了默认配置文件地址,在/etc中。**,下面使用CONF_PREFIX替代目录。

我这里先用psk(preshared key,也就是没有使用证书以及IKE呗)测试一下。后面再学习IKE。

4.1. H3配置

打开${CONF_PREFIX}/swanctl/conf.d/h3.conf。需要使用你自己的地址。文件名不重要。

connections {
   h2h {
      local_addrs  = 192.168.34.3
      remote_addrs = 192.168.45.5
      local {
         auth = psk
         id = h3
      }
      remote {
         auth = psk
         id = h5
      }
      children {
         h2h {
            updown = /usr/libexec/ipsec/_updown iptables
            local_ts = 192.168.13.0/24
            remote_ts = 192.168.25.0/24
            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-h2h {
    id = h3
    secret = simplepsk
  }
}

4.2. H5配置

打开${CONF_PREFIX}/swanctl/conf.d/h5.conf。需要使用你自己的地址。文件名不重要。

connections {
   h2h {
      local_addrs  = 192.168.45.5
      remote_addrs = 192.168.34.3

      local {
         auth = psk
         id = h5
      }
      remote {
         auth = psk
         id = h3
      }
      children {
         h2h {
            updown = /usr/libexec/ipsec/_updown iptables
            local_ts = 192.168.25.0/24
            remote_ts = 192.168.13.0/24
            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-h2h {
     id = h5
     secret = simplepsk
   }
}

4.3. 说明

  • h2h是连接的名字,启动协商的时候需要使用到。
  • secrets 下是用于认证的密钥信息,本实验中使用 PSK,预共享方式。auth也要设置为psk
  • id就是标识,随便起,不冲突能够标识就行(当然要自己能看懂、有意义啊)
  • 如果不配置local_ts和remote_ts字段,则对所有的ip报文加解密。如果指定上述字段,则是对指定的数据流进行加密。设置这个注意是成对的,两个配置是相反的。否则可能会报错:"Traffic Selectors Unacceptable"。不设置的情形没有测试。

5. 测试

5.1. 在H3和H5开启ipsec

# H3 和 H5
## 开启ipsec
$ sudo ipsec start
$ sudo ipsec status
Security Associations (0 up, 0 connecting):
  none

# 启动strongswan的后台(这里后面应该是需要的,还是起来吧)
$ sudo systemctl restart strongswan.service
$ sudo systemctl status strongswan.service
● strongswan.service - strongSwan IPsec IKEv1/IKEv2 daemon using swanctl
     Loaded: loaded (/lib/systemd/system/strongswan.service; disabled; vendor preset: enabled)
     Active: active (running) since Fri 2023-08-04 15:44:01 CST; 9min ago
    Process: 12141 ExecStartPost=/usr/sbin/swanctl --load-all --noprompt (code=exited, status=0/SUCCESS)
   Main PID: 12122 (charon-systemd)
     Status: "charon-systemd running, strongSwan 5.9.6, Linux 5.15.0-78-generic, x86_64"
      Tasks: 19 (limit: 18943)
     Memory: 2.8M
     CGroup: /system.slice/strongswan.service
             └─12122 /usr/sbin/charon-systemd


## 加载conf文件
$ sudo swanctl --loadl-all
loaded ike secret 'ike-h2h'
no authorities found, 0 unloaded
no pools found, 0 unloaded
loaded connection 'h2h'
successfully loaded 1 connections, 0 unloaded


# H3和H5有一个开启就行了
$ sudo ipsec up h2h
initiating IKE_SA h2h[2] to 192.168.45.5
generating IKE_SA_INIT request 0 [ SA KE No N(NATD_S_IP) N(NATD_D_IP) N(FRAG_SUP) N(HASH_ALG) N(REDIR_SUP) ]
sending packet: from 192.168.34.3[500] to 192.168.45.5[500] (240 bytes)
received packet: from 192.168.45.5[500] to 192.168.34.3[500] (248 bytes)
parsed IKE_SA_INIT response 0 [ SA KE No N(NATD_S_IP) N(NATD_D_IP) N(FRAG_SUP) N(HASH_ALG) N(CHDLESS_SUP) N(MULT_AUTH) ]
selected proposal: IKE:AES_CBC_128/HMAC_SHA2_256_128/PRF_HMAC_SHA2_256/CURVE_25519
authentication of 'h3' (myself) with pre-shared key
establishing CHILD_SA h2h{4}
generating IKE_AUTH request 1 [ IDi N(INIT_CONTACT) IDr AUTH SA TSi TSr N(MULT_AUTH) N(EAP_ONLY) N(MSG_ID_SYN_SUP) ]
sending packet: from 192.168.34.3[500] to 192.168.45.5[500] (256 bytes)
received packet: from 192.168.45.5[500] to 192.168.34.3[500] (208 bytes)
parsed IKE_AUTH response 1 [ IDr AUTH SA TSi TSr ]
authentication of 'h5' with pre-shared key successful
IKE_SA h2h[2] established between 192.168.34.3[h3]...192.168.45.5[h5]
scheduling reauthentication in 10331s
maximum IKE_SA lifetime 11411s
selected proposal: ESP:AES_GCM_16_128/NO_EXT_SEQ
CHILD_SA h2h{4} established with SPIs c3d933bf_i cb582e19_o and TS 192.168.13.0/24 === 192.168.25.0/24
connection 'h2h' established successfully

# 查看ipsec状态
$ sudo ipsec status
Security Associations (1 up, 0 connecting):
         h2h[2]: ESTABLISHED 82 seconds ago, 192.168.34.3[h3]...192.168.45.5[h5]
         h2h{4}:  INSTALLED, TUNNEL, reqid 1, ESP SPIs: c3d933bf_i cb582e19_o
         h2h{4}:   192.168.13.0/24 === 192.168.25.0/24

5.2. 在H1 ping H2

ping 192.168.25.2 -I 192.168.13.1

5.3. 在H4抓包

查看H4抓包,可以看到抓的包已经经过了ESP的处理,且使用的是隧道的地址。

$ sudo tcpdump -nvvv -i any esp -xxx
tcpdump: listening on any, link-type LINUX_SLL (Linux cooked v1), capture size 262144 bytes
15:01:06.860259 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto ESP (50), length 140)
    192.168.34.3 > 192.168.45.5: ESP(spi=0xcb582e19,seq=0x86f), length 120
        0x0000:  0000 0001 0006 a036 9f10 4702 0000 0800
        0x0010:  4500 008c 0000 4000 4032 69e7 c0a8 2203
        0x0020:  c0a8 2d05 cb58 2e19 0000 086f 8e3b a3f7
        0x0030:  4220 aae0 cde9 d6d3 53c8 b01e d5a1 2695
        0x0040:  832b e2bb 44ad bc81 5819 42dc 3295 14c2
        0x0050:  bfc8 e2e5 7ddb 257d 1481 a1f6 442d 0347
        0x0060:  10c0 c308 adba d2f2 dc8e 8f22 478b 636d
        0x0070:  26df c54b 612c 948a 8883 df8b fc76 d72d
        0x0080:  8398 f606 c57c 975b 0e8a 352a 1a8c 4b8f
        0x0090:  e776 7d6b 07f9 bb3f fb95 3041
15:01:06.860264 IP (tos 0x0, ttl 63, id 0, offset 0, flags [DF], proto ESP (50), length 140)
    192.168.34.3 > 192.168.45.5: ESP(spi=0xcb582e19,seq=0x86f), length 120
        0x0000:  0004 0001 0006 a036 9f10 32ba 0000 0800
        0x0010:  4500 008c 0000 4000 3f32 6ae7 c0a8 2203
        0x0020:  c0a8 2d05 cb58 2e19 0000 086f 8e3b a3f7
        0x0030:  4220 aae0 cde9 d6d3 53c8 b01e d5a1 2695
        0x0040:  832b e2bb 44ad bc81 5819 42dc 3295 14c2
        0x0050:  bfc8 e2e5 7ddb 257d 1481 a1f6 442d 0347
        0x0060:  10c0 c308 adba d2f2 dc8e 8f22 478b 636d
        0x0070:  26df c54b 612c 948a 8883 df8b fc76 d72d
        0x0080:  8398 f606 c57c 975b 0e8a 352a 1a8c 4b8f
        0x0090:  e776 7d6b 07f9 bb3f fb95 3041

6. 问题记录

使用swanctl --load-all,有时候无法连接上,即便使用了ipsec start也不行,会显示

connecting to 'unix:///var/run/charon.ctl' failed: Connection refused
failed to connect to stroke socket 'unix"//var/run/charon.ctl'

具体原因不知道,怎么解决也不知道。

可以使用下面的这个命令,

$ sudo /usr/libexec/ipsec/charon

但是这个命令启动之后,可能还是无法解决问题,查看journalctl -xe会找到

charon too long to start... - kill kill

这个时候就只能等着了,等过一会儿它自己就好了。怀疑是我配置有问题,但是不知道到底哪里有问题。所以每次修改了配置重启都很痛苦。

最后排查发现,可能就是差了一个sudo systemctl restart strongswan.service,所以还是把这个服务给启动吧,然后上述解决步骤应该就不需要了。

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

Pq37jUF4UeqZ