shell 封禁拉黑刷接口ip 支持nginx httpd

本文介绍了一种脚本,通过监控Nginx和HTTPD日志,每分钟检测并封禁1分钟内访问超过限制次数的IP,同时根据时间解封已过期的封禁。使用iptables实现动态防火墙规则管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

解释说明

支持nginx和httpd的日志格式,直接拿去用就行,用的是centos7环境,依托iptables,没有的需要安装并启动iptables
yum -y install iptables-services; systemctl start iptables

复制代码粘贴到服务器,init方法会自动添加services系统服务,使用systemctl就可以管理

bash 脚本.sh init 
systemctl start blacklist
shell代码
#!/bin/bash

:<<!
 实现原理:
	每隔1分钟执行封禁ip操作:检测日志前1分钟内所有ip的访问次数,大于限制次数就封禁ip,记录封禁ip的时间
	执行封禁ip操作后,执行解封操作:找出<=当前时间减去封禁时长的ip地址,解封此ip
!

# 监控日志路径,只支持nginx和httpd的日志格式
WebLog='/var/log/httpd/access_log'

# 封禁ip日志
BlockLog='/var/log/httpd/blacklist.log'

# pid文件目录
PidFile='/tmp/.blacklist.pid'

# 封禁端口(中间逗号隔开,不可有空格)
DropPort=80,443

# 每分钟最大访问量,1分钟内超出则封禁
MaxNum=100
# 封建时长(秒)
BlockTime=600

function Block {

	function getIpList {
		# 获取1分钟前所有超过访问最大次数的ip
		time1m=$(date -d '60 second ago' '+%d/%b/%Y:%T' | sed 's/..$//')
		grep "${time1m}" $WebLog |awk '{print $1}'|sort -n|uniq -c|awk '$1>count{print $1"-"$2}' count=$MaxNum

	}

	function blockIp {
		# 封禁传入的ip地址,并写入封禁日志
      	if [ $(iptables -t filter -nL BLACKLIST | grep $1 &>/dev/null;echo $?) -ne 0 ]
      	then
      		block_time=$(date '+%F %T')
      		block_time_stamp=$(date -d "$block_time" +%s)

      		iptables -t filter -I BLACKLIST -p tcp -s $1 -m comment --comment "$block_time $block_time_stamp $2" -m multiport --dports $DropPort -j DROP
			# echo "$block_time - $1 - $2" | tee $BlockLog
			echo "$block_time - $1 - $2" >> $BlockLog
			echo "BlockIp: $1 - $2" 
      	fi
	}

  	for i in $(getIpList)
    do
    	arr=(`echo $i|tr '-' ' '`)
    	blockIp ${arr[1]} ${arr[0]} 
  	done

}

function UnBlock {

	function getIdList {
		# 获得需要解禁的iptables的链号,从大到小排序
		time_stamp=$(date -d "$(date -d "$BlockTime second ago")" +%s)
		iptables -t filter -nL BLACKLIST --line-numbers| sed "1,2d" | sort -nr | awk '$10<=stamp{print $1}' stamp=$time_stamp
	}

    for i in $(getIdList)
    do  
        iptables -t filter -D BLACKLIST $i
    done

}

function Contrl {

	function start {

		# 创建pid文件
		echo $$ > $PidFile

		# 创建iptables链
		iptables -t filter -N BLACKLIST
		iptables -t filter -I INPUT -p tcp -j BLACKLIST

		while : 
		do
			Block
			UnBlock
			sleep 60
		done
	}

	function stop {

		# 杀死进程 删除进程pid文件
		kill $(cat $PidFile) &> /dev/null
		rm -rf $PidFile
		
		# 清空并删除iptables链
		iptables -t filter -F BLACKLIST
		iptables -t filter -D INPUT -p tcp -j BLACKLIST
	  	iptables -t filter -X BLACKLIST

	}

	case $1 in
	init)
		echo -e "[Unit]\nDescription=blacklist service\nAfter=iptables.service\nRequires=iptables.service\n\n[Service]\nExecStart=/bin/bash $(pwd)/$0 start\nExecReload=/bin/bash $(pwd)/$0 restart\nExecStop=/bin/bash $(pwd)/$0 stop\nPIDFile=${PidFile}\n\n[Install]\nWantedBy=multi-user.target" > /usr/lib/systemd/system/blacklist.service
		;;
	start)
		[ ! -f $WebLog ] && echo -e "\033[1;31mCan not find file:'$WebLog' \033[0m" && exit 1
		[ -f $PidFile ] && [ $(ps -ef | grep $(cat $PidFile) | grep -v 'grep' &> /dev/null ; echo $?) -eq 0 ] && echo -e "\033[1;31mProcess already exists. pid:`cat $PidFile` \033[0m" && exit 2

	  	start 
	  	;;
	stop)
		stop 
		;;
	restart)
		stop
		start 
		;;
	esac

}

Contrl $1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值