The principle of firewall iptables under linux platform (transfer)

Introduction to iptables

    Netfilter/iptables (abbreviated as iptables) constitutes a packet filtering firewall under the Linux platform. Like most Linux software, this packet filtering firewall is free. It can replace expensive commercial firewall solutions to complete packet filtering and packet redirection. and Network Address Translation (NAT).

iptables basics


    Rules (rules) are actually pre-defined conditions by network administrators. The rules are generally defined as "if the packet header meets such conditions, process the packet like this". Rules are stored in the packet filtering table in kernel space. These rules specify source address, destination address, transport protocol (such as TCP, UDP, ICMP) and service type (such as HTTP, FTP, and SMTP), etc. When packets match the rules, iptables processes the packets according to the methods defined by the rules, such as accept, reject, and drop. The main job of configuring a firewall is to add, modify, and delete these rules.

The relationship between iptables and netfilter:

    This is the first place to say, the relationship between Iptables and netfilter is a very confusing issue. Many people know iptables but not netfilter. In fact, iptables is just a management tool for Linux firewalls, located in /sbin/iptables. What really implements the firewall function is netfilter, which is an internal structure that implements packet filtering in the Linux kernel.

The process of iptables transmitting packets

① When a data packet enters the network card, it first enters the PREROUTING chain, and the kernel determines whether it needs to be forwarded according to the destination IP of the data packet. 
② If the data packet is entering the machine, it will move down the graph to reach the INPUT chain. Once the packet reaches the INPUT chain, any process will receive it. Programs running on this machine can send data packets, which will go through the OUTPUT chain and then reach the POSTROUTING chain output. 
③ If the data packet is to be forwarded and the kernel allows forwarding, the data packet will move to the right as shown in the figure, pass through the FORWARD chain, and then reach the POSTROUTING chain output.




The rule table and chain of iptables:

    Tables provide specific functions. iptables has 4 built-in tables, namely filter table, nat table, mangle table and raw table, which are used to implement packet filtering, network address translation, packet reconstruction (modification) and data tracking processing respectively. .

   Chains are the paths through which data packets travel. Each chain is actually a checklist among many rules, and each chain can have one or several rules. When a packet arrives in a chain, iptables checks from the first rule in the chain to see if the packet meets the conditions defined by the rule. If it is satisfied, the system will process the packet according to the method defined by the rule; otherwise, iptables will continue to check the next rule. If the packet does not meet any of the rules in the chain, iptables will follow the pre-defined default of the chain. Policy to process packets.

    Iptables adopts a hierarchical structure of "tables" and "chains". In REHL4 are three tables and five chains. Now REHL5 has become four tables and five chains, but the extra table is not used too much, so it is basically the same as before. The four tables and five chains are listed below. Note that you must understand the relationship and function of these tables and chains.



Rules table:

1.filter表——三个链:INPUT、FORWARD、OUTPUT
作用:过滤数据包  内核模块:iptables_filter.
2.Nat表——三个链:PREROUTING、POSTROUTING、OUTPUT
作用:用于网络地址转换(IP、端口) 内核模块:iptable_nat
3.Mangle表——五个链:PREROUTING、POSTROUTING、INPUT、OUTPUT、FORWARD
作用:修改数据包的服务类型、TTL、并且可以配置路由实现QOS内核模块:iptable_mangle(别看这个表这么麻烦,咱们设置策略时几乎都不会用到它)
4.Raw表——两个链:OUTPUT、PREROUTING
作用:决定数据包是否被状态跟踪机制处理  内核模块:iptable_raw
(这个是REHL4没有的,不过不用怕,用的不多)

规则链:


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


规则表之间的优先顺序:

Raw——mangle——nat——filter
规则链之间的优先顺序(分三种情况):

第一种情况:入站数据流向

    从外界到达防火墙的数据包,先被PREROUTING规则链处理(是否修改数据包地址等),之后会进行路由选择(判断该数据包应该发往何处),如果数据包的目标主机是防火墙本机(比如说Internet用户访问防火墙主机中的web服务器的数据包),那么内核将其传给INPUT链进行处理(决定是否允许通过等),通过以后再交给系统上层的应用程序(比如Apache服务器)进行响应。

第二冲情况:转发数据流向

    来自外界的数据包到达防火墙后,首先被PREROUTING规则链处理,之后会进行路由选择,如果数据包的目标地址是其它外部地址(比如局域网用户通过网关访问QQ站点的数据包),则内核将其传递给FORWARD链进行处理(是否转发或拦截),然后再交给POSTROUTING规则链(是否修改数据包的地址等)进行处理。

第三种情况:出站数据流向
     防火墙本机向外部地址发送的数据包(比如在防火墙主机中测试公网DNS服务器时),首先被OUTPUT规则链处理,之后进行路由选择,然后传递给POSTROUTING规则链(是否修改数据包的地址等)进行处理。

管理和设置iptables规则

 

iptables命令格式(较为复杂)

iptables    [-t   table]    command    [chain]    [rules]    [-j   target]
(1)table ------- 指定表名(raw表、mangle表、nat表、filter表)
(2)command ------- 对链的操作命令(-A:追加规则(最下面进行追加规则)、-I:插入(一般在相应的哪条规则前后插入))
(3)chain ------- 链名(prerouting链、forward链、input链、output链、postrouting链)
(4)rules ------- 规则
(5)target ------- 动作如何进行

 

1、表选项table

 表选项用于指定命令应用于哪个iptables内置表,iptables内置包括:filter表、nat表、mangle表和raw表
使用的参数为: -t  +  表名,如果不使用-t参数,那么默认是使用filter表

2、命令选项command

命令

说明

-P或--policy + <链名>

定义默认策略

-L或--list + <链名>

查看iptables规则列表

-A或--append + <链名> 

在规则列表的最后增加1条规则

-I或--insert + <链名>

在指定的位置插入1条规则

-D或--delete + <链名> + number

从规则列表中删除1条规则

-R或--replace + <链名> + number

替换规则列表中的某条规则

-F或--flush + <链名>
-X或--delete-chain + <用户自定义的链名>

删除表中所有规则(注意:无法清空默认规则)
删除用户自定义的链(前提:这些用户自定义的链上必须没有任何规则,所以删除用户自定义链时,必须先删除该用户定义的链上的所有规则),如果不跟用户自定义的链名,那么将删除所有用户自定义的链)
以下几个例子
(1)iptables  -t  filter  -F,清除filter表中的所有规则(包括系统内置的链上的所有规则,以及用户自定义的链上的所有规则)
(2)iptables  -t  filter  -F  INPUT,清除filter表中的INPUT链上的所有规则
(3)iptables -t  filter  -X,删除filter表中所有用户自定义链(系统内置的链:INPUT、OUTPUT、FOWRARD..无法清除)
(4)iptables -t  filter  -X  mydefine,删除filter表中自定义的链mydefine

-Z或--zero + <链名>
-N或 --new-chain + <用户自定义的链名>

将表中数据包计数器和流量计数器归零
新建一个用户自定义的链,且链名必须跟内置那些链名不同
例如:iptables  -t  filter  -N  "CentOS-TCP" ,在filter这个表中新增一个名为"CentOS-TCP"的链,该链用于处理TCP数据包的;以后我们把相应的规则写到CentOS-TCP链里
例如:iptables  -t  filter  -N  "CentOS-UDP" ,在filter这个表中新增一个名为"CentOS-UDP"的链,该链用于处理UDP数据包的;以后我们把相应的规则写到CentOS- UDP链里
最后,运用时,我们可以把它们运用到内置那些表的INPUT链或者其它内置的链里,如
iptables  -t  filter  -A  INPUT  -p  tcp  -j  CentOS-TCP
iptables  -t  filter  -A  INPUT  -p  udp  -j  CentOS-UDP

 

3、匹配选项(rules)

匹配

说明

-i或--in-interface + <网络接口名>

指定数据包从哪个网络接口进入,如ppp0、eth0和eth1等
注意:该参数只能用于INPUT,  FORWARD  and PREROUTING这三个链

-o或--out-interface + <网络接口名>

指定数据包从哪块网络接口输出,如ppp0、eth0和eth1等
注意:该参数只能用于OUTPUT,  POSTROUTING这两个链

-p或---prot + < 协议类型>

指定数据包匹配的协议,如TCP、UDP和ICMP等

-s或--source + <源地址或子网>
-m  state  --state + 状态

-m  mac   --mac-source + MAC地址

-m  limit   --limit
-m  owner

指定数据包匹配的源地址
基于状态的匹配(状态可以是:NEW,ESTABLISHED,INVALID,RELATED中的一个或者它们的组合)
基于MAC地址的匹配(注意:该参数只能用于PREROUTING, FORWARD or INPUT这三个链)
基于封包数量的匹配
基于uid、gid的匹配(注意:该参数只对OUTPUT链有效)

--sport + <源端口号>

指定数据包匹配的源端口号,可以使用 "起始端口号:结束端口号" 的格式指定一个范围的端口

-d或--destination + <目标地址或子网>

指定数据包匹配的目标地址

--dport + 目标端口号

指定数据包匹配的目标端口号,可以使用 "起始端口号:结束端口号" 的格式指定一个范围的端口

 

4、动作选项(-j  target)

动作

说明

ACCEPT

接受数据包

DROP

丢弃数据包

REDIRECT

与DROP基本一样,区别在于它除了阻塞包之外, 还向发送者返回错误信息

SNAT 

源地址转换,即改变数据包的源地址
例如:将局域网的IP(10.0.0.1/24) ==> 广域网的IP(222.101.98.54/24),且在NAT表的POSTROUTING链上进行该动作

DNAT

目标地址转换,即改变数据包的目的地址
例如:将的广域网IP(222.101.98.54/24) ==> 局域网的IP(10.0.0.1/24),且在NAT表的PREROUTING链上进行该动作

MASQUERADE + IP

伪装,即是常说的NAT技术,MASQUERADE只能用于ADSL等拨号上网的IP伪装,也就是主机的IP是由ISP分配动态的;如果主机的IP地址是静态固定的,就要使用SNAT

LOG

日志功能,将符合规则的数据包的相关信息记录在日志中,以便管理员的分析和排错                      

 

 

 

二、iptables的语法

1.定义默认策略

1)作用:当数据包不符合链中任一条规则时,iptables将根据该链预先定义的默认策略来处理数据包

2)默认策略的定义格式: iptables   [-t  表名]   <-P>   <链名>   <动作>
参数说明如下:
(1)[-t   表名]
指默认策略将应用于哪个表,可以使用filter、nat和mangle,如果没有指定使用哪个表,iptables就默认使用filter表
(2)<-P>
定义默认策略
(3)<链名>
指默认策略将应用于哪个链,可以使用INPUT、OUTPUT、FORWARD、PREROUTING、OUTPUT和POSTROUTING
(4)<动作>
处理数据包的动作,可以使用ACCEPT(接受数据包)和DROP(丢弃数据包) 

2.查看iptables规则

 

查看iptables规则的命令格式为:iptables   [-t  表名]   <-L>   [链名]
参数说明如下:
(1)[-t  表名]
指查看哪个表的规则列表,表名用可以使用filter、nat和mangle,如果没有指定使用哪个表,iptables就默认查看filter表的规则列表
(2)<-L>
查看指定表和指定链的规则列表
(3)[链名]
指查看指定表中哪个链的规则列表,可以使用INPUT、OUTPUT、FORWARD、PREROUTING、OUTPUT和POSTROUTING,如果不指明哪个链,则将查看某个表中所有链的规则列表

3.增加、插入、删除、替换iptables规则

iptables  [-t表名]  <-A | I | D | R>  链名  [规则编号]  [-i | o 网卡名称]  [-p 协议类型]  [-s 源IP地址 | 源子网]  [--sport 源端口号]  [-d目标IP地址 | 目标子网]  [--dport目标端口号]  <-j动作>

例如:
iptables -t  filter  -I   INPUT  3   -p  tcp  --dport   80  -j  ACCEPT(#在INPUT链第3条规则之前插入)
iptables -t  filter  -I   INPUT  -p  tcp  --dport  80  -j  ACCEPT (#在INPUT链规则的第1条规则之前插入)
iptables -t  filter  -R   INPUT  3 -p tcp   --dport  22  -j   ACCEPT

参数说明如下:
(1)[-t 表名]
定义默认策略将应用于哪个表,可以使用filter、nat和mangle,如果没有指定使用哪个表,iptables就默认使用filter表
(2)-A
新增加一条规则,该规则将会增加到规则列表的最后一行,该参数不能使用规则编号
(3)-I
插入一条规则,原本该位置上的规则将会往后顺序移动,如果没有指定规则编号,则在第一条规则前插入
(4)-D
从规则列表中删除一条规则,可以输入完整规则,或直接指定规则编号加以删除
(5)-R
替换某条规则,规则被替换并不会改变顺序,必须要指定替换的规则编号
(6)<链名>
指定查看指定表中哪个链的规则列表,可以使用INPUT、OUTPUT、FORWARD、PREROUTING、OUTPUT和POSTROUTING
(7)[规则编号]
规则编号用于插入、删除和替换规则时用,编号是按照规则列表的顺序排列,规则列表中第一条规则的编号为1
(8)[-i | o 网卡名称]
i是指定数据包从哪块网卡进入,o是指定数据包从哪块网卡输出。网卡名称可以使用ppp0、eth0和eth1等
(9)[-p 协议类型]
可以指定规则应用的协议,包含TCP、UDP和ICMP等
(10)[-s 源IP地址 | 源子网]
源主机的IP地址或子网地址
(11)[--sport 源端口号]
数据包的IP的源端口号
(12)[-d目标IP地址 | 目标子网]
目标主机的IP地址或子网地址
(13)[--dport目标端口号]
数据包的IP的目标端口号
(14)<-j动作>
处理数据包的动作,各个动作的详细说明可以参考前面的说明

4、清除规则和计数器

在新建规则时,往往需要清除原有的、旧的规则,以免它们影响新设定的规则。如果规则比较多,一条条删除就会十分麻烦,这时可以使用iptables提供的清除规则参数达到快速删除所有的规则的目的。
定义参数的格式为:iptables   [-t  表名]  <-F | Z>
参数说明如下:
(1)[-t 表名]
指定默认策略将应用于哪个表,可以使用filter、nat和mangle,如果没有指定使用哪个表,iptables就默认使用filter表
(2)-F
删除指定表中所有规则
(3)-Z
将指定表中的数据包计数器和流量计数器归零

 

三、NAT

1、什么是NAT

2、NAT的类型

1) 静态NAT(Static NAT,SNAT)(局域网的IP==>广域网的IP)
静态NAT设置起来最为简单和最容易实现的一种,内部网络中的每个主机都被永久映射成外部网络中的某个合法的地址

2)动态地址NAT(Pooled NAT,DNAT)(广域网的IP==>局域网的IP)
(1)动态地址NAT是在外部网络中定义了一系列的合法地址,采用动态分配的方法映射到内部网络
(2)动态地址NAT只是转换IP地址,它为每一个内部的IP地址分配一个临时的外部IP地址,主要应用于拨号,对于频繁的远程联接也可以采用动态NAT

3)网络地址端口转换NAPT(Port-Level NAT)
NAPT是把内部地址映射到外部网络的一个IP地址的不同端口上
最熟悉的一种转换方式。NAPT普遍应用于接入设备中,它可以将中小型的网络隐藏在一个合法的IP地址后面。NAPT与动态地址NAT不同,它将内部连接映射到外部网络中的一个单独的IP地址上,同时在该地址上加上一个由NAT设备选定的TCP端口号

 

三、规则走向        规则的应用顺序:从上到下的,最后才是默认规则

四、INPUT与OUTPUT区别

 

对于input而言
--dport -d都是指你自己的端口和地址
--sport和-s指的是发起连接者的端口和地址
对于output而言
--sport -s都是指你自己的端口和地址
--dport -d指的dest地址

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327016140&siteId=291194637