文章目录

什么是防火墙

一道保护性的安全屏障

作用是保护和隔离的作用

RHEL7默认使用的是firewalld作为防火墙软件

但是firewalld底层调用的还是包过滤防火墙iptables

iptables的表、链结构

iptables防火墙具有4表5链

  • iptables的4个表(区分大小写):

nat表(地址转换表)

filter表(数据过滤表)

raw表(状态跟踪表)

mangle表(包标记表)。

  • iptables的5个链(区分大小写):

INPUT链(入站规则)

OUTPUT链(出站规则)

FORWARD链(转发规则)

PREROUTING链(路由前规则)

POSTROUTING链(路由后规则)

四表五链概念

  • filter表——过滤数据包
  • Nat表——用于网络地址转换(IP、端口)
  • Mangle表——修改数据包的服务类型、TTL、并且可以配置路由实现QOS
  • Raw表——决定数据包是否被状态跟踪机制处理

  • INPUT链——进来的数据包应用此规则链中的策略
  • OUTPUT链——外出的数据包应用此规则链中的策略
  • FORWARD链——转发数据包时应用此规则链中的策略
  • PREROUTING链——对数据包作路由选择前应用此链中的规则(所有的数据包进来的时侯都先由这个链处理)
  • POSTROUTING链——对数据包作路由选择后应用此链中的规则(所有的数据包出来的时侯都先由这个链处理)

包过滤匹配流程

iptables防火墙规则的顺序非常重要,内核按顺序检查这些规则,如果发现有匹配的规则条目,则立刻执行相关动作,停止继续向下查找规则
如果所有的防火墙规则都未能匹配成功,则按照默认的策略处理。

iptables的使用方法

iptables命令格式

  • 程序位置
[root@proxy ~]# which iptables
  • 命令用法
iptables [-t 表名]  选项  [链名]   [条件]  [-j  目标操作]
  • 命令示例1:
[root@proxy ~]# ifconfig eth0 | grep netmask
inet 192.168.4.5 netmask 255.255.255.0 broadcast 192.168.4.255

[root@client ~]# ping 192.168.4.5 #未配置策略前,正常通信
PING 192.168.4.5 (192.168.4.5) 56(84) bytes of data.
64 bytes from 192.168.4.5: icmp_seq=1 ttl=64 time=0.267 ms
64 bytes from 192.168.4.5: icmp_seq=2 ttl=64 time=0.433 ms
  • 命令示例2:
[root@proxy ~]# iptables  -t  filter  -I  INPUT -p  icmp  -j  REJECT
# -t 指定 filter表
# -I INPUT 在INPUT链的开头插入
# -p icmp 匹配ICMP协议
# -j REJECT 动作为拒绝通过
#立即生效
[root@client ~]# ping 192.168.4.5
PING 192.168.4.5 (192.168.4.5) 56(84) bytes of data.
From 192.168.4.5 icmp_seq=1 Destination Port Unreachable
From 192.168.4.5 icmp_seq=2 Destination Port Unreachable
  • 命令示例3:
[root@proxy ~]# iptables -t filter -I  INPUT   -p  icmp  -j  ACCEPT
# -t 指定 filter表
# -I INPUT 在INPUT链的开头插入
# -p icmp 匹配ICMP协议
# -j ACCEPT 允许数据包通过/放行
[root@client ~]# ping 192.168.4.5
PING 192.168.4.5 (192.168.4.5) 56(84) bytes of data.
64 bytes from 192.168.4.5: icmp_seq=1 ttl=64 time=0.305 ms
64 bytes from 192.168.4.5: icmp_seq=2 ttl=64 time=0.322 ms
  • 命令示例4:
[root@proxy ~]# iptables  -I  INPUT  -p  icmp  -j  REJECT
#可以不指定表,默认为filter表
[root@client ~]# ping 192.168.4.5
PING 192.168.4.5 (192.168.4.5) 56(84) bytes of data.
From 192.168.4.5 icmp_seq=1 Destination Port Unreachable

注意事项

  • 可以不指定表,默认为filter表
  • 可以不指定链,默认为对应表的所有链
  • 如果没有匹配的规则,则使用防火墙的默认规则
  • 选项/链名/目标操作用大写字母,其余都是小写

基本的目标操作

  • ACCEPT:允许数据包通过/放行
  • DROP:直接丢弃,不给出任何回应
  • REJECT:拒绝通过,必要时会给出提示
  • LOG:将数据包信息记录syslog日志,然后传给下一条规则
  • DNAT:目标地址转换
  • SNAT:源地址转换
  • MASQUERADE:地址欺骗
  • REDIRECT:重定向

常用的管理选项

添加规则

-A:在链的末尾追加一条规则

-I:在链的开头(或指定序号)插入一条规则

查看规则

-L:列出所有的规则条目
-n:以数字形式显示地址、端口等信息
–line-numbers:查看规则时,显示规则的序号

删除规则

-D:删除链内指定序号(或内容)的一条规则
-F:清空所有的规则

替换规则

-R:替换防火墙规则

默认策略

-P:为指定链设置默认规则

其他选项

-Z:清空防火墙数据表统计信息

匹配参数

参数

功能描述

[!] -p

匹配协议,!代表取反

[!] -s

匹配源地址

[!] -d

匹配目标地址

[!] -i

匹配入站网卡接口

[!] -o

匹配出站网卡接口

[!] --sport

匹配源端口

[!] --dport

匹配目标端口

[!] --src–range

匹配源地址范围

[!] --dst-range

匹配目标地址范围

[!] --limit

匹配数据表速率

[!] --mac-source

匹配源MAC地址

[!] --sports

匹配源端口

[!] --dports

匹配目标端口

[!] --state

匹配状态(INVALID、ESTABLISHED、NEW、RELATED)

[!] --string

匹配应用层子串

iptables基本使用

关闭firewalld,启动iptables服务

[root@proxy ~]# systemctl stop firewalld.service 
[root@proxy ~]# systemctl disable firewalld.service

查看filter表的所有规则

[root@proxy ~]# iptables -nL      #可以不指定表,默认为filter表

查看nat表的所有规则

[root@proxy ~]# iptables -t nat -nL

清空filter表中的所有规则

[root@proxy ~]# iptables -F

追加写入规则

[root@proxy ~]# iptables  -t  filter  -A  INPUT  -p tcp  -j  ACCEPT
#追加规则至filter表中的INPUT链的末尾,允许任何人使用TCP协议访问本机

插入规则

[root@proxy ~]# iptables  -I  INPUT  -p  udp  -j  ACCEPT
#插入规则至filter表中的INPUT链的开头,允许任何人使用UDP协议访问本机
[root@proxy ~]# iptables -I INPUT 2 -p icmp -j ACCEPT
#插入规则至filter表中的INPUT链的第2行,允许任何人使用ICMP协议访问本机

查看INPUT链的规则

[root@proxy ~]#  iptables  -nL  INPUT     #仅查看INPUT链的规则
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0
ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0
[root@proxy ~]# iptables -L INPUT --line-numbers #查看规则,显示行号
Chain INPUT (policy ACCEPT)
num target prot opt source

删除规则

[root@proxy ~]# iptables  -D  INPUT  3
#删除filter表中INPUT链的第3条规则
[root@proxy ~]# iptables -nL INPUT #查看规则,确认是否删除
[root@proxy ~]# iptables -F
#清空filter表中所有链的防火墙规则
[root@proxy ~]# iptables -t nat -F
#清空nat表中所有链的防火墙规则
[root@proxy ~]# iptables -t mangle -F
#清空mangle表中所有链的防火墙规则
[root@proxy ~]# iptables -t raw -F
#清空raw表中所有链的防火墙规则

设置防火墙默认规则

[root@proxy ~]# iptables  -t  filter  -P  INPUT  DROP
[root@proxy ~]# iptables -nL #此时ssh远程连接会断开
Chain INPUT (policy DROP)
target prot opt source destination

Chain FORWARD (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source