nginx扩展 OpenResty 实现防cc攻击教程

图片[1]-nginx扩展 OpenResty 实现防cc攻击教程-北冥博客

使用openresty来实现防cc攻击的功能。openresty官网http://openresty.org/cn/index.html。下面是防cc攻击的流程图。 根据流程图,我们知道防cc攻击主要包括两部分,一是限制请求速度,二是给用户发送js跳转代码进行验证请求是否合法。

安装依赖

RHEL/Centos:
yum install readline-devel pcre-devel openssl-devel
ubuntu:
apt-get install libreadline-dev libncurses5-dev libpcre3-dev libssl-dev perl

luajit安装

cd /tmp/
git clone http://luajit.org/git/luajit-2.0.git
cd luajit-2.0/
make && make install
ln -sf luajit-2.0.0-beta10 /usr/local/bin/luajit
ln -sf /usr/local/lib/libluajit-5.1.so.2 /usr/lib/

openresty安装

cd /tmp
get http://agentzh.org/misc/nginx/ngx_openresty-1.2.4.13.tar.gz
tar xzf ngx_openresty-1.2.4.13.tar.gz
cd ngx_openresty-1.2.4.13/
./configure --prefix=/usr/local/openresty --with-luajit
 make && make install

Nginx配置

nginx.conf:

    http{
    [......]
    lua_shared_dict limit 10m;
    lua_shared_dict jsjump 10m;
     
        server {
    #lua_code_cache off;
            listen       80;
            server_name  www.centos.bz;
     
            location / {
    default_type  text/html;
    content_by_lua_file "/usr/local/openresty/nginx/conf/lua";
            }
            location @cc {
                internal;
                root   html;
                index  index.html index.htm;
            }
        }
    }

/usr/local/openresty/nginx/conf/lua文件:

    local ip = ngx.var.binary_remote_addr
    local limit = ngx.shared.limit
    local req,_=limit:get(ip)
    if req then
            if req > 20 then
                    ngx.exit(503)
            else
                    limit:incr(ip,1)
            end
    else
            limit:set(ip,1,10)
    end
     
    local jsjump = ngx.shared.jsjump
    local uri = ngx.var.request_uri
    local jspara,flags=jsjump:get(ip)
    local args = ngx.req.get_uri_args()
    if jspara then
        if flags then
            ngx.exec("@cc")
        else
                    local p_jskey=''
                    if args["jskey"] and type(args["jskey"])=='table' then
                             p_jskey=args["jskey"][table.getn(args["jskey"])]
                    else
                             p_jskey=args["jskey"]
                    end
            if p_jskey and p_jskey==tostring(jspara) then
                            jsjump:set(ip,jspara,3600,1)
                            ngx.exec("@cc")
            else
                            local url=''
                            if ngx.var.args then
                                   url=ngx.var.scheme.."://"..ngx.var.host..uri.."&jskey="..jspara
                            else
                                   url=ngx.var.scheme.."://"..ngx.var.host..uri.."?jskey="..jspara
                            end
                            local jscode="window.location.href='"..url.."';"
                            ngx.say(jscode)
            end
        end
    else
    math.randomseed( os.time() );
        local random=math.random(100000,999999)
        jsjump:set(ip,random,60)
        local url=''
        if ngx.var.args then
            url=ngx.var.scheme.."://"..ngx.var.host..uri.."&jskey="..random
        else
            url=ngx.var.scheme.."://"..ngx.var.host..uri.."?jskey="..random
        end
        local jscode="window.location.href='"..url.."';"
        ngx.say(jscode)
    end

最后

 lua代码部分解释

1、1-12行是限速功能实现,第5和第10行表示10秒钟内容最多只能请求20次。

2、14-48行是验证部分,24行中的3600表示验证通过后,白名单时间为3600秒,即1小时。

温馨提示:本文最后更新于2021-11-18,若文件或内容有错误或已失效,请在下方留言
© 版权声明
THE END
喜欢就支持一下吧
点赞0赞赏 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容