自动拉黑IP的shell脚本

shell

#!/bin/bash
#该脚本可以根据web日志的访问量,自动拉黑IP(加入计划任务,结合计划任务在固定时间段内执行,并根据该时间段内产生的日志进行分析)

# bt 网站访问日志格式
# 139.59.61.4 - - [25/Mar/2022:15:16:37 +0800] "POST /admin/ HTTP/1.1" 200 31 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:62.0) Gecko/20100101 Firefox/62.0"
log_file_name=abczq.top.log
log_file_bak=/file/logs/${log_file_name}
log_file_run=/www/wwwlogs/${log_file_name}
max_num=20
if [! -d "/file/logs/"];then
    mkdir -p /file/logs/
fi

#首先把日志保存到根目录一份,计算日志有多少行
line1=`wc -l |awk '{print$1}'`
cp ${log_file_run} ${log_file_bak}
 
#计算现有的日志有多少行
line2=`wc -l /www/wwwlogs/${log_file_name} |awk '{print$1}'`

line_count=$((line2-line1))

#根据上一次备份的日志和现在拥有的行数差值,作为单位时间内分析日志访问量
tail -n $line_count $log_file_run|awk '{print$1}'|sort -n|uniq -c|sort -r>${log_file_bak}.txt

cat ${log_file_bak}.txt|while read line
do
    echo $line > ${log_file_bak}_line.txt
    num=`awk '{print$1}' ${log_file_bak}_line.txt`
    if((num<max_num))
    then
        break
    fi
    ip=`awk '{print$2}' ${log_file_bak}_line.txt`
    echo $ip
    # firewall-cmd --add-rich-rule="rule family=ipv4 source address='${ip}' port port=80 protocol=tcp reject" --permanent
    # firewall-cmd --reload
done

# firewall-cmd --list-all 
# /etc/firewalld/zones/public.xml
# https://www.freebuf.com/articles/web/235370.html
# */2 * * * * /file/cc.sh

lua

添加到location模块

access_by_lua_file "file/nginx/rate-limit.lua"

lua脚本

local function close_redis(red)
    if not red then
        return
    end
 
    local pool_max_idle_time = 10000
    local pool_size = 100
    local ok, err = red:set_keepalive(pool_max_idle_tme, pool_size)
    if not ok then
        ngx.say("set keepalive err : ", err)
    end
end
 
local ip_block_time = 120 --封禁IP时间(秒)
local ip_time_out = 60    --指定ip访问频率时间段(秒)
local ip_max_count = 10 --指定ip访问频率计数最大值(秒)
local BUSINESS = "sqd-ratelimit" --nginx的location中定义的业务标识符,我这里写死了。
 
--连接redis
local redis = require "resty.redis"
local conn = redis:new()
ok, err = conn:connect("127.0.0.1", 6379)
conn:set_timeout(2000) --超时时间2秒
 
--如果连接失败,跳转到脚本结尾
if not ok then
    --goto FLAG
    close_redis(conn)
end
 
local count, err = conn:get_reused_times()
if 0 == count then
    ----新建连接,需要认证密码
    ok, err = conn:auth("test111")
    if not ok then
        ngx.say("failed to auth: ", err)
        return
    end
elseif err then
    ----从连接池中获取连接,无需再次认证密码 ngx.say("failed to get reused times: ", err)
    return
end
 
--查询ip是否被禁止访问,如果存在则返回403错误代码
is_block, err = conn:get(BUSINESS .. "-BLOCK-" .. ngx.var.remote_addr)
if is_block == '1' then
    ngx.exit(429)
    close_redis(conn)
end
 
--查询redis中保存的ip的计数器
ip_count, err = conn:get(BUSINESS .. "-COUNT-" .. ngx.var.remote_addr)
 
if ip_count == ngx.null then
    --如果不存在,则将该IP存入redis,并将计数器设置为1、该KEY的超时时间为ip_time_out
    res, err = conn:set(BUSINESS .. "-COUNT-" .. ngx.var.remote_addr, 1)
    res, err = conn:expire(BUSINESS .. "-COUNT-" .. ngx.var.remote_addr, ip_time_out)
else
    if tonumber(ip_count) >= ip_max_count then
        --如果超过单位时间限制的访问次数,则添加限制访问标识,限制时间为ip_block_time
        res, err = conn:set(BUSINESS .. "-BLOCK-" .. ngx.var.remote_addr, 1)
        res, err = conn:expire(BUSINESS .. "-BLOCK-" .. ngx.var.remote_addr, ip_block_time)
    else
        res, err = conn:incr(BUSINESS .. "-COUNT-" .. ngx.var.remote_addr)
    end
end
 
-- 结束标记
local ok, err = conn:close()
posted @ 2022-03-25 16:36  绝技小嗨皮  阅读(167)  评论(0编辑  收藏  举报
Title