10.4 Linux的防火墙
SELinux
SELinux是linux系统特有的安全机制,这种机制限制较多,配置也比较繁琐,所以一般把SELinux关闭。
查看selinux的状态有两种方法:
- getenforce 命令是单词get(获取)和enforce(执行)连写,可查看selinux状态
[root@localhost ~]# getenforce
Enforcing
- /usr/sbin/sestatus
[root@localhost ~]# /usr/sbin/sestatus
SELinux status: enabled
SELinuxfs mount: /sys/fs/selinux
SELinux root directory: /etc/selinux
Loaded policy name: targeted
Current mode: enforcing
Mode from config file: enforcing
Policy MLS status: enabled
Policy deny_unknown status: allowed
Max kernel policy version: 31
SELinux status:selinux防火墙的状态,enabled表示启用selinux防火墙
Current mode:表示selinux防火墙当前的安全策略,enforcing 表示强
关闭SELinux:
- 临时关闭:
setenforce 0 :用于关闭selinux防火墙,但重启后失效(setenforce 1 用于打开selinux,重启失效)
[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce
Permissive
- 永久关闭:
- 打开selinux的配置文件:
[root@localhost ~]# vim /etc/selinux/config
- 修改selinux的配置文件:
将SELINUX=enforcing改为SELINUX=disabled,保存后退出
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=enforcing
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
- 此时获取当前selinux防火墙的安全策略仍为Enforcing,配置文件并未生效
[root@localhost ~]# getenforce
Enforcing
- 需要重启:
[root@localhost ~]# reboot
- 验证:
[root@localhost ~]# getenforce
Disabled
netfilter
在CentOS7之前的CentOS版本的防火墙为netfilter,CentOS7的防火墙为firewalld 。
iptables是针对防火墙的一个工具,iptables是用来设置、维护和检查Linux内核的IP包过滤规则的。在了解netfilter之前,需要先把firewalld关闭,然后开启之前版本的iptables。
# systemctl stop firewalld //关闭firewalld服务
# systemctl disable firewalld //禁止firewalld服务开机启动
# yum install -y iptables-services //安装iptables-services
# systemctl enable iptables //让iptables开机启动
# systemctl start iptables //启动iptables服务
CentOS上默认设有iptables规则,一般建议先清除规则,然后把清除后的规则保存一下
[root@localhost ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
190 16296 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
2 148 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
1 52 INPUT_direct all -- * * 0.0.0.0/0 0.0.0.0/0
1 52 INPUT_ZONES_SOURCE all -- * * 0.0.0.0/0 0.0.0.0/0
1 52 INPUT_ZONES all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate INVALID
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 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
0 0 FORWARD_direct all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 FORWARD_IN_ZONES_SOURCE all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 FORWARD_IN_ZONES all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 FORWARD_OUT_ZONES_SOURCE all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 FORWARD_OUT_ZONES all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate INVALID
0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT 192 packets, 17907 bytes)
pkts bytes target prot opt in out source destination
192 17907 OUTPUT_direct all -- * * 0.0.0.0/0 0.0.0.0/0
上面 -nvL选项表示查看规则,其中 -n表示不针对IP反解析主机名,-L表示列出,-v表示列出的信息更加详细
[root@localhost ~]# iptables -F;service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ 确定 ]
-F选项表示清除当前所有规则,但清除只是暂时的,重启iptables服务后还会加载已经保存的规则,所以使用 service iptables save 保存一下规则。从上面可以看到,防火墙规则保存在/etc/sysconfig/iptables中
netfilter的5个表:
1.filter表
filter表 是iptables的默认表,用于过滤包,如果你没有自定义表,那么就默认使用filter表,它具有3个内建链:
- INPUT链 – 处理来自外部的数据
- OUTPUT链 – 处理向外发送的数据
- FORWARD链 – 将数据转发到本机的其他网卡设备上
2.nat表
nat表 用于网络地址转换,它也有3个内建链:
- PREROUTING链 – 处理刚到达本机并在路由转发前的数据包。它会转换数据包中的目标IP地址(destination ip address),通常用于DNAT(destination NAT)
- OUTPUT链 – 改变本机产生的包的目的地址
- POSTROUTING链 – 处理即将离开本机的数据包。它会转换数据包中的源IP地址(source ip address),通常用于SNAT(source NAT)
3.mangle表
mangle表 用于指定如何处理数据包,它能改变TCP头中的QoS位,Mangle表具有5个内建链:
- PREROUTING链
- INPUT链
- FORWARD链
- OUTPUT链
- POSTROUTING链
4.raw表
raw表 可以实现不追踪某些数据包,默认系统的数据包都会被追踪,用于处理异常,它具有2个内建链:
- PREROUTING链
- OUTPUT链
5.security表
security表 用于强制访问控制(MAC)的网络规则,在CentOS6中没有该表
netfilter的5个链:
- PREROUTING:数据包进入路由器之前
- INPUT:通过路由表后,目的地为本机
- FORWARD:通过路由表后,目的地不为本机
- OUTPUT:由本级产生,向外转发
- POSTROUTING:发送到网卡接口之前
iptables基本语法:
- 查看规则以及清除规则
[root@localhost ~]# iptables -t nat -nvL
Chain PREROUTING (policy ACCEPT 1 packets, 52 bytes)
pkts bytes target prot opt in out source destination
1 52 PREROUTING_direct all -- * * 0.0.0.0/0 0.0.0.0/0
1 52 PREROUTING_ZONES_SOURCE all -- * * 0.0.0.0/0 0.0.0.0/0
1 52 PREROUTING_ZONES all -- * * 0.0.0.0/0 0.0.0.0/0
Chain INPUT (policy ACCEPT 1 packets, 52 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 88 packets, 5914 bytes)
pkts bytes target prot opt in out source destination
88 5914 OUTPUT_direct all -- * * 0.0.0.0/0 0.0.0.0/0
Chain POSTROUTING (policy ACCEPT 88 packets, 5914 bytes)
pkts bytes target prot opt in out source destination
88 5914 POSTROUTING_direct all -- * * 0.0.0.0/0 0.0.0.0/0
88 5914 POSTROUTING_ZONES_SOURCE all -- * * 0.0.0.0/0 0.0.0.0/0
88 5914 POSTROUTING_ZONES all -- * * 0.0.0.0/0 0.0.0.0/0
-t 选项后面跟表名,不加-t 选项默认指 filter表
[root@localhost ~]# iptables -F
[root@localhost ~]# iptables -Z
-F选项表示删除所有规则,-Z选项把包以及流量计数器置零
2. 增加/删除/插入一条规则
[root@localhost ~]# iptables -A INPUT -s 192.168.200.1 -p tcp --sport 1234 -d 192.168.200.200 --dport 80 -j DROP
- -A/-D/-I:表示增加/删除/插入一条规则(-I可以插入到最前面,-A只能增加到最后面)
- -p 指定协议,可以是tcp、udp、icmp或all
- –dport 必须和-p一起使用,表示指定目标端口
- –sport 必须和-p一起使用,表示指定源端口
- -s 指定源ip(可以是一个ip段)
- -d 指定目的ip(可以是一个ip段)
- -j 后面跟动作,其中ACCEPT表示允许包,DROP表示丢掉包,REJECT表示拒绝包
- -i 指定接收进来数据包的网卡
- -o 指定发送出去数据包的网卡
- –line-number 显示规则编号(可以根据编号来删除规则)
- -P 默认规则,默认为ACCEPT所有包(可以更改为DROP,表示丢弃所有包,不过这是危险操作,尽量不要尝试)
举例:
[root@localhost ~]# iptables -I INPUT -s 1.1.1.1 -j DROP
表示插入一条规则,把来自1.1.1.1的数据包全部丢掉
[root@localhost ~]# iptables -D INPUT -s 1.1.1.1 -j DROP
表示删除把来自1.1.1.1的数据包全部丢掉这条规则(删除一条规则时,必须和插入或者增加的规则一致,除了-A、-D、-I的区别)
[root@localhost ~]# iptables -I INPUT -s 2.2.2.2 -p tcp --dport 80 -j DROP
表示把来自2.2.2.2并且是TCP协议到本机80端口的数据包丢掉(–dport/–sport必须和-p选项一起使用,否则会出错)
[root@localhost ~]# iptables -I OUTPUT -p tcp --dport 22 -d 10.0.1.14 -j DROP
表示把发送到10.0.1.14的22端口的数据包丢掉
[root@localhost ~]# iptables -A INPUT -s 192.168.1.0/24 -i eth0 -j ACCEPT
[root@localhost ~]# iptables -nvL |grep '192.168.1.0'
0 0 ACCEPT all -- eth0 * 192.168.1.0/24 0.0.0.0/0
表示把来自192.168.1.0/24这个网段且作用在eth0上的包放行
[root@localhost ~]# iptables -nvL --line-numbers
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 1594 115K ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
2 2 148 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
3 25 1924 INPUT_direct all -- * * 0.0.0.0/0 0.0.0.0/0
4 25 1924 INPUT_ZONES_SOURCE all -- * * 0.0.0.0/0 0.0.0.0/0
5 25 1924 INPUT_ZONES all -- * * 0.0.0.0/0 0.0.0.0/0
6 0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate INVALID
7 24 1872 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
8 0 0 ACCEPT all -- eth0 * 192.168.1.0/24 0.0.0.0/0
表示查看iptables规则,显示规则编号
[root@localhost ~]# iptables -D INPUT 6
[root@localhost ~]# iptables -nvL --line-numbers
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 1792 128K ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
2 2 148 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
3 25 1924 INPUT_direct all -- * * 0.0.0.0/0 0.0.0.0/0
4 25 1924 INPUT_ZONES_SOURCE all -- * * 0.0.0.0/0 0.0.0.0/0
5 25 1924 INPUT_ZONES all -- * * 0.0.0.0/0 0.0.0.0/0
6 24 1872 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
7 0 0 ACCEPT all -- eth0 * 192.168.1.0/24 0.0.0.0/0
-D 后面依次跟链名、规则num,表示删除对应num的规则
[root@localhost ~]# iptables -P INPUT ACCEPT
-P 表示预设策略,后面跟链名,策略内容为ACCEPT,或者是DROP(如果是远程服务器时,切勿执行DROP操作,否则远程连接就会被断开)
[root@localhost ~]# iptables -I INPUT -m iprange -src 192.168.100.0-192.168.188.255 -j DROP
-m 后面跟模块名字,iprange是一个模块名字,用来支持一个网段
–src-range 指定源ip范围
–dst-range 指定目标ip范围
[root@localhost ~]# iptables -I INPUT -m state --state NEW,RELATED,ESTABLISHED -j ACCEPT
–state 指定要匹配包的的状态
在iptables上有四种状态:NEW、ESTABLISHED、INVALID、RELATID
- NEW:conntrack模块看到的某一连接的第一个包
- ESTABLISHED:只要发送并接收到应答,连接就是EATABLISHED状态
- RELATID:当一个连接和某个已处于EATABLISHED状态的连接有关系时,就可以认为是RELATID状态
- INVALID:INVALID意味着这个包没有已知的流或连接与之关联,也可能是它包含的数据或包头有问题
[root@localhost ~]# iptables -I INPUT -p icmp --icmp-type 8 -j DROP
–icmp-type需要跟-p icmp一起使用,后面指定类型编号,8表示能在本机ping通其他机器,而其他机器无法ping通本机
nat表的应用
A机器两块网卡ens33(192.168.20.128)、ens37(192.168.100.1),ens33可以上外网,ens37仅仅是内部网络,B机器只有ens34(192.168.100.100),和A机器ens37可以通信互联。
A机器配置:
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.20.128 netmask 255.255.255.0 broadcast 192.168.100.255
inet6 fe80::b76:caa8:3d7c:71bc prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:e4:fc:a5 txqueuelen 1000 (Ethernet)
RX packets 313 bytes 28159 (27.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 264 bytes 33270 (32.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.20.130 netmask 255.255.255.0 broadcast 192.168.100.255
ether 00:0c:29:e4:fc:a5 txqueuelen 1000 (Ethernet)
ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.100.1 netmask 255.255.255.0 broadcast 192.168.133.255
ether 00:0c:29:e4:fc:af txqueuelen 1000 (Ethernet)
RX packets 48 bytes 14388 (14.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 58 bytes 9360 (9.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
B机器配置:
ens34: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.100.100 netmask 255.255.255.0 broadcast 192.168.133.255
ether 00:0c:29:e4:fc:af txqueuelen 1000 (Ethernet)
RX packets 48 bytes 14388 (14.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 58 bytes 9360 (9.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
- 需求:可以让B机器连接外网
A机器上打开路由转发 echo “1”>/proc/sys/net/ipv4/ip_forward
A上执行 iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o ens33 -j MASQUERADE
B上设置网关为192.168.100.1
[root@localhost ~]# cat /proc/sys/net/ipv4/ip_forward
0
[root@localhost ~]# echo "1"> /proc/sys/net/ipv4/ip_forward
[root@localhost ~]# cat /proc/sys/net/ipv4/ip_forward
1
表示打开路由转发功能
[root@localhost ~]# iptables -t nat -A POSTROUTING -s 192.168.133.0/24 -o ens33 -j MASQUERADE
[root@localhost ~]# iptables -t nat -nvL
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
42 13256 PREROUTING_direct all -- * * 0.0.0.0/0 0.0.0.0/0
42 13256 PREROUTING_ZONES_SOURCE all -- * * 0.0.0.0/0 0.0.0.0/0
42 13256 PREROUTING_ZONES all -- * * 0.0.0.0/0 0.0.0.0/0
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
104 7831 OUTPUT_direct all -- * * 0.0.0.0/0 0.0.0.0/0
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
104 7831 POSTROUTING_direct all -- * * 0.0.0.0/0 0.0.0.0/0
104 7831 POSTROUTING_ZONES_SOURCE all -- * * 0.0.0.0/0 0.0.0.0/0
104 7831 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
iptables对nat表做IP转发操作,-o 指定发送出去数据包的网卡,这里就是ens33,MASQUERADE表示伪装
[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.20.2 0.0.0.0 UG 100 0 0 ens33
192.168.20.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.100.0 0.0.0.0 255.255.255.0 U 0 0 0 ens34
[root@localhost ~]# route add default gw 198.168.100.1
[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.100.1 0.0.0.0 UG 0 0 0 ens34
0.0.0.0 192.168.20.2 0.0.0.0 UG 100 0 0 ens33
192.168.20.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.100.0 0.0.0.0 255.255.255.0 U 0 0 0 ens34
对B机器设置网关,route -n 查看网关 ,route add default gw 设置默认网关
[root@localhost ~]# ping 192.168.100.1
PING 192.168.100.2 (192.168.100.2) 56(84) bytes of data.
64 bytes from 192.168.100.2: icmp_seq=1 ttl=128 time=0.178 ms
64 bytes from 192.168.100.2: icmp_seq=2 ttl=128 time=0.284 ms
64 bytes from 192.168.100.2: icmp_seq=3 ttl=128 time=0.197 ms
64 bytes from 192.168.100.2: icmp_seq=4 ttl=128 time=0.216 ms
64 bytes from 192.168.100.2: icmp_seq=5 ttl=128 time=0.227 ms
64 bytes from 192.168.100.2: icmp_seq=6 ttl=128 time=0.166 ms
[root@localhost~]#vi /etc/resolv.conf
# Generated by NetworkManager
nameserver 119.29.29.29
给ens34设置DNS:119.29.29.29
[root@localhost~]#ping www.baidu.com
PING www.a.shifen.com (14.215.177.38) 56(84) bytes of data.
64 bytes from 14.215.177.38 (115.239.211.112): icmp_seq=1 ttl=128 time=36.10 ms
64 bytes from 14.215.177.38 (115.239.211.112): icmp_seq=2 ttl=128 time=34.81 ms
64 bytes from 14.215.177.38 (115.239.211.112): icmp_seq=3 ttl=128 time=34.07 ms
64 bytes from 14.215.177.38 (115.239.211.112): icmp_seq=4 ttl=128 time=35.81 ms
64 bytes from 14.215.177.38 (115.239.211.112): icmp_seq=5 ttl=128 time=39.24 ms
64 bytes from 14.215.177.38 (115.239.211.112): icmp_seq=6 ttl=128 time=34.4 ms
64 bytes from 14.215.177.38 (115.239.211.112): icmp_seq=7 ttl=128 time=34.27 ms
64 bytes from 14.215.177.38 (115.239.211.112): icmp_seq=8 ttl=128 time=35.25 ms
64 bytes from 14.215.177.38 (115.239.211.112): icmp_seq=9 ttl=128 time=35.89 ms
^C
--- www.a.shifen.com ping statistics ---
9 packets transmitted, 9 received, 0% packet loss, time 14821ms
rtt min/avg/max/mdev = 34.07/36.770/34.458/33.038 ms
B可以ping通 www.baidu.com,实现B通过nat借助A来上网,需求满足