码迷,mamicode.com
首页 > 其他好文 > 详细

iptables之NAT

时间:2016-10-24 02:58:22      阅读:338      评论:0      收藏:0      [点我收藏+]

标签:iptables   nat   

iptables之NAT

==============================================================================

概述:





NAT:

1)Firewall

源地址转换(SNAT):POSTROUTING

  主要用于实现让内网客户端访问外部主机地址时使用,要定义在POSTROUTING链或INPUT链

  • 静态转换:外网地址是固定的;

  • 动态转换:外网地址不固定

目标地址转换(DNAT):PREROUTING

PAT:Port Address Translati:端口映射(了解即可)

SNAT实验测试如下:

  实验环境是基于上一篇网络防火墙的环境,这里不再过多赘述:


  1.实验操作之前,内网主机的可以访问外网主机,也可以ping通,如下:

技术分享 

  通过外网的日志查看和请求ping报文清除的知道源地址是内网主机,内网主机没有做任何的转换

技术分享

   2.接下来我们要把内网地址转换成网关主机的外网地址,已达到伪装,隐藏之源地址的目的。

   1)定义规则源地址转换为网关主机的外网地址10.1.252.161,允许内网的所有主机访问外网的所有服务

[root@centos7 ~]# iptables -vnL  # 定义规则前查看
Chain INPUT (policy ACCEPT 16 packets, 1130 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain FORWARD (policy ACCEPT 12 packets, 1008 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 6 packets, 1160 bytes)
 pkts bytes target     prot opt in     out     source               destination         

 定义规则,指明源内网地址,因为访问的是外网的所有服务所以地址和端口不用指定,指明源地址转化为网关
 主机的外网地址
[root@centos7 ~]# iptables -t nat -A POSTROUTING -s 192.168.22.0/24 -j SNAT --to-source 10.1.252.161

[root@centos7 ~]# iptables -t nat -vnL  指明nat表查看定义的规则
Chain PREROUTING (policy ACCEPT 59 packets, 6309 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain INPUT (policy ACCEPT 21 packets, 3188 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 1 packets, 156 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain POSTROUTING (policy ACCEPT 1 packets, 156 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    1    84 SNAT       all  --  *      *       192.168.22.0/24      0.0.0.0/0            to:10.1.252.161

  接下来,内网主机再次访问外网的web服务和ping操作,外网抓包和访问日志已经伪装成网关主机的外网地址,如下:

技术分享

   为了验证是在哪个环节发生的地址转换,我们在网关主机的内网网卡和外网网卡分别抓包,如下:

技术分享


DNAT目标地址转换实验如下:

   实验前环境准备:

   1.因为DNAT为目标地址转换,即让内网中的服务器在外网中可以被访问到,所以,为了增加实验的说服力,我这里在内网的同一块网卡上再添加一个ip地址,让每一个ip地址对应一个服务,如下:

技术分享 

   2.如上,地址已经添加成功,假设22地址对应的是web服务,23的地址对应的是ssh服务(这里可以想象成两个主机,但实际上是一个主机,为了演示效果);为了不影响实验效果这里我先把上例中定义的SNAT规则清空,如下:

[root@centos7 ~]# iptables -t nat -F
[root@centos7 ~]# iptables -t nat -vnL
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination


实验如下:

   假设内网地址192.168.22.2上提供了web服务,我们希望外网用户把对网关主机外网网卡请求的web服务的80端口全都转化到内网的192.168.22.2上,设置如下:

分析:这里一定是在nat表的PREROUTING链上定义,指明目标IP为网关主机的外网地址10.1.252.161
,指明协议为tcp协议(如果不指明的话所有的服务会统统转到一台内网主机之上),指明目标端口
为网关主机外网地址的80端口(实际上根本没有,因为网关主机跟本就没有提供服务),指明转换的
目标地址为提供服务的内网主机地址192.168.22.2,这里没指明端口,默认就是网关主机的端口80,也
可以不相同,也就是端口映射;

[root@centos7 ~]# iptables -t nat -A PREROUTING -d 10.1.252.161 -p tcp --dport 80 -j DNAT --to-destination 192.168.22.2
[root@centos7 ~]# iptables -t nat -vnL
Chain PREROUTING (policy ACCEPT 3 packets, 675 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DNAT       tcp  --  *      *       0.0.0.0/0            10.1.252.161         tcp dpt:80 to:192.168.22.2

Chain INPUT (policy ACCEPT 3 packets, 675 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination

 查看网关主机确认是没有开通web服务的80端口

[root@centos7 ~]# ss -tnl
State       Recv-Q Send-Q                                      Local Address:Port                                                     Peer Address:Port              
LISTEN      0      25                                                      *:514                                                                 *:*                  
LISTEN      0      128                                                     *:22                                                                  *:*                  
LISTEN      0      128                                             127.0.0.1:631                                                                 *:*                  
LISTEN      0      100                                             127.0.0.1:25                                                                  *:*                  
LISTEN      0      128                                             127.0.0.1:6010                                                                *:*                  
LISTEN      0      25                                                     :::514                                                                :::*                  
LISTEN      0      128                                                    :::22                                                                 :::*                  
LISTEN      0      128                                                   ::1:631                                                                :::*                  
LISTEN      0      100                                                   ::1:25                                                                 :::*                  
LISTEN      0      128                                                   ::1:6010                                                               :::*

 使用外网主机请求网关主机的web服务,如下:

技术分享

 

  如上,我们只是把web服务转发到了一个内网服务上,是有选择行的。如果我们现在用外网去ping网关主机的外网地址,是不通的,要想通定义如下:

[root@centos7 ~]# iptables -t nat -F # 清空服务

把上例中指定的协议和端口去掉即可,即外网用户的所有任意服务访问,全都转到内网主机为192.168.22.2的主机
[root@centos7 ~]# iptables -t nat -A PREROUTING -d 10.1.252.161  -j DNAT --to-destination 192.168.22.2
[root@centos7 ~]# iptables -t nat -vnL
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DNAT       all  --  *      *       0.0.0.0/0            10.1.252.161         to:192.168.22.2

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination

  外网主机去ping网关主机的10.1.252.161,在内网主机上抓包如下:

技术分享

















































iptables之NAT

标签:iptables   nat   

原文地址:http://1992tao.blog.51cto.com/11606804/1864825

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!