文章地址:https://blog.csdn.net/firehive/article/details/81260169 文章作者:firehive
iptables直接针对ip进行封禁,在ip数量不大的时候是没什么问题的,但当有大量ip的时候性能会严重下降,iptables是O(N)的性能。而ipset就像一个集合,把需要封闭的ip地址写入这个集合中,ipset 是O(1)的性能,可以有效解决iptables直接封禁大量IP的性能问题。
当然也可以不加timeout 3600 ,也可以只写ipset create blacklist hash:ip , blacklist 为集合名称,可随意
当然,也可以封禁黑名单IP的所有请求。
iptables -I INPUT -p tcp -m set --match-set blacklist src -m multiport -j DROP 基于自定义访问频率阈值或者请求敏感关键字来创建自动筛选恶意IP的脚本/data/iptables_ipset_deny.sh。 FILES: nginx的access.log文件 sensitive: 敏感关键字 threshold: 一分钟内请求频率阈值 #!/bin/bash FILES="/data/nginx/logs/access.log" sensitive="sensitive_word" threshold=1000 ip_file="/tmp/ip_file" sensitive_file="/tmp/sensitive_file" DATE=`date -d '1 minutes ago' +%Y:%H:%M` grep ${DATE} ${FILES} | awk '{print $1}' | sort | uniq -c | sort -n | tail -n 1 > ${ip_file} grep ${DATE} ${FILES} | grep -i ${sensitive} | awk '{print $1}' | sort -n | uniq > ${sensitive_file} ip_file_number=`awk '{print $1}' ${ip_file}` ip_file_ip=`awk '{print $2}' ${ip_file}` if [[ $ip_file_number -gt $threshold ]];then ipset add blacklist ${ip_file_ip} timeout 3600 fi if [ -s ${sensitive_file} ];then for sensitive_ip in `cat ${sensitive_file}` do ipset add blacklist ${sensitive_ip} done fi 用crontab定时启动脚本。 echo "* * * * * bash /data/iptables_ipset_deny.sh" >> /etc/crontab当然也可以配置crontab一分钟执行一次定时任务:
*/1 * * * * bash /data/iptables_ipset_deny.sh当前文章为博主亲试,有疑问的小伙伴可以给我留言哦o( ̄︶ ̄)o