#Linux 防火墙之 ipset 表应用

本文由 简悦 SimpRead 转码, 原文地址 www.sfjvip.com

简介提示线上生产环境不要随便重启 iptables, 会导致 docker 已经映射好的关系表都没有了. 不要执行 iptables-save, 会把当前临时的一些规则保存到 / etc/sysconfig/iptabl......

#简介

提示线上生产环境不要随便重启 iptables, 会导致 docker 已经映射好的关系表都没有了.
不要执行 iptables-save, 会把当前临时的一些规则保存到 / etc/sysconfig/iptables, 如有变动, 重启又会被 load 出来.
不要执行 iptables -F 清空规则.
增删改表单后, 不要重启 iptables, 规则都是立即生效.

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

  • 一次性存储大量的 ip 或者端口,用以 iptables 匹配

  • 在不影响性能的前提下,动态更新 iptables 规则(针对 ip 或者端口), 表单更新时不用频繁重启 iptabels 服务, 立即生效

  • 期望使用 ipset 的告诉匹配,或者在一条 iptables 规则中表达复杂的 ip / 端口规则, 使规则看起来更简单明了

#安装 ipset

yum install ipset


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 -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
DROP       all  --  0.0.0.0/0            0.0.0.0/0            match-set opsdenyport src
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            match-set opsallowport src
DROP       all  --  0.0.0.0/0            0.0.0.0/0            match-set opsdeny src
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            match-set opsallow 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
ipset create denytemp1d hash:ip timeout 86400
iptables -A INPUT -p tcp -m set --match-set denytemp1d src -j DROP
[root@opstest zzw]
[root@opstest zzw]
[root@opstest zzw]
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"

编程学习分享 » Linux 防火墙之 ipset 表应用