linux(centos8):firewalld使用ipset管理ip地址的集合

一,firewalld中ipset的用途:

1,用途

ipset是ip地址的集合,

firewalld使用ipset可以在一条规则中处理多个ip地址,

执行效果更高

​对ip地址集合的管理也更方便 

2,注意与iptables所用的ipset命令的不同,

   不要混合使用firewall-cmd的ipset参数与linux平台上的ipset命令,

    避免引起冲突,

    firewalld的ipset会记录到/etc/firewalld/ipsets/目录下

 

说明:刘宏缔的架构森林是一个专注架构的博客,

网站:https://blog.imgtouch.com
本文: https://blog.imgtouch.com/index.php/2023/05/22/centos8linuxfirewalld-yong-ipset-guan-li-ip-di-zhi-de-ji-he/

         对应的源码可以访问这里获取: https://github.com/liuhongdi/

说明:作者:刘宏缔 邮箱: 371125307@qq.com

 

二,firewalld中ipset的操作例子:

1,新建一个set

#--new-ipset=sshblock: 指定新ipset的名字为:sshblock

#--type=hash:ip    指定类型为 hash:ip,这种形式不允许重复而且只有一个ip

[root@blog ipsets]# firewall-cmd --permanent --new-ipset=sshblock --type=hash:ip
success

查看ipset文件是否已生成?

说明:默认的目录是:/etc/firewalld/ipsets

[root@blog ipsets]# more /etc/firewalld/ipsets/sshblock.xml
<?xml version="1.0" encoding="utf-8"?>
<ipset type="hash:ip">
</ipset>

 

2,在set中添加ip

[root@blog ipsets]# firewall-cmd --permanent --ipset=sshblock --add-entry=121.122.123.105
success

查看添加ip的效果

[root@blog ipsets]# more /etc/firewalld/ipsets/sshblock.xml
<?xml version="1.0" encoding="utf-8"?>
<ipset type="hash:ip">
  <entry>121.122.123.105</entry>
</ipset>

 

3,从set中删除ip

[root@blog ipsets]# firewall-cmd --permanent --ipset=sshblock --remove-entry=121.122.123.105
success

查看删除ip的效果

[root@blog ipsets]# more /etc/firewalld/ipsets/sshblock.xml
<?xml version="1.0" encoding="utf-8"?>
<ipset type="hash:ip">
</ipset>

 

4,删除一个set

[root@blog ipsets]# firewall-cmd --permanent --delete-ipset=sshblock
success

查看sshblock这个set的配置文件是否还存在?

[root@blog ipsets]# more /etc/firewalld/ipsets/sshblock.xml
more: stat of /etc/firewalld/ipsets/sshblock.xml failed: No such file or directory

 

5,打印一个set的文件路径:

[root@blog ipsets]# firewall-cmd --permanent --path-ipset=sshblock
/etc/firewalld/ipsets/sshblock.xml

 

6,打印一个set的内容:

[root@blog ipsets]# firewall-cmd --permanent --info-ipset=sshblock
sshblock
  type: hash:ip
  options:
  entries: 121.122.123.105

 

7,列出一个set下的所有entry

[root@blog ipsets]# firewall-cmd --permanent --ipset=sshblock --get-entries
121.122.123.105
...

 

8,判断一个ip是否存在于set中?

[root@blog ipsets]# firewall-cmd --permanent --ipset=sshblock --query-entry=1.1.1.1
no

[root@blog ipsets]# firewall-cmd --permanent --ipset=sshblock --query-entry=121.122.123.105
yes

 

9,列出所有的ipsets

[root@blog ipsets]# firewall-cmd --permanent --get-ipsets
sshblock

 

10,得到所有的默认ipset类型 

[root@blog ipsets]# firewall-cmd --get-ipset-types
hash:ip hash:ip,mark hash:ip,port hash:ip,port,ip hash:ip,port,net hash:mac 
hash:net hash:net,iface hash:net,net hash:net,port hash:net,port,net

 

三,firewalld中使用ipset

1,把一个ipset加入到禁止的规则

[root@blog ipsets]# firewall-cmd --permanent --add-rich-rule 'rule family="ipv4" source ipset="sshblock" drop'
success

使生效

[root@blog ipsets]# firewall-cmd --reload
success

查看xml中的记录:

[root@blog ipsets]# more /etc/firewalld/zones/public.xml
...
  <rule family="ipv4">
    <source ipset="sshblock"/>
    <drop/>
  </rule>
...

 

2,把ip地址中ipset中删除

注意:没写入到磁盘

[root@blog ipsets]# firewall-cmd --ipset=sshblock --remove-entry=121.122.123.105
success
[root@blog ipsets]# firewall-cmd --ipset=sshblock --query-entry=121.122.123.105
no
[root@blog ipsets]# firewall-cmd --ipset=sshblock --get-entries

可见已删除成功,

 

如果想永久性的记录下来:写入到磁盘后 reload一次

[root@blog ipsets]# firewall-cmd --permanent --ipset=sshblock --remove-entry=121.122.123.105
success
[root@blog ipsets]# more /etc/firewalld/ipsets/sshblock.xml
<?xml version="1.0" encoding="utf-8"?>
<ipset type="hash:ip">
</ipset>
[root@blog ipsets]# firewall-cmd --reload
success

 

四,添加到ipset中的ip地址数据是否会重复?

因为使用了hash类型,当ip重复时firewall-cmd会报错:

新建ipset

[root@blog ipsets]# firewall-cmd --permanent --new-ipset=sshblock --type=hash:ip
success

添加ip

[root@blog ipsets]# firewall-cmd --permanent --ipset=sshblock --add-entry=121.122.123.105
success

查看文件

[root@blog ipsets]# more /etc/firewalld/ipsets/sshblock.xml
<?xml version="1.0" encoding="utf-8"?>
<ipset type="hash:ip">
  <entry>121.122.123.105</entry>
</ipset>

再次添加ip

[root@blog ipsets]# firewall-cmd --permanent --ipset=sshblock --add-entry=121.122.123.105
Warning: ALREADY_ENABLED: 121.122.123.105
success

查看文件:

[root@blog ipsets]# more /etc/firewalld/ipsets/sshblock.xml
<?xml version="1.0" encoding="utf-8"?>
<ipset type="hash:ip">
  <entry>121.122.123.105</entry>
</ipset>

没有出现重复的情况

 

五,使用脚本抓取有问题的ip加入到拒绝访问的ipset

常用的几类ip:

1,被firewalld防火墙reject的ip

2,nginx日志中访问过于频率的ip

3,secure日志中登录失败的ip

 

我们以secure日志中登录失败的ip为例:

先用命令抓取到登录失败的ip:

[root@blog log]# grep -i 'Failed password' /var/log/secure | awk '{print $11}' | sort -n | uniq -c | sort -k1nr | awk '{if ($1>5) print $2}'
...

 

写一段脚本,放到crond中定时执行即可:

[root@blog ~]# vi ./addlogifailip2firewall.sh

内容:

#!/bin/bash
for LINE in `grep -i 'Failed password' /var/log/secure | awk '{print $11}' | sort -n | uniq -c | sort -k1nr | awk '{if ($1>3) print $2}'`; do
    echo "${LINE}";
    firewall-cmd --permanent --ipset=sshblock --add-entry="${LINE}";
done;
firewall-cmd --reload;

 

六,如何防止自己被误关在防火墙外?使用ip白名单:

把允许访问的ip加入到trusted区域:

[root@blog zones]# firewall-cmd --permanent --zone=trusted --add-source=121.122.123.105

使生效:

[root@blog zones]# firewall-cmd --reload 

 

注意此处不要使用ipset,

使用ipset后,如果同一个ip也被加入了被拒绝的set,

则此ip还是会关到外面。

原因在于firewalld把规则转到nftables的处理机制,

它把set的处理合并到默认的public zone中去处理了.

大家可以用nft的命令验证 :

[root@blog log]# nft list ruleset

 

七,查看firewalld的版本

[root@blog ~]# firewall-cmd --version
0.6.3

 

八,查看linux的版本:

[root@blog ~]# cat /etc/redhat-release
CentOS Linux release 8.0.1905 (Core) 

 

posted @ 2020-05-27 16:29  刘宏缔的架构森林  阅读(5054)  评论(0编辑  收藏  举报