给网站增加IP黑名单禁止IP访问

突然哪一天,你的网站来了好几百的UV请求都是来自奇怪的源,并且写着:某某流量推广,这些肯定是无用流量,不消费内容,纯粹给他们打广告,就好像在你家里放了个大喇叭,每天在你家里吵吵,别说技术人了,普通人都不能忍,试了好些方法,居然挡不住他们的机器人,想想都是泪,今天决定彻底挡住这些IP的请求,所以,找到一种有效的方法后,终于是安静了很多!


类似这样子的请求:

09:59:10

必应网页 (免费快排实战教程-6公众号:chongtian-ai)

http://www.it3q.com/?http://

61.165.238.33

上海市-上海市


连续好几天都是几百的UV的独立请求,这最大可能是机器人吧,但是是不是找错了目标,试了好几种方法,防火墙、Nginx都没能防住机器人广告轰炸请求,小站是不是很苦?


1、iptables禁止IP访问:

iptables -A INPUT -s 221.213.91.172 -p tcp --dport 80 -j DROP

iptables -A INPUT -s 221.213.91.172 -p tcp --dport 443 -j DROP


但我们知道服务器看到的客户端地址是内网IP,经过NAT转换的IP你不一定看得到,所以这波配置很大可能无效!


2、ufw的防火墙配置,配置完了也没有挡住这些ip的访问,每天依旧是在刷!

ufw deny from  171.212.126.109


3、nginx配置deny ip:

   deny 43.243.12.116;

   deny 43.241.242.243;

   allow all;

无效!


4、openresty+lua的waf工具,利用开源软件自建WAF系统--OpenResty+unixhot,参考链接,升级openresty的nginx版本,下载https://github.com/unixhot/waf


配置rule-config/blackip.rule 列表,发现还是有问题,问题出在获取源ip的方法上,lib.lua文件中有get_client_ip()的方法:

--Get the client IP
function get_client_ip()
    CLIENT_IP = ngx.req.get_headers()["X_real_ip"]
    if CLIENT_IP == nil then
        CLIENT_IP = ngx.req.get_headers()["X_Forwarded_For"]
    end
    if CLIENT_IP == nil then
        CLIENT_IP  = ngx.var.remote_addr
    end
    if CLIENT_IP == nil then
        CLIENT_IP  = "unknown"
    end
    return CLIENT_IP
end

修改:

--Get the client IP
function get_client_ip()
    local headers=ngx.req.get_headers()
    local clientIP = headers["x-forwarded-for"]
    if clientIP == nil or string.len(clientIP) == 0 or clientIP == "unknown" then
        clientIP = headers["Proxy-Client-IP"]
    end
    if clientIP == nil or string.len(clientIP) == 0 or clientIP == "unknown" then
        clientIP = headers["WL-Proxy-Client-IP"]
    end
    if clientIP == nil or string.len(clientIP) == 0 or clientIP == "unknown" then
        clientIP = ngx.var.remote_addr
    end
    -- 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
    if clientIP ~= nil and string.len(clientIP) >15  then
        local pos  = string.find(clientIP, ",", 1)
        clientIP = string.sub(clientIP,1,pos-1)
    end

    return clientIP
end


当前看只有openresty+lua的waf工具能过滤这些IP,世界终于安静了!


呱牛笔记

本文为呱牛笔记原创文章,转载无需和我联系,但请注明来自呱牛笔记 ,it3q.com

请先登录后发表评论
  • 最新评论
  • 总共0条评论