1.SNAT策略概述
SNAT(Source Network Address Translation,源地址转换)是Linux防火墙的一种地址转换操作,也是iptables命令中的一种数据包控制类型,其作用是根据特定的条件修改数据包的源IP地址。
SNAT应用环境
- 局域网主机共享单个公网IP地址的Internet(私有IP不能在Internet中正常路由)
局域网共享上网
SNAT策略的原理:
- 源地址转换,Source Network Address Translation
- 修改数据包的源地址
SNAT源地址转换过程
-
数据包从内网发送到公网时,SNAT会把数据包的源地址由私网IP转换成公网IP。
-
当相应的数据包从公网发送到内网时,会把数据包的目的地址由公网IP转换为私网IP。
-
当内网有多台主机访问外网时,SNAT在转换时会自动分配端口,不同内网主机会通过端口号进行区分。
SNAT策略概述的应用
前提条件
- 局域网各主机正确设置P地址/子网掩码
- 局域网各主机正确设置默认网关地址
1.1Linux网关支持lP路由转发
1.2.实验:SNAT地址转换
准备:3台虚拟机
- 1台坐客户端(地址转换)
- 1台网关服务器
- 1一台外部服务器
1.先配置环境
外部服务器修改网卡
网关服务器添加一张网卡并修改
客户端修改一下网卡
2.修改网关服务器的网卡配置
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
//修改ens33网卡配置
[root@localhost network-scripts]# ls
ifcfg-ens33 ifdown-isdn ifup ifup-plip ifup-tunnel
ifcfg-lo ifdown-post ifup-aliases ifup-plusb ifup-wireless
ifdown ifdown-ppp ifup-bnep ifup-post init.ipv6-global
ifdown-bnep ifdown-routes ifup-eth ifup-ppp network-functions
ifdown-eth ifdown-sit ifup-ib ifup-routes network-functions-ipv6
ifdown-ib ifdown-Team ifup-ippp ifup-sit
ifdown-ippp ifdown-TeamPort ifup-ipv6 ifup-Team
ifdown-ipv6 ifdown-tunnel ifup-isdn ifup-TeamPort
[root@localhost network-scripts]# vim ifcfg-ens33
[root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-ens36
//直接复制ens33的配置当做模板
[root@localhost network-scripts]# vim ifcfg-ens36
//通过ens33的模板修改ens36网卡配置
[root@localhost network-scripts]# systemctl restart network
[root@localhost network-scripts]# vim /var/log/messages
重启报错的话就查看日志修改配置即可
重启网卡以后查看,网卡修改成功
3.修改外部服务器
修改外部服务器的网卡配置
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# vim ifcfg-ens33
重启网卡并查看网卡配置
[root@localhost network-scripts]# systemctl restart network
[root@localhost network-scripts]# ifconfig
4.修改客户端(用作地址转换的机器)
修改网卡配置
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# vim ifcfg-ens33
主要:DNS不需要,注释掉
5.修改外部服务器
[root@localhost network-scripts]# yum install -y httpd
安装httpd服务
[root@localhost network-scripts]# systemctl start httpd
启动httpd服务
使用浏览器测试一下12.0.0.12是不是通的
在网关服务器测试也是可以通的
使用网关服务器ping12.0.0.12是可以通的
ping客户端也是可以通的
使用客户端分别去ping
修改网关服务器的内核配置文件
[root@localhost network-scripts]# vim /etc/sysctl.conf
修改内核配置文件
这个服务是用来起来IP的路由的转发功能
[root@localhost network-scripts]# sysctl -p
net.ipv4.ip_forward = 1
修改完配置以后 -p 加载一下配置
1 代表开始 IP路由转发功能
此时再用客户端去ping 12.0.0.12 就可以通了
6.去外部服务器开启日志
[root@localhost network-scripts]# cd /var/log/httpd
httpd的日志文件
[root@localhost httpd]# ls
access_log error_log
[root@localhost httpd]# tail -f access_log
跟踪日志的情况
用客户机去访问 12.0.0.12
查看外部服务器的httpd日志
目前只是实现了IP地址的转发,没有实现源地址的转换,所以网关服务器的日志中能看到是哪一台主机来访问的,能查看到内网IP的IP地址
主意:目前只是模拟环境,并不是生产环境,没有实现跟踪路由,在真实的生产环境可以ping通,但是无法访问网站
7.实现源IP地址的公网转换
修改网关服务器的规则
确保nat表中的规则是清空的
[root@localhost network-scripts]# iptables -t nat -A POSTROUTING -s 192.168.85.0/24 -o ens36 -j SNAT --to-source 12.0.0.254
查看nat表的规则
使用客户端访问 12.0.0.12 网站
再去查看外部服务器httpd日志
此时的日志就变成可公网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 #读取修改后的配置
SNAT转换1:固定的公网IP地址
#配置SNAT策略,实现SNAT功能,将所有192.168.72.0这个网段内的ip的源地址改为12.0.0.2
iptables -t nat -A POSTROUTING -s 192.168.72.0/24 -o ens33 -j SNAT --to 12.0.0.2
或
iptables -t nat -A POSTROUTING -s 192.168.72.0/24 -o ens33 -j SNAT --to-source 12.0.0.2-12.0.0.10
#-A POSTROUTING 指定POSTROUTING链
#-s 192.168.72.0/24 源地址所处的网段(内网IP)
#-o ens33 出站网卡
#-j SNAT
#--to 12.0.0.2 外网IP
#--to-source 12.0.0.2-12.0.0.10 外网地址池
SNAT转换2:非固定的公网IP地址(共享动态IP地址)
iptables -t nat -A POSTROUTING -s 192.168.72.0/24 -o ens33 -j MASQUERADE
小知识扩展:
一个IP地址做sNAT转换,一般可以让内网1oo到200台主机实现上网。
2.DNAT
服务器一般不会暴露在公网中,极易被人攻击。服务器一般使用内网IP,所以访问服务器时需要进行目标地址转换。
DNAT策略的的典型应用环境:
- 在Internet中发布位于企业局域网内的服务器
DNAT策略的原理
- 目标地址转换,Destination Network Address Translation
- 修改数据包的目标地址
2.1.DNAT转换前提条件
- 局域网的Web服务器能够访问Internet
- 网关的外网地址有正确的DNS解析记录
- Linux网关开启IP路由转发
DNAT转换的情况
实验DNAT
准备环境
进入网关服务器
修改网卡配置
systemctl stop firewalld //必须关闭防火墙
setenforce 0
systemctl status iptables.service //查看一下iptables是不是打开的状态
yum -y install iptables-services.x86_64 //未打开就安装软件
systemctl status iptables.service //再看一下状态
systemctl start iptables.service //重启一下服务
iptables -F && iptables -t nat -F //清空filter表和nat表
iptables -t nat -nL //查看nat表中是否还存在规则
开启 ACCEPT
[root@localhost network-scripts]# iptables -P INPUT ACCEPT
修改网卡配置
[root@localhost network-scripts]# pwd
/etc/sysconfig/network-scripts
[root@localhost network-scripts]# vim ifcfg-ens33
[root@localhost network-scripts]# systemctl restart network //重启网卡
外部服务器
修改网卡配置
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# vim ifcfg-ens33
[root@localhost network-scripts]# systemctl restart network //重启网卡
ping 网关是可以ping通的
由于做外网服务器,所有需要安装httpd服务
[root@localhost network-scripts]# yum -y install httpd
[root@localhost network-scripts]# systemctl start httpd
启动一下服务
访问一下本机服务器
进入一台linu客户机
[root@localhost ~]# iptables -F && iptables -t nat -F
清空一下防火墙规则
修改网卡配置
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# vim ifcfg-ens33
[root@localhost network-scripts]# systemctl restart network
重启网卡
ping一下网关
打开window客户机 设置一下网卡配置
ping网关是可以通的
修改规则
给网关服务器配置规则
修改内核文件
[root@localhost network-scripts]# vim /etc/sysctl.conf
[root@localhost network-scripts]# sysctl -p
net.ipv4.ip_forward = 1
加载一下,使修改永久生效
修改nat表中的规则
[root@localhost network-scripts]# iptables -t nat -A POSTROUTING -s 192.168.85.0/24 -o ens36 -j SNAT --to 12.0.0.254
[root@localhost network-scripts]# iptables -t nat -A PREROUTING -d 12.0.0.254 -p tcp --dport 80 -i ens36 -j DNAT --to 192.168.85.20:80
使用windows服务器测试一下
去外部服务器看一下httpd日志
[root@localhost network-scripts]# cd /var/log/httpd/
[root@localhost httpd]# ls
access_log error_log
[root@localhost httpd]# tail access_log
使用Linux客户机去访问 12.0.0.254
使用Linux客户机安装httpd服务
从内网访问外网
[root@localhost network-scripts]# yum -y install httpd
区分前面的实验给网页写入一点类容作为区分
[root@localhost network-scripts]# cd /var/www/html/
[root@localhost html]# ls
[root@localhost html]# echo 'this is internet test web!' > index.html
[root@localhost html]# ls
index.html
[root@localhost html]# systemctl start httpd //重启一下服务
本地访问网页
外部服务器查看网页也是可以查看的
查看日志
[root@localhost html]# cd /var/log/httpd/
[root@localhost httpd]# ls
access_log error_log
[root@localhost httpd]# tail access_log
结合上面的DNAT实验过程,配置DNS分离解析
客户端
网关服务器
在网关服务上配置DNS分离解析
先下载bind软件
yum -y install bind
修改主配置文件
[root@localhost etc]# vim /etc/named.conf //编辑主配置文件
-----------------------------------
options {
listen-on port 53 { any; };
#listen-on-v6 port 53 { ::1; };
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
allow-query { any; };
52 #zone "." IN {
53 # type hint;
54 # file "named.ca";
55 #};
修改区域配置文件
[root@localhost etc]# vim /etc/named.rfc1912.zones
--------------------------------------------------
view "lan" {
match-clients { 192.168.85.0/24; };
zone "wangsan.com" IN {
type master;
file "wangsan.com.zone.lan";
};
zone "." IN {
type hint;
file "named.ca";
};
};
view "wan" {
match-clients { any; };
zone "wangsan.com" IN {
type master;
file "wangsan.com.zone.wan";
};
};
~
修改内网解析地址库文件:
[root@localhost etc]# cd /var/named/
[root@localhost named]# cp -p named.localhost wang.com.zone.lan
[root@localhost named]# vim wang.com.zone.lan
$TTL 1D
@ IN SOA @ wangsan.com. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS wangsan.com.
A 192.168.85.254
www A 192.168.85.20
~
修改外网解析地址库文件。之后启动named服务
[root@localhost named]# cp -p named.localhost wang.com.zone.wan
[root@localhost named]# vim wang.com.zone.wan
$TTL 1D
@ IN SOA @ wangsan.com. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS wangsan.com.
A 12.0.0.254
www IN A 12.0.0.254
~
[root@localhost ~]# systemctl restart named //c重启服务
[root@localhost named]# host www.wangsan.com
www.wangsan.com has address 192.168.85.20
[root@localhost named]# cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 192.168.85.254
nameserver 12.0.0.254
再去Linux客户机
[root@localhost httpd]# systemctl status httpd //查看是否开启
此时就解析成功了
tcpdump 抓包
网络数据包截获分析工具。支持针对网络层、协议、主机、网络或端口的过滤。并提供and、or、not等逻辑语句帮助去除无用的信息。
安装 tcpdump
/ # apk update
/ # apk add tcpdump
tcpdump top -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后可以抓到完整的数据包(5)-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)分析
例题
要在ens33网卡上抓取http协议相关的数据包
tcpdump tcp port 80 and -i ens33 -s0 -w ./pkg.cap
要在ens33网卡上抓取和192.168.80.100主机通信的http协议相关的数据包
tcpdump tap port 80 and -i ens33 -s0 -w ./pkg.cap an[i host 192.168.85.100