防火墙之ipset表应用

简介

由于公司线上生产环境firewalld默认都是关闭的,所以只能依赖iptables的表来方便管理IP黑白名单.
提示线上生产环境不要随便重启iptables,会导致docker已经映射好的关系表都没有了.
不要执行iptables-save,会把当前临时的一些规则保存到/etc/sysconfig/iptables,如有变动,重启又会被load出来.
不要执行iptables -F 清空规则.
增删改表单后,不要重启iptables,规则都是立即生效.

本本主要内容:ipset表应用
ip set是linux内核的一个内部框架,可由ipset工具管理.
ipset 适用于以下几种场景:

  • 一次性存储大量的ip或者端口,用以iptables匹配
  • 在不影响性能的前提下,动态更新iptables规则(针对ip或者端口),表单更新时不用频繁重启iptabels服务,立即生效
  • 期望使用ipset的告诉匹配,或者在一条iptables规则中表达复杂的 ip/端口规则,使规则看起来更简单明了

安装ipset

#安装ipset,大多数系统7已经默认安装过了 yum install ipset  #备份原有的iptabels到文件 iptables -nL >myiptabls.oldlist iptables-save >./myiptables.old 

定义hash:net类型黑白名单

任何端口协议

#白名单 ipset create opsallow hash:net maxelem 10000 iptables -A INPUT -m set --match-set opsallow src -j ACCEPT  #黑名单 ipset create opsdeny hash:net maxelem 500000 iptables -A INPUT -m set --match-set opsdeny src -j DROP   #查看表 ipset list 表名  #保存表 ipset save 表名 -f 表名bak.txt  #恢复表 ipset restore -f 表名bak.txt  #查看iptables表(这里注意优先级) iptables -nL INPUT Chain INPUT (policy ACCEPT) target     prot opt source               destination ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            match-set opsallow src DROP       all  --  0.0.0.0/0            0.0.0.0/0            match-set opsdeny src ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            match-set opsallowipport src,dst DROP       tcp  --  0.0.0.0/0            0.0.0.0/0            match-set opsdenyipport src,dst DROP       tcp  --  0.0.0.0/0            0.0.0.0/0            match-set denytemp1d src    
#加白名单,把公司自己IP先加进去 ipset add opsallow 123.123.123  #加黑名单 #ipset add opsdeny 45.9.20.101  #删白名单 ipset del opsallow 1.1.1.1 #删黑名单 ipset del opsallow 1.1.1.1  #网段 ipset add opsallow 192.168.255.0/24 ipset add opsallow 172.17.0.0/16  #清空表单 ipset flush 表名  #删除表 ipset destroy 表名  #测试是否在表中匹配到 ipset test opsdeny 192.168.255.166  

定义hash:ip,port类型黑白名单

指定端口

#白名单 ipset create opsallowipport hash:ip,port maxelem 10000 iptables -A INPUT -p tcp -m set --match-set opsallowipport src,dst -j ACCEPT  #黑名单 ipset create opsdenyipport hash:ip,port maxelem 500000 iptables -A INPUT -p tcp -m set --match-set opsdenyipport src,dst -j DROP    #增删 ipset add opsallowipport 192.168.255.166,80 ipset del opsallowipport 192.168.255.166,80 ipset del opsallowipport 192.168.255.166,80-89 ipset del opsallowipport 192.168.255.166,udp:53 ipset add opsdenyipport 192.168.255.166,tcp:80 ipset add opsdenyipport 192.168.255.161-192.168.255.166,8080   #这里走了些弯路,特此整理成表,只告诉了怎么加到ipset表,缺很少告诉你src,dst都需要指定,只写src导致ipset无效  ipset type        | iptables match-set | Packet fields  ------------------+--------------------+---------------------------------  hash:ip,port      | src,dst            | src IP address, dst port  hash:net,port,net | src,dst,dst        | src IP address, dst port, dst IP address  hash:net,port,net | dst,src,src        | dst IP address, src port, src IP address  hash:ip,port,ip   | src,dst,dst        | src IP address, dst port, dst IP address  hash:ip,port,ip   | dst,src,src        | dst IP address, src port, src ip address  hash:mac          | src                | src mac address  hash:mac          | dst                | dst mac address  hash:ip,mac       | src,src            | src IP address, src mac address  hash:ip,mac       | dst,dst            | dst IP address, dst mac address  hash:ip,mac       | dst,src            | dst IP address, src mac address 

其他

 #规则优先级 #这里也很容易走弯路如果ipset的allow和deny里都有,iptables中opsallowipport在opsdenyipport前面,那么allow优先; #也就是说ipables中谁在前面,谁先匹配到,谁就有优先权. #因此根据实际需要,要注意iptables条目的顺序先后.  #查看排序规则 #iptables -nL INPUT [root@opstest zzw]# iptables -nL Chain INPUT (policy ACCEPT) target     prot opt source               destination DROP       tcp  --  0.0.0.0/0            0.0.0.0/0            match-set opsdenyipport src,dst ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            match-set opsallowipport src,dst #加到最前面 iptables -I INPUT -p tcp -m set --match-set opsallowipport src,dst -j ACCEPT #追加到后面 iptables -A INPUT -p tcp -m set --match-set opsallowipport src,dst -j ACCEPT 
#创建自动1d解封 ipset create denytemp1d hash:ip timeout 86400 iptables -A INPUT -p tcp -m set --match-set denytemp1d src -j DROP  
#添加注释 [root@opstest zzw]# ipset create opsdenyipport hash:ip,port maxelem 500000 comment [root@opstest zzw]# ipset add opsdenyipport 192.168.255.167,tcp:80 comment "bad man" [root@opstest zzw]# ipset list opsdenyipport Name: opsdenyipport Type: hash:ip,port Revision: 5 Header: family inet hashsize 1024 maxelem 500000 comment Size in memory: 240 References: 0 Number of entries: 1 Members: 192.168.255.167,tcp:80 comment "bad man"   

商匡云商
Logo
注册新帐户
对比商品
  • 合计 (0)
对比
0
购物车