10.12 firewalld和netfilter
1、selinux
安全增强式linux。是一个Linux内核的安全模组,其提供了访问控制安全策略机制。selinux 主要由美国国家安全局开发。2.6 及以上版本的 Linux 内核都已经集成了 selinux 模块。selinux配置比较麻烦,一般我们都是把它关闭(disabled)。
我们可以用 getenforce 来查看selinux当前的状态:enforcing、permissive、disabled。
还可以用 setenforce 来设置临时状态。
更改 /etc/selinux/config 配置文件可以永久保存设置的selinux状态。
2、netfilter和firewalld
在centos6及以下版本linux上用的防火墙是netfilter,其配置工具为iptables。centos7则用的是firewalld防火墙,其配置工具也是iptables。
在centos7中firewalld默认是开启的,我们先关闭firewalld再配置netfilter。
命令:systemctl disable firewalld(系统启动时关闭) 这里我们需要root权限。
[liang@liang-00 ~]$ systemctl disable firewalld ==== AUTHENTICATING FOR org.freedesktop.systemd1.reload-daemon === Authentication is required to reload the systemd state. Authenticating as: root Password: ==== AUTHENTICATION COMPLETE === [liang@liang-00 ~]$
命令:systemctl stop firewalld 停止当前开启的firewalld
之后我们切换到root用户下来操作。
netfilter和iptables。
首先安装 iptables。
yum install -y iptables-services
安装完之后设置开机启动iptables和开启iptables。
[root@liang-00 ~]# systemctl enable iptables Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service. [root@liang-00 ~]# systemctl start iptables [root@liang-00 ~]#
用 iptables -nvL 查看默认规则。
[root@liang-00 ~]# iptables -nvL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 356 30056 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 1 40 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22 1 229 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 325 packets, 80984 bytes) pkts bytes target prot opt in out source destination [root@liang-00 ~]#
总结:在CentOS7中打开netfilter的方法:
firewalld-->关闭-->systemctl disable firewalld(关闭开机启动);systemctl stop firewalld(关闭firewalld)
netfilter-->开启-->安装iptables-services包-->systemctl enable iptables(开机启动);systemctl start iptables(开启)
10.13 netfilter5表5链介绍
iptables中有5个表(centos6以下版本只有四个表):
- filter 用于过滤包
- nat 用于网络地址转换
- mangle 用于给数据包做标记
- raw 用于实现不追踪某些数据包
- security(只存在于CentOS7中) 用于强制网路控制(MAC)网路规则
iptables中的链
- INPUT链:处理输入数据包。
- OUTPUT链:处理输出数据包。
- FORWARD链:处理转发数据包。
- PREROUTING链:用于路由选择前更改数据包。
- POSTROUTING链:用于路由选择后更改数据包。
iptables简单的数据传输流程如下图:来源 https://www.cnblogs.com/metoy/p/4320813.html
- 当一个数据包进入网卡时,它首先进入PREROUTING链,内核根据数据包目的IP判断是否需要转送出去。
- 如果数据包就是进入本机的,它就会沿着图向下移动,到达INPUT链。数据包到了INPUT链后,任何进程都会收到它。本机上运行的程序可以发送数据包,这些数据包会经过 OUTPUT链,然后到达POSTROUTING链输出。
- 如果数据包是要转发出去的,且内核允许转发,数据包就会如图所示向右移动,经过FORWARD链,然后到达POSTROUTING链输出。
10.14 iptables语法
iptables 选项:
- -t<表>:指定要操纵的表;
- -A:向规则链中添加条目;
- -D:从规则链中删除条目;
- -i:向规则链中插入条目;
- -R:替换规则链中的条目;
- -L:显示规则链中已有的条目;
- -F:清楚规则链中已有的条目;
- -Z:清空规则链中的数据包计算器和字节计数器;
- -N:创建新的用户自定义规则链;
- -P:定义规则链中的默认目标;
- -h:显示帮助信息;
- -p:指定要匹配的数据包协议类型;
- -s:指定要匹配的数据包源ip地址;
- -j<目标>:指定要跳转的目标;
- -i<网络接口>:指定数据包进入本机的网络接口;
- -o<网络接口>:指定数据包要离开本机所使用的网络接口。
用iptables -nvL 查看规则(默认查看的是filter表)
[root@liang-00 ~]# iptables -nvL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 728 58744 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 1 40 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22 139 13258 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 682 packets, 118K bytes) pkts bytes target prot opt in out source destination [root@liang-00 ~]#
iptables 默认规则保存在 /etc/sysconfig/iptables 文件中,在重启iptables时会加载此规则。
[root@liang-00 ~]# cat /etc/sysconfig/iptables # sample configuration for iptables service # you can edit this manually or use system-config-firewall # please do not ask us to add additional ports/services to this default configuration *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] -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@liang-00 ~]#
iptables -F 清空当前规则(配置文件不会清空)
[root@liang-00 ~]# iptables -F [root@liang-00 ~]# iptables -nvL Chain INPUT (policy ACCEPT 19 packets, 1332 bytes) pkts bytes target prot opt in out source destination Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 12 packets, 1120 bytes) pkts bytes target prot opt in out source destination [root@liang-00 ~]#
当我们要保存 iptables 规则时可以用:service iptables save 保存自己的iptables规则。
重启 iptables :service iptables restart。
iptables -Z 清空计数器,每个规则都有两个计数器:
- pkts:此规则所匹配的所有报文的个数;
- bytes:此规则所匹配的所有报文的大小之和。
[root@liang-00 ~]# iptables -Z;iptables -nvL Chain INPUT (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 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 0 packets, 0 bytes) pkts bytes target prot opt in out source destination [root@liang-00 ~]#
在默认的 filter 表中追加一条规则:iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP
其中:-A 追加;INPUT input链;-s 192.168.188.1 来源ip;-p tcp 协议;--sport 1234 来源端口;
-d 192.168.188.128 目的ip;--dport 80 目的端口;-j DROP/REJECT 目标动作或跳转(DROP表示直接丢掉,REJECT看过后在丢掉)
在filter表中插入一条规则:iptables -I INPUT -p tcp --dport 80 -j DROP
-I 表示插入(最前边);当使用 --sport/--dport 时前面必须有-p 协议。
当数据进入INPUT链中时,会从上到下逐条匹配,一旦匹配就会执行。比如,在下面filter表中一旦匹配第一条就会直接DROP掉,就没有之后。
[root@liang-00 ~]# iptables -I INPUT -p tcp --dport 80 -j DROP
[root@liang-00 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
695 53480 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
13 1165 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
0 0 DROP tcp -- * * 192.168.188.1 192.168.188.128 tcp spt:1234 dpt:80
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 9 packets, 848 bytes)
pkts bytes target prot opt in out source destination
[root@liang-00 ~]#
删除规则:-D 选项。
[root@liang-00 ~]# iptables -D INPUT -p tcp --dport 80 -j DROP [root@liang-00 ~]# iptables -D INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP [root@liang-00 ~]# iptables -nvL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 1002 78788 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 17 1628 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 20 packets, 1856 bytes) pkts bytes target prot opt in out source destination [root@liang-00 ~]#
给每条规则前加上编号:iptables -nvL --line-numbers。
加上编号后我们就可以根据规则编号来删除指定规则。
[root@liang-00 ~]# iptables -nvL --line-numbers
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
2 1055 82508 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
3 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0
4 0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
5 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
6 17 1628 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
7 0 0 DROP tcp -- * * 192.168.188.1 192.168.188.128 tcp spt:1234 dpt:80
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT 14 packets, 1480 bytes)
num pkts bytes target prot opt in out source destination
[root@liang-00 ~]# iptables -D INPUT 7
用 -i 来给指定网卡添加规则。
-P:定义规则链中的默认目标;iptables表中每一个链都有指定的规则,如果不设置的话它会按默认规则来对数据包执行,一般默认规则为:ACCEPT 。
iptables -P OUTPUT DROP(此规则在远程连接中千万不能执行,执行会马上断开远程连接)
10.15 iptables filter表小案例
编写shell脚本对特定端口、IP段、协议、对filter表规则制定。
1)vim /usr/local/sbin/iptables.sh 编辑iptables.sh脚本,内容:
以下为对filter表的规则设置。
ipt=“/usr/sbin/iptables” #此条命令是为了方便以下使用 /usr/sbin/iptables 时变得简洁。使用绝对路径是为了避免出现错误。
$ipt -F #清空INPUT表
$ipt -P INPUT DROP #对INPUT默认策略改为:DROP
$ipt -P OUTPUT ACCEPT #对OUTPUT默认策略改为:ACCEPT
$ipt -P FORWARD ACCEPT #对FORWARD默认策略改为:ACCEPT
$ipt -A INPUT -m state --state RELATED,ESTABLISHD -j ACCEPT #在INPUT链中:放行已经建立连接的相关连接。
$ipt -A INPUT -s 192.168.37.0/24 -p tcp --dport 22 -j ACCEPT #在INPUT链中:放行目的端口为22的,192.168.37.0/24 网段的tcp。
$ipt -A INPUT -p tcp --dport 80 -j ACCEPT #在INPUT链中:放行目的端口为80的tcp。
$ipt -A INPUT -p tcp --dport 21 -j ACCEPT #在INPUT链中:放行目的端口为22的tcp
2)执行脚本 /usr/local/sbin/iptables.sh
[root@liang-00 local]# sh /usr/local/sbin/iptables.sh [root@liang-00 local]# iptables -nvL Chain INPUT (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 20 1424 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 0 0 ACCEPT tcp -- * * 192.168.37.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 13 packets, 1212 bytes) pkts bytes target prot opt in out source destination
禁止外网ping本机
iptables -I INPUT -p icmp --icmp-type 8 -j DROP
这样就可以实现其它主机无法ping通本机,但本机可以正常ping通外机。
[root@liang-00 local]# iptables -I INPUT -p icmp --icmp-type 8 -j DROP [root@liang-00 local]# iptables -nvL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 DROP icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 8 35 2472 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 15 packets, 1396 bytes) pkts bytes target prot opt in out source destination [root@liang-00 local]#
10.16 iptables nat表应用
nat表应用
A机器两块网卡ens33(192.168.37.200)、ens37(192.168.100.1),ens33可以上外网,ens37仅仅是内部网络,B机器只有ens37(192.168.100.100),和A机器ens37可以通信互联。
需求1:可以让B机器连接外网
步骤:
1)在A机器上添加一块网卡,名字为ens37,连接到LAN区段自定义的网路交换机上。
B机器上的设置和A机器一样,添加网卡,连接同一个网路交换机。
2)将A机器 ens37 网卡网络设置为:192.168.100.1/24;B机器网络设置为:192.168.100.100/24。之后在A机器上 ping B机器的地址可以ping通,证明两台机器本地私网联通良好。
这时B机器是无法ping通A机器的ens33网卡的。
外网也无法ping通B机器。
3)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
[root@liang-00 ~]# echo "1" > /proc/sys/net/ipv4/ip_forward
[root@liang-00 ~]# iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o ens33 -j MASQUERADE
[root@liang-00 ~]# iptables -nvL -t nat
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
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
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 MASQUERADE all -- * ens33 192.168.100.0/24 0.0.0.0/0
B机器上设置网关为:192.168.100.1
这时B机器就可以ping通A级器的192.168.37.200地址了。设置DNS可以ping通外网了。
需求2:C机器只能和A通信,让C机器可以直接连通B机器的22端口
步骤:
A上打开路由转发echo "1">/ proc/sys/net/ipv4/ip_forward
A上执行iptables -t nat -A PREROUTING -d 192.168.37.200 -p tcp --dport 1122 -j DNAT --to 192.168.100.200:22
A上执行iptables -t nat -A POSTROUTING -s 192.168.100.200 -j SNAT --to 192.168.37.200
B上设置网关为192.168.100.1
临时清空filter表规则