linux防火墙过滤技术iptables的原理及操作命令详解( 二 )


FORWARD链 的规则可以存在于:mangle表,filter表 。
OUTPUT链 的规则可以存在于:raw表,mangle表,nat表,filter表 。
POSTOUTING链 的规则可以存在于:mangle表,nat表 。
但是,我们在实际的使用过程中,往往是通过"表"作为操作入口,对规则进行定义的,之所以按照上述过程介绍iptables,是因为从"关卡"的角度更容易从入门的角度理解,但是为了以便在实际使用的时候,更加顺畅的理解它们,我们还要将各"表"与"链"的关系罗列出来:
表(功能)<--> 链(钩子):
filter表<-->三个链:INPUT、FORWARD、OUTPUT
作用:过滤数据包 内核模块:iptables_filter.
Nat表<-->三个链:PREROUTING、POSTROUTING、OUTPUT
作用:用于网络地址转换(IP、端口) 内核模块:iptable_nat
Mangle表<-->五个链:PREROUTING、POSTROUTING、INPUT、OUTPUT、FORWARD
作用:修改数据包的服务类型、TTL、并且可以配置路由实现QOS内核模块
Raw表<-->两个链:OUTPUT、PREROUTING
作用:决定数据包是否被状态跟踪机制处理
iptables为我们定义了4张"表",当他们处于同一条"链"时,执行的优先级如下

linux防火墙过滤技术iptables的原理及操作命令详解

文章插图
优先级次序(由高而低):raw --> mangle --> nat --> filter
优先级次序(由高而低):raw --> mangle --> nat --> filter
iptables操作查看filter的详细规则:filter表<-->三个链:INPUT、FORWARD、OUTPUT
————————————————
[root@xing Desktop]# iptables -t filter -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
注:OUTPUT 出站链
pkts bytes target prot opt in out source destination
0 0 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED(三次握手状态)
0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0
0 0 ACCEPT all -- lo * 0.0.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(目标端口22)
2 318 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited(默认拒绝所有)
———————
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
注:FORWARD转发规则链(当源IP地址以及目标IP地址都不是本机的时候使用的规则)
pkts bytes target prot opt in out source destination
0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
———————
Chain OUTPUT (policy ACCEPT 4 packets, 240 bytes)
注:OUTPUT 出站链
pkts bytes target prot opt in out source destination
————————————————
iptables的基本语法格式iptables [-t 表名] 命令选项 [链名] [匹配条件] [-j 控制类型]
iptables 【-t filter】 -I 【INPUT】 【-p tcp --dport 80】 【-j ACCEPT】
命令选项:
————
添加新的规则
  • -A 在链的末尾追加一条规则
  • -I 在链的开头(或指定序号)插入一条规则
————
查看规则列表
  • -L 列出所有规则条目
  • -n 数字的形式显示地址、端口信息
  • -v 以更详细的方式显示规则信息
  • --line-number 查看规则时,显示规则的序号
[root@xing Desktop]# iptables -t filter -nvL --line-number
————
删除、清空规则
  • -D 删除链内指定序号(或内容)的一条规则
  • -F 清空所有的规则
[root@xing Desktop]# iptables -D FORWARD 2
[root@xing Desktop]# iptables -F(清空所有规则)
[root@xing Desktop]# service iptables restart
iptables: Setting chains to policy ACCEPT: filter [ OK ]
iptables: Flushing firewall rules: [ OK ]
iptables: Unloading modules: [ OK ]
iptables: Applying firewall rules: [ OK ]
————
设置默认策略
-P :设置默认策略的(默认策略一般只有两种 。默认是关的/默认是开的)
iptables -P INPUT (DROP|ACCEPT)
[root@xing Desktop]# iptables -P FORWARD DROP
———————
控制类型
ACCEPT 允许通过
DOROP 直接丢弃,不给出任何提示


推荐阅读