SELINUX
enforcing - SELinux security policy is enforced. 强制,每个受限进程都必须受限
permissive - SELinux prints warnings instead of enforcing. 每个受限进程违规操作不会被禁止,但是会被记录到审计日志
disabled - No SELinux policy is loaded.关闭
相关命令
获取selinux当前状态
[root@zys ~]# getenforce
Enforcing
[root@zys ~]# setenforce
usage: setenforce [ Enforcing | Permissive | 1 | 0 ] #设置linux状态,0代表permissive,1代表enforce
通过上面方法设置的linux状态在重启后无效
配置文件:/etc/selinux/config
SELINUX=enforcing | permissive | disabled
在配置文件中修改selinux状态可以永久生效
给文件重新打标签
[root@zys ~]# chcon --help
用法: chcon [选项]… 环境 文件…
或: chcon [选项]… [-u 用户] [-r 角色] [-l 范围] [-t 类型] 文件…
或: chcon [选项]… --reference=参考文件 文件…
Change the SELinux security context of each FILE to CONTEXT.
With --reference, change the security context of each FILE to that of RFILE.
Mandatory arguments to long options are mandatory for short options too.
–dereference affect the referent of each symbolic link (this is
the default), rather than the symbolic link itself
-u, --user=USER set user USER in the target security context #selinux用户
-r, --role=ROLE set role ROLE in the target security context #角色
-t, --type=TYPE set type TYPE in the target security context #类型
-l, --range=RANGE set range RANGE in the target security context #范围
还原文件的标签:
[root@zys ~]# restorecon --help
restorecon: invalid option – ‘-’
usage: restorecon [-iFnprRv0] [-e excludedir] pathname…
usage: restorecon [-iFnprRv0] [-e excludedir] -f filename
布尔型规则
getsebool
setsebool
getsebool: invalid option – ‘h’
usage: getsebool -a or getsebool boolean…
setsebool
Usage: setsebool [ -NPV ] boolean value | bool1=val1 bool2=val2…
防火墙的相关概念:
从逻辑上分类:
主机防火墙:为单个主机进行保护
网络防火墙:对网络入口进行防护,服务防火墙背后的本地局域网
主机(个人)防火墙和网络防火墙相辅相成滴
从物理上分类:
硬件防火墙:硬件级别实现部分防火墙功能,另一部份功能基于软件实现,性能高,成本也高
软件防火墙:应用软件处理逻辑运行于通用硬件平台之上的防火墙,性能低,成本低
IPTABLES:
iptables只是一个客户端代理的软件,将用户空间的安全设定执行到对应的安全框架中
框架:Netfilter
Netfilter:他才是防火墙真正的安全框架,位于内核空间当中
Netfilter———内核空间
iptables———用户空间的客户端代理,将用户空间的安全设定通过代理执行到Netfilter中
Netfilter+IPtables组成Linux凭条下的包过滤防火墙(免费的)
Netfilter是Linux操作系统核心层的一个数据包处理模块,IPTABLES守护进程,所以他不是一个服务,而是内核提供的功能:
1.网络地址转化
2.数据包内容的修改
3.数据包过滤
IPTABLES
IPTABLES基础:
IPtables根据他的rule进行匹配,对匹配到规则的数据包进行action:
accept(接收)、reject(拒绝)、drop(丢弃)等动作
rule存储在内核空间的信息顾虑表中,这些规则包含:SIP DIP 传输协议(TCP UDP ICMP等)和服务类型(http ftp等)
当一个主机接收到一个数据……Netfilter怎么工作
Netfilter位于内核空间中,是真正的防火墙,规则设置在input和output上
网卡驱动是在内核空间中,Netfilter也是在内核空间中,所有IPtables+Netfilter——内核空间可以在内核空间当中设置“关卡”,当用户去访问应用服务的时候,数据包通过网卡经过内核空间流入到用户空间。
A:路由前 prerouting
B:转发forward
C:路由后 postrouting
如果我们需要报文转发,不经过input链和out链
只需要:prerouting链,forward链,postrouting链
prerouting链:
做判断,目标是否为本机,如果本地,发给input链
如果不是,交由forward链
总结一下子,报文流向:
到本地的某个进程的报文:prerouting——>input
由本机转发的报文:prerouting——>forward——>postrouting
由本地响应的报文的流向:output——>postrouting
表的概念:
每个链上都有一串规则,但是有许多类似的规则,这些规则可能是:
对IP或端口进行过滤
对报文进行修改
对网络地址进行转换等等
如果把这些现实相同功能的规则放在一起,他们的集合叫做:表
iptables提供的表:
raw表:关闭nat上启用的连接追踪机制。 iptables_raw
mangle表:拆解报文,作出修改,并重新封装,iptables_mangle
nat表:网络地址转换,iptables_nat
filter表:负责过滤功能,防火墙;iptables_filter
内核模块:iptables_raw iptables_nat iptables_mangle iptables_filter
优先级的顺序:raw——>mangle——>nat——>filter
我们所有定义规则全部在这四张表之中
是不是每一种链都有以上4张表?肯定不是,每个链只能拥有某些规则,也就是某些表
总结:每个链中都存在哪些表:
prerouting: raw mangle nat
input: mangle filter nat
forward: mangle filter
output: raw mangle nat filter
postrouting: mangle nat
实际使用的过程当中,往往是以“表”为操作入口,对规则进行定义:
iptables -t raw/mangle/nat/filter
表————链
raw表:prerouting output
mangle表:prerouting input forward output postrouting
nat表:prerouting input output postrouting
filter表:input forward output
操作的时候,通过表,查询所定义的规则,以及该表的规则所用与的“链”
规则的概念
规则:根据指定的匹配条件来尝试匹配每个流经此处的数据报文,一旦匹配成功,则由后面指定的处理动作进行处理
匹配条件:
基本匹配条件:SIP DIP等
扩展匹配条件:Sport Dport等(-m 参数指定扩展模块)
扩展匹配条件也是Netfilter中的一部分,只不过是以模块的形式存在,若要使用,需要依赖的相应模块
处理动作
accept:接收
reject:拒绝
drop:丢弃
snat:源地址转换
dnat:目的地址转换
log:在/var/log/message文件中记录日志信息,然后将数据传递给下一个规则,不对数据包进行任何操作
masquerade:是snat的一种特殊形式,适用于动态的,临时汇编的IP上
redirect:在本地做端口映射
IPtables相关操作
[root@node1 ~]# iptables --help
iptables v1.4.21
Usage: iptables -[ACD] chain rule-specification [options]
iptables -I chain [rulenum] rule-specification [options]
iptables -R chain rulenum rule-specification [options]
iptables -D chain rulenum [options]
iptables -[LS] [chain [rulenum]] [options]
iptables -[FZ] [chain] [options]
iptables -[NX] chain
iptables -E old-chain-name new-chain-name
iptables -P chain target [options]
iptables -h (print this help information)
规则查询:
我们以filter进行实际的操作,filter表负责过滤数据报文,比如:禁止(允许)哪些IP地址访问
访问哪些端口等
iptables -t filter -L
-t : 指定表类型 raw > mangle > nat > filter
-L :列出表中的规则,默认所有规则
[root@node1 ~]# iptables -t filter -vxL INPUT
-v :显示详细信息
-x : 展开数字信息 (policy ACCEPT 0 packets, 0 bytes)
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
1599 117872 ACCEPT all – any any anywhere anywhere ctstate RELATED,ESTABLISHED
0 0 ACCEPT all – lo any anywhere anywhere
69 6012 INPUT_direct all – any any anywhere anywhere
69 6012 INPUT_ZONES_SOURCE all – any any anywhere anywhere
69 6012 INPUT_ZONES all – any any anywhere anywhere
0 0 DROP all – any any anywhere anywhere ctstate INVALID
67 5908 REJECT all – any any anywhere anywhere reject-with icmp-host-prohibited
policy : 当前链的默认策略
packets:当前链默认策略匹配到的包的数量
bytes: 当前链默认策略匹配到的包的大小
pkts: 对应匹配到的报文数
bytes: 对应匹配到的报文大小总和
target: 规则对应的“动作”
prot: 规则对应的“协议” all所有协议
opt: 规则对应的选项
in: 数据包由哪个接口(网卡)流入,any 任意网卡都生效,指定相应的网卡进行规则匹配策略
out: 数据包由哪个接口(网卡)流出,any 任意网卡都生效,指定相应的网卡进行规则匹配策略
source: 规则对应的源地址(IP地址或网段)
destination: 规则对应的目标地址(ip地址或网段)
源地址和目标地址都是anywhere,因此iptables默认进行了名称解析
anywhere 0.0.0.0/0
-n : 将信息以数字形式显示
–line-numbers:显示有序号
规则操作
iptables -t filter -F INPUT
-F: 清除规则
增加规则
iptables -t -A <匹配条件> -j
禁止ping 172.16.0.1 192.168.10.10
iptables -t filter -A INPUT -s 172.16.0.1 -j REJECT
iptables -t filter -I INPUT -s 192.168.10.10 -j DROP
[root@node1 ~]# iptables --line-numbers -t filter -vxnL INPUT
Chain INPUT (policy ACCEPT 6 packets, 428 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 DROP all – * * 192.168.10.10 0.0.0.0/0
2 4 240 REJECT all – * * 172.16.0.0/24 0.0.0.0/0 reject-with icmp-port-unreachable
-A: 追加规则 (latest)
-I: 插入规则(first)+ [number] 可以插入到指定位置
-s: 匹配条件中的源地址
-j: 匹配条件中的动作
删除规则:
方法一:根据具体的匹配条件去删除规则
方法二:可以根据规则的number进行删除
iptables -t filter -D INPUT -s 192.168.10.2 -j DROP
iptables -t filter -D INPUT 2
-D: 删除指定“链”中的规则 + [num] 指定规则
-F: 删除操作,删除所有规则
修改规则:
iptables -t filter -R INPUT [num] <匹配条件> -j
iptables -t filter -R INPUT 1 -s 192.168.10.0/24 -j DROP
-R: 修改指定规则
-P: 修改链的默认策略
[root@node1 ~]# iptables -t filter -P INPUT DROP
基本匹配条件:
-s: 指定源地址
-d:指定目标地址
-p:指定协议
-i / -o: 指定网卡的流入和流出
扩展匹配条件:需要扩展模块支持
-m [模式名]: 去使用扩展模式,定义扩展匹配条件
tcp/udp扩展模式
–dport: 指定目的端口
–sport:指定源端口
iprange扩展模块
–src-range: 指定源IP地址段
–dest-range: 指定目的IP地址段
192.168.0.0/24-192.168.31.0/24
string模块:指定匹配报文中的字符串
–algo: 指定匹配算法(bm和kmp)
–string: 指定需要匹配的字符串
time模块:时间段匹配,如果报文在到达时间范围内,则符合匹配条件
–timestart[起始时间] --timestop[结束时间]
–weekdays: 指定周
–mouthdays; 指定月
–datestart[指定日期] --datestop[结束日志]
connlimit模块:限制每个IP地址同时连接到server端的连接数量,不需要指定IP地址,默认就是针对
每个客户端IP地址
iptables -t filter -I INPUT -m connlimit --connlimit-above 2 -j REJECT
限制每个客户端IP的并发连接数高于2都被拒绝
ssh连接
-p tcp --dport 22 -m connlimit --connlimit-upto 2 -j ACCEPT
限制每个客户端IP的ssh连接并发连接数低于2时,都被接收
limit模块:对“报文达到的速率”进行限制的,就是限制单位时间内流入包的数量
-m limit --limit [速率]
速率:
/second
/minute
/hour
/day
state模块:对于连接报文中的状态可以分为:NET,ESTABLISHED,RELATED,INVALID,..
通过匹配连接状态,从而定义过滤规则
自定义链:
当默认链的规则非常多的时候,不方便我们管理;如果INPUT链中存放着几百条规则,有针推http
ftp服务,当我们想去修改http规则的时候,为了方便管理,则需要去创建自定义链
iptables -t filter -N [chain_name]
-N: 创建自定义链
增删改查同模式链的方法
http服务,只允许192.168.10.0/24 以及 172.16.0.0/16的客户端访问:
192.168.20.0/24
iptables -t filter -N http
1. iptables -t filter -A http -s 192.168.10.0/24 -j ACCEPT
2. iptables -t filter -A http -s 172.16.0.0/16 -j ACCEPT
参照别的“链”规则
删除自定义链:
-X: 删除自定链
[root@node1 ~]# iptables -t filter -X http
iptables: Directory not empty.
满足以下要求:
- 自定义链中没有任何模式的链引用,即自定义链的引用数为0
- 自定义链中没有任何规则,自定义链为空
iptables 规则保存和恢复
保存规则:
iptables-save > /etc/sysconfig/iptables
iptables-save > /iptables/input1
恢复规则:
iptables-restore < /iptables/input1
Firewalld概述
动态防火墙:后台程序firewalld提供了一个动态管理的防火墙,用于支持网络“zone”,以分配对一个网络机器相关链接和解面一定程序的信任,它具备ipv4和ipv6防火墙设置的支持
它支持以太网桥,并由分离运行时间和永久配置选项:它依次用iptables工具与执行数据包过滤的内核空间的Netfilter通信
图形化配置工具:firewall-config
命令行配置工具:firewall-cmd
firewalld和Iptables的关系
1.firewalld和Iptables一样本身不具备防火墙工具:二十将用户空间的安全设定执行到内核空间中的Netfilter(安全框架)
不同点: - firewalld service在/usr/lib/firewalld/(系统配置)和 /etc/firewalld/(用户配置)
iptables /etc/sysconfig/iptables中存贮配置 - iptables每一个单独更改意味着清除旧的规则和从/etc/sysconfig/iptables读取新的配置文件
- firewalld 不再创建任何新的规则,仅仅运行规则中的不同之处,因此Firewalld可以在运行时间内,改变设置而不丢失现有的连接状态
NetworkManager 服务:通知firewalld一个接口归属某个区域,新加入的接口被分配到默认区域中
zone区域:
trusted(信任区域):可以接受所有的网络连接
home(家庭区域):用户家庭网络,可以基本新人网络内的其他计算机
internal(内部区域):用于内部网络,可以基本信任网络内的其他计算机
worker(工作区域):用户工作区,可以基本新人网络内的其他计算机,选择不同的连接
public(公共区域)再公共区域内使用,firewalld默认区域
external(外部区域):出去的IPv4连接通过此区域伪装和转发,仅接受ssh连接
dmz(军事区域):用于非军事区域内的电脑,仅接受ssh连接
block(限制区域):拒绝所有网络连接
drop(丢弃):任何接收到的数据包都会丢弃,没有回复
firewalld管理操作