ipset和iptables配合来自动封闭和解封有问题的IP
2016-12-02 14:45
826 查看
iptables封掉少量ip处理是没什么问题的,但是当有大量ip攻击的时候性能就跟不上了,iptables是O(N)的性能。而ipset就像一个集合,把需要封闭的ip地址放入这个集合中,ipset 是O(1)的性能,用的hash方式所以特别快。
一、软件及安装
1、iptables(一般linux都已经安装好的)
2、ipset:
1、查看ip集的列表信息
hash:ip :为指定类型,还有其他好些类型,比如hash:net,hash:net,net等,具体可以man ipset
100000:为最大保存ip的数量
timeout: 为封闭ip的默认时间,这个参数可以不写,这样就永不解封,除非手动解封
3、增加ip地址到ip集
5、保存ip集到文件
1、创建ip集
3、根据条件判断需要封闭的ip
条件:在上一分钟的nginx的请求中,单一ip请求数超过1000及request_uri中包含passwd字符串的ip都直接封禁,1小时后自动解禁。脚本如下
在crontab中添加此脚本的自动运行
一、软件及安装
1、iptables(一般linux都已经安装好的)
2、ipset:
ubuntu:apt-get install ipset二、ipset的使用
1、查看ip集的列表信息
ipset list2、创建ip集
ipset create XXXX hash:ip maxelem 100000 timeout 3600XXXX:ip集的名字
hash:ip :为指定类型,还有其他好些类型,比如hash:net,hash:net,net等,具体可以man ipset
100000:为最大保存ip的数量
timeout: 为封闭ip的默认时间,这个参数可以不写,这样就永不解封,除非手动解封
3、增加ip地址到ip集
ipset add xxxx 1.1.1.1增加网段
ipset add xxxx 1.1.1.0/244、删除指定的ip或网段
ipset del xxxx 1.1.1.1 ipset del xxxx 1.1.1.0/24
5、保存ip集到文件
ipset save xxxx>ipset_list.txt6、还原ip集
ipset restore <ipset_list.txt三、自动封禁和解封
1、创建ip集
ipset create forbidip hash:ip timeout 1728002、创建iptables条目
iptables -A INPUT -p tcp -m set --match-set forbidip src -m multiport --dports 443,80 -j DROP或者
iptables -A INPUT -p tcp -m set --match-set forbidip src -m multiport -j DROP
3、根据条件判断需要封闭的ip
条件:在上一分钟的nginx的请求中,单一ip请求数超过1000及request_uri中包含passwd字符串的ip都直接封禁,1小时后自动解禁。脚本如下
#!/bin/bash FILES="/data/nginx_log/xxxxx/access.log" DATE=`date -d '1 minutes ago' +%Y:%H:%M` grep ${DATE} ${FILES}|awk -F'|' '{print $3}'|sort -n|uniq -c |sort -nr|head -1>/tmp/ips grep ${DATE} ${FILES}|grep -i passwd|awk -F'|' '{print $3}'|sort -n|uniq>/tmp/ippwd NUM=`awk '{print $1}' /tmp/ips` IP=`awk '{print $2}' /tmp/ips` IP2=`cat /tmp/ippwd` threshold=1000 if [[ $NUM -gt $threshold ]];then /sbin/ipset -! add forbidip $IP timeout 3600 fi if [ -s /tmp/ippwd ];then for i in $IP2 do /sbin/ipset -! add forbidip $i done fi4、脚本自动运行
在crontab中添加此脚本的自动运行
*/1 * * * * bash /path/to/script.sh
相关文章推荐
- Shell脚本配合iptables屏蔽来自某个国家的IP访问
- Shell脚本配合iptables屏蔽来自某个国家的IP访问
- shell只允许某些ipssh登录,配合iptables最好
- liunx 防火墙: 从 iptables 到 ipset 的过滤ip
- iptables禁IP与解封IP常用命令
- 用 iptables 屏蔽来自某个国家的 IP
- iptables防火墙与日志系统配合使用 监控服务器特点端口的防问源IP
- 用 iptables 屏蔽来自某个国家的 IP
- iptables一次性封多个ip,使用ipset 工具
- 详解Linux防火墙iptables禁IP与解封IP常用命令
- Linux防火墙:iptables禁IP与解封IP常用命令
- 用Iptables 屏蔽来自某个国家的IP
- 使用iptables屏蔽来自某个国家的IP
- Linux防火墙:iptables禁IP与解封IP常用命令
- iptables一次性封多个ip,使用ipset 工具
- Linux防火墙:iptables禁IP与解封IP常用命令
- Linux防火墙:iptables禁IP与解封IP常用命令
- Linux防火墙:iptables禁IP与解封IP常用命令