一、IPSec VPN在NAT场景中存在的问题
当某些组网中,有的分支连动态的公网IP地址也没有,只能由网络中的NAT设备进行地址转换,才能访问互联网,然而IPsec是用来保护报文不被修改的,而NAT需要修改报文的IP地址,我们分析一下ISAKMP消息报文和数据流量。
协商是由ISAKMP消息完成的,ISAKMP经过UDP封装,源目端口为500,NAT设备可以转换消息的IP地址和端口,也就是ISAKMP消息可以顺利完成NAT转换。
数据流量有两种协议,AH和ESP,我们先分析一下AH协议。
AH协议,不管是传输模式还是隧道模式,都验证整个数据包,包括AH报文头和IP报文头,也就是NAT修改之后,必然破坏数据包完整性,无法通过对端的验证,最终被丢弃。


ESP协议认证范围不包括外部IP报文头或者新的报文头,因此进行NAT转换时,不会破坏数据包的完整性,由于ESP具有加密功能,分析一下加密是否有影响,ESP报文会对TCP/UDP报文进行加密,即NAT设备是无法修改端口,因此ESP主要是存在加密问题导致NAT无法与其共存。


采用AH安全协议完全不支持NAT穿越场景,ESP安全协议受限于端口,需要用额外端口实现。
二、NAT穿越功能及原理
1、NAT穿越功能
为了解决上述问题,引入了NAT穿越功能,即NAT-Traversal,简称NAT-T,开启NAT穿越功能后,VPN网关会探测NAT设备,当检测到NAT设备时,VPN网关把ESP报文封装到一个新的UDP报文头中,源目端口为4500。


这样,不管是传输模式还是隧道模式,IPsec报文中都有不用加密和验证的IP报文头和UDP报文头,当IPsec报文到达NAT设备时,NAT设备即可修改其IP报文头和UDP报文头中的IP地址和端口。
2、NAT穿越原理

①开启NAT穿越功能后,IKEv1协商的第一阶段会互相发送标识NAT穿越功能的Vendor ID载荷,用于确认彼此是否支持NAT穿越功能。
②主模式消息3和消息4中发送NAT-D ( NAT Discovery )载荷。NAT-D载荷用于探测两个要建立IPSec隧道的网关之间是否存在NAT设备以及NAT设备的位置。
Remote HASH: 指将发送报文中的目的IP地址和端口号进行HASH运算后的数值。
Local HASH: 指将发送报文中的源IP地址和端口号进行HASH运算后的数值。
通过对比Remote HASH和Local HASH值,可以判断网关之间是否存在NAT设备以及NAT网关的位置。
③发现NAT设备后,后续ISAKMP消息(主模式从消息5开始)的端口号转换为4500。
三、NAT穿越会话保活机制
如图所示为NAPT场景,网关1处在NAT设备之后。若网关1未主动发起访问,则NAT设备不存在NAT会话表项,此时网关2无法访问网关1。
为解决以上问题,需要网关1开启NAT会话保活功能。开启后,网关1会定期发送NAT Keepalive报文,使NAT设备上产生并维持NAT表项,使得网关2可以主动访问网关1。

NAT Keepalive报文格式非常简单,在UDP头部之后为两个十六进制F,用于刷新NAT设备会话表项。
华为防火墙检测出IPSec VPN处于NAT穿越场景后,NAT设备内侧设备(发起方网关1)会定期发送NAT Keepalive报文,保证中间NAT设备上的源NAT会话不老化。
四、NAT穿越场景
场景一:经过NAT的分支公网IP地址固定。

基础配置不再这里赘述,主要讲解安全策略以及IPsec的配置和NAT配置,由于转换后的公网地址是固定的,因此FW1可以采用模板方式或者使用ISAKMP方式来配置。即使使用了ISAKMP方式配置IPsec,此处总部也无法主动与分部建立IPsec隧道。
java
[FW1]firewall zone trust
[FW1-zone-trust]add interface g1/0/1
[FW1-zone-trust]quit
[FW1]firewall zone untrust
[FW1-zone-untrust]add interface g1/0/0
[FW1-zone-untrust]quit
[FW1]acl number 3000
[FW1-acl-adv-3000]rule 5 permit ip source 192.168.0.0 0.0.0.255 destination 172.16.1.0 0.0.0.255
[FW1]ike proposal 10
[FW1]ipsec proposal pro1
[FW1-ipsec-proposal-pro1]transform esp
[FW1-ipsec-proposal-pro1]quit
[FW1]ike peer fenzhi
[FW1-ike-peer-fenzhi]undo version 2
[FW1-ike-peer-fenzhi]pre-shared-key test
Info: The password you entered does not meet the minimum complexity requirement.
The password is suggested to contain at least three types of characters from the
following four groups: Uppercase letters <A-Z>; Lowercase letters <a-z>; Numera
ls <0-9>; Symbols (all characters not defined as letters or numerals), such as !
,$,#, and %.
[FW1-ike-peer-fenzhi]ike-proposal 10
[FW1-ike-peer-fenzhi]nat traversal
[FW1-ike-peer-fenzhi]remote-address 2.2.2.10
[FW1-ike-peer-fenzhi]remote-address authentication-address 172.16.0.2
[FW1]ipsec policy policy1 1 isakmp
Info: The ISAKMP policy sequence number should be smaller than the template poli
cy sequence number in the policy group. Otherwise, the ISAKMP policy does not ta
ke effect.
[FW1-ipsec-policy-isakmp-policy1-1]security acl 3000
[FW1-ipsec-policy-isakmp-policy1-1]proposal pro1
[FW1-ipsec-policy-isakmp-policy1-1]ike-peer fenzhi
[FW1-ipsec-policy-isakmp-policy1-1]quit
[FW1]interface g1/0/0
[FW1-GigabitEthernet1/0/0]ipsec policy policy1
[FW1]ip route-static 0.0.0.0 0 1.1.1.2
java
[FW2]acl number 3000
[FW2-acl-adv-3000]rule 5 permit ip source 172.16.1.0 0.0.0.255 destination 192.168.0.0 0.0.0.255
[FW2-acl-adv-3000]quit
[FW2]ike proposal 10
[FW2-ike-proposal-10]display this
2024-03-10 11:17:22.240
#
ike proposal 10
encryption-algorithm aes-256
dh group14
authentication-algorithm sha2-256
authentication-method pre-share
integrity-algorithm hmac-sha2-256
prf hmac-sha2-256
#
return
[FW2-ike-proposal-10]quit
[FW2]ipsec proposal pro1
[FW2-ipsec-proposal-pro1]transform esp
[FW2-ipsec-proposal-pro1]quit
[FW2]ike peer zongbu
[FW2-ike-peer-zongbu]undo version 2
[FW2-ike-peer-zongbu]pre-shared-key test
Info: The password you entered does not meet the minimum complexity requirement.
The password is suggested to contain at least three types of characters from the
following four groups: Uppercase letters <A-Z>; Lowercase letters <a-z>; Numera
ls <0-9>; Symbols (all characters not defined as letters or numerals), such as !
,$,#, and %.
[FW2-ike-peer-zongbu]ike-proposal 10
[FW2-ike-peer-zongbu]remote-address 1.1.1.1
[FW2-ike-peer-zongbu]nat traversal
[FW2-ike-peer-zongbu]quit
[FW2]ipsec policy policy1 1 isakmp
Info: The ISAKMP policy sequence number should be smaller than the template poli
cy sequence number in the policy group. Otherwise, the ISAKMP policy does not ta
ke effect.
[FW2-ipsec-policy-isakmp-policy1-1]security acl 3000
[FW2-ipsec-policy-isakmp-policy1-1]proposal pro1
[FW2-ipsec-policy-isakmp-policy1-1]ike-peer zongbu
[FW2-ipsec-policy-isakmp-policy1-1]quit
[FW2]interface g1/0/0
[FW2-GigabitEthernet1/0/0]ipsec policy policy1
[FW2-GigabitEthernet1/0/0]quit
[FW2]ip route-static 0.0.0.0 0 172.16.0.1
java
[FW1-policy-security]display this
2024-03-10 12:55:27.460
#
security-policy
rule name untr_to_local
source-zone untrust
destination-zone local
source-address 2.2.2.0 mask 255.255.255.0
destination-address 1.1.1.0 mask 255.255.255.0
action permit
rule name untr_to_tr
source-zone untrust
destination-zone trust
source-address 172.16.1.0 mask 255.255.255.0
destination-address 192.168.0.0 mask 255.255.255.0
action permit
#
return
[FW2]security-policy
[FW2-policy-security]display this
2024-03-10 12:03:26.930
#
security-policy
rule name tr_to_untr
source-zone trust
destination-zone untrust
source-address 172.16.1.0 mask 255.255.255.0
destination-address 192.168.0.0 mask 255.255.255.0
action permit
rule name untr_to_local
source-zone untrust
destination-zone local
source-address 1.1.1.0 mask 255.255.255.0
destination-address 172.16.0.0 mask 255.255.255.0
action permit
rule name local_to_untr
source-zone local
destination-zone untrust
source-address 172.16.0.0 mask 255.255.255.0
destination-address 1.1.1.0 mask 255.255.255.0
action permit
#
return
java
[NAT]acl 2001
[NAT-acl-basic-2001]rule permit source 172.16.1.0 0.0.0.255
[NAT-acl-basic-2001]rule permit source 172.16.0.0 0.0.0.255
[NAT-acl-basic-2001]quit
[NAT]nat address-group 1 2.2.2.10 2.2.2.10
[NAT]interface g0/0/2
[NAT-GigabitEthernet0/0/2]nat outbound 2001 address-group 1
测试并抓包分析(防火墙G1/0/0抓包,此处源IP地址还未转换)。

抓包可知,主模式下,ISAKMP协商交换的包6个。(防火墙G1/0/0抓包,此处源IP地址还未转换)

第一阶段会互相发送标识NAT穿越功能的Vendor ID载荷,用于检查通信双方是否支持 NAT-T。当双方都在各自的消息中包含了该载荷时,才会进行相关的 NAT-T 协商。(防火墙G1/0/0抓包,此处源IP地址还未转换)

主模式消息3和消息4中发送NAT-D ( NAT Discovery )载荷。NAT-D载荷用于探测两个要建立IPSec隧道的网关之间是否存在NAT设备以及NAT设备的位置。
Remote HASH: 指将发送报文中的目的IP地址和端口号进行HASH运算后的数值。
Local HASH: 指将发送报文中的源IP地址和端口号进行HASH运算后的数值。
通过对比Remote HASH和Local HASH值,可以判断网关之间是否存在NAT设备以及NAT网关的位置。
FW2:

FW1:

两方计算的哪个Hash值不相等,表明哪个设备在NAT网关后面,由于FW2的LOCAL与FW1的REMOTE HASH不同,因此在FW2处有NAT设备。
发现NAT设备后,后续ISAKMP消息(主模式从消息5开始)的端口号转换为4500。

NAT穿越会话保活机制(防火墙G1/0/0抓包,此处源IP地址还未转换)

参考资料:防火墙和VPN技术与实践------李学昭