iptables防火墙 (SNAT、DNAT)
iptables防火墙 (SNAT、DNAT)
- iptables防火墙 (SNAT、DNAT)
- SNAT策略及应用
- SNAT策略概述
- SNAT策略的典型应用环境
- SNAT策略的原理
- SNAT转换前提条件:
- 临时打开:
- 永久打开:
- SNAT实验
- 实验准备
- SNAT转换
- DNAT策略及应用
- DNAT 应用环境:
- DNAT原理:
- DNAT转换前提条件:
- 编写DNAT转换规则
- 在内网上配置
- 在网关服务器添加iptables规则
- 测试外网是否能访问内网
- 规则的导入、导出
- 规则的备份及还原
- iptables-save 命令
- 2. iptables-restore 命令
- tcpdump—Linux抓包
iptables防火墙 (SNAT、DNAT)
SNAT策略及应用
SNAT策略概述
SNAT策略的典型应用环境
局域网主机共享单个公网IP地址接入Internet(私有IP不能在Internet中正常路由)
局域网共享上网
未作SNAT转换时的情况
进行SNAT转换后的情况
SNAT策略的原理
- 源地址转换
- 修改数据包的源地址
SNAT转换前提条件:
- 局域网各主机正确设置IP地址、子网掩码、默认网关地址
- Linux网关支持IP路由转发
临时打开:
echo 1 > /proc/sys/net/ipv4/ip_forward
或
sysctl -w net.ipv4.ip_forward=1
永久打开:
vim /etc/sysctl1.conf
net.ipv4.ip_forward=1 #将此行写入配置文件
sysctl -p #读取修改后的配置
SNAT实验
实验准备
- web服务器ip:10.0.0.12(Vmnet3)、关闭防火墙和selinux、开启http服务
- 网关服务器内网ip:192.168.100.100(Vmnet2);外网ip:10.0.0.1(Vmnet3)、关闭防火墙和selinux、开启http服务
- win7客户端ip:192.168.100.110(Vmnet2)
- VMware的虚拟网络编辑器中默认Vmne1模式网段:192.168.100.0,Vmnet2模式网段:10.0.0.0
一、 配置网关服务器(192.168.100.100/10.0.0.1)的相关配置
1.
- 复制并修改ens36与ens33网卡
ens36网卡改为:
NAME=ens36
DEVICE=ens36
IPADDR=10.0.0.1(外网ip)
GATEWAY=10.0.0.1(自己做自己网关)
- 重启网络
systemctl restart network
- 开启SNAT
[root@localhost network-scripts]#
vim /etc/ sysctl. conf
net. ipv4.ip_ forward = 1 #将此行写入配置文件
[root@localhost network-scripts]#sysctl -P #读取修改后的配置
- 配置iptables规则
iptables -nL #查看规则
iptables -nL -t nat #查看规则
iptables -F #清除iptables的规则
iptables -F -t nat #清除iptables的规则
iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o ens33 -j SNAT --to 12.0.0.1
//可换成单独的ip 出栈 外网网卡 外网ip
二、配置外网服务器(10.0.0.12)的相关配置
1.
- 配置网卡ens33
-
重启网络
-
ping网关测试
-
重启网卡、关闭防火墙和增强、开启http服务
systemctl stop firewalld.service
setenforce 0
systemctl start httpd
三、配置客户机(192.168.100.110)
1.
- 配置IP地址
验证结果:局域网主机192.168.100.110能够访问外网的web服务器10.0.0.12
SNAT转换
SNAT转换1:固定的公网IP地址:
iptables -t nat -A POSTROUTING -s 192.168.80.0/24 -o ens36 -j SNAT --to 12.0.0.1
或
iptables -t nat -A POSTROUTING -s 192.168.80.0/24(内网IP) -o(出站) ens36(外网网卡) -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 ens36 -j MASQUERADE
DNAT策略及应用
DNAT 应用环境:
在Internet中发布位于局域网内的服务器
进行DNAT转换后的情况
DNAT原理:
修改数据包的目的地址。
DNAT转换前提条件:
- 局域网的服务器能够访问Internet
- 网关的外网地址有正确的DNS解析记录
- Linux网关开启IP路由转发
编写DNAT转换规则
iptables -t nat -A PREROUTING -i ens33 -d 218.29.30.31(外网接口的IP地址) -p tcp --dport 80(发布的服务端口) -j DNAT --to-destination 192.168.1.6(web主机的内网IP地址)
在内网上配置
#在内网上安装httpd并开启服务
[root@localhost yum.repos.d]# yum install -y httpd
[root@localhost yum.repos.d]# systemctl start httpd
#关闭防火墙和selinux
[root@localhost yum.repos.d]# systemctl stop firewalld.service
[root@localhost yum.repos.d]# setenforce 0
在网关服务器添加iptables规则
#先清空规则
[root@localhost yum.repos.d]#iptables -F -t nat
#添加规则
[root@localhost yum.repos.d]#iptables -t nat -A PREROUTING -i ens38 -d 12.0.0.1 -p tcp --dport 80 -j DNAT --to 192.168.100.102
#查看规则
[root@localhost yum.repos.d]#iptables -nL -t nat
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
DNAT tcp -- 0.0.0.0/0 12.0.0.1 tcp dpt:80 to:192.168.100.102
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 ~]# curl 12.0.0.1
#在内网服务器上
[root@localhost yum.repos.d]# tail /etc/httpd/logs/access_log
127.0.0.1 - - [02/Nov/2021:18:05:31 +0800] "GET / HTTP/1.1" 403 4897 "-" "curl/7.29.0"
12.0.0.100 - - [02/Nov/2021:18:19:45 +0800] "GET / HTTP/1.1" 403 4897 "-" "curl/7.29.0"
规则的导入、导出
在 Linux 系统中,iptables 为我们提供了批量备份与恢复规则的命令,也提供了标准的系统服务以便开启、关闭防火墙功能。
规则的备份及还原
防火墙规则的批量备份、还原用到两个命令,即 iptables-save 和 iptables-restore,分别用来保存(Save)和恢复(Restore)。
iptables-save 命令
iptables-save 命令用来批量导出 Linux 防火墙规则。直接执行 iptables-save 命令时, 将显示出当前启用的所有规则。
[root@localhost ~]# iptables-save
# Generated by iptables-save v1.4.7 on Wed Sep 24 08:25:33 2014
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [54:7037]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited COMMIT
[root@localhost ~]# iptables-save > /opt/iprules_all.txt //备份所有表的规则
2. iptables-restore 命令
iptables-retore 命令用来批量导入 Linux 防火墙规则
[root@localhost ~]# iptables-restore < /opt/iprules_all.txt //从备份文件恢复规则
tcpdump—Linux抓包
tcpdump tcp-i ens33 -t -s 0 -c 100 and dst port ! 22 and src net 192.168.1.0/24 -w ./target.cap
(1)tcp∶ ip icmp arp rarp 和 tcp、udp、icmp这些选项等都要放到第一个参数的位置,用来过滤数据报的类型
(2)-i ens33 ∶只抓经过接口ens33的包
(3)-t ∶不显示时间戳
(4)-s 0 ∶ 抓取数据包时默认抓取长度为68字节。加上-s 0 后可以抓到完整的数据包
(5)-c 100 ∶只抓取100个数据包
(6)dst port ! 22 ∶不抓取目标端口是22的数据包
(7)src net 192.168.1.0/24 ∶数据包的源网络地址为192.168.1.0/24。Net:网段,host:主机
(8)-w ./target.cap ∶ 保存成cap文件,方便用ethereal (即wireshark)分析