翻译:使用带strongSwan的L2TP,即使是OpenVZ

译者注:希望你看英文不晕,否则请关掉吧。

yep, pretty strong swan

如果你想找一个全平台制霸,连路由器都不用刷的VPN,那基本上就是PPTP和L2TP+IPsec这两个中选了。因为安全和NAT穿越(以及GFW——Beining注)的问题,这年头不流行PPTP了。而L2TP的无用内容(overhead)有点多,而且还有个L2包的传输——这玩意有啥用啊。

挺奇怪,虽然IPsec很灵活,很成熟,网上却没有非L2TP的IPsec指南。黑喂狗。

深♂入IPsec

大体上,“所有的IPsec都是VPN”不大准确。IPsec不是为了VPN开发的,是为了签名并端到端发送加密IP数据的。IPsec在IP层的上面(OSI的第3层——网络层,相对于PPTP和L2TP的第二层——数据链路层  Beining注)和一般创建一个新的网络层,数据被路由到上面的VPN不同,IPsec只加密外部服务器和客户端的连接。神奇吧。

现代的IPsec使用这些东西:

  • 认证头(AH),“保证被传输分组的完整性和可靠性”(wikipedia——Beining注)。这不仅仅加密数据,也包括包头,除了有些可变的东西,例如ToS和TTL。
  • 封装安全载荷(ESP),“对分组提供了源可靠性、完整性和保密性的支持”。
  • 安全关联(SA),“为了提供安全的通讯环境”。
  • 因特网密钥交换(IKE/IKEv2),”归属于IPsec协议族之下,用以创建安全联结”。

AH和ESP都在IP头有自己的协议号。在大局域网要建成,满地都是内网IP的时候,这种东西更加常见。IPsec支持ESP包的UDP封装,可以内网穿越;AH不能和NAT共用。

IPsec有两种模式:

  • 透明模式:签名IP头和数据(AH),或签名并加密数据(ESP)。不隐藏目标IP。在L2TP+IPsec中使用。
  • 隧道模式:签名(AH)而且加密(ESP)整个包。

IKE协议允许你使用X.509证书,预共享密钥或者扩展认证协议(EAP)认证。二元认证也可以。

所有的现代桌面操作系统(Windows  Vista以及以后,OSX,Linux),移动系统(Android,iOS, Blackberry,甚至WP)以及一些路由器支持IPsec使用ESP的隧道模式,我们就用这个。

注意:是IPsec,不是那个Cisco IPsec。

Linux下的IPsec

IPsec(AH/ESP, SA)在内核模式工作,只需要为客户端安装并设置IKE守护进程。虽然选择不少,但是只有两个还活跃:strongSwan和libreswan。第二个我不评价,没用过。(我也没用过——Beining注)第一个不错。而且,这个东西是唯一自带IPSec用户空间的,所以可以和OpenVZ的老内核一起用(这个东西的IPsec路由是坏的)

OpenVZ的IPsec注释

OVZ可以开L2TP,但是路由有问题。可以加防火墙规则解决,但是问题是——不可能这么做。你需要用strongSwan用户IPsec空间,libIPsec。编译strongSwan时,加上 —enable-kernel-libIPsec

链接:

lowendtalk.com/discussion/33226/need-someone-to-test-ipsec-on-their-boxes

lists.strongswan.org/pipermail/users/2014-February/005822.html
forum.openvz.org/index.php?t=tree&goto=39937
bugzilla.redhat.com/show_bug.cgi?id=1081804
 

strongSwan版本需要大于等于5.0.0,我推荐5.2.0+因为有个新的swanctl,比老的强得多。仅供提供信息使用。

Life with swanctl

Life without swanctl

需要安装这些东西:

  • xauth-noauth. 假的XAUTH认证,任何用户名和密码都能登陆。给没有XAUTH的设备用。(iOS的那个Cisco IPsec)
  • vici. swanctl 的内部接口
  • libIPsec. 各种容器的IPsec用户空间

如果没有,也不用特意安一个OVZ,如果不管iOS。Debian不支持,所以我写一下。

开工

Windows,Linux和BB用IKEv2,IKEv1+XAUTH给iOS,OSX和Android,IKEv2+EAP-TLS,只使用X.509密钥给人权机。娘们才用PSK呢。

strongSwan自己有一个密钥——“IPsec PSK”,难用,就是OpenSSL打个包加点花。我fork了一个Easy-RSA,这样可以自己生成IPsec和OpenVPN共用的密钥了!

github.com/ValdikSS/easy-rsa-ipsec

EASY-RSA很好用。

初始化PKI,生成CA和服务器客户端密钥。服务器必须有一个全名(FQDN)!

$ git clone https://github.com/ValdikSS/easy-rsa-ipsec.git
$ cd easy-rsa-ipsec/easyrsa3
$ ./easyrsa init-pki
init-pki complete; you may now create a CA or requests.
$ ./easyrsa build-ca nopass
Generating a 2048 bit RSA private key
…
Common Name (eg: your user, host, or server name) [Easy-RSA CA]:IPsec CA
…
$ ./easyrsa build-server-full uk1.pvpn.pw nopass
Generating a 2048 bit RSA private key
…
Write out database with 1 new entries
Data Base Updated
$ ./easyrsa build-client-full client1 nopass    
Generating a 2048 bit RSA private key
…
Write out database with 1 new entries
Data Base Updated
$ ./easyrsa export-p12 client1 nopass
Successful export of p12 file. Your exported file is at the following
location…

 

生成密钥了。有一个nopass,你以后可以自己加。

把密钥拷到strongSwan密钥目录。

# cp pki/ca.crt /etc/ipsec.d/cacerts/
# cp pki/issued/uk1.pvpn.pw.crt /etc/ipsec.d/certs/
# cp pki/private/uk1.pvpn.pw.key /etc/ipsec.d/private/

 

私钥加到/etc/ipsec.secrets

# This file holds shared secrets or RSA private keys for authentication.
# RSA private key for this host, authenticating it to any other host
# which knows the public part.
# this file is managed with debconf and will contain the automatically created private key
include /var/lib/strongswan/ipsec.secrets.inc
: RSA uk1.pvpn.pw.key

 

strongSwan配置文件这样:

# ipsec.conf - strongSwan IPsec configuration file
# basic configuration
config setup
    # strictcrlpolicy=yes
    # uniqueids = no
include /var/lib/strongswan/ipsec.conf.inc
conn %default
    dpdaction=clear
    dpddelay=35s
    dpdtimeout=200s
    fragmentation=yes
    ike=aes256gcm16-aes256gcm12-aes128gcm16-aes128gcm12-aesxcbc-sha256-sha1-modp4096-modp2048-modp1024,aes256-aes128-sha256-sha1-modp4096-modp2048-modp1024!
    esp=aes128gcm12-aes128gcm16-aes256gcm12-aes256gcm16-modp4096-modp2048-modp1024,aes128-aes256-sha1-sha256-modp4096-modp2048-modp1024!
    # left - local (server) side
    left=%any
    leftauth=pubkey
    leftcert=uk1.pvpn.pw.crt
    leftsendcert=always
    leftsubnet=0.0.0.0/0,2000::/3
    # right - remote (client) side
    right=%any
    rightauth=pubkey
    rightsourceip=192.168.103.0/24,2002:25f7:7489:3::/112
    rightdns=8.8.8.8,2001:4860:4860::8888
conn ikev2-pubkey
    keyexchange=ikev2
    auto=add
conn ikev1-fakexauth
    keyexchange=ikev1
    rightauth2=xauth-noauth
    auto=add
conn ikev2-eap-tls
    also="ikev2-pubkey"
    rightauth=eap-tls
    eap_identity=%identity

 

配置文件分块。“config setup”部分有两个注释:

  • “strictcrlpolicy = yes”要求真的证书吊销名单用于检验,
  • “uniqueids = no” 允许多个客户端使用同一个证书。
  • “%default”放一些默认可继承内容:
  • “dpdaction=clear” 开启失效对等项检测(Dead Peer Detection, DPD),如果超时就忘记客户端
  • “dpddelay=35s” — DPD等待
  • “dpdtimeout=200s” — DPD超时
  • “fragmentation=yes” 开启断裂,对某些垃圾ISP有用
  • ike — IKE安全连接的密钥
  • esp — ESP连接的密钥
  • left and right — 监听所有网络界面,接受所有客户端
  • leftauth/rightauth=pubkey — X.509验证
  • leftsubnet — 客户端要路由的子网。包括v4和v6.不用v6 就关掉。
  • rightsourceip — 客户端IP池。不用v6关掉。、
  • rightdns — DNS IP 地址

看一下IKE和ESP密钥。为了兼容性,什么都丢进去。第一个是AEAD(不加其他的认证算法),然后是D-H组。这个是最快的,然后是AES的CBC。关闭完全正向保密(Perfect Forward Secrecy,PFS)就不能连接了。

如果没安xauth-noauth,改成“xauth”,用户“client”的密码“clientpass“在”/etc/ipsec.secrets“。

client1 : XAUTH "clientpass"

 

现在应该是3种连接了:ikev2-pubkey 和 IKEv2一起用, ikev1-fakexauth 和 IKEv1以及 fake login/password authentication用,  ikev2-eap-tls IKEv2+EAP-TLS 给人权机。

“swanctl -L”这样:

$ swanctl -L
ikev2-pubkey: IKEv2
  local:  %any
  remote: %any
  local public key authentication:
    id: CN=uk1.pvpn.pw
    certs: CN=uk1.pvpn.pw
  remote public key authentication:
  ikev2-pubkey: TUNNEL
    local:  0.0.0.0/0 2000::/3
    remote: dynamic
ikev1-fakexauth: IKEv1
  local:  %any
  remote: %any
  local public key authentication:
    id: CN=uk1.pvpn.pw
    certs: CN=uk1.pvpn.pw
  remote public key authentication:
  remote XAuth authentication:
  ikev1-fakexauth: TUNNEL
    local:  0.0.0.0/0 2000::/3
    remote: dynamic
ikev2-eap-tls: IKEv2
  local:  %any
  remote: %any
  local public key authentication:
    id: CN=uk1.pvpn.pw
    certs: CN=uk1.pvpn.pw
  remote EAP authentication:
  ikev2-eap-tls: TUNNEL
    local:  0.0.0.0/0 2000::/3
    remote: dynamic

 

MTU问题

因为不同的IPsec实现和问题,服务器不能猜客户端的MTU。Android是1500,strongSwan客户端是1400,Cisco是1300.我们改一下TCP MSS,避免v4的TCP包大于1360B,v6的大于1340B。也就是1400.

# iptables -t mangle -I FORWARD -p tcp -m policy --pol IPsec --dir in --syn -m tcpmss --mss 1361:1536 -j TCPMSS --set-mss 1360
# iptables -t mangle -I FORWARD -p tcp -m policy --pol IPsec --dir out --syn -m tcpmss --mss 1361:1536 -j TCPMSS --set-mss 1360
# ip6tables -t mangle -I FORWARD -p tcp -m policy --pol IPsec --dir in --syn -m tcpmss --mss 1341:1536 -j TCPMSS --set-mss 1340
# ip6tables -t mangle -I FORWARD -p tcp -m policy --pol IPsec --dir out --syn -m tcpmss --mss 1341:1536 -j TCPMSS --set-mss 1340

 

服务器做好了,别忘了NAT!

客户端

基本上:

导入p12证书,建立IPsec PKI/IPsec XAUTH RSA/ IKEv2连接,写入客户端证书和服务器地址。服务器域名和服务器密钥的名字必须一样。不能用IP地址连接。

Windows 7, 8, 8.1

导入证书 wiki.strongswan.org/projects/strongswan/wiki/Win7Certs

设置 wiki.strongswan.org/projects/strongswan/wiki/Win7Config

连接 wiki.strongswan.org/projects/strongswan/wiki/Win7Connect

Windows Vista

wiki.strongswan.org/projects/strongswan/wiki/WindowsVista

OS X and iOS

wiki.strongswan.org/projects/strongswan/wiki/IOS_%28Apple%29#iOS-client-configuration

Android

原生的(IKE)和strongSwan客户端(IKEv2)都可以。strongSwan客户端更快更稳定。

结论

IPsec很好,可以用作OpenVPN的替代。不知道为什么很多VPN还用L2TP+IPsec,因为RADIUS,审计,和插件都有了。IPsec无所不包,甚至可以用EAP-SIM 或者 EAP-AKA,用SIM卡验证。

对于反政府监控的,请读一下nohats.ca/wordpress/blog/2014/12/29/dont-stop-using-ipsec-just-yet/ 

作者:ValdikSS

lowendtalk.com/discussion/44964/vpn-everywhere-ipsec-without-l2tp-with-strongswan-even-in-openvz#latest

译者:Beining

3 thoughts on “翻译:使用带strongSwan的L2TP,即使是OpenVZ

  1. 张先生

    你好。请问一下。我服务端centos 6.5配置用的是strongswan+ipsec/l2tp。我在客户端拨号的话有一个问题,就是同一局域网内同一时间只能有一台设备连上ipsec/l2tp的vpn,好像是ipsec的问题,请问你有遇到这种情况吗?是否有解决方案?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *