用活Firewalld防火墙之direct

原文地址:http://www.excelib.com/article/294/show

学生在前面已经给大家介绍过了Firewalld中direct的作用,使用他可以直接使用iptables、ip6tables中的规则进行配置,下面学生就给大家介绍direct的具体用法。

direct结构

我们还是先从配置文件入手,direct的配置文件为/etc/firewalld/direct.xml文件,结构如下

1 <?xml version="1.0" encoding="utf-8"?>
2 <direct>
3    [ <chain ipv="ipv4|ipv6" table="table" chain="chain"/> ]
4    [ <rule ipv="ipv4|ipv6" table="table" chain="chain" priority="priority"> args </rule> ]
5    [ <passthrough ipv="ipv4|ipv6"> args </passthrough> ]
6 </direct>

大家可以看到这里的direct一共有三种节点:chain、rule和passthrough,他们都是可选的,而且都可以出现多次。

属性

  • ipv:这个属性非常简单,表示ip的版本

  • table:chain和rule节点中的table属性就是iptables/ip6tables中的table

  • chain:chain中的chain属性用于指定一个自定义链的名字,注意,不可与已有链重名;rule中的chain属性既可以是内建的(也就是iptables/ip6tables中的五条链),也可以是在direct中自定义的chain

  • priority:优先级,用于设置不同rule的优先级,就像iptables中规则的前后顺序,数字越小优先级越高

  • args:rule和passthrough中的args就是iptables/ip6tables中的一条具体的规则,不过他们可以使用我们自定义的chain。

使用

  因为direct的使用跟iptables/ip6tables非常相似,换句话说,想用好direct需要有iptables/ip6tables的基础,而iptables/ip6tables并不是我们这套教程的重点,所以这里学生就不给大家详细讲解了。

  direct中自定义chain跟iptables/ip6tables中使用-N新建chain类似,创建完之后就可以在规则中使用-j或者-g来使用了。

Firewalld中跟direct相关的命令如下

 1 firewall-cmd [--permanent] --direct --get-all-chains
 2 firewall-cmd [--permanent] --direct --get-chains { ipv4 | ipv6 | eb } table
 3 firewall-cmd [--permanent] --direct --add-chain { ipv4 | ipv6 | eb } table chain
 4 firewall-cmd [--permanent] --direct --remove-chain { ipv4 | ipv6 | eb } table chain
 5 firewall-cmd [--permanent] --direct --query-chain { ipv4 | ipv6 | eb } table chain
 6  
 7 firewall-cmd [--permanent] --direct --get-all-rules
 8 firewall-cmd [--permanent] --direct --get-rules { ipv4 | ipv6 | eb } table chain
 9 firewall-cmd [--permanent] --direct --add-rule { ipv4 | ipv6 | eb } table chain priority args
10 firewall-cmd [--permanent] --direct --remove-rule { ipv4 | ipv6 | eb } table chain priority args
11 firewall-cmd [--permanent] --direct --remove-rules { ipv4 | ipv6 | eb } table chain
12 firewall-cmd [--permanent] --direct --query-rule { ipv4 | ipv6 | eb } table chain priority args
13  
14 firewall-cmd --direct --passthrough { ipv4 | ipv6 | eb } args
15 firewall-cmd --permanent --direct --get-all-passthroughs
16 firewall-cmd --permanent --direct --get-passthroughs { ipv4 | ipv6 | eb }
17 firewall-cmd --permanent --direct --add-passthrough { ipv4 | ipv6 | eb } args
18 firewall-cmd --permanent --direct --remove-passthrough { ipv4 | ipv6 | eb } args
19 firewall-cmd --permanent --direct --query-passthrough { ipv4 | ipv6 | eb } args

下面我们来看个文档(firewalld.direct(5))中提供的例子

1 <?xml version="1.0" encoding="utf-8"?>
2 <direct>
3     <chain ipv="ipv4" table="raw" chain="blacklist"/>
4     <rule ipv="ipv4" table="raw" chain="PREROUTING" priority="0">-s 192.168.1.0/24 -j blacklist</rule>
5     <rule ipv="ipv4" table="raw" chain="PREROUTING" priority="1">-s 192.168.5.0/24 -j blacklist</rule>
6     <rule ipv="ipv4" table="raw" chain="blacklist" priority="0">-m limit --limit 1/min -j LOG --log-prefix "blacklisted: "</rule>
7     <rule ipv="ipv4" table="raw" chain="blacklist" priority="1">-j DROP</rule>
8 </direct>

在这个例子中首先自定义了一个叫blacklist的链,然后将所有来自192.168.1.0/24和192.168.5.0/24的数据包都指向了这个链,最后定义了这个链的规则:首先进行记录,然后drop,记录的方法是使用“blacklisted: ”前缀并且限制1分钟记录一次。

当然,使用相似的方法大家也可以写出来上一节学生给大家留下的那个问题:对ping请求进行限制。

转载于:https://www.cnblogs.com/bldly1989/p/7215816.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值