【网络教程】Iptables官方教程-学习笔记6-IPTABLES TARGETS

一、IPTABLES TAGRETS

本章节介绍Iptables 的目标和跳转(targets and jumps),目标和跳转负责告诉规则如何处理与规则匹配部分完全匹配的包。“ACCEPT”和“DROP”是一对基础目标,他们是首先处理的。在学习目标是如何完成之前,我们先看下跳转是如何完成的。

跳转跟目标类似,但是跳转是在同一个表的链之间进行跳转。要跳转到一个特定的链,前提是该链已存在。通过“-N”命令创建一个用户定义的链,如下所示:

iptables -N tcp_packets

设置一个跳转目标如下所示:

iptables -A INPUT -p tcp -j tcp_packets

表示我们将从INPUT链跳转到tcp_packets链,并开始遍历tcp_packets链。当我们到达该tcp_packets链的末端,我们就会回到INPUT链,数据包开始从它跳转到另一个链(tcp_packets),按接下来的规则进行遍历。如果一个数据包在一个子链中被接受,它将在超集链中也被接受,并且它将不再遍历任何超集链。但是,请注意,包将以正常方式遍历其他表中的所有其他链。更多信息参考: Traversing of tables and chains【网络教程】IPtables官方教程–学习笔记3

目标(targets)是指要对有问题的包采取的操作。例如,基于我们的需求,我们可以接受或丢弃数据包。跳转到目标的操作可能会导致不同的结果,某些目标可以将导致包停止遍历上面所述的特定链和上级链,比如DROP和ACCEPT。被停止的规则将不会在链中或更高级的链中通过接下来的任一规则。其他目标可能对数据包采取行动,之后数据包将继续通过其余的规则。一个很好的例子是LOG、ULOG和TOS目标。这些目标可以记录数据包,破坏它们,然后将它们传递给同一组链中的其他规则。例如,我们可能希望这样做,以便我们还可以同时破坏特定包/流的TTL和TOS值。一些目标将接受额外的选项(使用什么TOS值等),而其他目标不一定需要任何选项——但如果我们愿意,我们可以包括它们(日志前缀,伪装到端口等)。

下面就让我们来看看有IPTABLES有哪些目标。

1.1 ACCEPT target

这个目标不需要更多的选项。一旦完全满足数据包的匹配规范,并且指定ACCEPT为目标,则接受该规则,并且不再继续遍历当前链或同一表中的任何其他链。但是请注意,在一个链中接受的数据包仍然可能通过其他表中的链传输,并且仍然可能被丢弃。ACCEPT目标没有任何特别之处,它不需要也不可能向目标添加选项。要使用这个目标,只需指定-j ACCEPT。

注意:适配于Linux 内核2.3, 2.4, 2.5, 2.6

1.2 CLASSIFY target

分类目标可以被用于分类包的方式,可以被一对不同的qdisc(queue disciplines)使用。例如,atm、cbq、dsmark、pfifo_fast、htb和prio qdisc。更多请参考: Linux Advanced Routing and Traffic Control HOW-TO

分类目标仅在mangle表的POSTROUTING 链中有效

Option –set-class
Example iptables -t mangle -A POSTROUTING -p tcp --dport 80 -j CLASSIFY --set-class 20:10
Explanation The CLASSIFY target only takes one argument, the --set-class. This tells the target how to class the packet. The class takes 2 values separated by a coma sign, like this MAJOR:MINOR. Once again, if you want more information on this, check the Linux Advanced Routing and Traffic Control HOW-TO webpage.

1.3 CLUSTERRIP target

CLUSTERIP目标用于以轮询方式创建响应相同IP和MAC地址的简单节点集群。这是一种简单的集群形式,您在参与集群的所有主机上设置一个Virtual IP (VIP),然后在每个应该响应请求的主机上使用CLUSTERIP。CLUSTERIP匹配不需要特殊的负载平衡硬件或机器,它只是在机器集群的每个主机部分上执行它的工作。它是一个非常简单的集群解决方案,不适合大型和复杂的集群,它也没有内置心跳处理,但加心跳处理也可以用脚本实现。

集群中的所有服务器都为一个虚拟IP使用一个通用的多播MAC,然后在CLUSTERIP目标中使用一个特殊的散列算法来确定哪些集群参与者应该响应每个连接。多播MAC是指以01:00:5e作为前24位的MAC地址。一个多播MAC的例子是01:00:5e:00:00:20。虚拟IP可以是任何IP地址,但在所有主机上也必须是相同的。

记住,CLUSTERIP可能会破坏SSH等协议。连接将正常进行,但是如果您尝试同一时间再次连接到同一主机,您可能连接到集群中的另一台计算机,使用不同的秘钥集,因此您的ssh客户机可能拒绝连接或给您错误。由于这个原因,这在某些协议中不能很好地工作,添加可用于维护和管理的单独地址可能是一个好主意。另一种解决方案是在参与集群的所有主机上使用相同的SSH密钥。

集群可以使用三种哈希模式进行负载平衡。第一个是只有源IP (sourceip),第二个是源IP和源端口(sourceip-sourceport),第三个是源IP、源端口和目的端口(sourceip-sourceport-destport)。第一个可能是一个好主意,你需要记住连接之间的状态,例如一个web服务器的购物车保持连接之间的状态,这种负载平衡可能会变得有点不平衡——不同的机器可能比其他机器获得更高的负载,等等——因为来自同一源IP的连接将到相同的服务器。sourceip-sourceport散列可能是一个好主意,如果您希望负载平衡稍微均匀一点,并且不必在每个服务器上的连接之间保存状态。例如,一个大型信息网页加上一个简单的搜索引擎可能是一个好主意。第三种也是最后一种散列模式sourceip-sourceport-destport可能是个好主意,因为您的主机上运行着多个服务,不需要在连接之间保留任何状态。例如,这可能是同一主机上的一个简单的ntp、dns和www服务器。因此,到每个新目的地的每个连接都将被“重新协商”——实际上没有进行任何协商,它基本上只是一个轮询系统,每个主机接收一个连接。

每个CLUSTERIP 集群在/proc/net/ipt_CLUSTERIP路径下都有一个单独的文件,文件基于虚拟IP来存放。如果虚拟IP是192.168.0.5,我们可以cat /proc/net/ipt_CLUSTERIP/192.168.0.5 来查看该相应哪个节点。为了让这台机器为另一台机器应答,我们设节点2,使用echo “+2” >>/proc/net/ipt_CLUSTERIP / 192.168.0.5。要删除它,运行echo “-2” >>/proc/net/ipt_CLUSTERIP / 192.168.0.5。

Option –new
Example iptables -A INPUT -p tcp -d 192.168.0.5 --dport 80 -j CLUSTERIP --new …
Explanation This creates a new CLUSTERIP entry. It must be set on the first rule for a VIP, and is used to create a new cluster. If you have several rules connecting to the same CLUSTERIP you can omit the --new keyword in any secondary references to the same VIP.
Option –hashmode
Example iptables -A INPUT -p tcp -d 192.168.0.5 --dport 443 -j CLUSTERIP --new --hashmode sourceip …
Explanation The --hashmode keyword specifies the kind of hash that should be created. The hashmode can be any of the following three.
sourceip
sourceip-sourceport
sourceip-sourceport-destport
The hashmodes has been extensively explained above. Basically, sourceip will give better performance and simpler states between connections, but not as good load-balancing between the machines. sourceip-sourceport will give a slightly slower hashing and not as good to maintain states between connections, but will give better load-balancing properties. The last one may create very slow hashing that consumes a lot of memory, but will on the other hand also create very good load-balancing properties.
Option –clustermac
Example iptables -A INPUT -p tcp -d 192.168.0.5 --dport 80 -j CLUSTERIP --new --hashmode sourceip --clustermac 01:00:5e:00:00:20 …
Explanation The MAC address that the cluster is listening to for new connections. This is a shared Multicast MAC address that all the hosts are listening to. See above for a deeper explanation of this.
Option –total-nodes
Example iptables -A INPUT -p tcp -d 192.168.0.5 --dport 80 -j CLUSTERIP --new --hashmode sourceip --clustermac 01:00:5e:00:00:20 --total-nodes 2 …
Explanation The --total-nodes keyword specifies how many hosts are participating in the cluster and that will answer to requests. See above for a deeper explanation.
Option –local-node
Example iptables -A INPUT -p tcp -d 192.168.0.5 --dport 80 -j CLUSTERIP --new --hashmode sourceip --clustermac 01:00:5e:00:00:20 --total-nodes 2 --local-node 1
Explanation This is the number that this machine has in the cluster. The cluster answers in a round-robin fashion, so once a new connection is made to the cluster, the next machine answers, and then the next after that, and so on.
Option –hash-init
Example iptables -A INPUT -p tcp -d 192.168.0.5 --dport 80 -j CLUSTERIP --new --hashmode sourceip --clustermac 01:00:5e:00:00:20 --hash-init 1234
Explanation Specifies a random seed for hash initialization.

该目标违反了[ RFC 1812 - Requirements for IP Version 4 Routers]要求,因此要警惕可能出现的任何问题。具体来说,3.3.2节规定了一个路由器绝对不能信任另一个声称它正在使用多播mac的主机或路由器

1.4 CONNMARK target

ONNMARK目标用于在整个连接上设置标记,其方式与mark目标非常相似。然后,它可以与连接标记匹配一起使用。例如,假设我们在报头中看到一个特定的模式,我们不想只标记那个包,而是标记整个连接。在这种情况下,CONNMARK目标是一个完美的解决方案。CONNMARK目标在所有链和所有表中都可用,但请记住,nat表只由连接中的第一个包遍历,因此如果您试图将它用于这里的第一个包之后的后续包,那么CONNMARK目标将没有任何作用。它可以采用以下四种不同的选项之一。

Option –set-mark
Example iptables -t nat -A PREROUTING -p tcp --dport 80 -j CONNMARK --set-mark 4
Explanation This option sets a mark on the connection. The mark can be an unsigned long int, which means values between 0 and 4294967295l is valid. Each bit can also be masked by doing --set-mark 12/8. This will only allow the bits in the mask to be set out of all the bits in the mark. In this example, only the 4th bit will be set, not the 3rd. 12 translates to 1100 in binary, and 8 to 1000, and only the bits set in the mask are allowed to be set. Hence, only the 4th bit, or 8, is set in the actual mark.
Option –save-mark
Example iptables -t mangle -A PREROUTING --dport 80 -j CONNMARK --save-mark
Explanation The --save-mark target option is used to save the packet mark into the connection mark. For example, if you have set a packet mark with the MARK target, you can then move this mark to mark the whole connection with the --save-mark match. The mark can also be masked by using the --mask option described further down.
Option –restore-mark
Example iptables -t mangle -A PREROUTING --dport 80 -j CONNMARK --restore-mark
Explanation This target option restores the packet mark from the connection mark as defined by the CONNMARK. A mask can also be defined using the --mask option as seen below. If a mask is set, only the masked options will be set. Note that this target option is only valid for use in the mangle table.
Option –mask
Example iptables -t mangle -A PREROUTING --dport 80 -j CONNMARK --restore-mark --mask 12
Explanation The --mask option must be used in unison with the --save-mark and --restore-mark options. The --mask option specifies an and-mask that should be applied to the mark values that the other two options will give. For example, if the restored mark from the above example would be 15, it would mean that the mark was 1111 in binary, while the mask is 1100. 1111 and 1100 equals 1100.

1.5 CONNSECMARK targe

CONNSECMARK目标将SELinux安全上下文标记设置为包标记或从包标记中设置。有关SELinux的更多信息,请阅读安全增强Linux主页。该目标仅在mangle表中有效,并与SECMARK目标一起使用,其中SECMARK目标用于设置原始标记,然后使用CONNSECMARK在整个连接上设置标记。SELinux超出了本文的范围,但基本上它是对Linux的强制访问控制的一个补充。这比大多数Linux和Unix安全控件的原始安全系统更细粒度。每个对象都可以有连接到它的安全属性或安全上下文,然后在允许或拒绝执行特定任务之前,将这些属性相互匹配。此目标将允许在连接上设置安全上下文。

Option –save
Example iptables -t mangle -A PREROUTING -p tcp --dport 80 -j CONNSECMARK --save
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值