iptables nat 测试
2015-09-10 13:49
405 查看
网络环境:
node01: eth0: 10.11.1.197/22
node02: eth0: 10.11.1.198/22, eth1: 192.168.1.198/24
node03: eth0: 192.168.1.199/24
SNAT 测试
node02始终作为NAT的节点,因为它可以同时访问到node01和 node03
模拟场景, 从node01 ping node03 的ip (192.168.1.199)经过node02的时候source ip通过SNAT转换为node02的ip(192.168.1.199)
当前网络环境 node01 不能ping通 192.168.1.198 和 192.168.1.199 因为node01还没建立192.168.1.x的路由。
1. 在node02建立相应的SNAT规则(SNAT规则一般建在POSTROUTING上):
$ iptables -t nat -A POSTROUTING -s 10.11.1.197 -j DNAT --to-destination 192.168.1.198
2. 在node01上建立相应的路由规则:
$ route add -net 192.168.1.0/24 gw 10.11.1.198
3. 从node01上ping 192.168.1.199
$ ping 192.168.1.199
PING 192.168.1.199 (192.168.1.199) 56(84) bytes of data.
64 bytes from 192.168.1.199: icmp_seq=1 ttl=64 time=2.83 ms
64 bytes from 192.168.1.199: icmp_seq=2 ttl=64 time=1.10 ms
4. 从node03上抓icmp的包, node03查看的是来自192.168.1.198(node02)的包:
$ tcpdump -nei eth0 icmp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
01:47:46.394650 00:50:56:b0:74:08 > 00:50:56:b0:52:d7, ethertype IPv4 (0x0800), length 98: 192.168.1.198 > 192.168.1.199: ICMP echo request, id 32146, seq 5, length 64
01:47:46.394716 00:50:56:b0:52:d7 > 00:50:56:b0:74:08, ethertype IPv4 (0x0800), length 98: 192.168.1.199 > 192.168.1.198: ICMP echo reply, id 32146, seq 5, length 64
DNAT 测试
为了更好的测试,在测试之前最好清空SNAT测试的规则,路由等。
模拟场景: 在node02 eth0上增加一个secondary ip, node01访问这个ip的时候,其实真正访问的是node03
1. 往node02上增加一个secondary ip
$ ip addr add 10.11.1.199/22 dev eth0
2. 建立DNAT规则, (DNAT规则一般建立在PREROUTING上), 将访问10.11.1.199的包转换为访问192.168.1.199的包。
$ iptables -t nat -A PREROUTING -d 10.11.1.199 -j DNAT --to-destination 192.168.1.199
3. 从node01上ping 10.11.1.199
$ ping 10.11.1.199
PING 10.11.1.199 (10.11.1.199) 56(84) bytes of data.
64 bytes from 10.11.1.199: icmp_seq=1 ttl=64 time=1.42 ms
64 bytes from 10.11.1.199: icmp_seq=2 ttl=64 time=0.905 ms
4. 从node03上抓icmp包, node03上同样抓的是来自192.168.1.198(node02)的包。
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
01:54:24.446025 00:50:56:b0:74:08 > 00:50:56:b0:52:d7, ethertype IPv4 (0x0800), length 98: 192.168.1.198 > 192.168.1.199: ICMP echo request, id 32156, seq 1, length 64
01:54:24.446266 00:50:56:b0:52:d7 > 00:50:56:b0:74:08, ethertype IPv4 (0x0800), length 98: 192.168.1.199 > 192.168.1.198: ICMP echo reply, id 32156, seq 1, length 64
5. 为了让node01能ssh到node03, 在node02上打开FORWARD chain的ssh端口:
$ iptables -A FORWARD -p tcp --dport 22 -j ACCEPT
6. 从node01上ssh 10.11.1.199, 虽然10.11.1.199绑定在node02上, 真正访问的节点其实是 node03
$ ssh root@10.11.1.199
root@10.11.1.199's password:
Last login: Thu Sep 10 01:37:10 2015 from 192.168.1.198
[root@node03~]#
node01: eth0: 10.11.1.197/22
node02: eth0: 10.11.1.198/22, eth1: 192.168.1.198/24
node03: eth0: 192.168.1.199/24
SNAT 测试
node02始终作为NAT的节点,因为它可以同时访问到node01和 node03
模拟场景, 从node01 ping node03 的ip (192.168.1.199)经过node02的时候source ip通过SNAT转换为node02的ip(192.168.1.199)
当前网络环境 node01 不能ping通 192.168.1.198 和 192.168.1.199 因为node01还没建立192.168.1.x的路由。
1. 在node02建立相应的SNAT规则(SNAT规则一般建在POSTROUTING上):
$ iptables -t nat -A POSTROUTING -s 10.11.1.197 -j DNAT --to-destination 192.168.1.198
2. 在node01上建立相应的路由规则:
$ route add -net 192.168.1.0/24 gw 10.11.1.198
3. 从node01上ping 192.168.1.199
$ ping 192.168.1.199
PING 192.168.1.199 (192.168.1.199) 56(84) bytes of data.
64 bytes from 192.168.1.199: icmp_seq=1 ttl=64 time=2.83 ms
64 bytes from 192.168.1.199: icmp_seq=2 ttl=64 time=1.10 ms
4. 从node03上抓icmp的包, node03查看的是来自192.168.1.198(node02)的包:
$ tcpdump -nei eth0 icmp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
01:47:46.394650 00:50:56:b0:74:08 > 00:50:56:b0:52:d7, ethertype IPv4 (0x0800), length 98: 192.168.1.198 > 192.168.1.199: ICMP echo request, id 32146, seq 5, length 64
01:47:46.394716 00:50:56:b0:52:d7 > 00:50:56:b0:74:08, ethertype IPv4 (0x0800), length 98: 192.168.1.199 > 192.168.1.198: ICMP echo reply, id 32146, seq 5, length 64
DNAT 测试
为了更好的测试,在测试之前最好清空SNAT测试的规则,路由等。
模拟场景: 在node02 eth0上增加一个secondary ip, node01访问这个ip的时候,其实真正访问的是node03
1. 往node02上增加一个secondary ip
$ ip addr add 10.11.1.199/22 dev eth0
2. 建立DNAT规则, (DNAT规则一般建立在PREROUTING上), 将访问10.11.1.199的包转换为访问192.168.1.199的包。
$ iptables -t nat -A PREROUTING -d 10.11.1.199 -j DNAT --to-destination 192.168.1.199
3. 从node01上ping 10.11.1.199
$ ping 10.11.1.199
PING 10.11.1.199 (10.11.1.199) 56(84) bytes of data.
64 bytes from 10.11.1.199: icmp_seq=1 ttl=64 time=1.42 ms
64 bytes from 10.11.1.199: icmp_seq=2 ttl=64 time=0.905 ms
4. 从node03上抓icmp包, node03上同样抓的是来自192.168.1.198(node02)的包。
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
01:54:24.446025 00:50:56:b0:74:08 > 00:50:56:b0:52:d7, ethertype IPv4 (0x0800), length 98: 192.168.1.198 > 192.168.1.199: ICMP echo request, id 32156, seq 1, length 64
01:54:24.446266 00:50:56:b0:52:d7 > 00:50:56:b0:74:08, ethertype IPv4 (0x0800), length 98: 192.168.1.199 > 192.168.1.198: ICMP echo reply, id 32156, seq 1, length 64
5. 为了让node01能ssh到node03, 在node02上打开FORWARD chain的ssh端口:
$ iptables -A FORWARD -p tcp --dport 22 -j ACCEPT
6. 从node01上ssh 10.11.1.199, 虽然10.11.1.199绑定在node02上, 真正访问的节点其实是 node03
$ ssh root@10.11.1.199
root@10.11.1.199's password:
Last login: Thu Sep 10 01:37:10 2015 from 192.168.1.198
[root@node03~]#
相关文章推荐
- Linux下主机充当防火墙的巧妙应用之iptables
- iptables做策略屏蔽QQ与MSN
- Nginx+iptables屏蔽访问Web页面过于频繁的IP(防DDOS,恶意访问,采集器)
- Shell脚本实现监控iptables规则是否被修改
- 修改iptables防火墙规则解决vsftp登录后不显示文件目录的问题
- 一键配置CentOS iptables防火墙的Shell脚本分享
- linux增加iptables防火墙规则的示例
- Linux防火墙iptables入门教程
- linux服务器下通过iptables+Denyhost抵御暴力破解的配置方法
- win2003 配置DNS、NAT及搭建IIS网站的方法
- CentOS最严格iptables规则
- 簡單認識 snort rules 條件格式 IDS
- 簡單使用 psad 端口功擊偵測系統 iptables snort
- 簡單設定 kernel 選項在使用 iptables 前
- iptables之SNAT和DNAT
- boot2docker(Mac) NAT 容器PORT 给 主机,外部访问
- iptables简易脚本
- NAT_Static静态NAT的简单配置
- 动态NAT的配置