• IPSec NAT穿越原理


    一、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隧道。

    1. [FW1]firewall zone trust
    2. [FW1-zone-trust]add interface g1/0/1
    3. [FW1-zone-trust]quit
    4. [FW1]firewall zone untrust
    5. [FW1-zone-untrust]add interface g1/0/0
    6. [FW1-zone-untrust]quit
    7. [FW1]acl number 3000
    8. [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
    9. [FW1]ike proposal 10
    10. [FW1]ipsec proposal pro1
    11. [FW1-ipsec-proposal-pro1]transform esp
    12. [FW1-ipsec-proposal-pro1]quit
    13. [FW1]ike peer fenzhi
    14. [FW1-ike-peer-fenzhi]undo version 2
    15. [FW1-ike-peer-fenzhi]pre-shared-key test
    16. Info: The password you entered does not meet the minimum complexity requirement.
    17. The password is suggested to contain at least three types of characters from the
    18. following four groups: Uppercase letters ; Lowercase letters ; Numera
    19. ls <0-9>; Symbols (all characters not defined as letters or numerals), such as !
    20. ,$,#, and %.
    21. [FW1-ike-peer-fenzhi]ike-proposal 10
    22. [FW1-ike-peer-fenzhi]nat traversal
    23. [FW1-ike-peer-fenzhi]remote-address 2.2.2.10
    24. [FW1-ike-peer-fenzhi]remote-address authentication-address 172.16.0.2
    25. [FW1]ipsec policy policy1 1 isakmp
    26. Info: The ISAKMP policy sequence number should be smaller than the template poli
    27. cy sequence number in the policy group. Otherwise, the ISAKMP policy does not ta
    28. ke effect.
    29. [FW1-ipsec-policy-isakmp-policy1-1]security acl 3000
    30. [FW1-ipsec-policy-isakmp-policy1-1]proposal pro1
    31. [FW1-ipsec-policy-isakmp-policy1-1]ike-peer fenzhi
    32. [FW1-ipsec-policy-isakmp-policy1-1]quit
    33. [FW1]interface g1/0/0
    34. [FW1-GigabitEthernet1/0/0]ipsec policy policy1
    35. [FW1]ip route-static 0.0.0.0 0 1.1.1.2
    1. [FW2]acl number 3000
    2. [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
    3. [FW2-acl-adv-3000]quit
    4. [FW2]ike proposal 10
    5. [FW2-ike-proposal-10]display this
    6. 2024-03-10 11:17:22.240
    7. #
    8. ike proposal 10
    9. encryption-algorithm aes-256
    10. dh group14
    11. authentication-algorithm sha2-256
    12. authentication-method pre-share
    13. integrity-algorithm hmac-sha2-256
    14. prf hmac-sha2-256
    15. #
    16. return
    17. [FW2-ike-proposal-10]quit
    18. [FW2]ipsec proposal pro1
    19. [FW2-ipsec-proposal-pro1]transform esp
    20. [FW2-ipsec-proposal-pro1]quit
    21. [FW2]ike peer zongbu
    22. [FW2-ike-peer-zongbu]undo version 2
    23. [FW2-ike-peer-zongbu]pre-shared-key test
    24. Info: The password you entered does not meet the minimum complexity requirement.
    25. The password is suggested to contain at least three types of characters from the
    26. following four groups: Uppercase letters ; Lowercase letters ; Numera
    27. ls <0-9>; Symbols (all characters not defined as letters or numerals), such as !
    28. ,$,#, and %.
    29. [FW2-ike-peer-zongbu]ike-proposal 10
    30. [FW2-ike-peer-zongbu]remote-address 1.1.1.1
    31. [FW2-ike-peer-zongbu]nat traversal
    32. [FW2-ike-peer-zongbu]quit
    33. [FW2]ipsec policy policy1 1 isakmp
    34. Info: The ISAKMP policy sequence number should be smaller than the template poli
    35. cy sequence number in the policy group. Otherwise, the ISAKMP policy does not ta
    36. ke effect.
    37. [FW2-ipsec-policy-isakmp-policy1-1]security acl 3000
    38. [FW2-ipsec-policy-isakmp-policy1-1]proposal pro1
    39. [FW2-ipsec-policy-isakmp-policy1-1]ike-peer zongbu
    40. [FW2-ipsec-policy-isakmp-policy1-1]quit
    41. [FW2]interface g1/0/0
    42. [FW2-GigabitEthernet1/0/0]ipsec policy policy1
    43. [FW2-GigabitEthernet1/0/0]quit
    44. [FW2]ip route-static 0.0.0.0 0 172.16.0.1
    1. [FW1-policy-security]display this
    2. 2024-03-10 12:55:27.460
    3. #
    4. security-policy
    5. rule name untr_to_local
    6. source-zone untrust
    7. destination-zone local
    8. source-address 2.2.2.0 mask 255.255.255.0
    9. destination-address 1.1.1.0 mask 255.255.255.0
    10. action permit
    11. rule name untr_to_tr
    12. source-zone untrust
    13. destination-zone trust
    14. source-address 172.16.1.0 mask 255.255.255.0
    15. destination-address 192.168.0.0 mask 255.255.255.0
    16. action permit
    17. #
    18. return
    19. [FW2]security-policy
    20. [FW2-policy-security]display this
    21. 2024-03-10 12:03:26.930
    22. #
    23. security-policy
    24. rule name tr_to_untr
    25. source-zone trust
    26. destination-zone untrust
    27. source-address 172.16.1.0 mask 255.255.255.0
    28. destination-address 192.168.0.0 mask 255.255.255.0
    29. action permit
    30. rule name untr_to_local
    31. source-zone untrust
    32. destination-zone local
    33. source-address 1.1.1.0 mask 255.255.255.0
    34. destination-address 172.16.0.0 mask 255.255.255.0
    35. action permit
    36. rule name local_to_untr
    37. source-zone local
    38. destination-zone untrust
    39. source-address 172.16.0.0 mask 255.255.255.0
    40. destination-address 1.1.1.0 mask 255.255.255.0
    41. action permit
    42. #
    43. return
    1. [NAT]acl 2001
    2. [NAT-acl-basic-2001]rule permit source 172.16.1.0 0.0.0.255
    3. [NAT-acl-basic-2001]rule permit source 172.16.0.0 0.0.0.255
    4. [NAT-acl-basic-2001]quit
    5. [NAT]nat address-group 1 2.2.2.10 2.2.2.10
    6. [NAT]interface g0/0/2
    7. [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技术与实践——李学昭

  • 相关阅读:
    盘点渗透测试常用工具,新手收藏少走弯路
    【前端】不同类型数据组装拼接
    【Verilog刷题篇】硬件工程师从0到入门3|组合逻辑复习+时序逻辑入门
    不会接口测试?用Postman轻松入门(六)——Post请求xml格式
    蓝桥杯 微生物增殖 C语言
    PostgreSQL修炼之道笔记之基础篇(九)
    【无标题】
    MM32F0140 UART1中断接收和UART1中断发送
    三个非负整数 马蹄集
    JAVA 实现 UNIX tail -f 命令功能
  • 原文地址:https://blog.csdn.net/Mario_Ti/article/details/136602313