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

摘要:
˃2,在集合[root@blogipsets]#Firewall cmd--permanent--ipset=sshblock-add entry=121122.123.105success查看添加ip的效果[root@blogipsets]#更多/etc/firewalld/ipsets/sshblock.xml4,删除集合[root@blogipsets]#Firewall cmd--permanent--delete ipset=sshblocksuccess检查sshblockset的配置文件是否仍然存在?

一,firewalld中ipset的用途:

1,用途

ipset是ip地址的集合,

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

执行效果更高

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

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

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

    避免引起冲突,

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

说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest

         对应的源码可以访问这里获取: 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) 

免责声明:文章转载自《linux(centos8):firewalld使用ipset管理ip地址的集合》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇IceCTF Crypto Substituted字符串与模式匹配算法(六):Needleman–Wunsch算法下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

[linux]进程(六)——守护进程

15,守护进程 概念:守护进程(Daemon)是运行在后台的一种特殊进程。它独立于控制终端并且周期性地执行某种任务或等待处理某些发生的事件。守护进程的特点:守护进程必须与其运行前的环境隔离开来。这些环境包括未关闭的文件描述符,控制终端,会话和进程组,工作目录以及文件创建掩模等。这些环境通常是守护进程从执行它的父进程(特别是shell)中继承下来的。守护进程...

(转)Linux网络接口配置文件ifcfg-eth0解析

原文:http://blog.51cto.com/xtbao/1671739 https://www.cnblogs.com/arvintang/p/5990599.html http://blog.csdn.net/jmyue/article/details/17288467 在一个计算机系统中,可以有多个网络接口,分别对应多个网络接口配置文件,在/et...

linux 安装网易云音乐

1.首先去官网下载最新的网易云 网易云音乐曾经推出官方Linux版本,提供的下载安装包有:deepin15(32位):http://s1.music.126.net/download/pc/net ... 0_i386.debdeepin15(64位):http://s1.music.126.net/download/pc/net ... _amd64.d...

linux上如何让other用户访问没有other权限的目录

目前遇到一个问题,一个other用户要访问一个目录,他需要在这个目录下创建文件,因此这个目录需要一个写权限,于是就给了这个目录777的权限,这样这个权限有点太大了,很容易出现安全问题,那我们应该怎么办呢。 我们先来看为什么一定要给这个目录777的权限呢?例如这个other用户为lbh,这个目录为/home/tmp_test/。我们在/home目录下创建tm...

Linux基础——查看IP及port的简单实现

需要注意,不同的机器,有的可能为大端字节序,有的可能为小端字节序。 小端就是低位字节排放在内存的低地址端即该值的起始地址,高位字节排放在内存的高地址端。 大端就是高位字节排放在内存的低地址端即该值的起始地址,低位字节排放在内存的高地址端。 实现代码如下: 1 #include <stdio.h> 2 #include <stdlib...

Linux计划任务.md

crond crond是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自动启动crond进程,crond进程每分钟会定期检查是否有要执行的任务,如果有要执行的任务,则自动执行该任务。 Linux下的任务调度分为两类,系统任务调度和用户任务调度。...