10.12firewalld和netfilter 10.15 iptables filter表案例 10.16 iptables nat表应用
10.12 firewalld和netfilter
• selinux临时关闭
setenforce 0
• selinux永久关闭
编辑配置文件/etc/selinux/config
vi /etc/selinux/config
• centos7之前使用netfilter防火墙,centos7开始使用firewalld防火墙,但在centos7系统上也可以运行netfilter防火墙,centos7默认开启firewalld防火墙,想要在centos7上使用netfilter防火墙,需要手动进行开启。
• 关闭firewalld开启netfilter方法
关闭firewalld防火墙服务
systemctl stop firewalld
禁止firewalld防火墙服务开机自启动
systemctl disable firewalld
打开netfilter,打开前需要先安装一个包 iptables-service
yum install -y iptables-services
允许netfilter防火墙服务开机自启动
systemctl enable iptables
开启netfilter防火墙服务
systemctl start iptables
查看防火墙规则
[[email protected] ~]# iptables -nvLChain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
77 5124 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0
0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT 41 packets, 5032 bytes)
pkts bytes target prot opt in out source destination
systemctl disable firewalld.service 禁止firewalld防火墙服务开机自启动
systemctl disable firewalld.service 关闭firewalld防火墙服务
yum install -y iptables-services 打开netfilter,打开前需要先安装一个包 iptables-service
systemctl enable iptables 允许netfilter防火墙服务开机自启动
systemctl start iptables 开启netfilter防火墙服务
netfilter是防火墙名字iptables是一个工具
10.13 netfilter5表5链介绍
• netfilter的5个表:filter,nat,managle,raw,security
• filter表用于过滤包,最常用的表,有INPUT、FORWARD、OUTPUT三个链
• nat表用于网络地址转换,有PREROUTING、OUTPUT、POSTROUTING三个链
• managle表用于给数据包做标记(不常用)
• raw表可以实现不追踪某些数据包(不常用)
• security表在centos6中并没有,用于强制访问控制(MAC)的网络规则(不常用)
• netfilter最常用的5个链:INPUT、OUTPUT、FORWARD、PREROUTING、POSTROUTING
所谓链,可以看做是工作台,不同数据流向的数据包,会经过相应的工作台(链),到达工作台后,工作台可对数据包进行相应的操作(封堵、放通等)
INPUT:数据包进入本机时经过的链,可针对数据包源IP以及访问的目标端口等做一些操作限制。
OUTPUT:在本机产生的一些包,在出去之前需要经过的链,可针对数据包目的IP等做一些操作限制。
FORWARD:数据包进入本机时需要经过内核判断目的地是否为本机,如果不是,则经过FORWARD链进 行转发,可以通过FORWARD链对转发地址进行修改等操作。
PREROUTING:在数据包进入网卡时的那一刻(还未到本机),需要经过的链,可用于做端口映射。
端口映射:将实际可以被访问到的公网端口A所接收的数据传输给想要被访问却不能直接被访问的内网端口 B(A映射到B)。
POSTROUTING:在数据包发送到网卡时的那一刻(已从本机出去),需要经过的链,可用于做代理上网。
代理上网(地址转换):不同的内网用户与公网通信时,自身IP地址均会转换为出口的公网IP,用于和公网 进行通信。
• iptables传输数据包的过程
当一个数据包进入网卡时,它首先进入PREROUTING链,内核根据数据包目的IP判断是否需要转送出去
数据包传输流程总体分为两种:
1.数据包需要进入本机,PREROUTING-->INPUT-->OUTPUT-->POSTROUTING
2.数据包不需要进入本机,PREROUTING-->FORWARD-->POSTROUTING
https://blog.****.net/Andy_YF/article/details/7730525
10.14 iptables语法
• 查看iptables规则
iptables -nvL
• 清空规则
iptables -F
注:如果清空后未保存至规则配置文件(/etc/sysconfig/iptables),则服务重启或系统重启后,netfilter防火墙服务依旧会重新加载规则配置文件中的原有规则。
• 重启iptables服务
service iptables restart
• 保存规则
service iptables save
• 查看指定的表
iptables -t 表名 -nvL
注:不加-t,默认为filter表
• 计数器清零
iptables -Z
清零前
[[email protected] ~]# iptables -nvLChain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
382 29930 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0
0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
157 16186 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT 309 packets, 23350 bytes)
pkts bytes target prot opt in out source destination
清零后
[[email protected] ~]# iptables -Z
[[email protected] ~]# iptables -nvLChain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
6 428 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0
0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT 4 packets, 448 bytes)
pkts bytes target prot opt in out source destination
注:清零后,往往还会有数据包,因为规则一直在生效
iptables -I/-A/-D 针对的链 -s 源IP -p 协议类型 --sport 源端口 -d 目的IP --dport 80 -i 网卡名称 -j 动作
-I 插入一条规则(放置在所有规则的最前面)-A 增加一条规则-D 删除一条规则-s 来源IP-p 指定协议--sport 来源端口-d 目标IP--dport 目标端口-i 指定网卡-j 进行操作
规则匹配机制:由上至下匹配,如数据包匹配上第一条规则,即使后续还有其他规则也可与数据包相匹配,数据包也不会继续向下匹配,立即执行它所匹配到的第一条规则。
• 例1:-A增加一条规则,针对INPUT链,-s (source)来源IP为192.168.188.1,-p 指定协议针对TCP协议,sport 来源端口为1234,-d 目标IP为192.168.188.128, dport目标端口为80,针对ens33网卡,最后-j执行丢弃的动作 扔掉
iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -i ens33 -j DROP
TIPS:与DROP相似的动作还有REJECT,区别在于,DROP直接将数据包丢掉,REJECT是查看数据包后将其拒绝。
规则放到最后面 DROP包丢掉 tcp的 来源IP 目标IP 来源端口 目标端口
加--dport 前面必须加-p 否则报错
-A -I 区别 -I就是插队,规则直接到第一条去了
• 例2:插入/增加/删除一条规则,针对INPUT链,来源IP为1.1.1.1,执行丢弃的动作
iptables -I/-A/-D INPUT -s 1.1.1.1 -j DROP
• 例3:插入一条规则,针对INPUT链,来源IP为192.168.1.0/24网段,执行放通的动作
iptables -I INPUT -s 192.168.1.0/24 -i eth0 -j ACCEPT
• 查看iptables规则,并显示规则相对应的序号
iptables -nvL --line-numbers
• 根据规则相对应的序号,对相应规则进行快速删除
iptables -D 针对的链 规则序号
• 默认规则
[[email protected] ~]# iptables -nvLChain INPUT (policy ACCEPT 0 packets, 0 bytes) //默认规则
pkts bytes target prot opt in out source destination
6 428 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0
0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) //默认规则
pkts bytes target prot opt in out source destination
0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT 4 packets, 448 bytes) //默认规则
pkts bytes target prot opt in out source destination
• 修改默认规则
iptables -P 针对的链 执行的动作
注:不要轻易修改默认规则,否则容易造成误拦截!
七周四次课 10.15 iptables filter表案例 /iptables nat表应用
10.15 iptables filter表案例
10.16/10.17/10.18 iptables nat表应用
扩展
1. iptables应用在一个网段 http://www.aminglinux.com/bbs/thread-177-1-1.html
2. sant,dnat,masquerade http://www.aminglinux.com/bbs/thread-7255-1-1.html
3. iptables限制syn速率 http://www.aminglinux.com/bbs/thread-985-1-1.html
10.15 iptables filter表案例
需求:
把80,22,21端口放行,但22端口指定一个IP段,只允许这个IP段的IP访问的时候,才可访问到,其他段的一概拒绝。
根据上述需求,我们可以通过编写一个脚本进行实现
编辑脚本: /usr/local/sbin/iptables.sh
[[email protected] ~]# vim /usr/local/sbin/iptables.sh
添加内容:
#! /bin/bash
ipt="/usr/sbin/iptables"
#这里ipt是定义个一个变量(写脚本的时候,写全局的路径,就是绝对路径,就是后面再加载它,用变量去代替,看着更加简单)$ipt -F#清空之前的规则——>在没有 -t 指定表的时候,默认的就是filter表$ipt -P INPUT DROP#把IPPUT的策略给扔掉$ipt -P OUTPUT ACCEPT#把OUTPUT放行$ipt -P FORWARD ACCEPT#把FORWARD放行$ipt -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT#增加规则,-m --state 指定了状态,并针对这些状态放行(-m --state这种用法并不多见,但是这条规则必须写进来,目的是让相关的数据包放行)$ipt -A INPUT -s 192.168.88.0/24 -p tcp --dport 22 -j ACCEPT#把该网段的22端口数据包放行——>这里的IP段根据自己的IP段来做实验$ipt -A INPUT -p tcp --dport 80 -j ACCEPT#把80端口数据包放行$ipt -A INPUT -p tcp --dport 21 -j ACCEPT#把21端口数据包放行
RELATED状态,这是一个边缘状态
比如:客户端和服务端相互了通信,建立完连接之后,还会有一些额外的链接出来,这时候状态就变成了RELATED(若仅仅只有ESTABLISHED,而没有RELATED,很有可能导致其他的通信被禁掉,因为默认策略是INPUT DROP)
ESTABLISHED状态, 保持连接
有时,在没有增加-m --state这条规则时,导致增加了80或21端口,但是不能正常通信,加这条规则的目的是为了让通信能够正常进行
执行脚本:
[[email protected] ~]# sh /usr/local/sbin/iptables.sh
查看结果:
Chain INPUT (policy DROP 21 packets, 1638 bytes)
pkts bytes target prot opt in out source destination
48 3224 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
0 0 ACCEPT tcp -- * * 192.168.88.0/24 0.0.0.0/0 tcp dpt:22
0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:21
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 31 packets, 2732 bytes)
pkts bytes target prot opt in out source destination
使用脚本执行的好处:因为这里有INPUT DROP,肯定要把你的远程连接给断开,所以需要执行脚本把你的命令批量执行,一次性添加所有规则。
icmp示例
iptables -I INPUT -p icmp --icmp-type 8 -j DROP 这个规则会产生一个效果,让你ping外面的机器还可以ping通,但是ping本机的时候,就不会通了
[[email protected] ~]#iptables -I INPUT -p icmp --icmp-type 8 -j DROP
添加规则后,会发现可ping通外面的网络,但自己的虚拟机和物理机则无法连接
[[email protected] ~]# ping www.baidu.com
PING www.a.shifen.com (111.13.100.92) 56(84) bytes of data.
64 bytes from 111.13.100.92 (111.13.100.92): icmp_seq=1 ttl=128 time=24.6 ms
64 bytes from 111.13.100.92 (111.13.100.92): icmp_seq=2 ttl=128 time=24.0 ms
64 bytes from 111.13.100.92 (111.13.100.92): icmp_seq=3 ttl=128 time=23.0 ms
64 bytes from 111.13.100.92 (111.13.100.92): icmp_seq=4 ttl=128 time=24.5 ms
^C
--- www.a.shifen.com ping statistics ---
5 packets transmitted, 4 received, 20% packet loss, time 8094ms
rtt min/avg/max/mdev = 23.062/24.063/24.648/0.623 ms
这时用自己的物理机去ping虚拟机,会发现无法连接
Microsoft Windows [版本 10.0.10586]
(c) 2015 Microsoft Corporation。保留所有权利。
C:\Users\LEM>ping 192.168.88.5
正在 Ping 192.168.88.5 具有 32 字节的数据:
请求超时。
请求超时。
请求超时。
请求超时。
192.168.88.5 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 0,丢失 = 4 (100% 丢失),
这时,再来删除这条命令
[[email protected] ~]# iptables -D INPUT -p icmp --icmp-type 8 -j DROP
service iptables restart 重启iptables服务
[[email protected] ~]# service iptables restart
Redirecting to /bin/systemctl restart iptables.service
这里物理机去ping虚拟机IP,会发现再次ping通
C:\Users\LEM>ping 192.168.88.5 -t
正在 Ping 192.168.88.5 具有 32 字节的数据:
来自 192.168.88.5 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.88.5 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.88.5 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.88.5 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.88.5 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.88.5 的回复: 字节=32 时间<1ms TTL=64
192.168.88.5 的 Ping 统计信息:
数据包: 已发送 = 6,已接收 = 6,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 0ms,最长 = 0ms,平均 = 0ms
注:这里的物理机和虚拟机不通,并不指不能连接,这里仅仅是做了一个禁ping而已(主机可以ping通外网的,但其他人无法ping通主机)
10.16/10.17/10.18 iptables nat表应用
案例环境:
A机器两块网卡ens33(192.168.88.5)、ens37(192.168.100.1),ens33可以上外网,ens37仅仅是内部网络,B机器只有ens37(192.168.100.100),和A机器ens37可以通信互联。
• 需求1:可以让B机器连接外网(代理上网)
• 需求2:C机器只能和A通信,让C机器可以直接连通B机器的22端口(端口映射)
需求1:
1.根据上述需求,首先需要准备两台符合需求的虚拟机
第一台虚拟机原有一块可以上网的网卡(ens33),需要新增一块网卡(ens37),并以虚拟交换机的形式连接(LAN区段)
没有LAN区段则需要手动添加并选择添加完成的LAN区段
同理,第二台虚拟机也在网卡ens37上做相同配置(如果第二太虚拟机由第一台克隆而来,可以将无关网卡禁用掉)
用命令行修改第一台虚拟机的IP(一次性有效,重启后恢复默认值,永久修改IP可将ens33的配置文件拷贝一份,作为ens37的网卡配置文件,进行相应的修改)
ifconfig 网卡名称 IP地址/子网掩码
[[email protected] ~]# ifconfig ens37 192.168.100.1/24
[[email protected] ~]# ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.88.5 netmask 255.255.255.0 broadcast 192.168.88.255
inet6 fe80::531b:14ea:3a75:f113 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:58:d2:aa txqueuelen 1000 (Ethernet)
RX packets 375 bytes 35033 (34.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 351 bytes 39083 (38.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.100.1 netmask 255.255.255.0 broadcast 192.168.100.255
inet6 fe80::97b3:ed:1e9b:98c8 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:58:d2:b4 txqueuelen 1000 (Ethernet)
RX packets 136 bytes 46512 (45.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 352 bytes 61248 (59.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
同理修改第二台虚拟机网卡的IP(因为没有IP,不能远程连接,因此只能在虚拟机里进行设置)
配置完成后可以互相ping通
[[email protected] ~]# ping 192.168.100.100
PING 192.168.100.100 (192.168.100.100) 56(84) bytes of data.
64 bytes from 192.168.100.100: icmp_seq=1 ttl=64 time=0.889 ms
64 bytes from 192.168.100.100: icmp_seq=2 ttl=64 time=0.367 ms
64 bytes from 192.168.100.100: icmp_seq=3 ttl=64 time=0.229 ms
64 bytes from 192.168.100.100: icmp_seq=4 ttl=64 time=0.359 ms
64 bytes from 192.168.100.100: icmp_seq=5 ttl=64 time=0.260 ms
64 bytes from 192.168.100.100: icmp_seq=6 ttl=64 time=0.258 ms
64 bytes from 192.168.100.100: icmp_seq=7 ttl=64 time=0.236 ms
^C
--- 192.168.100.100 ping statistics ---
7 packets transmitted, 7 received, 0% packet loss, time 6006ms
rtt min/avg/max/mdev = 0.229/0.371/0.889/0.218 ms
2.完成准备工作后,打开第一台虚拟机的端口转发
[[email protected] ~]# cat /proc/sys/net/ipv4/ip_forward //默认值为0,未打开0
[[email protected] ~]# echo "1" > !$ //将值修改为1,打开端口转发(!$代表引用上一条命令的变量)
echo "1" > /proc/sys/net/ipv4/ip_forward
[[email protected] ~]# cat /proc/sys/net/ipv4/ip_forward //已开启端口转发1
3.为192.168.100.0网段做地址转换
[[email protected] ~]# iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o ens33 -j MASQUERADE
[[email protected] ~]# iptables -t nat -nvL
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
187 14174 POSTROUTING_direct all -- * * 0.0.0.0/0 0.0.0.0/0
187 14174 POSTROUTING_ZONES_SOURCE all -- * * 0.0.0.0/0 0.0.0.0/0
187 14174 POSTROUTING_ZONES all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 MASQUERADE all -- * ens33 192.168.100.0/24 0.0.0.0/0
MASQUERADE:这个设定值就是『IP伪装成为封包出去(-o)的那块装置上的IP』不管现在外网网卡的出口获得了怎样的动态ip,MASQUERADE会自动读取外网网卡现在的ip地址然后做SNAT出去,这样就实现了很好的动态SNAT地址转换。
4.为第二台虚拟机设置网关192.168.100.1(可以理解为,不同网段间通信,需要通过网关转发)
5.验证网络连通性(为第二台虚拟机配置好DNS,即可正常访问公网域名)
需求2:
1.根据上述需求,首先需要准备两台符合需求的虚拟机(需求1已完成)
2.完成准备工作后,打开第一台虚拟机的端口转发(需求1已完成)
3.在第一台虚拟机设置目的地址转换(端口映射)
iptables -t nat -A PREROUTING -d 192.168.88.5 -p tcp --dport 1122 -j DNAT --to 192.168.100.100:22
//将192.168.88.5的1122端口映射到192.168.100.100的22端口
4.在第一台虚拟机设置源地址转换(代理上网,可理解为回包)
iptables -t nat -A POSTROUTING -s 192.168.100.100 -j SNAT --to 192.168.88.5
5.查看nat表规则
[[email protected] ~]# iptables -t nat -nvLChain PREROUTING (policy ACCEPT 2 packets, 156 bytes)
pkts bytes target prot opt in out source destination
422 69368 PREROUTING_direct all -- * * 0.0.0.0/0 0.0.0.0/0
422 69368 PREROUTING_ZONES_SOURCE all -- * * 0.0.0.0/0 0.0.0.0/0
422 69368 PREROUTING_ZONES all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 DNAT tcp -- * * 0.0.0.0/0 192.168.88.5 tcp dpt:1122 to:192.168.100.100:22
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
204 15474 POSTROUTING_direct all -- * * 0.0.0.0/0 0.0.0.0/0
204 15474 POSTROUTING_ZONES_SOURCE all -- * * 0.0.0.0/0 0.0.0.0/0
204 15474 POSTROUTING_ZONES all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 SNAT all -- * * 192.168.100.100 0.0.0.0/0 to:192.168.88.5
6.为第二台虚拟机设置网关192.168.100.1(需求1已完成)
7.测试需求结果