企业级iptables防火墙 [TOC]
一、Iptables介绍
Iptables是unix/linux自带的一款优秀且开源的,基于包过滤(对OSI模型的四层或者是四层以下进行过滤)的防火墙工具对流入和流出服务器的数据包进行很精细的控制。主要针对网络访问。
iptables不是真正的防火墙,为一个客户端的代理,用户是通过iptables这个代理,将用户的安全设定执行到对应的“安全框架”中,这个“安全框架”才是真正的防火墙。这个框架叫做“netfilter”。
包过滤防火墙在网络层截取网络数据包的包头 (header),针对数据包的包头,根据事先定义好的防火墙过滤规则进行对比,根据对比结果,再执行不同的动作。包过滤防火墙一般工作在网络层,所以也称为“网络防火墙”,通过检查数据流中每一个数据包的源ip地址,目标ip地址,源端口,目标端口,协议类型(tcp,udp,icmp等),状态等信息来判断是否符合规则。
netfilter :内核空间 ,真正实现防火墙的功能。
iptables :用户空间 ,在/sbin/iptables存在的防火墙,通过iptables提供管理 、修改 、删除 或者插入 规则;用户和内核交互的工具就是iptables。简单理解为“iptables”是真正实现用户防火墙规则的“桥梁”。
Iptable弊端: 大并发的情况,考虑不要开启iptables。因为高并发,iptables会加大延迟。除非并发小,如果服务器必须处于公网。考虑开启防火墙。建议利用硬件防火墙提升架构安全。
1. iptables工作原理分类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 主机防火墙:主要是用来防范单台主机的进出报文;-----filter表 INPUT链 网络防火墙: 能够实现对进出本网络的所有主机报文加以防护----nat表 raw mangle nat filter raw表:用于对数据包进行连接跟踪前的处理。 mangle表:用于修改数据包的IP头或其他信息。 nat表:用于网络地址转换,如源地址转换和目标地址转换。 filter表:用于执行包过滤,是最常用的表,决定数据包是否被接受、丢弃或拒绝。 ======================================================================== iptables缺点: (1)防火墙虽然可以过滤互联网的数据包,但却无法过滤内部网络的数据包。因此若有人从内部网络攻击时,防火墙没有作用。 (2)电脑本身的操作系统亦可能因一些系统漏洞,使入侵者可以利用这些漏洞绕过防火墙过滤,从而入侵电脑。 (3)防火墙无法有效阻挡病毒攻击,尤其是隐藏在数据中的病毒。
2. iptables工作流程
防火墙是一层层过滤的。 按照配置规则的顺序从上到下,从前到后进行过滤的。此时,第一条规则匹配成功。则不会继续匹配。
如果匹配上了规则,即明确表明是阻止还是通过,此时数据包就不在向下匹配新规则了。匹配到即停止继续匹配。
如果所有规则中没有明确表明是阻止还是通过这个数据包,也就是没有匹配上规则,向下进行匹配,直到匹配默认规则得到明确的阻止还是通过。
防火墙的默认规则是对应链的所有的规则执行完以后才会执行的(最后执行的规则)。
二、iptables概念 1. 什么是 iptables 举个例子,如果把Netfilter看成是某个小区的一栋楼。那么表(tables)就是楼里的其中的一套房子。这套房子”表(tables)”属于这栋楼“Netfilter/iptables”。
2. 什么是表(tables) 表(tables)是链的容器,即所有的链(chains)都属于其对应的表(tables).如上,如果把Netfilter看成是某个小区的一栋楼.那么表(tables)就是楼里的其中的一套房子。
3 什么是链(chains) 链(chains)是规则(Policys)的容器。如果把表(tables)当作有一套房子,那么链(chains)就可以说是房子里的家具(柜子等)。
4 什么是规则(Policy) 规则(Policy)就比较容易理解了,就是iptables系列过滤信息的规范和具体方法条款了.可以理解为柜子如何增加并摆放柜子东西等。
匹配规则:提供了需要执行对应动作的目标匹配机制。通常可以匹配协议类型、目的地址、源地址、目的端口、源端口、目的网段、源网段、接收数据包的网卡、发送数据包的网卡、协议头、连接状态等。
动作目标:又称为Target。是数据包满足匹配规则时触发的相应的动作。Target分为两种类型:终止目标和非终止目标。
基本术语如下表格所示:
Netfilter/iptables
表(tables)
链(chains)
规则(Policy)
一栋楼
楼里的房子
房子里的柜子
柜子里衣服,摆放规则
三、iptables 表和链 默认情况下,iptables根据功能和表的定义划分包含四个表,filter,nat,mangle,其每个表又包含不同的操作链。 实际iptables包含4张表和五个链,主要记住filter即可。
1、四张表
raw 表 :设置数据包的连接跟踪标志,通常用于在连接跟踪生效前对数据包进行处理。 设置数据包是否绕过连接跟踪机制。
mangle 表 :修改数据包的某些字段或标记数据包。修改数据包的标志位等。提供修改数据包IP头部 的功能
==nat 表 ==:用于修改数据包的网络地址(如源地址和目的地址),主要应用于网络地址转换(NAT),如 SNAT 和 DNAT。修改数据包的源地址(SNAT)或目的地址(DNAT)。
==filter 表 ==:最常用的表,用来判断一个数据包是否可以通过 。在防火墙领域,filter表提供的功能通常被称为“过滤”包。这个表提供了防火墙的一些常见功能。专门用于数据包的过滤(即决定数据包是否被接受、拒绝、或丢弃)。此表重要用于接受(ACCEPT)、拒绝(REJECT)、丢弃(DROP)数据包。
表的应用顺序 ::raw ---> mangle ---> nat ---> filter
2、五条链 1 2 3 4 5 6 7 8 9 10 11 12 五链:(必须是大写)链里面写的是规则。 PREROUTING -----------------进路由之前数据包 INPUT -----------------就是过滤进来的数据包(输入) FORWARD -----------------转发 OUTPUT -----------------发出去的数据包 POSTROUTING -----------------路由之后数据包 # 所有的访问都是按顺序: 入站:比如访问自身的web服务流量。先PREROUTING(是否改地址),再INPUT(是否允许)到达程序。 转发:经过linux网关的流量.先PREROUTING(是否改地址),然后路由。转发给FORWARD(转发或者丢弃),最后经过POSTROUTING(看看改不改地址。) 出站:源自linux自身的流量.先OUTPUT,再给POSTROUTING(是否改IP)。 # 规则顺序:逐条匹配,匹配即停止。
3、四表五链
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 raw表里面: PREROUTING OUTPUT 总结:数据包跟踪 内核模块iptables_raw =============================================================================== mangel表里面有5个链: PREROUTING INPUT FORWARD OUTPUT POSTROUTING 路由标记用的表。内核模块iptables_mangle ================================================================================ nat表里面的链: PREROUTING # 在数据包到达netfilter系统时,在进行路由判断之前执行该链上的规则,作用是改变数据包的目的地址、目的端口等,起到DNAT的作用; INPUT OUTPUT # 用来处理从主机发出去的数据包。 POSTROUTING # 数据包发出时,当数据包经过了路由判断后执行该链上的规则,作用是改变数据包的源地址、源端口等,起到SNAT的作用; 转换地址的表(改IP,改端口。当网关使用的linux。保护内外网流量。内核模块叫iptable_nat) ================================================================================= filter表有三个链:重点 INPUT # 用来过滤进入主机的数据包; FORWARD # 负责转发流经主机的数据包,起到转发的作用,和NAT关系很大。想要主机支持转发需要设置相关内核参数:net.ipv4.ip_forward=1 OUTPUT # 用来处理从主机发出去的数据包。 总结:根据规则来处理数据包,如转或者丢。就是实现主机型防火墙的主要表。 内核模块 iptable_filter
四 iptables操作 1、安装Iptabls 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 # >>> 安装Iptables [root@iptables ~]# yum install -y iptables iptables-services # >>> 关闭firewalld和selinux [root@iptables ~]# systemctl disable --now firewalld [root@iptables ~]# setenforce 0 # >>> 启动iptables [root@iptables ~]# systemctl enable --now iptables # >>>查看版本 [root@iptables ~]# iptables -V iptables v1.4.21 # >>> 记录规则配置文件 [root@iptables ~]# rpm -ql iptables | grep iptables /etc/sysconfig/iptables-config
2、参数解释 1 2 3 4 5 6 7 8 9 10 -L: 列出一个链或所有链中的规则信息 -n: 以数字形式显示地址、端口等信息 -v: 以更详细的方式显示规则信息 --line-numbers: 查看规则时,显示规则的序号(方便之处,通过需要删除规则-D INPUT 1 -F: 清空所有的规则(-X是清理自定义的链,用的少;-Z清零规则序号) -D: 删除链内指定序号(或内容)的一条规则 -P: 为指定的链设置默认规则 -A: 在链的末尾追加一条规则 -I: 在链的开头(或指定序号)插入一条规则 -t: 指定表名
3、参数使用 :如果不写-t 默认使用filter表1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 # >>> 查看指定表规则 [root@iptables-server ~]# iptables -t nat -L # >>> 默认查看规则 [root@iptables ~]# iptables -L Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED ACCEPT icmp -- anywhere anywhere ACCEPT all -- anywhere anywhere ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh REJECT all -- anywhere anywhere reject-with icmp-host-prohibited # >>> 以数字的形式显示ip和端口与协议 [root@iptables ~]# iptables -nL Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0 ACCEPT all -- 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 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT) target prot opt source destination REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT) target prot opt source destination # >>> 显示规则行号 [root@iptables ~]# iptables -nL --line Chain INPUT (policy ACCEPT) num target prot opt source destination 1 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 2 ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0 3 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 4 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22 5 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT) num target prot opt source destination 1 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT) num target prot opt source destination # >>> 清空规则: [root@iptables ~]# iptables -F [root@iptables ~]# iptables -nL Chain INPUT (policy ACCEPT) target prot opt source destination Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination # >>> 清空单独的某一个链里面的规则 [root@iptables ~]# iptables -F 链名 # >>> 清空单独的某一个表里的,某一个链里面的规则 [root@iptables ~]# iptables -t nat -F INPUT # >>> 删除某条规则 [root@iptables ~]# iptables -t filter -D INPUT 5 # >>> 保存规则: [root@iptables ~]# iptables-save > /etc/sysconfig/iptables
4、iptables语法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 iptables -t 表名 动作 [链名] [-p 匹配条件] [-j 控制类型] -j:控制类型, 通过前面匹配到之后是丢弃还是保留数据包的处理方式: ACCEPT允许, REJECT拒绝, DROP丢弃。 不会给用户返回任何的拒绝消息,不推荐使用。 ====================================================== 动作:添规则还是删除规则 -p:匹配条件:数据包特征ip,端口等 如果不写-t 默认使用filter表 ======================================================= 动作 修改默认规则: -P (大p) 删除规则:-D 修改规则:-R 追加规则: -A 默认追加到链的末尾 插入规则:-I (大i),在链的开头(或指定序号)插入一条规则
5、查看添加删除规则 观察iptable规则添加的方法,删除和查询的方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 # >>> 插入规则到最后一行 $ iptables -t filter -A INPUT -p tcp -j ACCEPT # >>> 插入规则到第一行 $ iptables -I INPUT -p udp -j ACCEPT # >>> 插入规则到第四行 $ iptables -I INPUT 4 -p icmp -j ACCEPT # >>> 查看filter表所有的规则 $ iptables -nL # >>> 删除Filter表INPUT链中第三行规则 $ iptables -D INPUT 3 # >>> 清空filter表中所有的规则 $ iptables -F # >>> 持久化规则 $ iptables-save > /etc/sysconfig/iptables # >>> 重启iptables $ systemctl restart iptables
2、规则匹配条件
通用匹配(协议),可以独立使用。iptables只能匹配四层及四层一下的协议
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 协议:-p (小p) tcp ---用的最多 udp icmp ---ping的时候用的协议 # 使用协议的时候可以不指定端口,使用端口的时候必须指定协议。 # >>> 禁止自己被ping,在filter表的INPUT链插入一个拒绝icmp的规则。 1)#>>> 清空所有Iptables规则 [root@iptables ~]# iptables -F 2)#>>> 禁止所有主机使用icmp协议ping自己 [root@iptables ~]# iptables -A INPUT -p icmp -j REJECT [root@iptables ~]# iptables -nL Chain INPUT (policy ACCEPT) target prot opt source destination REJECT icmp -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination # >>> 验证:(更换其他主机测试) [root@localhost ~]# ping 192.168.174.141 PING 192.168.174.141 (192.168.174.141) 56(84) bytes of data. From 192.168.174.141 icmp_seq=1 Destination Port Unreachable # >>> 丢弃所有主机通过icmp协议发过来的数据包 [root@iptables ~]# iptables -I INPUT 1 -p icmp -j DROP(丢弃) [root@iptables ~]# iptables -nL Chain INPUT (policy ACCEPT) target prot opt source destination REJECT icmp -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable DROP icmp -- 0.0.0.0/0 0.0.0.0/0 Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination # >>> 测试(其他主机测试) [root@localhost ~]# ping 192.168.174.141 PING 192.168.174.141 (192.168.174.141) 56(84) bytes of data.
通过端口规则匹配:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 # 端口: --sport --源端口 --dport --目标端口 # >>> 拒绝192.168.246.201这台机器通过ssh连接到这台服务器 $ iptables -I INPUT -s 192.168.246.201 -p tcp --dport 22 -j REJECT [root@iptables ~]# iptables -Ln Chain INPUT (policy ACCEPT) target prot opt source destination REJECT tcp -- 192.168.174.142 0.0.0.0/0 tcp dpt:22 reject-with icmp-port-unreachable REJECT icmp -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable DROP icmp -- 0.0.0.0/0 0.0.0.0/0 Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination # >>> 拒绝192.168.246.201这台机器通过22端口到80端口的访问,包括22和80端口在内 $ iptables -I INPUT -s 192.168.246.201 -p tcp --dport 22:80 -j REJECT [root@iptables ~]# iptables -nL Chain INPUT (policy ACCEPT) target prot opt source destination REJECT tcp -- 192.168.174.142 0.0.0.0/0 tcp dpts:22:80 reject-with icmp-port-unreachable REJECT tcp -- 192.168.174.142 0.0.0.0/0 tcp dpt:22 reject-with icmp-port-unreachable REJECT icmp -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable DROP icmp -- 0.0.0.0/0 0.0.0.0/0 Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination # >>> 验证:(更换192.168.246.201) $ curl -I http://192.168.246.200 curl: (7) Failed connect to 192.168.246.200:80; Connection refused $ ssh root@192.168.246.200 ssh: connect to host 192.168.246.200 port 22: Connection refused # >>> 拒绝所有机器通过ssh连接到这台服务器 $ iptables -I INPUT -p tcp --dport 22 -j REJECT # >>> 拒绝所有机器通过22端口到80端口的访问,包括22和80端口在内 $ iptables -I INPUT -p tcp --dport 22:80 -j REJECT
通过ip地址
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 # >>> 禁止源192.168.246.201主机ping进来。(换个主机ping一下,就可以通信) $ iptables -I INPUT -s 192.168.246.201 -p icmp -j REJECT # >>> 验证(切换192.168.2) $ ping 192.168.246.200 PING 192.168.246.200 (192.168.246.200) 56(84) bytes of data. From 192.168.246.200 icmp_seq=1 Destination Port Unreachable # >>> 拒绝多个ip地址:后面跟ip地址可以更多个ip地址用逗号隔开 $ iptables -t filter -I INPUT -s 192.168.246.201,192.168.246.133 -p icmp -j REJECT $ iptables -t filter -I INPUT -s 192.168.246.201,192.168.246.133 -p tcp --dport 22:80 -j REJECT # >>> 验证:在源ip地址通过curl访问 $ curl -I http://192.168.246.200 curl: (7) Failed connect to 192.168.246.200:80; Connection refused $ ssh root@192.168.246.200 ssh: connect to host 192.168.246.200 port 22: Connection refused # >>> 限制源192.168.10.0/24网段的数据包。 $ iptables -I INPUT -s 192.168.10.0/24 -j REJECT
修改规则
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 $ iptables -nL target prot opt source destination REJECT tcp -- 192.168.246.133 anywhere tcp dpts:ssh:http reject-wi REJECT tcp -- 192.168.246.201 anywhere tcp dpts:ssh:http reject-wi REJECT icmp -- 192.168.246.201 anywhere reject-with icmp-port-unreachable # >>> 将修改第二条规则访问80端口 $ iptables -R INPUT 2 -p tcp --dport 80 -s 192.168.246.201 -j ACCEPT $ iptables -L Chain INPUT (policy ACCEPT) target prot opt source destination REJECT tcp -- 192.168.246.133 anywhere tcp dpts:ssh:http reject-with icmp-port-unreachable ACCEPT tcp -- 192.168.246.201 anywhere tcp dpt:http REJECT icmp -- 192.168.246.201 anywhere reject-with icmp-port-unreachable # >>> 验证在修改为允许访问的源ip机器上 $ curl -I http://192.168.246.200 HTTP/1.1 200 OK ======================================================================================= $ iptables -R INPUT 1 -p tcp -s 192.168.62.185 --dport 22 -j ACCEPT 验证在修改为允许访问的源ip机器上: # ssh 192.168.62.135
5、icmp类型匹配
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 禁止ping策略原则 iptables服务器是ping命令发起者或是接受者 -i --in-interface:在INPUT链配置规则中,指定从哪一个网卡接口进入的流量(只能配置在INPUT链上) -o --out-interface:在OUTPUT链配置规则中,指定从哪一个网卡接口出去的流量(只能配置在OUTPUT链上) ==================================================================================================== icmp的类型: 0: Echo Reply——回显应答(Ping应答)ping的结果返回。 8: Echo request——回显请求(Ping请求),发出去的请求。 ==================================================================================================== iptables服务器-----发起者:ping 别的机器 # >>> 自己不能ping别人,但是别人可以ping自己: $ iptables -I OUTPUT -o ens33 -p icmp --icmp-type 8 -j REJECT # >>> 本机验证 $ ping 192.168.246.133 PING 192.168.246.133 (192.168.246.133) 56(84) bytes of data. ping: sendmsg: Operation not permitted # >>> 其他主机验证 $ ping 192.168.246.200 PING 192.168.246.200 (192.168.246.200) 56(84) bytes of data. 64 bytes from 192.168.246.200: icmp_seq=1 ttl=64 time=0.280 ms ==================================================================================================== iptables服务器作为接受者。也就是别人ping自己: # >>> 本机可以ping其他机器。其他机器不能ping通本机 $ iptables -I OUTPUT -o ens33 -p icmp --icmp-type 0 -j REJECT # >>> 验证 $ ping 192.168.246.201 PING 192.168.246.201 (192.168.246.201) 56(84) bytes of data. 64 bytes from 192.168.246.201: icmp_seq=1 ttl=64 time=0.491 ms $ ping 192.168.246.200 PING 192.168.246.200 (192.168.246.200) 56(84) bytes of data. ========================================================================================= # >>> 拒绝任何ping的协议 $ iptables -I INPUT -p icmp -j DROP
3、扩展匹配 显示匹配:如端口匹配,IP范围,MAC地址,等特殊匹配
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 # iptables -m iprange --help 1.指定ip范围: 语法: -m iprange --src-range # iptables -I INPUT -p tcp --dport 80 -m iprange --src-range 192.168.246.199-192.168.246.206 -j REJECT 2.指定多端口范围:一次拒绝多个指定端口 语法: -m multiport --sports #源端口 -m multiport --dports #目的端口 # iptables -A INPUT -p tcp -m multiport --dports 22,80 -s 192.168.246.133 -j REJECT 验证:在246.133机器上 # ssh root@192.168.246.200 ssh: connect to host 192.168.246.200 port 22: Connection refused 3.MAC地址匹配 拒绝MAC地址的匹配:只能匹配源MAC地址 语法: -m mac --mac-source # iptables -I INPUT -p icmp -m mac --mac-source 0:0c:29:cd :26:77 -j REJECT # iptables -I INPUT -m mac --mac-source 00:0C:29:64:E3:8D -j REJECT
通过网卡接口
1 # iptables -I INPUT -i ens33 -j DROP
保存和删除规则
1 2 3 4 5 6 删除: # iptables -D INPUT 3 ========================================================================================= 保存: [root@iptables-server ~]# iptables-save > /etc/sysconfig/iptables 最后写完规则后记得保存!
1 2 3 4 5 题库: 1. 开放本机的22端口,对所有地址开放? iptables -t filter -A INPUT -p tcp --dport 22 -j ACCEPT 2. 开放本机的80端口,对所有地址开放? iptables -t filter -A INPUT -p tcp --dport 80 -j ACCEPT
企业案例: 工作中。Mysql,Redis等服务一般放在内网服务器上,但是为了保证其绝对的安全性,可能会把防火墙打开,打开之后,需要我们配置一些规则,来拒绝大都数的访问,只允许指定的ip地址来访问自身的服务
案例:redis服务器允许指定ip地址访问自身服务:
1 2 3 4 5 6 7 8 redis服务器上操作: # iptables -I INPUT -p tcp --dport 6379 -s 192.168.62.188 -j ACCEPT # iptables -I INPUT -p tcp --dport 6379 -s 192.168.62.185 -j ACCEPT # iptables -I INPUT -p tcp --dport 6379 -j REJECT 注意,顺序不能乱 被允许ip服务器上验证: # telnet 192.168.62.135 6379
作业:Mysql服务器允许指定ip地址访问自身服务
六、企业级Firewalld防火墙 rhel 7:firewall-cmd工具,firewalld服务
1、区域: firewalld将网卡对应到不同的区域(zone),通过不同的zone定义了不同的安全等级
1 2 3 4 5 6 7 8 trusted :允许所有流量通过 home/internal:仅允许ssh数据通过 work:仅允许ssh,ipp-client,dhcpv6-client数据通过 public:默认区域,仅允许ssh,dhcpv6-client数据通过 external:仅允许ssh数据通过,通过该区域的数据将会伪装(SNAT/DNAT) dmz:仅允许ssh数据通过 block:任何传入的网络数据包都将被阻止。拒绝所有流量 drop:丢弃所有流量,没有返回回应消息
2、命令详解 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 firewall-cmd --permanent --permanent #永久生效的配置参数、资源、端口以及服务等信息 1、域zone相关的命令 --get-default-zone #查询默认的区域名称 --set-default-zone=<区域名称> #设置默认的区域 --get-active-zones #显示当前正在使用的区域与网卡名称 --get-zones #显示总共可用的区域 2、services管理的命令 --add-service=<服务名> --zone=<区域> #设置指定区域允许该服务的流量 --remove-service=<服务名> --zone=<区域> #设置指定区域不再允许该服务的流量 3、Port相关命令 --add-port=<端口号/协议> --zone=<区域> #设置指定区域允许该端口的流量 --remove-port=<端口号/协议> --zone=<区域> #设置指定区域不再允许该端口的流量 4、查看所有规则的命令 --list-all --zone=<区域> 显示指定区域的网卡配置参数、资源、端口以及服务等信息 --reload #让“永久生效”的配置规则立即生效,并覆盖当前的配置规则
3、firewalld配置使用
查看默认区域:
1 2 3 4 5 6 7 8 9 10 $ firewall-cmd --get-default-zone public 验证: 在192.168.246.201机器上访问192.168.246.200 $ curl -I http://192.168.246.200 curl: (7) Failed connect to 192.168.246.200:80; No route to host $ ssh root@192.168.246.200 root@192.168.246.200's password:
更改默认区域
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 $ firewall-cmd --set -default-zone=trusted success $ firewall-cmd --reload success $ firewall-cmd --get-default-zone trusted 验证: 在192.168.246.201机器上访问192.168.246.200 $ curl -I http://192.168.246.200 HTTP/1.1 200 OK ================================================================================== 修改回默认区域: $ firewall-cmd --set -default-zone=public success $ firewall-cmd --reload success
向public区域添加服务
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 $ firewall-cmd --permanent --add-service=http --zone=public success $ firewall-cmd --reload success 验证: 在192.168.246.201机器上访问192.168.246.200 $ curl -I http://192.168.246.200 HTTP/1.1 200 OK 移除: $ firewall-cmd --permanent --zone=public --remove-service=http success $ firewall-cmd --reload success
指定IP地址为192.168.246.201/24的客户端进入drop区域
1 2 3 4 5 6 7 8 9 $ firewall-cmd --permanent --add-source=192.168.246.201/24 --zone=drop success $ firewall-cmd --reload success 验证: 在192.168.246.201的机器上访问246.200 $ curl -I http://192.168.246.200
将192.168.246.201/24移除drop区域
1 2 3 4 5 6 7 8 9 10 $ firewall-cmd --permanent --remove-source=192.168.246.201/24 --zone=drop success $ firewall-cmd --reload success 验证: 在192.168.246.201的机器上面访问246.200 $ curl -I http://192.168.246.200 HTTP/1.1 200 OK
向pubic区域添加服务,以添加端口的方式
1 2 3 4 5 6 7 8 9 10 $ firewall-cmd --permanent --add-port=80/tcp --zone=public success $ firewall-cmd --reload success 验证: 用192.168.246.201访问192.168.246.200机器 $ curl -I http://192.168.246.200 HTTP/1.1 200 OK
删除服务、端口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 $ firewall-cmd --permanent --remove-service=http --zone=public success $ firewall-cmd --reload success $ firewall-cmd --permanent --remove-port=80/tcp --zone=public success [root@iptables-server ~]# firewall-cmd --reload success 验证: 在192.168.246.201访问192.168.246.200机器 [root@iptables-test ~]# curl -I http://192.168.246.200 #访问失败 curl: (7) Failed connect to 192.168.246.200:80; No route to host
允许指定ip访问某个端口 仍以Redis为例
1 2 3 4 5 Redis服务端操作: # firewall-cmd --permanent --add-rich-rule="rule family=" ipv4" source address=" 192.168.142.16" port protocol=" tcp" port=" 6379" accept" 被允许ip地址验证: # telnet 192.168.62.135 6379
七、企业级防火墙配置 1、清除防火墙规则
2、修改默认规则为拒绝(修改前先放行22端口,保证自己能够连上主机)
1 2 3 4 5 6 [root@iptables-server ~]# iptables -A INPUT -p tcp --dport 22 -j ACCEPT #放开22号端口 [root@iptables-server ~]# iptables -P INPUT DROP #将默认所有进来的请求设置为全部拒绝掉 [root@iptables-server ~]# iptables -P FORWARD DROP #将默认所有的转发的规则设置为全部拒绝掉 注意:修改默认规则: 只能使用ACCEPT和DROP # iptables -P INPUT DROP ----拒绝 # iptables -P INPUT ACCEPT ----允许
3、放行指定的端口
1 2 3 [root@iptables-server ~]# iptables -A INPUT -i lo -j ACCEPT #允许通过lo网卡进入的请求 [root@iptables-server ~]# iptables -A INPUT -p tcp -m multiport --dport 80,443 -j ACCEPT #允许访问80和443端口 [root@iptables-server ~]# iptables -A INPUT -s 192.168.246.0/24 -j ACCEPT #允许这个内网网段连接服务器
4、保存iptables配置
1 2 3 4 [root@iptables-server ~]# service iptables save iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ] 或者 [root@iptables-server ~]# iptables-save > /etc/sysconfig/iptables