8张图带你了解iptables的前世今生
阅读原文时间:2021年05月12日阅读:1

1 安全技术和防火墙

  • 入侵检测系统(Intrusion Detection Systems):特点是不阻断任何网络访问,量化、定位来自内 外网络的威胁情况,主要以提供报警和事后监督为主,提供有针对性的指导措施和安全决策依据,类 似于监控系统一般采用旁路部署方式

  • 入侵防御系统(Intrusion Prevention System):以透明模式工作,分析数据包的内容如:溢出攻 击、拒绝服务攻击、木马、蠕虫、系统漏洞等进行准确的分析判断,在判定为攻击行为后立即予以 阻断,主动而有效的保护网络的安全,一般采用在线部署方式

  • 防火墙( FireWall ):隔离功能,工作在网络或主机边缘,对进出网络或主机的数据包基于一定的 规则检查,并在匹配某规则时由规则定义的行为进行处理的一组功能的组件,基本上的实现都是默 认情况下关闭所有的通过型访问,只开放允许访问的策略,会将希望外网访问的主机放在DMZ (demilitarized zone)网络中.

    防水墙
    广泛意义上的防水墙:防水墙(Waterwall),与防火墙相对,是一种防止内部信息泄漏的安全产品。网络、外设接口、存储介质和打印机构成信息泄漏的全部途径。
    防水墙针对这四种泄密途径,在事前、事中、事后进行全面防护。其与防病毒产品、外部安全产品一起构成完整的网络安全体系。

按保护范围划分:

  • 主机防火墙:服务范围为当前一台主机

  • 网络防火墙:服务范围为防火墙一侧的局域网

按实现方式划分:

  • 硬件防火墙:在专用硬件级别实现部分功能的防火墙;另一个部分功能基于软件实现,如:华为, 山石hillstone,天融信,启明星辰,绿盟,深信服, PaloAlto , fortinet, Cisco, Checkpoint, NetScreen(Juniper2004年40亿美元收购)等

  • 软件防火墙:运行于通用硬件平台之上的防火墙的应用软件,Windows 防火墙 ISA --> Forefront TMG

按网络协议划分:

  • 网络层防火墙:OSI模型下四层,又称为包过滤防火墙

  • 应用层防火墙/代理服务器:proxy 代理网关,OSI模型七层

包过滤防火墙

网络层对数据包进行选择,选择的依据是系统内设置的过滤逻辑,被称为访问控制列表(ACL),通过 检查数据流中每个数据的源地址,目的地址,所用端口号和协议状态等因素,或他们的组合来确定是否 允许该数据包通过

优点:对用户来说透明,处理速度快且易于维护

缺点:无法检查应用层数据,如病毒等

应用层防火墙

应用层防火墙/代理服务型防火墙,也称为代理服务器(Proxy Server) 将所有跨越防火墙的网络通信链路分为两段 内外网用户的访问都是通过代理服务器上的“链接”来实现

优点:在应用层对数据进行检查,比较安全

缺点:增加防火墙的负载

提示:现实生产环境中所使用的防火墙一般都是二者结合体,即先检查网络数据,通过之后再送到应用

层去检查

2 Linux 防火墙的基本认识

Linux防火墙是由Netfilter组件提供的,Netfilter工作在内核空间,集成在linux内核中

Netfilter 是Linux 2.4.x之后新一代的Linux防火墙机制,是linux内核的一个子系统。Netfilter采用模块 化设计,具有良好的可扩充性,提供扩展各种网络服务的结构化底层框架。Netfilter与IP协议栈是无缝 契合,并允许对数据报进行过滤、地址转换、处理等操作

Netfilter官网文档:https://netfilter.org/documentation/

[root@centos8 ~]#grep -m 10 NETFILTER /boot/config-4.18.0-240.15.1.el8_3.x86_64
CONFIG_NETFILTER=y
CONFIG_NETFILTER_ADVANCED=y
CONFIG_BRIDGE_NETFILTER=m
CONFIG_NETFILTER_INGRESS=y
CONFIG_NETFILTER_NETLINK=m
CONFIG_NETFILTER_FAMILY_BRIDGE=y
CONFIG_NETFILTER_FAMILY_ARP=y
# CONFIG_NETFILTER_NETLINK_ACCT is not set
CONFIG_NETFILTER_NETLINK_QUEUE=m
CONFIG_NETFILTER_NETLINK_LOG=m
[root@centos8 ~]#

root@ubuntu20:~# grep -m 10 NETFILTER /boot/config-5.8.0-50-generic
CONFIG_NETFILTER=y
CONFIG_NETFILTER_ADVANCED=y
CONFIG_BRIDGE_NETFILTER=m
CONFIG_NETFILTER_INGRESS=y
CONFIG_NETFILTER_NETLINK=m
CONFIG_NETFILTER_FAMILY_BRIDGE=y
CONFIG_NETFILTER_FAMILY_ARP=y
CONFIG_NETFILTER_NETLINK_ACCT=m
CONFIG_NETFILTER_NETLINK_QUEUE=m
CONFIG_NETFILTER_NETLINK_LOG=m
root@ubuntu20:~#

[root@centos7 ~]# grep -m 10 NETFILTER /boot/config-3.10.0-1160.el7.x86_64
CONFIG_NETFILTER=y
# CONFIG_NETFILTER_DEBUG is not set
CONFIG_NETFILTER_ADVANCED=y
CONFIG_BRIDGE_NETFILTER=m
CONFIG_NETFILTER_NETLINK=m
CONFIG_NETFILTER_NETLINK_ACCT=m
CONFIG_NETFILTER_NETLINK_QUEUE=m
CONFIG_NETFILTER_NETLINK_LOG=m
CONFIG_NETFILTER_NETLINK_QUEUE_CT=y
CONFIG_NETFILTER_SYNPROXY=m
[root@centos7 ~]#

2.2.1 iptables

由软件包iptables提供的命令行工具,工作在用户空间,用来编写规则,写好的规则被送往netfilter,告 诉内核如何去处理信息包

[root@centos8 ~]#yum -y install iptables
[root@centos8 ~]#rpm -qi iptables
Name        : iptables
Version     : 1.8.4
Release     : 15.el8_3.3
Architecture: x86_64
Install Date: Tue 11 May 2021 09:32:18 AM CST
Group       : Unspecified
Size        : 1992105
License     : GPLv2 and Artistic 2.0 and ISC
Signature   : RSA/SHA256, Fri 18 Dec 2020 09:02:38 AM CST, Key ID 05b555b38483c65d
Source RPM  : iptables-1.8.4-15.el8_3.3.src.rpm
Build Date  : Fri 18 Dec 2020 07:27:23 AM CST
Build Host  : x86-02.mbox.centos.org
Relocations : (not relocatable)
Packager    : CentOS Buildsys <bugs@centos.org>
Vendor      : CentOS
URL         : http://www.netfilter.org/projects/iptables
Summary     : Tools for managing Linux kernel packet filtering capabilities
Description :
The iptables utility controls the network packet filtering code in the
Linux kernel. If you need to set up firewalls and/or IP masquerading,
you should either install nftables or this package.

Note: This package contains the nftables-based variants of iptables and
ip6tables, which are drop-in replacements of the legacy tools.
[root@centos8 ~]#

[root@centos8 ~]#iptables --version
iptables v1.8.4 (nf_tables)
[root@centos8 ~]#ll /usr/sbin/iptables
lrwxrwxrwx 1 root root 17 Dec 18 07:27 /usr/sbin/iptables -> xtables-nft-multi*
[root@centos8 ~]#

root@ubuntu20:~# iptables --version
iptables v1.8.5 (nf_tables)
root@ubuntu20:~# ll /usr/sbin/iptables
lrwxrwxrwx 1 root root 26  4月 30 16:25 /usr/sbin/iptables -> /etc/alternatives/iptables*
root@ubuntu20:~#

[root@centos7 ~]# yum -y install iptables
[root@centos7 ~]# iptables --version
iptables v1.4.21
[root@centos7 ~]# ll /usr/sbin/iptables
lrwxrwxrwx. 1 root root 13 3月  25 10:18 /usr/sbin/iptables -> xtables-multi
[root@centos7 ~]#

示例:安装 iptables 的 service 包

[root@centos8 ~]#dnf -y install iptables-services
[root@centos8 ~]#rpm -ql iptables-services
/etc/sysconfig/ip6tables
/etc/sysconfig/iptables
/usr/lib/systemd/system/ip6tables.service
/usr/lib/systemd/system/iptables.service
/usr/libexec/initscripts/legacy-actions/ip6tables
/usr/libexec/initscripts/legacy-actions/ip6tables/panic
/usr/libexec/initscripts/legacy-actions/ip6tables/save
/usr/libexec/initscripts/legacy-actions/iptables
/usr/libexec/initscripts/legacy-actions/iptables/panic
/usr/libexec/initscripts/legacy-actions/iptables/save
/usr/libexec/iptables
/usr/libexec/iptables/ip6tables.init
/usr/libexec/iptables/iptables.init
[root@centos8 ~]#

2.2.2 firewalld

从CentOS 7 版开始引入了新的前端管理工具

软件包

  • firewalld

  • firewalld-config

管理工具

  • firewall-cmd 命令行工具

  • firewall-config 图形工作

2.2.3 nftables

此软件是CentOS 8 新特性,Nftables最初在法国巴黎的Netfilter Workshop 2008上发表,然后由长期的 netfilter核心团队成员和项目负责人Patrick McHardy于2009年3月发布。它在2013年末合并到Linux内 核中,自2014年以来已在内核3.13中可用。

它重用了netfilter框架的许多部分,例如连接跟踪和NAT功能。它还保留了命名法和基本iptables设计的 几个部分,例如表,链和规则。就像iptables一样,表充当链的容器,并且链包含单独的规则,这些规 则可以执行操作,例如丢弃数据包,移至下一个规则或跳至新链。

从用户的角度来看,nftables添加了一个名为nft的新工具,该工具替代了iptables,arptables和 ebtables中的所有其他工具。从体系结构的角度来看,它还替换了内核中处理数据包过滤规则集运行时 评估的那些部分。

查看 nftables 软件包

[root@centos8 ~]#rpm -qi nftables
Name        : nftables
Epoch       : 1
Version     : 0.9.3
Release     : 16.el8
Architecture: x86_64
Install Date: Sat 08 May 2021 02:51:22 PM CST
Group       : Unspecified
Size        : 811386
License     : GPLv2
Signature   : RSA/SHA256, Wed 12 Aug 2020 05:05:16 AM CST, Key ID 05b555b38483c65d
Source RPM  : nftables-0.9.3-16.el8.src.rpm
Build Date  : Wed 12 Aug 2020 04:59:03 AM CST
Build Host  : x86-02.mbox.centos.org
Relocations : (not relocatable)
Packager    : CentOS Buildsys <bugs@centos.org>
Vendor      : CentOS
URL         : http://netfilter.org/projects/nftables/
Summary     : Netfilter Tables userspace utillites
Description :
Netfilter Tables userspace utilities.
[root@centos8 ~]#

CentOS 8 支持三种防火墙服务

[root@centos8 ~]#systemctl status iptables.service
● iptables.service - IPv4 firewall with iptables
   Loaded: loaded (/usr/lib/systemd/system/iptables.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
[root@centos8 ~]#systemctl status firewalld.service
● 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@centos8 ~]#systemctl status nftables.service
● nftables.service - Netfilter Tables
   Loaded: loaded (/usr/lib/systemd/system/nftables.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: man:nft(8)
[root@centos8 ~]#

Netfilter在内核中选取五个位置放了五个hook(勾子) function(INPUT、OUTPUT、FORWARD、 PREROUTING、POSTROUTING),而这五个hook function向用户开放,用户可以通过一个命令工具 (iptables)向其写入规则 由信息过滤表(table)组成,包含控制IP包处理的规则集(rules),规则被分组放在链(chain)上

提示:从 Linux kernel 4.2 版以后,Netfilter 在prerouting 前加了一个 ingress 勾子函数。可以使用这 个新的入口挂钩来过滤来自第2层的流量,这个新挂钩比预路由要早,基本上是 tc 命令(流量控制工 具)的替代品

三种报文流向

  • 流入本机:PREROUTING --> INPUT-->用户空间进程

  • 流出本机:用户空间进程 -->OUTPUT--> POSTROUTING

  • 转发:PREROUTING --> FORWARD --> POSTROUTING

iptables由五个表table和五个链chain以及一些规则组成

链(chain)

  • 内置链:每个内置链对应于一个钩子函数

  • 自定义链:用于对内置链进行扩展或补充,可实现更灵活的规则组织管理机制;只有 Hook 钩子调用自定义链时,才生效

五个内置链chain

  • INPUT

  • OUTPUT

  • FORWARD

  • PREROUTING

  • POSTROUTING

五个表table

  • filter:过滤规则表,根据预定义的规则过滤符合条件的数据包,默认表

  • nat:network address translation 地址转换规则表

  • mangle:修改数据标记位规则表

  • raw:关闭启用的连接跟踪机制,加快封包穿越防火墙速度

  • security:用于强制访问控制(MAC)网络规则,由Linux安全模块(如SELinux)实现

优先级由高到低的顺序为

security -->raw-->mangle-->nat-->filter

表和链对应关系

内核中数据包的传输过程

  • 当一个数据包进入网卡时,数据包首先进入PREROUTING链,内核根据数据包目的IP判断是否需要转送出去

  • 如果数据包是进入本机的,数据包就会沿着图向下移动,到达INPUT链。数据包到达INPUT链后, 任何进程都会收到它。本机上运行的程序可以发送数据包,这些数据包经过OUTPUT链,然后到达 POSTROUTING链输出

  • 如果数据包是要转发出去的,且内核允许转发,数据包就会向右移动,经过FORWARD链,然后到 达POSTROUTING链输出

查看 iptables 表

[root@centos8 ~]#iptables -vnL -t filter
[root@centos8 ~]#iptables -vnL -t nat
[root@centos8 ~]#iptables -vnL -t mangle
[root@centos8 ~]#iptables -vnL -t raw
[root@centos8 ~]#iptables -vnL -t security

# CentOS 6 nat表不支持INPUT链
[root@centos6 ~]#iptables -vnL -t nat

3 iptables

3.1.1 iptables 规则组成

规则rule:根据规则的匹配条件尝试匹配报文,对匹配成功的报文根据规则定义的处理动作作出处理,

规则在链接上的次序即为其检查时的生效次序

匹配条件:默认为与条件,同时满足

基本匹配:IP,端口,TCP的Flags(SYN,ACK等)

扩展匹配:通过复杂高级功能匹配

处理动作:称为target,跳转目标

  • 内建处理动作:ACCEPT,DROP,REJECT,SNAT,DNAT,MASQUERADE,MARK,LOG…

  • 自定义处理动作:自定义chain,利用分类管理复杂情形

规则要添加在链上,才生效;添加在自定义链上不会自动生效

白名单:只有指定的特定主机可以访问,其它全拒绝(生产环境建议)

# 白名单,没有被明确允许的都拒绝

iptables -A INPUT -s 10.0.0.10 -j ACCETP                # 允许本机(windows)机器

iptables -A INPUT -i lo -j ACCEPT                      # 允许本地回环地址

iptables -A INPUT -j REJECT                             # 默认全部拒绝

iptables -I INPUT 2 -s 10.0.0.12 -p icmp -j REJECT      # 拒绝 10.0.0.12 ping 本机

黑名单:只有指定的特定主机拒绝访问,其它全允许,默认方式

3.1.2 iptables规则添加时考量点

  • 要实现哪种功能:判断添加在哪张表上

  • 报文流经的路径:判断添加在哪个链上

  • 报文的流向:判断源和目的

  • 匹配规则:业务需要

3.1.3 环境准备

为了防止 iptables 和 firewalld 出现冲突,我们可以关闭 firewalld

CentOS7+

systemctl stop firewalld.service
systemctl disable  firewalld. service
# 或者
systemctl disable --now firewalld. service

CentOS6

service iptables stop
chkconfig iptables off

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章