
掌握IPSec密钥协商过程。
Windows Server2003、Wireshark抓包软件、Cisco Packet Tracer






























①查看第一条从192.168.127.139到地址192.168.127.254的ISAKMP报文数据,可以看到


②继续往下查看ISAKMP数据包:可以看到发送者和响应者的cookie都不为0,交换类型已经变为快速模式,并显示了加密数据的相关信息,如封装方式、加密算法和生存时间等。

Commit显示接收方已经收到,至此发送者和接受者已确认,后面开始正式通信。

通过以上数据包可以猜测共享密钥已经被加密了

设备名称 | 接口 | IP | 子网掩码 | 所接设备及接口 |
|---|---|---|---|---|
Router0 | F0/0 | 10.0.0.1 | 255.255.255.0 | Router1,F0/0 |
Router0 | F0/1 | 192.168.1.1 | 255.255.255.0 | Switch0,F0/1 |
Router1 | F0/0 | 10.0.0.2 | 255.255.255.0 | Router0,F0/0 |
Router1 | F0/1 | 192.168.2.1 | 255.255.255.0 | Switch1,F0/1 |
PC0 | F0 | 192.168.1.2 | 255.255.255.0 | Switch0,F0/2 |
PC1 | F0 | 192.168.1.3 | 255.255.255.0 | Switch0,F0/4 |
PC2 | F0 | 192.168.1.4 | 255.255.255.0 | Switch0,F0/6 |
PC3 | F0 | 192.168.2.2 | 255.255.255.0 | Switch1,F0/2 |
PC4 | F0 | 192.168.2.3 | 255.255.255.0 | Switch1,F0/4 |
PC5 | F0 | 192.168.2.4 | 255.255.255.0 | Switch1,F0/6 |

设备名称 | 接口 | IP | 子网掩码 |
|---|---|---|---|
Router0 | F0/0 | 10.0.0.1 | 255.255.255.0 |
Router0 | F0/1 | 192.168.1.1 | 255.255.255.0 |
Router1 | F0/0 | 10.0.0.2 | 255.255.255.0 |
Router1 | F0/1 | 192.168.2.1 | 255.255.255.0 |
初始配置router0的配置命令如下:
Router>enable //进入特权模式
Router#config terminal //进入全局配置状态
Router(config)#interface f0/0
Router(config-if)#ip add 10.0.0.1 255.255.255.0 //配置f0/0接口的IP和子网掩码,该网卡连接公网
Router(config-if)#no shutdown //启动接口
Router(config-if)#end
Router#conf t
Router(config)#int f0/1
Router(config-if)#ip add 192.168.1.1 255.255.255.0 //配置f0/1的IP和子网掩码
Router(config-if)#no shutdown //启动接口
Router(config-if)#end同理,完成Router1的配置。

Router1的配置如下:


设备名称 | IP | 子网掩码 | 默认网关 | 所接设备及接口 |
|---|---|---|---|---|
PC0 | 192.168.1.2 | 255.255.255.0 | 192.168.1.1 | Switch0,F0/2 |
PC1 | 192.168.1.3 | 255.255.255.0 | 192.168.1.1 | Switch0,F0/4 |
PC2 | 192.168.1.4 | 255.255.255.0 | 192.168.1.1 | Switch0,F0/6 |
PC3 | 192.168.2.2 | 255.255.255.0 | 192.168.2.1 | Switch1,F0/2 |
PC4 | 192.168.2.3 | 255.255.255.0 | 192.168.2.1 | Switch1,F0/4 |
PC5 | 192.168.2.4 | 255.255.255.0 | 192.168.2.1 | Switch1,F0/6 |
双击PC图标,选择Desktop,选择IP Configuration,配置PC的IP地址和子网掩码。示例如下:


Router>en
Router#conf t
Enter configuration commands, one per line. End with CNTL/Z.
Router(config)#ip route 0.0.0.0 0.0.0.0 f0/0
Router(config)#ip route 192.168.1.0 255.255.255.0 f0/1
Router(config)#end
配置路由器Router1的配置代码如下:
Router>en
Router#conf t
Enter configuration commands, one per line. End with CNTL/Z.
Router(config)#ip route 0.0.0.0 0.0.0.0 f0/0
Router(config)#ip route 192.168.2.0 0255.255.255.0 f0/1
Router(config)#end
双击PC0(IP:192.168.1.2)图标,弹出对话框,选择Desktop,选择Command Prompt,进行ping 192.168.2.3测试是否连通,可以看到能够连通。

定义IKE策略,就是router 0和router1之间的密钥交换策略。 IKE是密钥的交换策略,我们在使用对称加密和非对称加密算法的时候,需要密钥来对数据加密,上面的IKE策略只是建立一条管理连接,负责加密生成的各种密钥。之后,定义数据的加密方式和认证方式,配置IPSec。然后,将map映射到公网端口f0/0,一个端口只能映射一个map。 配置代码如下:
//配置 IKE(ISAKMP)策略
Router>en
Router#conf t
Enter configuration commands, one per line. End with CNTL/Z.
Router(config)#crypto isakmp policy 10 //建立IKE策略,优先级为10
Router(config-isakmp)#authentication pre-share //设置认证方法为预共享密钥
Router(config-isakmp)#hash md5 //用于完整性检查的哈希算法为MD5
Router(config-isakmp)#encryption des //加密算法为DES
Router(config-isakmp)#group 2 //设置Diffie-Hellman(DH)组为2,组2对应于1024位密钥长度的DH组
Router(config-isakmp)#lifetime 86400 //生存时间设置为86400
Router(config-isakmp)#exit
Router(config)# crypto isakmp key LLrbr address 10.0.0.2
//设置要使用的预共享秘钥LLrber 和指定VPN对端路由器的IP地址10.0.0.2。
Router(config)#access-list 110 permit ip 192.168.1.0 0.0.0.255 192.168.2.0 0.0.0.255
//用扩展acl 110来定义通过 VPN 传输的流量,允许从192.168.1.0/24网络到192.168.2.0/24网络的IP流量。
Router(config)#crypto ipsec transform-set mine esp-des esp-md5-hmac
//定义IPSec变换集transform set命名为mine,加密算法为esp-des, HMAC 算法为esp-MD5-HMAC
Router(config)#crypto map mymap 202 ipsec-isakmp //创建一个名为mymap的crypto map,序列号为202,使用ISAKMP进行IKE协商
% NOTE: This new crypto map will remain disabled until a peer
and a valid access list have been configured.
Router(config-crypto-map)#match address 110 //使用之前创建的访问控制列表110来匹配需要通过VPN传输的流量。
Router(config-crypto-map)#set peer 10.0.0.2 //指定VPN对端路由器的IP地址为10.0.0.2
Router(config-crypto-map)#set pfs group2 //启用完美前向保密(PFS),并指定使用Diffie-Hellman组2。
Router(config-crypto-map)#set transform-set mine //指定使用之前定义的变换集mine
Router(config-crypto-map)#set security-association lifetime seconds 86400
//设置安全关联(SA)的生命周期为86400秒(即一天)
Router(config-crypto-map)#exit
Router(config)#int f0/0
Router(config-if)#crypto map mymap //使用名为"mymap"的加密映射
*Jan 3 07:16:26.785: %CRYPTO-6-ISAKMP_ON_OFF: ISAKMP is ON
//表明ISAKMP已经被启用。
Router(config-if)#end ① 使用show crypto isakmp policy查看IKE策略: 可以看到IKE策略的优先级为10,加密算法为DES,用于完整性检查的哈希算法为MD5,认证方法为预共享密钥,DH为2(1024bit),存活时间为86400s

② 使用show crypto ipsec transform-set查看IPSec变换集: 可以看到IPSec变换集transform set为mine,加密算法为esp-des, HMAC 算法为esp-MD5-HMAC

③ 使用show crypto map 查看crypto maps: 可以看到名为mymap的crypto map,序列号为202,使用ISAKMP进行IKE协商;访问控列表110允许从192.168.1.0/24网络到192.168.2.0/24网络的IP流量。

//配置 IKE(ISAKMP)策略
Router>en
Router#conf t
Enter configuration commands, one per line. End with CNTL/Z.
Router(config)#crypto isakmp policy 10 //建立IKE策略,优先级为10
Router(config-isakmp)#authentication pre-share //设置认证方法为预共享密钥
Router(config-isakmp)#hash md5 //用于完整性检查的哈希算法为MD5
Router(config-isakmp)#encryption des //加密算法为DES
Router(config-isakmp)#group 2 //设置Diffie-Hellman(DH)组为2,组2对应于1024位密钥长度的DH组
Router(config-isakmp)#lifetime 86400 //生存时间设置为86400
Router(config-isakmp)#exit
Router(config)# crypto isakmp key LLrbr address 10.0.0.1
//设置要使用的预共享秘钥LLrbr 和指定VPN对端路由器的IP地址10.0.0.1。
Router(config)#access-list 110 permit ip 192.168.2.0 0.0.0.255 192.168.1.0 0.0.0.255
//用扩展acl 110来定义通过 VPN 传输的流量,允许从192.168.2.0/24网络到192.168.1.0/24网络的IP流量。
Router(config)#crypto ipsec transform-set mine esp-des esp-md5-hmac
//定义IPSec变换集transform set命名为mine,加密算法为esp-des, HMAC 算法为esp-MD5-HMAC
Router(config)#crypto map mymap 101 ipsec-isakmp //创建一个名为mymap的crypto map,序列号为101,使用ISAKMP进行IKE协商
% NOTE: This new crypto map will remain disabled until a peer
and a valid access list have been configured.
Router(config-crypto-map)#match address 110 //使用之前创建的访问控制列表110来匹配需要通过VPN传输的流量。
Router(config-crypto-map)#set peer 10.0.0.1 //指定VPN对端路由器的IP地址为10.0.0.1
Router(config-crypto-map)#set pfs group2 //启用完美前向保密(PFS),并指定使用Diffie-Hellman组2。
Router(config-crypto-map)#set transform-set mine //指定使用之前定义的变换集mine
Router(config-crypto-map)#set security-association lifetime seconds 86400
//设置安全关联(SA)的生命周期为86400秒(即一天)
Router(config-crypto-map)#exit
Router(config)#int f0/0
Router(config-if)#crypto map mymap //使用名为"mymap"的加密映射
*Jan 3 07:16:26.785: %CRYPTO-6-ISAKMP_ON_OFF: ISAKMP is ON
//表明ISAKMP已经被启用。
Router(config-if)#end 

使用PC0(IP:192.168.1.2)的命令行,再次ping 192.168.2.2(PC3),然后在simulation Panel的Play Control区域中选择Play开始,观察工作区动画: 可以在拓扑图看到4个ICMP数据包从PC0——Switch0——Router0——Router1——Switch——PC3,PC3——Swit1——Router1——Router0——Switch0——PC0

同时每完成一个数据包的发送和返回,命令行返回一条数据。

Simulation Pamel可以看到发送和返回数据的全部过程,ICMP协议类型即为刚刚ping命令发送和返回的ICMP数据包:

点击目的为路由器router 0处数据包,弹出面板可以分析出数据包的信息:

从图中可以看到,进入路由器Router0的数据包(左侧)的信息源IP为192.168.1.2(PC0),目的IP为192.168.2.2(PC3),但是路由器出去的数据包的源IP转为了10.0.0.1(Router0),目的IP为10.0.0.2(Router1)。 另外可以在Out Layer第三层网络层下面的第6条信息中,可以看到ESP的encrypts the received packet包。由此可以看出,从PC0发往PC3的数据包经过路由器的IPSec VPN模块加密处理,隐藏了内网的IP地址信息,从而保护了内网的数据。
Router>en
Router#conf t
Enter configuration commands, one per line. End with CNTL/Z.
Router(config)#int f0/0
Router(config-if)#no crypto map mymap
%CRYPTO-6-ISAKMP_ON_OFF: ISAKMP is OFF断开一个端口,再次ping 192.168.2.2在EVENT LIST查看Router0的PDU,可以看到数据没有加密,IP地址没有隐藏。经过Router0的数据包源IP为192.168.1.1,目的IP为192.168.2.2

断开一端无法ping通。两个端口都关闭isakmp时,可以正常通信,但是数据没有进行加密传输。
