Linux iptables防火墙


Linux包过滤防火墙概述

Linux的防火墙体系主要工作在网络层,针对TCP/IP数据包实施过滤和限制,属于典型的包过滤防火墙(又或者称为网络层防火墙)。Linux系统的防火墙体系基于内核编码实现,具有非常稳定的性能和极高的效率,也因此获得广泛的应用。


netfilter和iptables区别
netfilter和iptables在许多安全技术资料中都用来指Linux防火墙,往往使人产生迷惑。它们的主要区别是:

·netfilter:Linux内核中实现包过滤防火墙的内部结构,不以程序或文件的形式存在,属于“内核态”(又称为内核空间)的防火墙功能体系。

·iptables:用来管理Linux防火墙的命令程序,通常位于/sbin/iptables目录下,属于“用户态”(又称为用户空间)的防火墙管理体系。



包过滤的工作层次
·主要是网络层,针对IP数据包
·体现在对包内的IP地址、端口等信息处理上
Linux iptables防火墙




iptables的表、链结构

规则链
·规则的作用:数据包进行过滤或处理
·链的作用:容纳各种防火墙规则
·链的分类依据:处理数据包的不同时机


默认包括5种规则链
·INPUT:处理入站数据包
·OUTPUT:处理出站数据包
·FORWARD:处理转发数据包
·POSTROUTING:在进行路由选择后处理数据包
·PREROUTING:在进行路有选择前处理数据包


规则表
·表的作用:容纳各种规则链
·标的划分依据:防火墙规则的作用相似

默认包括4个规则表
·raw表:确定是否对该数据包进行状态跟踪
·mangle表:为数据包设置标记
·nat表:修改数据包中的源、目标IP地址或端口
·filter表:确定是否放行该数据包(过滤)



默认的表、链结构示意图
Linux iptables防火墙



数据包过滤的匹配流程

规则表之间的顺序
·raw→mangle→nat→filter


规则链之间的顺序
·入站:PREROUTING→INPUT
·出站:OUTPUT→POSTROUTING
·转发:PREROUTING→FORWARD→POSTROUTING


规则链内的匹配顺序
·按顺序依次检查,匹配即停止(LOG策略例外)
·若找不到相匹配的规则,则按该链的默认策略处理

匹配流程示意图
Linux iptables防火墙




iptables安装

关闭firewalld防火墙
[[email protected] ~]# systemctl stop firewalld.service
[[email protected] ~]# systemctl disable firewalld.service
(CentOS 7 默认使用firewalld防火墙,若想使用iptables防火墙,必须先关闭firewalld防火墙)

安装iptables防火墙
[[email protected] ~]# yum -y install iptables iptables-services
·设置iptables开机启动
[[email protected] ~]# systemctl start iptables.service
[[email protected] ~]# systemctl enable iptables.service
iptables的基本语法


语法构成
·iptables [-t 表名] 选项 [链名] [条件] [-j 控制类型]
例:
[[email protected] ~]# iptables -t filter -I INPUT -p icmp -j REJECT

注意事项
·不指定表名时,默认指filter表
·不指定链名时,默认指表内的所有链
·除非设置链的默认策略,否则必须制定匹配条件
·选项、链名、控制类型使用大写字母,其余均为小写



数据包的常见控制类型
·ACCEPT:允许通过
·DROP:直接丢弃,不给出任何回应
·REJECT:拒绝通过,必要时会给出提示
·LOG:记录日志信息,然后传给下一条规则继续匹配



iptables的管理选项

添加新的规则
·-A:在链的末尾追加一条规则
·-I:在链的开头(或指定序号)插入一条规则
例:
[[email protected] ~]# iptables -t filter -A INPUT -p tcp -j ACCEPT
[[email protected] ~]# iptables -I INPUT -p udp -j ACCEPT
[[email protected] ~]# iptables -I INPUT 2 -p icmp -j ACCEPT
(-p用来指定协议)



查看规则列表
·-L:列出所有的规则条目
·-n:以数字形式显示地址、端口等信息
·-v:以更详细的方式显示规则信息
·–line-numbers:查看规则时,显示规则的序号
例:
[[email protected] ~]# iptables -L INPUT --line-numbers
[[email protected] ~]# iptables -n -L INPUT (-n -L可合写为-nL)
删除、清空规则
·-D:删除链内指定序号(或内容)的一条规则
·-F:清空所有的规则



设置默认策略
·-P:为指定的链设置默认规则
例:
[[email protected] ~]# iptables -t filter -P FORWARD DROP
[[email protected] ~]# iptables -P OUTPUT ACCEPT
(清空所有默认策略ACCEPT或DROP表的所有链)



规则的匹配条件

通用匹配
·可直接使用,不依赖于其他条件或扩展
·包括网络协议、IP地址、网络接口等条件

隐含匹配
·要求以特定的协议匹配作为前提
·包括端口、TCP标记、ICMP类型等条件

显示匹配
·要求以“-m 扩展模块”的形式明确指出类型
·包括多端口、MAC地址、IP范围、数据包状态等条件



常见的通用匹配条件
·协议匹配:-p 协议名
·地址匹配:-s 源地址、-d 目的地址
·接口匹配:-i 入站网卡、-o 出站网卡
例:
[[email protected] ~]# iptables -I INPUT -p icmp -j DROP
[[email protected] ~]# iptables -A FORWARD ! -p icmp -j ACCEPT
(感叹号!表示条件取反,比如原本是允许通过的,取反后变为拒绝通过)
[[email protected] ~]# iptables -A FORWARD -s 192.168.1.11 -j REJECT
[[email protected] ~]# iptables -I INPUT -s 10.20.30.0/24 -j DROP
[[email protected] ~]# iptables -A INPUT -i ens33 -s 192.168.0.0/16 -j DROP
[[email protected] ~]# iptables -A INPUT -i ens33 -s 10.0.0.0/8 -j DROP
[[email protected] ~]# iptables -A INPUT -i ens33 -s 172.16.0.0/12 -j DROP
(ens33外网接口)


常用的隐含匹配条件
·端口匹配:–sport源端口、–dport目的端口
·ICMP类型匹配:–icmp-type ICMP类型
例:
[[email protected] ~]# iptables -A FORWARD -s 192.168.4.0/24 -p udp --dport 53 -j ACCEPT
[[email protected] ~]# iptables -A INPUT -p tcp --dport 20:21 -j ACCEPT
[[email protected] ~]# iptables -A INPUT -p icmp --icmp-type 8 -j DROP
[[email protected] ~]# iptables -A INPUT -p icmp-type 0 -j ACCEPT
[[email protected] ~]# iptables -A INPUT -p icmp-type 3 -j ACCEPT
[[email protected] ~]# iptables -A INPUT -p icmp -j DROP
(ICMP类型使用字符串或数字代码表示,如“Echo-Request”代码为8,表示请求;“Echo-Reply”代码为0,表示回复;“Destination-Unreachable”代码为3,表示不可达)



常用的显示匹配条件
·多端口匹配:-m multiport --sports源端口列表
-m multiport --dports目的端口列表
·IP范围匹配:-m iprange --src-range IP范围
·MAC地址匹配:-m mac --mac-source MAC地址
·状态匹配:-m state --state连接状态
例:
[[email protected] ~]# iptables -A INPUT -p tcp -m multiport 25,80,110,143 -j ACCEPT
[[email protected] ~]# iptables -A FORWAED -p tcp -m iprange --src-range 192.168.4.21-192.168.4.28 -j ACCEPT
[[email protected] ~]# iptables -A INPUT -m mac --mac-source 00:0c:29:c0:55:3f -j DROP
[[email protected] ~]# iptables -P INPUT DROP
[[email protected] ~]# iptables -I INPUT -p tcp -m multiport --dport 80 -j ACCEPT
[[email protected] ~]# iptables -I INPUT -p tcp -m state --state ESTABLISHED -j ACCEPT