SNAT和DNAT策略

77 篇文章 0 订阅
15 篇文章 1 订阅

1.SNAT策略及应用

1.1 SNAT原理与应用

SNAT应用环境:局域网主机共享单个公网IP地址接入Internet(私有不能在Internet中被正常路由)

SNAT原理: 修改数据包的源地址

SNAT转换前提条件:

  • 局域网各主机已正确设置IP地址、子网掩码、默认网关地址;

  • Linux网关开启IP路由转发;

#####临时打开
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      #加载,读取修改后的配置

1.2 SNAT策略的工作原理

SNAT的典型应用环境

局域网共享上网

在这里插入图片描述

未作SNAT转换时的情况

在这里插入图片描述

在这里插入图片描述

SNAT转换1:固定的公网IP地址;

iptables -t nat -A POSTROUTING -s 192.168.80.0/24 -o ens33 -j SNAT --to 12.0.0.1  #-t指定nat表,-s指定源地址或源地址网段,-o指定出站网卡,-j指定规则类型,--to修改源地址为网关服务器的外网网卡地址或者外网地址池

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

SNAT转换2:非固定的公网IP地址(共享动态IP地址);

iptables -t nat -A POSTROUTING -s 192.168.80.0/24 -o ens33 -j MASQUERADE   #-j MASQUERADE伪装成一个随机IP

小知识扩展:

一个IP地址做SNAT转换,一般可以让内网100到200 台主机实现上网。

1.3 实验步骤

实验环境说明:

内网客户端主机:CentOS 7-3

网关服务器:CentOS 7-2

外网服务端模拟:CentOS 7-4

SNAT的网络拓扑图:
在这里插入图片描述
注意:

  • 切记所有主机、服务端以及网关服务器都需要关闭防火墙,并禁止开机自启动功能

  • 需要在所有主机和服务器上清空iptables的所有规则以及iptables中nat表的所有规则

[root@myhost2 ~]# systemctl stop firewalld
[root@myhost2 ~]# setenforce 0
[root@myhost2 ~]# systemctl disable firewalld.service
[root@myhost2 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded 
   
[root@clr ~]# iptables -F && iptables -t nat -F

具体实验步骤:

外网服务端模拟:CentOS 7-4

[root@clr ~]# df  #查看光盘是否挂载
文件系统          1K-块    已用     可用 已用% 挂载点
/dev/sr0        4600876 4600876        0  100% /mnt
[root@clr ~]# yum -y install httpd   #安装http服务

[root@clr /var/www]# cd /var/www/html/  #http服务的网页目录
[root@clr /var/www/html]# ls
[root@clr /var/www/html]# echo 'this is test web!' > test.html   #在http的网页目录中写入数据this is test web!
[root@clr /var/www/html]# systemctl start httpd     #启动服务后,在网页地址栏中搜索验证该主机的http服务是否能够正常使用
[root@clr /var/www/html]# cd /etc/sysconfig/network-scripts/   
[root@clr /etc/sysconfig/network-scripts]# vim ifcfg-ens33   #修改外网服务端的IP以及网关地址
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=96bad9fa-55ed-43dd-a1a7-83b7db0e1b1a
DEVICE=ens33
ONBOOT=yes
IPADDR=12.0.0.200   #外网服务器地址
NETMASK=255.255.255.0
GATEWAY=12.0.0.30   #网关服务器的外网网卡地址
#DNS1=192.168.80.2
[root@clr ~]# cd /var/log/httpd/  #查看http服务的日志文件
[root@clr /var/log/httpd]# ls
access_log  error_log
[root@clr /var/log/httpd]# cd /etc/httpd/logs/
[root@clr /etc/httpd/logs]# ls
access_log  error_log
[root@clr /etc/httpd/logs]# grep 'test.html' access_log  #过滤包含有http服务网页目录的日志信息
192.168.80.1 - - [21/May/2023:17:20:36 +0800] "GET /test.html HTTP/1.1" 200 18 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36"
192.168.80.128 - - [21/May/2023:18:43:34 +0800] "GET /test.html HTTP/1.1" 200 18 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0"
12.0.0.30 - - [21/May/2023:19:00:43 +0800] "GET /test.html HTTP/1.1" 304 - "-" "Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0"
12.0.0.30 - - [21/May/2023:19:01:56 +0800] "GET /test.html HTTP/1.1" 200 18 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0"

内网客户端主机:CentOS 7-3

在这里插入图片描述

开启IP转发功能后,可以在内网客户端查看到外网服务端的网页文件;

在这里插入图片描述

内网网关服务器:CentOS 7-2

修改内网网关服务器对接外网网卡ens33的IP地址;

在这里插入图片描述

修改内网网关服务器对接内网网卡ens35的IP地址;

在这里插入图片描述

[root@clr ~]# df -h
文件系统        容量  已用  可用 已用% 挂载点
devtmpfs        2.0G     0  2.0G    0% /dev
tmpfs           2.0G     0  2.0G    0% /dev/shm
tmpfs           2.0G   13M  2.0G    1% /run
tmpfs           2.0G     0  2.0G    0% /sys/fs/cgroup
/dev/sda1        38G  5.6G   32G   16% /
tmpfs           394M   48K  394M    1% /run/user/1000
/dev/sr0        4.4G  4.4G     0  100% /run/media/cCLR/CentOS 7 x86_64
[root@clr ~]# mount /dev/sr0 /mnt/  #挂载挂盘
mount: /dev/sr0 写保护,将以只读方式挂载
[root@clr ~]# yum install -y iptables iptables.service   #安装iptables以及iptables.service服务
[root@clr ~]# rpm -q iptables
iptables-1.4.21-35.el7.x86_64
[root@clr ~]# systemctl start iptables.service
[root@clr ~]# systemctl enable iptables.service
[root@clr ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination  
[root@clr ~]# vim /etc/sysctl.conf
[root@clr ~]# sysctl -p  #加载
net.ipv4.ip_forward = 1  #开启IP转发功能 

[root@clr ~]# iptables -t filter -A FORWARD -s 192.168.80.0/24 -j ACCEPT  #在filter表中添加放通允许转发192.168.80.0/24网段的数据包
[root@clr ~]# iptables -t nat -A POSTROUTING -s 192.168.80.0/24 -o ens33 -j SNAT --to 12.0.0.30
[root@clr ~]# iptables -nL 
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  192.168.80.0/24      0.0.0.0/0           

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
[root@clr ~]# 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.80.0/24      0.0.0.0/0            to:12.0.0.30

在这里插入图片描述

2.DNAT策略

2.1 DNAT策略的概述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.1 DNAT原理与应用

DNAT 应用环境:
在Internet中发布位于局域网内的服务器;
DNAT原理:
修改数据包的目的地址。
DNAT转换前提条件:

1.局域网的服务器能够访问Internet;

2.网关的外网地址有正确的DNS解析记录;

3.Linux网关开启IP路由转发;

vim /etc/sysctl.conf
net.ipv4.ip_forward = 1 	
sysctl -p 		#加载

DNAT转换1:发布内网的Web服务

#把从ens33进来的要访问web服务的数据包目的地址转换为 192.168.80.10
iptables -t nat -A PREROUTING -i ens33 -d 12.0.0.1 -p tcp --dport 80 -j DNAT --to 192.168.80.10
或
iptables -t nat -A PREROUTING -i ens33 -d 12.0.0.1 -p tcp --dport 80 -j DNAT --to-destination 192.168.80.10
                             入站 外网网卡  外网IP								内网服务器IP
iptables -t nat -A PREROUTING -i ens33 -d 12.0.0.1 -p tcp --dport 80 -j DNAT --to 192.168.80.10-192.168.80.20

DNAT转换2:发布时修改目标端口

#发布局域网内部的OpenSSH服务器,外网主机需使用250端口进行连接
iptables -t nat -A PREROUTING -i ens33 -d 12.0.0.1 -p tcp --dport 250 -j DNAT --to 192.168.80.10:22

#在外网环境中使用SSH测试
ssh -p 250 root@12.0.0.1

yum -y install net-tools 		#若没ifconfig命令可提前使用yum进行安装
ifconfig ens33

注意:使用DNAT时,同时还需要配合SNAT使用,才能实现响应数据包的正确返回;
SNAT是内网访问到外网DNAT是外网访问内网SNAT+DNAT实现外网既可以访问内网,内网也可以访问外网

知识扩展:

主机型防火墙 主要使用== INPUT、OUTPUT 链,设置规则时一般要详细的指定到端口==.
网络型防火墙 主要使用 FORWARD链,设置规则时很少去指定到端口,一般指定到IP地址或者到网段即可.

2.3 实验步骤

实验环境说明:

外网客户端主机:CentOS 7-4

网关服务器:CentOS 7-2

内网服务端模拟:CentOS 7-3

在这里插入图片描述
网关服务器:CentOS 7-2
在这里插入图片描述
在这里插入图片描述
关闭防火墙,并禁止开机自启动功能;
在这里插入图片描述
在这里插入图片描述

[root@clr /etc/sysconfig/network-scripts]#  vim /etc/sysctl.conf  #修改该文件,开启ip路由转发功能
[root@clr /etc/sysconfig/network-scripts]# sysctl -p  #加载
net.ipv4.ip_forward = 1

在这里插入图片描述

[root@clr ~]# iptables -t nat -A PREROUTING -i ens33 -d 12.0.0.30 -p tcp --dport 8080 -j DNAT --to 192.168.80.20:80       #添加DNAT策略,修改入站的目的地址  
[root@clr ~]# iptables -nL -t nat
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         
DNAT       tcp  --  0.0.0.0/0            12.0.0.30            tcp dpt:8080 to:192.168.80.20: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         
[root@clr ~]# iptables -t nat -A POSTROUTING -s 192.168.80.0/24 -o ens33 -j SNAT --to 12.0.0.30   #添加SNAT策略,修改出站的源地址  
[root@clr ~]# iptables -nL -t nat
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         
DNAT       tcp  --  0.0.0.0/0            12.0.0.30            tcp dpt:8080 to:192.168.80.20: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.80.0/24      0.0.0.0/0            to:12.0.0.30

在这里插入图片描述

[root@clr /opt]# iptables -t nat -A PREROUTING -i ens33 -d 12.0.0.30 -p tcp --dport 2345 -j DNAT --to 192.168.80.20:22  #将12.0.0.30:2345的IP地址和端口号映射为192.168.80.20:22
[root@clr /opt]# iptables -t nat -nL
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         
DNAT       tcp  --  0.0.0.0/0            12.0.0.30            tcp dpt:8080 to:192.168.80.20:80
DNAT       tcp  --  0.0.0.0/0            12.0.0.30            tcp dpt:2345 to:192.168.80.20:22

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.80.0/24      0.0.0.0/0            to:12.0.0.30

此时使用其他主机连接内网服务器(192.168.80.20)的情况;
在这里插入图片描述

内网服务端模拟:CentOS 7-3
在这里插入图片描述

[root@myhost2 ~]# df -h
文件系统        容量  已用  可用 已用% 挂载点
devtmpfs        2.0G     0  2.0G    0% /dev
tmpfs           2.0G     0  2.0G    0% /dev/shm
tmpfs           2.0G   21M  1.9G    2% /run
tmpfs           2.0G     0  2.0G    0% /sys/fs/cgroup
/dev/sda2        38G  5.4G   32G   15% /
tmpfs           394M   36K  394M    1% /run/user/0
/dev/sr0        4.4G  4.4G     0  100% /run/media/root/CentOS 7 x86_64
[root@myhost2 ~]# mount /dev/sr0 /mnt
mount: /dev/sr0 写保护,将以只读方式挂载
[root@myhost2 ~]# yum install -y httpd  #安装http服务
[root@myhost2 ~]# systemctl restart network #重启网卡
[root@myhost2 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.80.20  netmask 255.255.255.0  broadcast 192.168.80.255
[root@myhost2 ~]# systemctl restart httpd
[root@myhost2 ~]# netstat -lntp | grep :80
tcp6       0      0 :::80                   :::*                    LISTEN      4169/httpd          

在这里插入图片描述

外网客户端主机:CentOS 7-4

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

3.规则的导出、导入

防火墙规则的备份和还原,导出(备份)所有表的规则

iptables-save > /opt/ipt.txt

导入(还原)规则

iptables-restore < /opt/ipt.txt
将iptables规则文件保存在 /etc/sysconfig/iptables 中,iptables服务启动时会自动还原规则
iptables-save > /etc/sysconfig/iptables
systemctl stop iptables						#停止iptables服务会清空掉所有表的规则
systemctl start iptables					#启动iptables服务会自动还原/etc/sysconfig/iptables 中的规则
tcpdump tcp -i ens33 -t -s 0 -c 100 and dst port ! 22 and src net 192.168.1.0/24 -w ./target.cap
选项选项操作含义
tcpip 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)分析

举例:

tcpdump tcp dst port 80 -i ens33 -s0 -w /opt/ens33.cap  #抓tcp协议的80号端口经过ens33网卡的完整数据包,并将抓取的数据包另存到/opt/ens33.cap文件中

在这里插入图片描述
经过DNAT和SNAT转换后的数据包;
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

[root@clr /opt]# iptables-save > /opt/ipt.txt  #自定义保存iptables的规则配置文件
[root@clr /opt]# ls
ens33.cap  ipt.txt  rh
[root@clr /opt]# vim ipt.txt
[root@clr /opt]# iptables -F
[root@clr /opt]# iptables -F -t nat  #清空iptables的规则
[root@clr /opt]# iptables-restore < ipt.txt  #从自定义的保存文件中,恢复iptables的策略
[root@clr /opt]# iptables -nL 
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  12.0.0.0/24          0.0.0.0/0           
ACCEPT     all  --  192.168.80.0/24      0.0.0.0/0           

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
[root@clr /opt]# iptables -nL -t nat
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         
DNAT       tcp  --  0.0.0.0/0            12.0.0.30            tcp dpt:8080 to:192.168.80.20:80
DNAT       tcp  --  0.0.0.0/0            12.0.0.30            tcp dpt:2345 to:192.168.80.20:22

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.80.0/24      0.0.0.0/0            to:12.0.0.30
[root@clr /opt]# cd /etc/sysconfig/
[root@clr /etc/sysconfig]# iptables-save > /etc/sysconfig/iptables  #将自定义的iptables策略写入到防火墙的默认文件中,实现开启iptables服务后,可以自动加载自定义的iptables策略

总结:windows用wireshark,linux用tcpdump.

4. 总结

SNAT 内网–>外网转换源地址

iptables -t nat -A POSTROUTING -s 源地址/网段 -o 出站网卡-j SNAT--to 外网地址

DNAT 外网–>内网 转换目的地址/端口

iptables -t nat -A PREROUTING -i 入站网卡  -d 原目的地址(一般为网关外网网卡地址)  -tcp 协议  --dport原目的端口  -j DNAT  --to  内网地址:端口(DNAT要转换成的地址/端口)

导出规则

iptables-save > XXX

导入规则

iptables-restore < XXX

默认规则文件

/etc/sysconfig/iptables

tcpdump Linux系统抓包工具

tcp  协议 net  网段 -i 网卡 -s 0  port端口 -w XXX.cap
          host IP地址
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陌上花开,静待绽放!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值