动态屏蔽恶意ip地址

栏目: 服务器 · 发布时间: 7年前

内容简介:网站运行时间久了,总有恶意的人来攻击网站。前不久,自己维护的一个论坛被攻击了。这里分享一个屏蔽恶意ip的方法。1.定时监控nginx的access.log,把高频率访问的ip解析出来

背景

网站运行时间久了,总有恶意的人来攻击网站。前不久,自己维护的一个论坛被攻击了。

这里分享一个屏蔽恶意ip的方法。

原理

1.定时监控nginx的access.log,把高频率访问的ip解析出来

2.定时监控nginx的access.log,通过关键字把恶意访问的ip过滤出来

3.把上面两个步骤解析出来的ip,写入的ipset

4.通过配置iptables,拒绝属于ipset的ip的访问

实现

1.监控脚本auto_ip_blacklist.sh

#!/bin/bash
# 日志文件列表,空格隔开
FILES="/home/wwwlogs/1.log /home/wwwlogs/access.log  /home/wwwlogs/2.log"
# 关键词,敏感词
sensitive="mysql|phpmyadmin|masscan|Shinka|id_rsa|id_ecdsa|winscp|ZmEu|myadmin2|myadmin"
threshold=1000

ip_file="/tmp/ip_file"
sensitive_file="/tmp/sensitive_file"
DATE=`date -d '1 minutes ago' +%Y:%H:%M`

grep -H ${DATE} ${FILES} | awk -F'[: ]' '{print $2}' | sort | uniq -c | sort -n | tail -n 1 > ${ip_file}
grep -H ${DATE} ${FILES} | egrep -i ${sensitive} | awk -F'[: ]' '{print $2}' | 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
    /usr/sbin/ipset add blacklist ${ip_file_ip} timeout 86400
fi

if [ -s ${sensitive_file} ];then
    for sensitive_ip in `cat ${sensitive_file}`
    do
        /usr/sbin/ipset add blacklist ${sensitive_ip}
    done
fi

2.加入crontab,定时运行脚本

*/3 * * * * /root/auto_ip_balcklist.sh

3.配置iptables

iptables -I INPUT -m set --match-set blacklist src -j DROP

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

PHP Cookbook

PHP Cookbook

Adam Trachtenberg、David Sklar / O'Reilly Media / 2006-08-01 / USD 44.99

When it comes to creating dynamic web sites, the open source PHP language is red-hot property: used on more than 20 million web sites today, PHP is now more popular than Microsoft's ASP.NET technology......一起来看看 《PHP Cookbook》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具