本文共 9598 字,大约阅读时间需要 31 分钟。
(1)selinux
安全增强型 Linux(Security-Enhanced Linux)简称 SELinux,它是一个 Linux 内核模块,也是 Linux 的一个安全子系统,seinux是Linux系统特有的安全机制,不过因为限制太多,所以很少人使用,一般我们都是关闭SELinux。临时关闭:setenforce 0; 永久关闭:编辑配置文件 vim /etc/selinux/config,找到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=disabled ###修改这一行为disabled\# 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的运行状态
1:enforcing #开启状态,会强制执行SELINUX的安全策略2:permissive #提示状态,会打印触发SELINUX安全策略的警告,但是不会执行相应的策略。3:disabled #关闭SELINUX,重启后也不会再启动SELINUX可以使用genenforce命令获得当前selinux的状态[root@luo ~]# getenforce Disabled
(2)netfilter
CentOS 5和6 的防火墙是netfilter;主要是使用iptables工具实现防火墙。CentOS7使用的是firewalld,在这里先关闭firewalld服务,使用CentOS6的netfilter演示。更换操作如下:systemctl stop firewalld 关闭firewalld服务systemctl disable firewalld 禁止firewalld服务开机启动yum install -y iptables-services 安装iptables-servicessystemctl enable iptables 让它开机启动systemctl start iptables 启动iptables服务[root@luo ~]# systemctl stop firewalld[root@luo ~]# systemctl disable firewalld.[root@luo ~]# systemctl status firewalld● firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled) Active: inactive (dead) Docs: man:firewalld(1)[root@luo ~]# yum install -y iptables-services^C #这里已经安装过了就不再安装了[root@luo ~]# systemctl enable iptablesCreated symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.[root@luo ~]# systemctl start iptables
(3)firewalld
CentOS7 的防火墙使用firewalld,不是netfilter5表:filter表、nat表、mangle表、raw表、security表
5链:PEROUTING、INPUT、FORWARD、OUTPUT、POSTROUTING下面介绍5表5链:filter表:主要用于过滤包,是系统预设的表。平时用的最多的表。该表内建3个链:IUPUT、OUTPUT、FORWARD,INPUT链作用进入本机的包,OUTPUT链作用于本机送出的包,FOEWARD作用于跟本机无关的包nat表:主要用于网络地址转换,它也有3个链。PREROUTING链的作用是在包刚刚到达防火墙时改变它的目的地址(非必须),OUTPUT链的作用是改变本地包的目的地址,POSTROUTING链的作用是在包即将离开防火墙时改变其源地址。偶尔用到!mangle表:主要用于给数据包做标记,然后根据标记去操作相应的包。(运维几乎不用,网络工程师用)raw表:可以实现不追踪某些数据包,默认系统的数据都会被跟踪。(也是几乎不用)security表:CentOS6没有的,它用于强制访问控制(MAC)的网络规则。(了解即可)PEROUTING:数据包进入路由表之前INPUT:通过路由表后目的地为本机FORWARD:通过路由表后,目的地不为本机OUTPUT:有本机产生,向外转发POSTROUTING:发送到网卡接口之前iptables数据包流向:表和链对应的关系:查看规则:iptables -t nat -nvL; 规则保存在/etc/sysconfig/iptables文件中;-t 指定表名;-nvL表示查看该表规则,-n表示不针对IP反解析主机名,-L表示列出,-v表示列出更加详细。不加-t,默认打印filter表的相关信息。
清空规则:iptables -F,临时清空,重启后会还原配置iptables文件的规则;保存规则:service iptables save包及流量计数器置零:iptables -Z增加/删除一条规则,用法:iptables -A INPUT -s 192.168.128.1 -p tcp --sport 1234 -d 192.168.128.28 --dport 80 -j DROP各个选项的作用:-A/-D 表示增加/删除一条规则-I 表示插入一条规则,其实效果跟-A一样(执行顺序不一样)-p 表示指定协议,可以是tcp、udp或者icmp-P 后面跟链名,它表示预设策略; 如iptables -P INPUT DROP; (使用这个选项是要小心,特别是远程服务器)--dport:跟-p一起使用,表示指定目标端口--sport:跟-p一起使用,表示指定源端口-s 表示指定源IP(可以是一个IP段)-d 表示指定目标IP(可以是一个IP段)-j 后面跟动作,其中ACCEPT表示允许包,DROP表示丢弃包,REJECT表示拒绝包-i 表示指定网卡举例:①插入一条规则,把来自2.2.2.2的所有数据包丢弃:iptables -I INPUT -s 2.2.2.2 -j DROP②下面是删除上面这条规则,注意删除规则时,必须和插入的规则一致,也就是说两条命令,除了-I和-D不一样,其他地方都是一样的:iptables -D INPUT -s 2.2.2.2 -j DROP③把来自2.2.2.2并且是TCP协议到本机80端口的数据包丢弃:iptables -I INPUT -s 2.2.2.2 -p tcp --dport 80 -j DROP; 注意--dport/--sport必须和-p一起使用,否则会报错。④把发送到10.0.0.21的22端口的数据包丢弃:iptables -I OUTPUT -p tcp -dport 22 -d 10.0.0.21 -j DROP有时候服务器上iptables过多,你想删除一条规则,但又忘记了创建是的规则,怎么删除呢?其实还有一种简单的方法:iptables -nvL --line-numbers查看规则的number[root@luo ~]# iptables -nvL --line-numberChain INPUT (policy ACCEPT 0 packets, 0 bytes)num pkts bytes target prot opt in out source destination 1 94 6648 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED2 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 3 0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 4 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:225 17 1634 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibitedChain 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-prohibitedChain OUTPUT (policy ACCEPT 68 packets, 5416 bytes)num pkts bytes target prot opt in out source destination
然后你想删除某一条规则:iptables -D INPUT 1; -D后面跟链名、规则num,这个num就是上面的数字
规则选项说明:-i或–in-interface <网络接口名> :指定数据包从哪个网络接口进入,如ppp0、eth0和eth1等-o或–out-interface <网络接口名> :指定数据包从哪块网络接口输出,如ppp0、eth0和eth1等-p或—proto协议类型 < 协议类型>:指定数据包匹配的协议,如TCP、UDP和ICMP等-s或–source <源地址或子网> :指定数据包匹配的源地址–sport <源端口号> :指定数据包匹配的源端口号,可以使用“起始端口号:结束端口号”的格式指定一个范围的端口-d或–destination <目标地址或子网> :指定数据包匹配的目标地址–dport <目标端口号> :指定数据包匹配的目标端口号,可以使用“起始端口号:结束端口号”的格式指定一个范围的端口 目标端口号> 目标地址或子网> 源端口号> 源地址或子网> 网络接口名> 网络接口名>
动作选项说明:
ACCEPT:接受数据包DROP:丢弃数据包REDIRECT:与DROP基本一样,区别在于它除了阻塞包之外, 还向发送者返回错误信息。SNAT:源地址转换,即改变数据包的源地址DNAT:目标地址转换,即改变数据包的目的地址MASQUERADE: IP伪装,即是常说的NAT技术, MASQUERADE只能用于ADSL等拨号上网的IP伪装,也就是主机的IP是由ISP分配动态的; 如果主机的IP地址是静态固定的,就要使用SNATLOG日志功能,将符合规则的数据包的相关信息记录在日志中,以便管理员的分析和排错
需求:只针对filter表,预设策略INPUT链DROP,其他两个链ACCEPT,然后针对192.168.66.0/24放行22端口,所有网段开放80端口,对所有网段开放21端口
可以编写一个shell脚本来实现。 vim /usr/local/sbin/iptables.sh ##编辑脚本文件,加入以下内容#! /bin/bash #定义执行脚本的shellIPT="/usr/sbin/iptables" #定义变量,iptables命令的绝对路径。$IPT -F #清空iptables规则$IPT -P INPUT DROP #默认规则,丢弃所有数据包。$IPT -P OUTPUT ACCEPT #默认规则,放行所有OUTPUT链的数据包$IPT -P FORWARD ACCEPT #默认规则,放行所有FORWARD链的数据包##下面这条规则允许通过RELATED和ESTABLISHED状态的数据包,这条规则必加,否则可能导致某些服务连不上。$IPT -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT $IPT -A INPUT -s 192.168.66.0/24cp --dport 22 -j ACCEPT #仅允许10.1.1.0/24网段链接22端口$IPT -A INPUT -p tcp --dport 80 -j ACCEPT #允许通过所有80端口的数据包$IPT -A INPUT -p tcp --dport 21 -j ACCEPT #允许通过所有21端口的数据包
[root@luo ~]# vi /usr/local/bin/iptables.sh[root@luo ~]# cat !$cat /usr/local/bin/iptables.sh#! /bin/bash IPT="/usr/sbin/iptables" $IPT -F $IPT -P INPUT DROP $IPT -P OUTPUT ACCEPT $IPT -P FORWARD ACCEPT $IPT -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT $IPT -A INPUT -s 192.168.66.0/24 -p tcp --dport 22 -j ACCEPT $IPT -A INPUT -p tcp --dport 80 -j ACCEPT $IPT -A INPUT -p tcp --dport 21 -j ACCEPT
执行这个脚本后查看添加的规则:
[root@luo ~]# iptables -nvLChain INPUT (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 33 2188 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 0 0 ACCEPT tcp -- * * 192.168.66.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:21Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 18 packets, 1688 bytes) pkts bytes target prot opt in out source destination
关于icmp的包有一个常见的应用(请牢记):
iptables -I INPUT -p icmp --icmp-type 8 -j DROP
这里--icmp-type选项要跟-p icmp一起使用,后面指定类型编号。这个8指的是本机ping通其他机器,而其他机器不能ping通本机。
A机器两块网卡ens33(192.168.66.130)、ens37(192.168.66.1),ens33可以上外网,ens37仅仅是内部网络,B机器只有ens37(192.168.100.100),和A机器ens37可以通信互联。
需求1:可以让B机器连接外网A机器上打开路由转发 echo "1">/proc/sys/net/ipv4/ip_forward[root@luo ~]# echo "1" > /proc/sys/net/ipv4/ip_forward[root@luo ~]# cat /proc/sys/net/ipv4/ip_forward1
A上执行 iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o ens33 -j MASQUERADE
[root@luo ~]# iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o ens33 -j MASQUERADE
B上设置网关为192.168.100.1
route add default gw 192.168.100.1 #设置一个默认网关echo "nameserver 119.29.29.29" > /etc/resolv.conf #添加一个临时的DNS服务器
这样B机器就可以连外网了
[root@localhost ~]# ping www.baidu.comPING www.a.shifen.com (14.215.177.38) 56(84) bytes of data.64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=2 ttl=128 time=6.99 ms64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=3 ttl=128 time=13.3 ms64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=4 ttl=128 time=7.54 ms64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=5 ttl=128 time=6.98 ms64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=6 ttl=128 time=9.47 ms64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=7 ttl=128 time=8.52 ms
需求2:端口转发
让PC能远程登录B
添加规则1,将访问192.168.66.130:1122端口的数据包转发给192.168.100.100:22端口[root@luo ~]# iptables -t nat -A PREROUTING -d 192.168.66.130 -p tcp --dport 1122 -j DNAT --to 192.168.100.100:22添加规则2,将来源于192.168.100.100的数据包的源地址转换为192.168.66.130[root@luo ~]# iptables -t nat -A POSTROUTING -s 192.168.100.100 -j SNAT --to 192.168.66.130
然后远程连接192.168.66.130:1122就可以间接的远程登录B主机了。
Connecting to 192.168.66.130:1122...Connection established.
扩展
selinux教程 selinux pdf电子书 iptables应用在一个网段 sant,dnat,masquerade iptables限制syn速率转载于:https://blog.51cto.com/13736286/2128783