防火牆之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"