设为首页收藏本站language 语言切换
查看: 2226|回复: 2
收起左侧

[分享] 【思科实验】IP NAT outside and inside source 综合实验

[复制链接]
 成长值: 64705
发表于 2022-12-26 16:26:37 | 显示全部楼层 |阅读模式
实验环境:
操作系统我使用的是 Mac OS X 10.11.4 beta 5(目前最新 OS X 公测版);模拟器用的是 GNS3 + IOU 的组合,大家也可以单纯用 GNS3 来做这个实验( PS :想要了解有关 GNS3 + IOU 的更多信息请点击这里)。

IP NAT outside and inside source综合实验拓扑图
IP NAT outside and inside source 综合实验拓扑图
基础配置:
大家可以通过刷命令的形式快速完成基础配置。

IOU1:
conf t
int l 0
ip add 172.16.1.1 255.255.255.255

int e 0/0
ip add 192.168.1.1 255.255.255.0
no sh

router eigrp 12
no au
net 192.168.1.1 0.0.0.0
net 172.16.1.1 0.0.0.0

IOU2:
conf t

int l 0
ip add 2.2.2.2 255.255.255.255
ip os 234 a 0

int e 0/0
ip add 192.168.1.2 255.255.255.0
no sh

int e 0/1
ip add 23.1.1.2 255.255.255.0
ip os 234 a 0
no sh

ip route 0.0.0.0 0.0.0.0 23.1.1.3

router ospf 234
router-id 2.2.2.2

router eigrp 12
no au
net 192.168.1.2 0.0.0.0
red static

ip access-list standard NAT
permit 172.16.1.1
permit 192.168.1.0 0.0.0.255

int e 0/0
ip nat inside

int e 0/1
ip nat outside

IOU3:
conf t
router ospf 234
router-id 3.3.3.3

int l 0
ip add 3.3.3.3 255.255.255.255
ip os 234 a 0

int e 0/1
ip add 23.1.1.3 255.255.255.0
ip os 234 a 0
no sh

int e 0/0
ip add 34.1.1.3 255.255.255.0
ip os 234 a 0
no sh

IOU4:
conf t
router ospf 234
router-id 4.4.4.4

int l 0
ip add 4.4.4.4 255.255.255.255
ip os 234 a 0

int e 0/0
ip add 34.1.1.4 255.255.255.0
ip os 234 a 0
no sh

实验一:
把内网源地址转换成外网源地址的 NAT( ip nat inside —— 转换 IP 包的源,这些 IP 包正在从内部传输到外部)

实验需求:IOU1 位于内网,IOU2 是边界路由器,IOU3 位于外网。现在需要内网( IOU1 )能 ping 通外网( IOU3 )。

IP NAT outside and inside source综合实验拓扑图 - 实验一
IP NAT outside and inside source综合实验拓扑图 – 实验一
IOU2 配置:

IOU2(config)#ip nat inside source list NAT interface loopback 0 overload
测试:

IOU1#p 3.3.3.3 so l 0
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 3.3.3.3, timeout is 2 seconds:
Packet sent with a source address of 172.16.1.1
...!!
Success rate is 40 percent (2/5), round-trip min/avg/max = 1/3/5 ms

IOU1#p 3.3.3.3 so l 0
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 3.3.3.3, timeout is 2 seconds:
Packet sent with a source address of 172.16.1.1
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 1/3/5 ms

IOU2#sh ip nat translations
Pro Inside global      Inside local       Outside local      Outside global
icmp 2.2.2.2:0         172.16.1.1:0       3.3.3.3:0          3.3.3.3:0
icmp 2.2.2.2:1         172.16.1.1:1       3.3.3.3:1          3.3.3.3:1
分析:数据方向:从内网到外网,数据包转换前:172.16.1.1 → 3.3.3.3 ,数据包转换后:2.2.2.2 → 3.3.3.3

ping 了两次通了,相信大家都会这个,接下来看一些特别的。

实验二:
把外网目的地址转换成内网目的地址的 NAT( ip nat inside —— 转换 IP 包的目的地,这些 IP 包正在从外部传输到内部)

实验需求:用外网( IOU3 )telnet 边界路由器( IOU2 )的 Lo0 时,自动将目的地址转换为内网( IOU1 )的 e 0/0 ,即实际 telnet 的是 IOU1 。

IP NAT outside and inside source综合实验拓扑图 - 实验二
IP NAT outside and inside source综合实验拓扑图 – 实验二
还原配置到实验一之前(删除实验一的配置):

IOU2(config)#no ip nat inside source list NAT interface loopback 0 overload
IOU1 上配置 telnet :

line vty 0 4
password cisco
login
transport input telnet
IOU2 配置:

IOU2(config)#ip nat inside source static 192.168.1.1 2.2.2.2 extendable
测试,IOU3 通过 telnet IOU2 的 Lo0 ,成功 telnet 到 IOU1 上:

IOU3#telnet 2.2.2.2
Trying 2.2.2.2 ... Open

User Access Verification

Password:
IOU1>q

[Connection to 2.2.2.2 closed by foreign host]
分析:数据方向:从外网到内网,数据包转换前:23.1.1.3 → 2.2.2.2 ,数据包转换后:23.1.1.3 → 192.168.1.1

实验三:
把一个外网目的地址转换成另一个外网目的地址的 NAT( ip nat outside —— 转换 IP 包的目的地,这些 IP 包正在从内部传输到外部)

实验需求:内网( IOU1 )访问外网 3.3.3.3 时,自动将目的地址从 3.3.3.3 转换为 4.4.4.4 ,即内网( IOU1 )使用 3.3.3.3 的地址去访问 4.4.4.4 。

IP NAT outside and inside source综合实验拓扑图 - 实验三
IP NAT outside and inside source综合实验拓扑图 – 实验三
删除实验二的 NAT 配置,并且配置实验一所述的那个 NAT :

IOU2(config)#no ip nat inside source static 192.168.1.1 2.2.2.2 extendable
IOU2(config)#ip nat inside source list NAT interface loopback 0 overload
为什么要加这个 NAT ?因为 IP 包要从内部传输到外部,需要这个 NAT 把内网 IP 地址转换成外网 IP 地址,否则 IOU1 无法跟外网通信。

IOU2 配置:

IOU2(config)#ip nat outside source static 4.4.4.4 3.3.3.3 extendable
测试,IOU1 ping IOU3 ,成功到达 IOU4 :

IOU4#deb ip icmp
ICMP packet debugging is on

IOU1#p 3.3.3.3  
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 3.3.3.3, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 1/6/12 ms

IOU4#
*Mar 13 05:16:53.507:ICMP:echo reply sent,src 4.4.4.4,dst 2.2.2.2,
topology BASE, dscp 0 topoid 0

*Mar 13 05:16:53.507:ICMP:echo reply sent,src 4.4.4.4,dst 2.2.2.2,
topology BASE, dscp 0 topoid 0

*Mar 13 05:16:53.513:ICMP:echo reply sent,src 4.4.4.4,dst 2.2.2.2,
topology BASE, dscp 0 topoid 0

*Mar 13 05:16:53.520:ICMP:echo reply sent,src 4.4.4.4,dst 2.2.2.2,
topology BASE, dscp 0 topoid 0

*Mar 13 05:16:53.531:ICMP:echo reply sent,src 4.4.4.4,dst 2.2.2.2,
topology BASE, dscp 0 topoid 0
分析:数据方向:从内网到外网,数据包转换前:192.168.1.1 → 3.3.3.3 ,数据包转换后:2.2.2.2 → 4.4.4.4

注意!数据包的源地址从 192.168.1.1 转换到 2.2.2.2 ,是通过 ip nat inside source list NAT interface loopback 0 overload 实现的;数据包的目的地址从 3.3.3.3 转换到 4.4.4.4 ,是通过 ip nat outside source static 4.4.4.4 3.3.3.3 extendable 实现的。

实验三(增强):
把内网目的地址转换成外网目的地址的 NAT( ip nat outside —— 转换 IP 包的目的地,这些 IP 包正在从内部传输到外部)

实验需求:内网( IOU1 )去 ping 一个不存在的内网 IP 地址,比如 192.168.1.100 ,将该目的地址转换成外网 IP 地址 4.4.4.4 。

IP NAT outside and inside source综合实验拓扑图 - 实验三(增强)
IP NAT outside and inside source综合实验拓扑图 – 实验三(增强)
删除实验三的 NAT 配置,并且配置实验一所述的那个 NAT :

IOU2(config)#no ip nat outside source static 4.4.4.4 3.3.3.3 extendable
IOU2(config)#ip nat inside source list NAT interface loopback 0 overload
为什么要加这个 NAT ?因为 IP 包要从内部传输到外部,需要这个 NAT 把内网 IP 地址转换成外网 IP 地址,否则 IOU1 无法跟外网通信。

内网是 ping 不通 192.168.1.100 这个地址的,因为这个地址不存在:

IOU1#p 192.168.1.100
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 192.168.1.100, timeout is 2 seconds:
.....
Success rate is 0 percent (0/5)
IOU2 配置:

IOU2(config)#ip nat outside source static 4.4.4.4 192.168.1.100 extendable
测试,经过 NAT 转换,这个不存在的地址 ping 通了!

IOU1#p 192.168.1.100
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 192.168.1.100, timeout is 2 seconds:
.!!!!
Success rate is 80 percent (4/5), round-trip min/avg/max = 1/3/5 ms
分析:因为 outside 是先转换再路由,所以 192.168.1.100 这个目的地址虽然不存在,但是转换后的目的地址 4.4.4.4 是可以路由的。

数据方向:从内网到外网,数据包转换前:192.168.1.1 → 192.168.1.100 ,数据包转换后:2.2.2.2 → 4.4.4.4

注意!数据包的源地址从 192.168.1.1 转换到 2.2.2.2 ,是通过 ip nat inside source list NAT interface loopback 0 overload 实现的;数据包的目的地址从 192.168.1.100 转换到 4.4.4.4 ,是通过 ip nat outside source static 4.4.4.4 192.168.1.100 extendable 实现的。

实验四:
把外网源地址转换成内网源地址的 NAT( ip nat outside —— 转换 IP 包的源,这些 IP 包正在从外部传输到内部)

实验需求:外网( IOU4 )去 ping 边界路由器( IOU2 ),边界路由器( IOU2 )将该数据包的源地址(注意不是目的地址)转换成 192.168.1.2 。

IP NAT outside and inside source综合实验拓扑图 - 实验四
IP NAT outside and inside source综合实验拓扑图 – 实验四
删除实验三(增强)的 NAT 配置,并且配置实验二所述的那个 NAT :

IOU2(config)#no ip nat outside source static 4.4.4.4 192.168.1.100 extendable
IOU2(config)#no ip nat inside source list NAT interface loopback 0 overload
IOU2(config)#ip nat inside source static 192.168.1.1 2.2.2.2 extendable
为什么要加这个 NAT ?因为 IOU4 在外网,IOU4 根据需求实际是要去试图连通内网的 IOU1 。而外网是没法主动发起连接去访问内网的主机的,所以需要这个 NAT ,将 192.168.1.1 这个内网地址映射成为一个外网地址,也就是 2.2.2.2 ,这样外网 IOU4 去 ping 2.2.2.2 实际是 ping 192.168.1.1 。

IOU2 配置:

IOU2(config)#ip nat outside source static 4.4.4.4 192.168.1.2 extendable
测试,IOU1 和 IOU2 先开启 debug :

IOU1#deb ip icmp
ICMP packet debugging is on

IOU2#deb ip icmp
ICMP packet debugging is on
然后 IOU4 用 Lo 0 去 ping 2.2.2.2 。

IOU4#p 2.2.2.2 so l 0
可以看到,数据包的源地址已经成功的从 4.4.4.4 转换为 192.168.1.2 :

IOU1#deb ip icmp
ICMP packet debugging is on
*Mar 13 ... :ICMP:echo reply sent,src 192.168.1.1,dst 192.168.1.2,
topology BASE, dscp 0 topoid 0

*Mar 13 ... :ICMP:echo reply sent,src 192.168.1.1,dst 192.168.1.2,
topology BASE, dscp 0 topoid 0

*Mar 13 ... :ICMP:echo reply sent,src 192.168.1.1,dst 192.168.1.2,
topology BASE, dscp 0 topoid 0

*Mar 13 ... :ICMP:echo reply sent,src 192.168.1.1,dst 192.168.1.2,
topology BASE, dscp 0 topoid 0

*Mar 13 ... :ICMP:echo reply sent,src 192.168.1.1,dst 192.168.1.2,
topology BASE, dscp 0 topoid 0

IOU2#deb ip icmp
ICMP packet debugging is on
*Mar 13 ... :ICMP:echo reply rcvd,src 192.168.1.1,dst 192.168.1.2,
topology BASE, dscp 0 topoid 0

*Mar 13 ... :ICMP:echo reply rcvd,src 192.168.1.1,dst 192.168.1.2,
topology BASE, dscp 0 topoid 0

*Mar 13 ... :ICMP:echo reply rcvd,src 192.168.1.1,dst 192.168.1.2,
topology BASE, dscp 0 topoid 0

*Mar 13 ... : CMP:echo reply rcvd,src 192.168.1.1,dst 192.168.1.2,
topology BASE, dscp 0 topoid 0

*Mar 13 ... :ICMP:echo reply rcvd,src 192.168.1.1,dst 192.168.1.2,
topology BASE, dscp 0 topoid 0
因为源地址是 192.168.1.2 ,数据包不返回给 4.4.4.4 了,所以 4.4.4.4 收不到回包。

IOU4#p 2.2.2.2 so l 0
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 2.2.2.2, timeout is 2 seconds:
Packet sent with a source address of 4.4.4.4
.....
Success rate is 0 percent (0/5)
分析:数据方向:从外网到内网,数据包转换前:4.4.4.4 → 2.2.2.2 ,数据包转换后:192.168.1.2 → 192.168.1.1

注意!数据包的源地址从 4.4.4.4 转换到 192.168.1.2 ,是通过 ip nat outside source static 4.4.4.4 192.168.1.2 extendable 实现的;数据包的目的地址从 2.2.2.2 转换到 192.168.1.1 ,是通过 ip nat inside source static 192.168.1.1 2.2.2.2 extendable 实现的。

总结:
命令        操作
ip nat outside source list       
转换 IP 包的源,这些 IP 包正在从外部传输到内部
转换 IP 包的目的地,这些 IP 包正在从内部传输到外部
ip nat inside source list       
转换 IP 包的源,这些 IP 包正在从内部传输到外部
转换 IP 包的目的地,这些 IP 包正在从外部传输到内部

您需要登录后才可以回帖 登录 | 论坛注册

本版积分规则

QQ|Archiver|手机版|小黑屋|sitemap|鸿鹄论坛 ( 京ICP备14027439号 )  

GMT+8, 2025-4-30 10:46 , Processed in 0.135174 second(s), 23 queries , Redis On.  

  Powered by Discuz!

  © 2001-2025 HH010.COM

快速回复 返回顶部 返回列表