IPSec 手動配置與自動配置(IKE動態協商)
手動配置IPSec 邏輯圖
關鍵邏輯:
- 無協商過程:所有參數(SPI、密鑰)預先靜態配置
- 對稱配置:兩端必須鏡像設置SPI(入/出方向相反)
- 密鑰固定:無自動更新機制,安全性較低
自動配置(IKE動態協商)邏輯圖
關鍵邏輯:
- 兩階段協商:
- 階段1:建立管理通道(IKE SA),使用UDP 500/4500端口
- 階段2:建立數據通道(IPSec SA),生成動態SPI和密鑰
- 動態密鑰:支持PFS(完美前向保密),定期更新密鑰
- 自動恢復:網絡中斷后自動重新協商
核心區別對比圖
階段 | 手動配置 | 自動配置(IKE) |
---|---|---|
參數定義 | 管理員靜態指定 | IKE協議動態協商 |
密鑰管理 | 固定不變 | 定期自動更新 |
SPI生成 | 手動配置 | 系統自動分配 |
加密算法 | 預定義 | 兩端協商最優算法 |
適用場景 | 測試/簡單環境 | 生產環境/復雜網絡 |
協議依賴 | 僅需ESP/AH | 依賴IKE(UDP 500+4500) |
配置復雜度 | 簡單但易錯 | 復雜但靈活 |
手動IPSec 協議交互流程圖
關鍵配置指令對應點:
-
SPI同步:
- R1出方向SPI 1002 = R3入方向SPI 1002
! R1配置 ipsec sa manual sa-to-r3 10outbound esp spi 1002 ! R3配置 ipsec sa manual sa-to-r1 10inbound esp spi 1002
-
加密過程:
- 出站數據匹配ACL 3001后,使用SPI 1002的密鑰加密
display ipsec sa brief # 查看加密計數
自動IPSec (IKEv1)協議交互流程圖
配置指令與協議交互對照:
-
階段1配置:
! 兩端必須匹配的IKE參數 ike proposal 10encryption-algorithm aes-256authentication-algorithm sha2-256dh group14 ike peer R3pre-shared-key cipher Huawei@123remote-address 23.0.0.3
-
階段2配置:
ipsec proposal tosHesp encryption-algorithm aes-256esp authentication-algorithm sha2-256 ipsec policy POLICY1 10 isakmpike-peer R3proposal tosH
-
動態SPI查看:
display ipsec sa # 輸出示例: SPI: 0x12345678 (動態生成) Encrypt algorithm: AES-256
關鍵配置與協議字段映射表
協議字段 | 華為配置指令 | 作用說明 |
---|---|---|
IKE SA_i/SA_r | ike proposal 10 | 協商加密算法/DH組 |
KE (Key Exchange) | dh group14 | 決定密鑰交換強度 |
Hash Payload | pre-shared-key cipher | 身份認證憑證 |
IPSec SPI | 自動生成(手動配置則固定) | 唯一標識安全關聯 |
ESP Encryption | esp encryption-algorithm aes-256 | 數據加密算法 |
-
現象:
display ike sa
無輸出 -
排查步驟:
- 檢查UDP 500可達性:
ping -c 1 -p 500 23.0.0.3
- 驗證提議匹配:
display ike proposal # 確認兩端aes-256/sha2-256一致
- 檢查UDP 500可達性:
-
現象:
display ipsec statistics
無計數增長 -
排查步驟:
- 檢查ACL匹配:
display acl 3001 # 確認規則命中測試流量
- 驗證策略應用:
display ipsec policy # 查看接口綁定狀態
- 檢查ACL匹配:
-
啟用NAT穿透(適用于中間有NAT設備):
ike peer R3nat-traversal enable # 自動使用UDP 4500封裝
-
配置PFS增強安全:
ipsec policy POLICY1 10 isakmppfs dh-group14 # 每次重協商使用新DH密鑰
-
設置SA生存時間:
ipsec policy POLICY1 10 isakmpsa duration time-based 86400 # 24小時后自動重協商
示例
[PC1] --- (Ethernet0/0/1) --- [AR1] --- (GE0/0/1: 2.2.2.1/24) --- [AR2 (ISP)] --- (GE0/0/0: 2.2.2.3/24) --- [AR3] --- (Ethernet0/0/1) --- [PC2]| | || IP: 192.168.10.0/24 | GE0/0/0: 192.168.10.254/24 | GE0/0/0: 192.168.20.254/24 | IP: 192.168.20.0/24| | | |
[Headquarters] [IPSec Tunnel] [Shangrao Branch]
一、基礎網絡配置(R1和R3必須先完成)
R1配置(總部)
sysname R1
# 配置內網接口(連接總部PC)
interface GigabitEthernet0/0/0ip address 192.168.10.1 255.255.255.0dhcp select interface # 可選:為PC分配IP
# 配置公網接口(連接ISP)
interface GigabitEthernet0/0/1ip address 2.2.2.1 255.255.255.0
# 配置默認路由指向ISP
ip route-static 0.0.0.0 0.0.0.0 2.2.2.2
R3配置(分部)
sysname R3
# 配置內網接口(連接分部PC)
interface GigabitEthernet0/0/0ip address 192.168.20.1 255.255.255.0dhcp select interface
# 配置公網接口
interface GigabitEthernet0/0/1ip address 2.2.2.3 255.255.255.0
# 默認路由
ip route-static 0.0.0.0 0.0.0.0 2.2.2.2
二、手動配置IPSec (靜態密鑰)
R1配置
# 定義需要加密的流量(總部→分部)
acl number 3000rule 5 permit ip source 192.168.10.0 0.0.0.255 destination 192.168.20.0 0.0.0.255# 1. 定義IPSec提案(加密/認證算法)
ipsec proposal aesp authentication-algorithm sha2-256 # ESP認證算法(完整性保護)esp encryption-algorithm aes-256 # ESP加密算法(數據加密)# 2. 創建手動IPSec策略
ipsec policy a 1 manual # 策略名HF,序號1,手動模式security acl 3000 # 綁定ACL 3000(定義感興趣流)proposal a # 引用上述提案tunnel local 12.0.0.1 # 本端隧道源IPtunnel remote 23.0.0.3 # 對端隧道目的IP# 3. 配置安全關聯 Security Association(SA)參數sa spi inbound esp 12345 # 入方向SPI值(對端出方向需匹配)sa string-key inbound esp cipher aaa # 入方向密鑰(cipher表示加密存儲)sa spi outbound esp 54321 # 出方向SPI值(對端入方向需匹配)sa string-key outbound esp cipher aaa # 出方向密鑰# 4. 應用到物理接口
interface GigabitEthernet0/0/1 # 公網接口ipsec policy a # 應用名為a 的IPSec策略
R3配置
acl number 3000rule 5 permit ip source 192.168.20.0 0.0.0.255 destination 192.168.10.0 0.0.0.255
ipsec proposal besp authentication-algorithm sha2-256esp encryption-algorithm aes-256ipsec policy b 1 manualsecurity acl 3000proposal btunnel local 23.0.0.3 # 本端IP與R1的remote IP對應tunnel remote 12.0.0.1 # 對端IP與R1的local IP對應# 注意SPI和密鑰與R1相反!sa spi inbound esp 54321 # 對應R1的outbound spisa string-key inbound esp cipher 123456sa spi outbound esp 12345 # 對應R1的inbound spisa string-key outbound esp cipher 123456interface GigabitEthernet0/0/1ipsec policy b
三、自動配置IPSec (IKE動態協商)
R1配置
# 階段1:IKE提議(加密管理通道)
ike proposal 10encryption-algorithm aes-256 # 加密算法dh group14 # 密鑰交換組authentication-algorithm sha2-256 # 完整性校驗authentication-method pre-share # 認證方式sa duration 86400000# 階段1:IKE對等體
ike peer R3ike-proposal 10pre-shared-key cipher Huawei@123 # 預共享密鑰remote-address 2.2.2.3 # 對端IP# 階段2:IPSec提案(加密數據通道)
ipsec proposal r3-propesp encryption-algorithm aes-256 # 數據加密esp authentication-algorithm sha2-256 # 數據完整性# 綁定策略
ipsec policy policy-to-r3 10 isakmpsecurity acl 3001 # 匹配加密流量ike-peer R3 # 引用IKE對等體proposal r3-prop # 引用IPSec提案# 應用到接口
interface GigabitEthernet0/0/1ipsec policy policy-to-r3
R3配置
ike proposal 10 # 參數必須與R1完全一致!encryption-algorithm aes-256dh group14authentication-algorithm sha2-256authentication-method pre-share
ike peer R1ike-proposal 10pre-shared-key cipher Huawei@123remote-address 2.2.2.1 # 指向R1的IP
ipsec proposal r1-propesp encryption-algorithm aes-256esp authentication-algorithm sha2-256
ipsec policy policy-to-r1 10 isakmpsecurity acl 3001ike-peer R1proposal r1-prop
interface GigabitEthernet0/0/1ipsec policy policy-to-r1
四、NAT豁免配置(防止IPSec流量被NAT干擾)
R1和R3均需配置
acl number 2000 # 創建NAT豁免ACLrule 5 deny ip source 192.168.10.0 0.0.0.255 destination 192.168.20.0 0.0.0.255 # 阻止總部→分部NATrule 10 permit ip # 允許其他流量NAT
# 應用到公網接口
interface GigabitEthernet0/0/1nat outbound 2000
五、驗證命令
# 查看IKE SA(階段1)
display ike sa
# 預期結果:State字段為"RD"(已建立)# 查看IPSec SA(階段2)
display ipsec sa brief
# 預期結果:有inbound/outbound ESP記錄# 測試連通性
ping -a 192.168.10.1 192.168.20.1 # 從總部PC ping分部PC
關鍵問題排查表
現象 | 可能原因 | 解決方法 |
---|---|---|
IKE SA無法建立 | 預共享密鑰不匹配/UDP 500被阻斷 | 檢查密鑰/抓包確認UDP 500可達性 |
IPSec SA無流量 | ACL未匹配測試流量 | 使用ping -a 指定源IP或調整ACL規則 |
能ping通但數據未加密 | NAT未豁免流量 | 檢查ACL 2000規則和nat outbound綁定 |