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://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) 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM