ipset介绍

ipset是iptables的扩展,它允许你创建 匹配整个地址集合的规则。而不像普通的iptables链只能单IP匹配, ip集合存储在带索引的数据结构中,这种结构即时集合比较大也可以进行高效的查找,除了一些常用的情况,比如阻止一些危险主机访问本机,从而减少系统资源占用或网络拥塞,IPsets也具备一些新防火墙设计方法,并简化了配置.官网:http://ipset.netfilter.org/

ipset配置步骤

一、安装ipset

yum -y install ipset

二、创建ipset规则

ipset create blacklist hash:net
ipset create whitelist hash:net

三、添加黑/白名单IP

ipset add blacklist 192.168.0.1
ipset add whitelist 192.168.0.2
ipset add blacklist 192.168.0.0/24

四、ipset默认存储位置,以及保存

/etc/sysconfig/ipset

如果需要保存到其他路径,使用如下命令

ipset save -f /opt/ipset.conf

五、关联ipset和iptables

iptables -I INPUT -m set --match-set blacklist src -j DROP -m comment --comment "Reject from blacklist"
iptables -I INPUT -m set --match-set whitelist src -j ACCEPT -m comment --comment "Allow from whitelist"

六、保存iptables规则

service iptables save

实现自动化的添加ip黑名单

通过脚本自动化的从query.log筛选出异常的IP地址并加入到ipset的blacklist中

#!/bin/bash
# query log location
query_location='/var/named/data/query.log'
# Get the time 2s before the current time.
date_time=$(date +%H:%M:%S -d '-2 Seconds')
printf "The IP traffic statistics time point is %s\n" $date_time
# Get the ip access count list at the current time in the dns query log, and display the corresponding information, such as: total number, ip number, maximum access ip.
num_list=`grep $date_time $query_location | awk -F '[ #]' '{print $5}' | sort | uniq -c | awk '{print $1}' | sort -n`
total_num=0
for n in $num_list
do
total_num=`echo "$total_num+$n" | bc`
done
printf "The total number of queries in 1 second is %s\n" $total_num
max_num=`echo $num_list | awk '{print $NF}' -`
max_num_ip=`grep $date_time $query_location | awk -F '[ #]' '{print $5}' | sort | uniq -c | grep $max_num | awk '{print $2}'`
printf "The highest queried IP is:\n%s\nThe total amount of queries is: %s\n" "$max_num_ip" $max_num
if [ "$max_num" -lt "100" ]
then
echo "There is no ip address for the exception query!"
else
ipset list
for l in $num_list
do
if [ "$l" -ge "100" ]
then
ip_stop=`grep $date_time $query_location | awk -F '[ #]' '{print $5}' | sort | uniq -c | grep $l | awk '{print $2}'`
echo $ip_stop
ipset add blacklist $ip_stop
fi
done
ipset list
echo "IPs with more than 100 ips have been blacklisted!"
fi

再添加cron任务即可。

至此,DNS异常IP的ipset配置已经完成。

最新文章

  1. git使用
  2. 【grunt第三弹】grunt在前端实际项目中的应用
  3. 自动拒绝恶意IP远程登录Linux服务器脚本
  4. AWIT DBackup 0.0.20 发布,备份系统
  5. 基于nginx tomcat redis分布式web应用的session共享配置
  6. android 数据存储----文件方式 总结
  7. Hive[4] 数据定义 HiveQL
  8. DevExpress 中 WaitForm 使用
  9. C# WinForm 透明控件 PictureBox透明 分类: WinForm 2014-07-30 13:27 591人阅读 评论(0) 收藏
  10. java--文件过滤器和简单系统交互
  11. Tomcat过滤器模拟
  12. Python 调用shell
  13. Android初级教程图片信息
  14. extjs +String2 +Spring 下的分页 以及返回json格式错误的问题
  15. linux清理缓存的命令
  16. sql递归查询 根据Id查所有子结点
  17. 关于Dubbo面试问题
  18. iptables 介绍
  19. Mac下如何进行端口转发,方便一系列需要使用80端口进行的调试工作
  20. mac obs直播软件 无法输出音频解决办法

热门文章

  1. 开发平台支持Arm Cortex-M的微控制器上人工智能训练
  2. 【NX二次开发】导入x_t,UF_PS_import_data
  3. java学习笔记1(入门级)
  4. docker0-常用命令-持续更新
  5. 【dog与lxy】8.25题解-necklace
  6. Java基准性能测试--JMH使用介绍
  7. 【Azure 应用服务】Azure Function集成虚拟网络,设置被同在虚拟网络中的Storage Account触发,遇见Function无法触发的问题
  8. excel vba的inputBox函数
  9. Keyboarding(信息学奥赛一本通-T1452)
  10. 通过修改Host实现chrome同步