资讯详情

iptables防火墙之SNAT与DNAT

一、SNAT

1、SNAT策略概述

2、SNAT工作原理

3、SNAT转换前提条件

4、开启SNAT命令

5、SNAT转换

6、SNAT示例

6.配置网关服务器

6.配置外网服务端

6.3配置虚拟机win10

6.4测试连通性

6.打开5个网关服务器SNAT

6.6客户端访问服务端

6.7服务端查看访问记录

6.8配置网关服务器iptables规则

6.9内网访问服务端

6.10服务端查看日志

二、DNAT

1、DNAT典型的策略应用环境

2、DNAT策略的原理

3、DNAT转换条件

4、DNAT转换

5、DNAT示例

5.配置网关服务器

5.配置内网服务端

5.3配置虚拟机外网win10

5.4开启网关服务器SNAT

5.5配置网关服务器iptables规则

5.外网客户端访问内网服务端

5.7服务端查看日志

三、备份和恢复规则

1、备份

2、还原

四、tcpdump—Linux抓包


一、SNAT

1、SNAT策略概述

  • 局域网主机共享单个公网IP地址接入Internet

  • 转换源地址,Source Network Address Translation
  • 修改数据包的源地址

2、SNAT工作原理

当数据包从内网发送到公网时,SNAT将数据包的源IP由私网IP转换成公网IP

当响应的数据包从公发送到内网时,数据包的目的将被发送到内网IP由公网IP转换为私网IP

3、SNAT转换前提条件

  • 正确设置局域网主机IP地址、子网掩码、默认网关地址
  • Linux网关开启IP路由转发

4、开启SNAT命令

echo 1 > /proc/sys/net/ipv4/ip_forward
或者
sysctl -w net.ipv4.ip_forward=1

vim /etc/ sysctl. conf
net. ipv4.ip_ forward=1  #将此行写入配置文件

sysctl -p			   #读取修改后的配置

5、SNAT转换

iptables -t nat -A POSTROUTING -s 192.168.52.0/24 -o ens33 -i SNAT --to 12.0.0.1
或
iptables -t nat -A POSTROUTING -s 192.168.52.0/24 -o ens33 -j SNAT --to-source 12.0.0.1-12.0.0.10
										内网IP		出站 外网网卡						外网IP或地址池

6、SNAT示例

6.1配置网关服务器

1、添加一张外网网卡网段为12.0.0.0/24

查看真机的网段与 虚拟机所设网段是否一致

 2、配置内网网卡ens33

[root@localhost ~]# systemctl stop firewalld.service 
[root@localhost ~]# setenforce 0
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33

3、配置外网网卡ens36

[root@localhost ~]# cd /etc/sysconfig/network-scripts 
[root@localhost network-scripts]# ifconfig
[root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-ens36
[root@localhost network-scripts]# vim ifcfg-ens36

3、重启服务

[root@localhost network-scripts]# systemctl restart network
[root@localhost network-scripts]# ifconfig

6.2配置外网服务端

1、配置网卡

[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 

2、重启网卡

[root@localhost ~]# systemctl stop firewalld.service 
[root@localhost ~]# setenforce 0
[root@localhost ~]# ifdown ens33 ; ifup ens33
成功断开设备 'ens33'。
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/10)

3、安装http服务

由于是内网环境仅主机模式下 所以要安装一个本地yum仓库

[root@localhost ~]# mount /dev/sr0 /mnt
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# ls
CentOS-Base.repo  CentOS-CR.repo  CentOS-Debuginfo.repo  CentOS-fasttrack.repo  CentOS-Media.repo  CentOS-Sources.repo  CentOS-Vault.repo
[root@localhost yum.repos.d]# mkdir m
[root@localhost yum.repos.d]# mv *.repo m/
[root@localhost yum.repos.d]# touch 1.repo
[root@localhost yum.repos.d]# vim 1.repo
[root@localhost yum.repos.d]# yum clean all && yum makecache 
[root@localhost yum.repos.d]# yum install httpd -y

4、启动httpd服务

[root@localhost ~]# systemctl start httpd.service 
[root@localhost ~]# netstat -atpn |grep httpd
tcp6       0      0 :::80                   :::*                    LISTEN      56112/httpd         

6.3配置虚拟机win10

网卡选择内网网卡

 

关闭防火墙

6.4测试连通性

1、外网服务器测试

2、服务端测试

6.5网关服务器开启SNAT

[root@localhost ~]# vim /etc/sysctl.conf
------------------------------------------
net.ipv4.ip_forward = 1

[root@localhost ~]# sysctl -p
net.ipv4.ip_forward = 1

6.6客户端访问服务端

6.7服务端查看访问记录

[root@localhost ~]# tail /var/log/httpd/access_log

6.8配置网关服务器的iptables规则

1、查看网关服务器的iptables规则并清除

iptables -nL			#查看规则
iptables -nL -t nat		#查看规则
iptables -F				#清除iptables的规则
iptables -F -t nat		#清除iptables的规则

2、添加规则

[root@localhost ~]# iptables -t nat -A POSTROUTING -s 192.168.192.0/24 -o ens36 -j SNAT 
--to 12.0.0.254                                       源地址(内网网段)      出站网卡
  外网网关                                                                                       
[root@localhost ~]# iptables -nL -t nat
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination         
SNAT       all  --  192.168.192.0/24     0.0.0.0/0            to:12.0.0.254

6.9内网访问服务端

1、清除浏览器缓存数据

6.10服务端查看日志

IP地址由内网本身的IP地址转变为设定的IP 12.0.0.254

[root@localhost ~]# tail /var/log/httpd/access_log

二、DNAT

1、DNAT策略的典型应用环境

  • 在lnternet中发布位于企业局域网内的服务器

2、DNAT策略的原理

  • 目标地址转换,Destination Network Address Translation
  • 修改数据包的目标地址

3、DNAT转换条件

  • 局域网的服务器能够访问Internet
  • 网关的外网地址有正确的DNS解析记录
  • Linux网关开启IP路由转发
vim /etc/sysctl.conf 
net.ipv4.ip_forward = 1 
sysct1 -p

4、DNAT转换 

#把从ens33进来的要访问web服务的数据包目的地址转换为 192.168.192.11
iptables -t nat -A PREROUTING -i ens33 -d   12.0.0.1 -p tcp --dport 80    -j DNAT --to 192.168.192.11:8080
            表        链         入站网卡    外网来的数据包的自的IP和目的端口  通过DNAT转换为内网的IP和端口 
                                                                                                                             
外网2.0.0.1:80 ----DNAT-—->192.168.192.11:8080

5、DNAT示例

5.1配置网关服务器

1、添加一张外网网卡网段为12.0.0.0/24

查看真机的网段与 虚拟机所设网段是否一致

 2、配置内网网卡ens33

[root@localhost ~]# systemctl stop firewalld.service 
[root@localhost ~]# setenforce 0
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33

3、配置外网网卡ens36

[root@localhost ~]# cd /etc/sysconfig/network-scripts 
[root@localhost network-scripts]# ifconfig
[root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-ens36
[root@localhost network-scripts]# vim ifcfg-ens36

3、重启服务

[root@localhost network-scripts]# systemctl restart network
[root@localhost network-scripts]# ifconfig

5.2配置内网服务端

1、配置网卡

[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 

2、关闭防火墙重启网卡

[root@localhost ~]# systemctl stop firewalld.service  //关闭防火墙
[root@localhost ~]# setenforce 0                      //关闭selinux
[root@localhost ~]# ifdown ens33 ; ifup ens33
成功断开设备 'ens33'。
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/10)

3、安装http服务

由于是内网环境仅主机模式下 所以要安装一个本地yum仓库

[root@localhost ~]# mount /dev/sr0 /mnt
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# ls
CentOS-Base.repo  CentOS-CR.repo  CentOS-Debuginfo.repo  CentOS-fasttrack.repo  CentOS-Media.repo  CentOS-Sources.repo  CentOS-Vault.repo
[root@localhost yum.repos.d]# mkdir m
[root@localhost yum.repos.d]# mv *.repo m/
[root@localhost yum.repos.d]# touch 1.repo
[root@localhost yum.repos.d]# vim 1.repo
[root@localhost yum.repos.d]# yum clean all && yum makecache 
[root@localhost yum.repos.d]# yum install httpd -y

4、启动httpd服务

[root@localhost ~]# systemctl start httpd.service 
[root@localhost ~]# netstat -atpn |grep httpd
tcp6       0      0 :::80                   :::*                    LISTEN      56112/httpd         

5.3配置虚拟机外网win10

网卡选择外网网卡

 

关闭防火墙

5.4网关服务器开启SNAT

[root@localhost ~]# vim /etc/sysctl.conf
------------------------------------------
net.ipv4.ip_forward = 1

[root@localhost ~]# sysctl -p
net.ipv4.ip_forward = 1

5.5配置网关服务器的iptables规则

1、查看网关服务器的iptables规则并清除

iptables -nL			#查看规则
iptables -nL -t nat		#查看规则
iptables -F				#清除iptables的规则
iptables -F -t nat		#清除iptables的规则

2、添加SNAT与DNAT规则

[root@localhost ~]# iptables -F -t nat
[root@localhost ~]# iptables -nL -t nat
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination         
[root@localhost ~]# iptables -t nat -A POSTROUTING -s 192.168.192.0/24 -o ens36 -j SNAT --to 12.0.0.254
[root@localhost ~]# iptables -t nat -A PREROUTING -i ens36 -d 12.0.0.254 -p tcp --dport 8080 -j DNAT --to 192.168.192.100:80
[root@localhost ~]# iptables -nL -t nat
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         
DNAT       tcp  --  0.0.0.0/0            12.0.0.254           tcp dpt:8080 to:192.168.192.100:80

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination         
SNAT       all  --  192.168.192.0/24     0.0.0.0/0            to:12.0.0.254

5.6外网客户端访问内网服务端

1、清除浏览器缓存数据

2、访问服务端

5.7服务端查看日志

IP地址由内网本身的IP地址转变为设定的IP 12.0.0.254

[root@localhost ~]# tail /var/log/httpd/access_log

三、规则的备份与还原

1、备份

默认备份文件 /etc/sysconfig/iptables

重定向

2、还原 

3、设置自己想要的规则

重定向输入到默认配置文件当中
[root@localhost sysconfig]#iptables-save >/etc/sysconfig/iptables      

重启防火墙
[root@localhost sysconfig]#systemctl stop iptables
[root@localhost sysconfig]#systemctl start iptables

查看规则
[root@localhost sysconfig]#iptables -nL
[root@localhost sysconfig]#iptables -nL -t nat

tcpdump tcp -i ens33 -t -s 0 -c 100 and dst port ! 22 and src net 192.168.1.0/24 -w ./target.cap
  • tcp∶ ip icmp arp rarp 和 tcp、udp、icmp这些选项等都要放到第一个参数的位置,用来过滤数据报的类型
  • -i ens33 ∶只抓经过接口ens33的包
  • -t ∶不显示时间戳
  • -s 0 ∶ 抓取数据包时默认抓取长度为68字节。加上-s 0 后可以抓到完整的数据包
  • -c 100 ∶只抓取100个数据包
  • dst port ! 22 ∶不抓取目标端口是22的数据包
  • src net 192.168.1.0/24 ∶数据包的源网络地址为192.168.1.0/24
  • -w ./target.cap ∶ 保存成cap文件,方便用ethereal (即wireshark)分析  

标签: 压差传感器dpt0100u1

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台