防火墙概述

Linux的防火墙工作在网络层,针对TCP/IP数据包进行过滤和限制,是典型的包过滤防火墙(网络层防火墙)。
在许多安全资料中,netfilter和iptables都用来指Linux防火墙,区别如下:

netfilter:指的是Linux内核中实现包过滤防火墙的内部结构,不以程序或文件的形式存在,属于“内核态”的防火墙。
iptables:指的是用来管理Linux防火墙的命令程序,通常位于/sbin/iptables目录下,属于“用户态”的防火墙。(常用iptables定义防火墙规则)

iptables的表、链结构

按照功能iptables表分为四个,分别是raw、mangle、nat、filter,用途如下:

raw:主要用来对数据包进行状态跟踪。包含两个规则链(OUTPUT、PREROUTING)
mangle:主要对数据包进行标记。包含五个规则链(PREROUTING、POSTROUTING、INPUT、OUTPUT、FORWARD)
nat:主要对数据包中的源、目标IP地址或端口进行修改。包含三个规则链(PERROUTING、POSTROUTING、OUTPUT)
filter:确定是否放行该数据包(过滤)。包含三个规则链(INPUT、FORWARD、OUTPUT)
五种规则链不同的介入时机如下:
INPUT链:当收到访问防火墙本机地址的数据包(入站)时,应用此链的规则。
OUTPUT链:当防火墙本机向外发送数据包(出站)时,应用测链的规则。
FORWARD链:当接受到需要通过防火墙中转发送给其他地址的数据包(转发)时,应用此链的规则。
PREROUTING链:在对数据包做路由选择之前,应用此链的规则。
POSTROUTING链:在对数据包做路由选择之后,应用此链的规则。
其中INPUT、OUTPUT链主要用在“主机型防火墙”中,意思是主要针对服务器本身进行保护的防火墙;FORWARD、PREROUTNIG、POSTROUTING链多用在“网络型防火墙”中,如使用Linux防火墙作为网关服务器,在公司的内网与公网之间进行安全控制。

数据包过滤的匹配流程

规则表之间的顺序

raw→mangle→nat→filter

规则链之间的顺序

入站:PREROUTING→INPUT
转发:PREROUTING→RORWARD→POSTROUTING
出站:OUTPUT→POSTROUTING
规则链内各条防火墙规则之间的顺序

按顺序依次检查,匹配即停止(LOG策略例外)
如果都不匹配,则按该链的默认策略处理(默认放通)

iptables安装

Centos 7 默认使用firewalld防火墙,若要使用iptables防火墙,必须先关闭firewalld防火墙

关闭firewalld防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service
安装iptables防火墙
yum -y install iptables iptables-services
设置iptables防火墙开机自启动
systemctl start iptables.service
systemctl enable iptables.service

iptables 基本语法

iptables [ -t 表名 ] 选项 [ 链名 ] [ 条件 ] [-j 控制类型]

注意事项

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

常见的控制类型

ACCEPT:允许通过
DROP:丢弃,不做任何回应
REJECT:拒绝通过,必要时会给提升
LOG:记录日志信息,然后传给下一条规则继续匹配

常用的选项

-A:在指定链末尾追加一条 示例:iptables -A INPUT
-I(大写i):在指定链中插入一条新的,未指定序号默认作为第一条 iptables -I INPUT
-P:指定默认规则 示例:iptables -P OUTPUT ACCEPT
-D:删除 示例:iptables -t nat -D INPUT
-R:修改、替换某一条规则 示例:iptables -t nat -R INPUT
-L:查看 示例:iptables -t nat -L
-n:所有字段以数字形式显示(比如任意ip地址是0.0.0.0而不是anywhere,比如显示协议端口号而不是服务名)
示例:iptables -L -n、iptables -nL、iptables -vnL
-v:查看时显示更详细信息,常跟-L起使用
–line-number规则带编号 示例:iptables -t nat -l -n --line-number 、iptables -t nat -L --line-number
-F:清除链中所有规则 示例:iptables -F
-X:清空自定义链的规则,不影响其他链 示例:iptables -X
-Z:清空链的计数器(匹配到的数据包的大小和总和) 示例:iptables -z
-S:查看链的所有规则或者某个链的规则/某个具体规则后面跟编号 示例:iptables -t nat -s.、iptables -t nat -S POSTROUTING 1

[root@localhost ~]# iptables -t filter -L //查看filter表的所有链的防火墙规则
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     udp  --  anywhere             anywhere             udp dpt:domain
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:domain
ACCEPT     udp  --  anywhere             anywhere             udp dpt:bootps
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:bootpsChain FORWARD (policy ACCEPT)
target     prot opt source               destination
ACCEPT     all  --  anywhere             192.168.122.0/24     ctstate RELATED,ESTABLISHED
ACCEPT     all  --  192.168.122.0/24     anywhere
ACCEPT     all  --  anywhere             anywhere
REJECT     all  --  anywhere             anywhere             reject-with icmp-port-unreachable
REJECT     all  --  anywhere             anywhere             reject-with icmp-port-unreachableChain OUTPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     udp  --  anywhere             anywhere             udp dpt:bootpc[root@localhost ~]# iptables -t filter -L INPUT //只查看filter表中的INPUT链的防火墙规则
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     udp  --  anywhere             anywhere             udp dpt:domain
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:domain
ACCEPT     udp  --  anywhere             anywhere             udp dpt:bootps
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:bootps
[root@localhost ~]# iptables -t filter -L INPUT --line-number //显示序号
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination
1    ACCEPT     udp  --  anywhere             anywhere             udp dpt:domain
2    ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:domain
3    ACCEPT     udp  --  anywhere             anywhere             udp dpt:bootps
4    ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:bootps
[root@localhost ~]# iptables -t filter -L INPUT --line-number -n //以数字显示
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination
1    ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0            udp dpt:53
2    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:53
3    ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0            udp dpt:67
4    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:67
[root@localhost ~]# iptables -t filter -nvL INPUT //显示详情
Chain INPUT (policy ACCEPT 6670 packets, 11M bytes)pkts bytes target     prot opt in     out     source               destination         0     0 ACCEPT     udp  --  virbr0 *       0.0.0.0/0            0.0.0.0/0            udp dpt:530     0 ACCEPT     tcp  --  virbr0 *       0.0.0.0/0            0.0.0.0/0            tcp dpt:530     0 ACCEPT     udp  --  virbr0 *       0.0.0.0/0            0.0.0.0/0            udp dpt:670     0 ACCEPT     tcp  --  virbr0 *       0.0.0.0/0            0.0.0.0/0            tcp dpt:67
[root@localhost ~]# iptables -t filter -D INPUT 1 //删除filter表INPUT链中第一条防火墙规则
[root@localhost ~]# iptables -t filter -L INPUT  //原本是四条记录,删除之后只剩下三条记录
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:domain
ACCEPT     udp  --  anywhere             anywhere             udp dpt:bootps
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:bootps
[root@localhost ~]# iptables -t filter -F //清空filter表中所有链的防火墙规则,如果只想清空指定链的规则,在-F之后加上链名
[root@localhost ~]# iptables -t filter -nvL
Chain INPUT (policy ACCEPT 16 packets, 1096 bytes)pkts bytes target     prot opt in     out     source               destination         Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)pkts bytes target     prot opt in     out     source               destination         Chain OUTPUT (policy ACCEPT 11 packets, 996 bytes)pkts bytes target     prot opt in     out     source               destination
[root@localhost ~]# iptables -t filter -P FORWARD DROP //设置filter表中FORWARD链的默认规则为直接丢弃数据包,不做回应
[root@localhost ~]# iptables -t filter -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         Chain FORWARD (policy DROP) //默认规则已修改
target     prot opt source               destination         Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

防火墙规则匹配

通用匹配

①、协议匹配 使用“ -p 协议名 ”的方式指定

[root@localhost ~]# iptables -t filter -A INPUT -p icmp -j REJECT //定义规则禁止所有主机ping本机
[root@localhost ~]# iptables -t filter -nL INPUT
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
[root@localhost ~]#

②、地址匹配 使用“ -s 源地址 ” 或者 “ -d 目标地址 ” 的形式指定

[root@localhost ~]# iptables -t filter -A FORWARD -s 192.168.177.110 -j REJECT //拒绝转发192.168.177.110的数据
[root@localhost ~]# iptables -t filter -nL FORWARD
Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
REJECT     all  --  192.168.177.110      0.0.0.0/0            reject-with icmp-port-unreachable
[root@localhost ~]#

③、网络接口匹配 使用 “ -i 接口名 ” 和 “ -o 接口名 ” 的形式指定,-i表示入站只可以在INPUT链中使用,-o表示出站只可以在OUTPUT链中使用

[root@localhost ~]# iptables -t filter -A INPUT -i ens33 -s 192.168.177.110 -j DROP //源ip192.168.177.110从网卡ens33进入的数据丢弃,不回应
[root@localhost ~]# iptables -t filter -A OUTPUT -o ens33 -s 192.168.177.120 -j DROP //源ip192.168.177.110从网卡ens33出去的数据丢弃,不回应
[root@localhost ~]# iptables -t filter -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       all  --  192.168.177.110      0.0.0.0/0           Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
REJECT     all  --  192.168.177.110      0.0.0.0/0            reject-with icmp-port-unreachableChain OUTPUT (policy ACCEPT)
target     prot opt source               destination
DROP       all  --  192.168.177.120      0.0.0.0/0
[root@localhost ~]# # 隐含匹配这种匹配方式需要以指定的协议匹配作为前提条件,无法独立使用。
①、端口匹配 使用“ --sport 源端口 ” 和 “ --dport 目标端口 ”的形式指定,针对的协议为TCP或UDP,端口范围用冒号“:”分隔。//允许网段192.168.177.0网段转发DNS数据包
[root@localhost ~]# iptables -t filter -A FORWARD -s 192.168.177.0/24 -p udp --dport 53 -j ACCEPT
[root@localhost ~]# iptables -t filter -A FORWARD -d 192.168.177.0/24 -p udp --sport 53 -j ACCEPT
[root@localhost ~]# iptables -t filter -nL FORWARD
Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
REJECT     all  --  192.168.177.110      0.0.0.0/0            reject-with icmp-port-unreachable
ACCEPT     udp  --  192.168.177.0/24     0.0.0.0/0            udp dpt:53
ACCEPT     udp  --  0.0.0.0/0            192.168.177.0/24     udp spt:53
[root@localhost ~]# ②、ICMP类型匹配 使用“ --icmp-type ICMP类型 ”的形式指定。针对的是ICMP协议,ICMP类型可以用字符串或者数字表示,对应关系是:Echo-Request=8、Echo-Reply=0、Destination-Unreachable=3,分别是请求、回应、不可达。[root@localhost ~]# iptables -t filter -A INPUT -p icmp --icmp-type 8 -j DROP //丢弃进来的请求包
[root@localhost ~]# iptables -t filter -A INPUT -p icmp --icmp-type 0 -j ACCEPT //允许进来的回应包
[root@localhost ~]# iptables -t filter -A INPUT -p icmp --icmp-type 3 -j ACCEPT //允许进来的不可达回应包
[root@localhost ~]# iptables -t filter -A INPUT -p icmp -j DROP //禁止ping本机
[root@localhost ~]# iptables -t filter -nL INPUT
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
DROP       icmp --  0.0.0.0/0            0.0.0.0/0            icmptype 8
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0            icmptype 0
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0            icmptype 3
DROP       icmp --  0.0.0.0/0            0.0.0.0/0
[root@localhost ~]#

显示匹配

需要有额外的内核模块提供支持,必须手动以 -m 模块名称 的形式调用相应的模块,然后才能设置匹配条件。lsmod命令可以查看相关的内核扩展模块
①、多端口匹配 使用 “-m multiport --dport 端口列表” “-m multiport --sport 端口列表” 的形式,用来检查数据包的源端口、目标端口

[root@localhost ~]# iptables -t filter -A INPUT -p tcp -m multiport --dport 25,80,110,143 -j ACCEPT//放通本机tcp协议的25 80 110 143 端口
[root@localhost ~]# iptables -t filter -nL INPUT
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
DROP       icmp --  0.0.0.0/0            0.0.0.0/0            icmptype 8
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0            icmptype 0
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0            icmptype 3
DROP       icmp --  0.0.0.0/0            0.0.0.0/0
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            multiport dports 25,80,110,143
[root@localhost ~]#

②、IP范围匹配 使用 “-m iprange --src-range IP范围” “-m iprange --dst-range IP范围” 的形式,用来检查数据包的源地址、目标地址,IP范围用“起始地址-结束地址”的形式表示。

[root@localhost ~]# iptables -F
[root@localhost ~]# iptables -t filter -A FORWARD -p tcp -m iprange --src-range 192.168.177.10-192.168.177.90 -j REJECT
[root@localhost ~]# iptables -t filter -nL FORWARD
Chain FORWARD (policy ACCEPT)
target     prot opt source               destination          //禁止IP段:192.168.177.10-90 TCP数据包的转发
REJECT     tcp  --  0.0.0.0/0            0.0.0.0/0            source IP range 192.168.177.10-192.168.177.90 reject-with icmp-port-unreachable
[root@localhost ~]#

③、MAC地址匹配 使用“-m mac --mac-source MAC地址”的形式,用来检查数据包的源MAC地址。由于MAC地址本身的局限性,此类匹配规则一般只适用于内部网络。

[root@localhost ~]# iptables -t filter -A INPUT -m mac --mac-source 00:0c:29:42:0e:7e -j DROP //禁止mac为00:0c:29:42:0e:7e访问本机的任何程序
[root@localhost ~]# iptables -t filter -nL INPUT
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP all – 0.0.0.0/0 0.0.0.0/0 MAC 00:0C:29:42:0E:7E
[root@localhost ~]#

④、状态匹配 使用“-m state --state 连接状态”的形式,基于iptables的状态跟踪机制用来检查数据包的连接状态(State)。常见的连接状态有:NEW(与任何连接无关的)、ESTABLISHED(响应请求或者已建立连接的)、RELATED(与已有连接有相关性的,如FTP数据连接)

[root@localhost ~]# iptables -F
[root@localhost ~]# iptables -t filter -A FORWARD -m state --state NEW -p tcp ! --syn -j DROP //禁止转发与tcp
连接无关的非syn请求数据包(如伪造的网络攻击数据包)
[root@localhost ~]# iptables -t filter -nL FORWARD
Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
DROP       tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp flags:!0x17/0x02
[root@localhost ~]#

小结

netfilter 是实现包过滤防火墙功能的内核机制(内核态),iptables是管理防火墙规则的用户态工具。
iptables的规则体系默认包括四个表(raw、mangle、nat、filter)五个链(PREROUTING、INPUT、FORWARD、POSTROUTING、OUTPUT)。
表的匹配顺序是:raw→mangle→nat→filter;链的匹配顺序取决于具体的数据流向;链内的规则遵循“匹配即停止”的原则(LOG策略除外)。
iptables规则的匹配条件类型包括通用匹配、隐含匹配、显示匹配、其中显示匹配必须以“ -m 模块名称 ”加载模块配合使用。

防火墙iptables相关推荐

  1. Linux下防火墙iptables用法规则详及其防火墙配置

    原博主文章更美丽: http://www.cnblogs.com/yi-meng/p/3213925.html iptables规则 规则--顾名思义就是规矩和原则,和现实生活中的事情是一样的,国有国 ...

  2. Linux防火墙iptables学习

    http://blog.chinaunix.net/uid-9950859-id-98277.html 要在网上传输的数据会被分成许多小的数据包,我们一旦接通了网络,会有很多数据包进入,离开,或者经过 ...

  3. linux 防火墙iptables简明教程

    前几天微魔部落再次遭受到个别别有用心的攻击者的攻击,顺便给自己充个电,复习了一下linux下常见的防火墙iptables的一些内容,但是无奈网上的很多教程都较为繁琐,本着简明化学习的目的,微魔为大家剔 ...

  4. 共创Linux防火墙,Linux防火墙iptables简明教程

    前几天微魔部落再次遭受到个别别有用心的攻击者的攻击,顺便给自己充个电,复习了一下linux下常见的防火墙iptables的一些内容,但是无奈网上的很多教程都较为繁琐,本着简明化学习的目的,微魔为大家剔 ...

  5. CentOS6.7防火墙(Iptables)的开启与关闭

    Linux防火墙(iptables)的开启与关闭 . Linux中的防火墙主要是对iptables的设置和管理. 1. Linux防火墙(Iptables)重启系统生效 开启: chkconfig i ...

  6. linux防火墙策略文件夹,Linux防火墙iptables的策略

    iptables策略 iptables -L #查看现有防火墙所有策略 iptables -F #清除现有防火墙策略 只允许特定流量通过,禁用其他流量 1.允许SSH流量(重要) iptables - ...

  7. linux防火墙--iptables(三)

    七.SNAT源地址转换 ·Source Network Address Translation ·修改数据包的源地址 ·仅用于nat表的POSTROUTING链 Example:局域网共享公网IP上网 ...

  8. linux 防火墙 iptables的简单使用

    linux 防火墙 iptables的简单使用 [root@web web]# uname -a Linux web 2.6.9-67.ELsmp #1 SMP Wed Nov 7 13:58:04 ...

  9. linux防火墙ip黑名单,【转】Linux防火墙(iptables)之黑名单

    iptables删除规则 So if you would like to delete second rule : iptables -D INPUT 2 ---------------------- ...

  10. Linux完全清除防火墙策略,linux下的软件防火墙iptables——规则的查看与清除、定义默认策略,...

    linux下的软件防火墙iptables--规则的查看与清除.定义默认策略软件防火墙IPTABLES --规则在Linux下查看和清除,定义默认策略,防火墙意味着用户限制某些ip或用户对其主机的访问. ...

最新文章

  1. Android -- Messenger与Service
  2. Android SearchView和ListView的结合使用
  3. 【bzoj4698】[Sdoi2008] Sandy的卡片 后缀数组
  4. Java语言程序设计(基础篇) 第十章 面向对象思考
  5. 预见2019:《2019年中国视频监控产业全景图谱》(附产业布局、政策环境、市场规模、发展趋势)
  6. Factory Method工厂方法
  7. java 生成枚举_为什么编译器在Java中生成Enums?
  8. Linux/Centos源码安装python3任意版本
  9. 利用Spring解耦VS的WebService
  10. c语言实现md5比java难_浅谈md5弱类型比较和强碰撞
  11. deep_learning 03. tf.nn.rnn_cell.MultiRNNCell()
  12. np.random.seed()
  13. csdn 积分/c币获取方法
  14. 笔记本电脑怎么拆开后盖_联想笔记本电脑怎么拆开后盖_联想笔记本怎么拆
  15. ATV930变频器转矩控制如何设置?
  16. HTML5制作诗歌锦集,短小优美的自创现代诗歌(精选5首)
  17. OpenCV-python 自制图片画框脚本
  18. 英文斜体复制_斜体英文转换器,斜体英文26个字母可复制
  19. 2022煤气上岗证题目及答案
  20. Ruby On Rails-2.0.2源代码分析(1)-Rails的启动

热门文章

  1. 【工大SCIR笔记】多模态信息抽取简述
  2. 【ACL 2019】腾讯AI Lab解读三大前沿方向及20篇入选论文
  3. AAAI 2019 使用循环条件注意力结构探索回答立场检测任务
  4. 20200216_re数据处理
  5. 机器学习面试-处理聚类问题
  6. 这份字节、阿里内部秘传的面经,被我搞到手了……
  7. Leetcode-树
  8. resnet18实现cifar10分类
  9. “对比Excel”系列再添新成员,手把手教你用Python实现报表自动化!
  10. Spark:超越Hadoop MapReduce