ipset 使用及屏蔽国外ip
ipset介绍
- ipset是ip地址的集合,防火墙使用ipset可以在一条规则中处理多个ip地址,
执行效果更高,对ip地址集合的管理也更方便。 - 注意firewalld与iptables所用的ipset命令的不同,不要混合使用firewall-cmd中的ipset参数和linux平台上的ipset命令避免引起冲突,firewalld的ipset命令会记录到
/etc/firewalld/ipsets/
目录下 建议只是用一种管理命令,如果使用iptables,则禁用firewalld,清空之前的规则。
systemctl stop firewalld.service systemctl disable firewalld.service iptables -P INPUT ACCEPT iptables -F
安装ipset
apt-get -y install ipset #debian
yum -y install ipset #centos
常用命令
ipset 常用命令
#常用命令 ipset create yoda hash:ip # 创建集合 ipset del yoda x.x.x.x # 从 yoda 集合中删除内容 ipset list yoda # 查看 yoda 集合内容 ipset list # 查看所有集合的内容 ipset flush yoda # 清空 yoda 集合 ipset flush # 清空所有集合 ipset destroy yoda # 销毁 yoda 集合 ipset destroy # 销毁所有集合 ipset save yoda # 输出 yoda 集合内容到标准输出 ipset save # 输出所有集合内容到标准输出 ipset restore # 根据输入内容恢复集合内容 #设置特殊标记 ipset create r2d2 hash:net #创建r2d2集合 ipset add r2d2 1.2.3.0/24 #添加1.2.3.0/24 ip段。 ipset add r2d2 1.2.3.0/30 nomatch #nomatch标记1.2.3.0/30 IP段不属于这个r2d2集合,适用于一个大ip段中的一部分离出来。 #设置黑白名单 iptables -I INPUT -m set --match-set yoda src -j DROP #添加yoda集合为黑名单。 iptables -I INPUT -m set ! --match-set yoda src -j DROP #添加yoda集合为白名单。 #过期自动解封 ipset create obiwan hash:ip timeout 300 #创建了名为obiwan的集合,后面多加了timeout参数值为300秒。 ipset add obiwan 1.2.3.4 #往这个集合添加ip自动过期时间为300秒。 ipset add obiwan 6.6.6.6 timeout 60 #也可以自定义其他过期时间60秒。 ipset -exist add obiwan 1.2.3.4 timeout 100 #-exist重新定义条目过期时间。 #更大规则数量 #hashsize, maxelem 这两个参数分别指定了创建集合时初始的 hash 大小,和最大存储的条目数量。如果没有指定,hashsize 的默认值是 1024,maxelem 的默认值是 65536 ipset create yoda hash:ip,port hashsize 4096 maxelem 1000000 #创建了名为 yoda 的集合,初始 hash 大小是 4096,如果满了,这个 hash 会自动扩容为之前的两倍。最大能存储的数量是 100000 个。 ipset add yoda 3.4.5.6,3306
- firewalld --ipset常用命令:
firewall-cmd --permanent --new-ipset=sshblock --type=hash:ip #新建一个set,#--new-ipset=sshblock: 指定新ipset的名字为:sshblock。#--type=hash:ip 指定类型为 hash:ip,这种形式不允许重复而且只有一个ip
firewall-cmd --permanent --ipset=sshblock --add-entry=121.122.123.105 success #在set中添加ip
firewall-cmd --permanent --ipset=sshblock --remove-entry=121.122.123.105 success #从set中删除ip
firewall-cmd --permanent --delete-ipset=sshblock #删除一个set
firewall-cmd --permanent --path-ipset=sshblock /etc/firewalld/ipsets/sshblock.xml #打印一个set的文件路径
firewall-cmd --permanent --info-ipset=sshblock #打印一个set的内容
firewall-cmd --permanent --ipset=sshblock --get-entries 121.122.123.105 #列出一个set下的所有entry
firewall-cmd --permanent --ipset=sshblock --query-entry=1.1.1.1 no #判断一个ip是否存在于set中?
firewall-cmd --permanent --ipset=sshblock --query-entry=121.122.123.105 yes #判断一个ip是否存在于set中?
firewall-cmd --permanent --get-ipsets #列出所有的ipsets
firewall-cmd --get-ipset-types #得到所有的默认ipset类型
使用iptables屏蔽国外ip
ipset -N cnip hash:net #创建一个名为cnip的规则
wget -P /root http://www.ipdeny.com/ipblocks/data/countries/cn.zone #下载中国国家IP段
for i in $(cat /root/cn.zone ); do ipset -A cnip $i; done #将IP段添加到cnip规则中
iptables -A INPUT -p tcp -m set ! --match-set cnip src -j ACCEPT #设置IP段白名单放行IP段。
iptables -P INPUT DROP #关掉所有端口
#以上即可实现只允许国内ip访问。
#关闭指定端口
iptables -A INPUT -p tcp --dport 80 -j DROP #关闭指定端口
iptables -A INPUT -p tcp --dport 443 -j DROP #关闭指定端口
#删除规则
iptables -D INPUT -p tcp -m set --match-set cnip src -j ACCEPT
iptables -D INPUT -p tcp --dport 443 -j DROP
设置防火墙后注意保存规则防止重启失效。
iptables-save -f /etc/iptables/iptables.rules #保存规则 iptables-restore /etc/iptables/iptables.rules #加载规则
使用firewalld-cmd --ipset 屏蔽国外ip
#屏蔽国外ip
wget -P /root http://www.ipdeny.com/ipblocks/data/countries/cn.zone #下载中国国家IP段
firewall-cmd --permanent --new-ipset=cnip --type=hash:ip #新建一个set。
firewall-cmd --permanent --ipset=cnip --add-entries-from-file=/root/cn.zone #导入列表中地址。
firewall-cmd --permanent --add-rich-rule 'rule family="ipv4" source ipset="cnip" accept' #将一个set加入允许规则因为firewalld防火墙默认是拒绝权限,所以就是白名单。
#防止自己被禁用,提前加到信任规则
firewall-cmd --permanent --zone=trusted --add-source=121.122.123.105
#任何操作记得重新加载防火墙规则
firewall-cmd --reload
#使用脚本抓取有问题的ip加入到拒绝访问的ipset
#解决常用的几类ip:1、被firewalld防火墙reject的ip。2、nginx日志中访问过于频率的ip。3、secure日志中登录失败的ip
#查询错误3次以上的登录ip并添加到ipset黑名单写一段脚本,放到crond中定时执行即可。
#!/bin/bash
for failed_ip in `grep -i 'Failed password' /var/log/auth.log | awk '{print $13}' | sort -n | uniq -c | sort -k1nr | awk '{if ($1>3) print $2}'`; do
echo "${failed_ip}";
firewall-cmd --permanent --ipset=sshblock --add-entry="${failed_ip}";
done;
firewall-cmd --reload;
本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。
评论已关闭