1. 簡介
IPSec全稱Internet Protocol Security,是通過對IP協議的分組進行加密和認證來保護IP協議的網絡傳輸協議集,IPSec的主要用途之一就是VPN。要想了解IPSec VPN原理可以參考IPSecVPN.ppt。
2. strongswan
strongswan是基於IPSec的VPN解決方案,它是一款開源軟件,支持Linux 2.6和3.x的內核,IKEv1和IKEv2的密鑰管理協議,以及各種認證方式、校驗、加密算法。
strongswan的安裝參考//172.17.92.252/yulin.chen/documents/Ubuntu 10.04下安裝strongSwan.docx
strongswan官網有大量的測試案例:https://www.strongswan.org/testresults.html。
下文將介紹如何部署基於strongswan的IPSec VPN。
3. 配置文件
strongswan主要的配置文件有3個:/etc/ipsec.conf,/etc/strongswan.conf,/etc/ipsec.secrets
3.1 ipsec.conf
config setup
conn %default
ikelifetime=60m // IPSec SA協商間隔
keylife=20m // ISAKMP SA協商間隔
rekeymargin=3m // 密鑰更新間隔
keyingtries=1 // ISAKMP SA協商次數
keyexchange=ikev1 // IKE模式
ike=3des-md5-modp1024 // IKE算法
esp=3des-md5-modp1024 // esp算法
authby=secret // 認證方式
conn s2s
left=10.0.0.10 // 本機IP
leftsubnet=192.168.1.0/24 // 本機內網
leftid=@delta // 本機標識
right=10.0.0.20 // 對端IP
rightsubnet=192.168.2.0/24 // 對端內網
rightid=@cisco // 對端標識
auto=add // 連接方式
上述是一個ipsec.conf配置案例,conn 表示一個策略,其中conn %default表示全局默認策略。
參數 | 說明 |
---|---|
ikelifetime | IPSec SA協商間隔 |
keylife | ISAKMP SA協商間隔 |
rekeymargin | 密鑰更新間隔 |
keyingtries | ISAKMP SA協商次數 |
keyexchange | IKE模式,可選”ikev1,ikev2” |
ike | IKE算法,分3個部分組成,用”-“連接,第一部分加密算法:3des,aes128,aes192,aes256,第二部分認證算法:md5,sha1,sha256,第三部分DH算法:modp1024,modp1536 |
esp | esp算法,分3個部分組成,用”-“連接,第一部分加密算法:3des,aes128,aes192,aes256,第二部分認證算法:md5,sha1,sha256,第三部分DH算法:modp1024,modp1536 |
authby | 認證方式,可選”secret,pubkey,psk,rsasig,xauthpsk,xauthrsasig,never” |
left | 本機IP |
leftsubnet | 本機內網,該網段會走VPN通道 |
leftid | 本機標識 |
right | 對端IP, |
rightsubnet | 對端內網,該網段會走VPN通道 |
rightid | 對端標識 |
auto | 連接方式,可選”add,route,start,ignore”,”add”表示手動,”route”表示由連接觸發,”start”表示自啓,”ignore”表示忽略該策略 |
left|rightauth | 指定left,right認證方式,可選”pubkey,psk,eap,xauth”,相當於細分authby |
left|rightauth2 | 指定left,right附加認證方式,可選”xauth” (IKev1) |
left|rightcert | 指定left,right證書,證書存放路徑”/etc/ipsec.d/certs” |
dpdaction | dpd(對端失效檢測)動作,可選”none,clear,hold,restart” |
dpddelay | dpd檢測間隔時間 |
dpdtimeout | dpd檢測超時時間 |
待補充 |
上述列舉了一些常用的參數,所有參數可以查看https://wiki.strongswan.org/projects/strongswan/wiki/ConnSection。
3.2 strongswan.conf
# strongswan.conf - strongSwan configuration file
#
# Refer to the strongswan.conf(5) manpage for details
#
# Configuration changes should be made in the included files
charon {
load_modular = yes
plugins {
include strongswan.d/charon/*.conf
}
}
參數 | 說明 |
---|---|
charon.i_dont_care_about_security_and_use_aggressive_mode_psk | 用於aggressive mode + PSK模式 |
charon.filelog | 配置log文件 |
待補充 |
詳詢https://wiki.strongswan.org/projects/strongswan/wiki/StrongswanConf
3.3 ipsec.secrets
# /etc/ipsec.secrets - strongSwan IPsec secrets file
: PSK "12345678" // 匹配任意策略,其preshared key爲12345678
1.1.1.1 2.2.2.2 : PSK "12345678" // 匹配"left=1.1.1.1 right=2.2.2.2"的策略
cisco : XAUTH "test" // 匹配id=cisco的策略,該策略xauth secret爲test
: RSA "test.pem" // 匹配authtype爲pubkey的任意策略
參數 | 說明 |
---|---|
PSK | [ ] : PSK |
RSA | : RSA [ | %prompt ],公鑰文件必須在/etc/ipsec.d/private |
XAUTH | [ ] : XAUTH “” |
待補充 |
詳詢https://wiki.strongswan.org/projects/strongswan/wiki/IpsecSecrets
4. IPSec VPN搭建實例
準備工作:兩臺安裝strongswan的ubuntu電腦,互相可以ping通。
假設這兩臺電腦分別爲ubuntu1(IP:172.17.92.138),ubuntu2(IP:172.17.92.209)。
4.1 ubuntu1配置
/etc/ipsec.conf 配置:
# ipsec.conf - strongSwan IPsec configuration file
# basic configuration
config setup
conn %default
ikelifetime=60m
keylife=20m
rekeymargin=3m
keyingtries=1
keyexchange=ikev1
authby=secret
conn c2c
left=172.17.92.138
right=172.17.92.209
ike=3des-md5-modp1024
esp=3des-md5-modp1024
auto=add
/etc/strongswan.conf 配置:
# strongswan.conf - strongSwan configuration file
#
# Refer to the strongswan.conf(5) manpage for details
#
# Configuration changes should be made in the included files
charon {
load_modular = yes
plugins {
include strongswan.d/charon/*.conf
}
}
/etc/ipsec.secrets 配置:
: PSK "12345678"
4.2 ubuntu2配置
/etc/ipsec.conf 配置:
# ipsec.conf - strongSwan IPsec configuration file
# basic configuration
config setup
conn %default
ikelifetime=60m
keylife=20m
rekeymargin=3m
keyingtries=1
keyexchange=ikev1
authby=secret
conn c2c
left=172.17.92.209
right=172.17.92.138
ike=3des-md5-modp1024
esp=3des-md5-modp1024
auto=add
/etc/strongswan.conf 配置:
# strongswan.conf - strongSwan configuration file
#
# Refer to the strongswan.conf(5) manpage for details
#
# Configuration changes should be made in the included files
charon {
load_modular = yes
plugins {
include strongswan.d/charon/*.conf
}
}
/etc/ipsec.secrets 配置:
: PSK "12345678"
4.3 建立VPN連接
在兩臺電腦上面運行ipsec restart
重新載入配置,然後在任意一臺運行ipsec up c2c
發起連接,連接成功如下圖所示:
5. 常見問題
待補充