banner
moeyy

moeyy

一条有远大理想的咸鱼。
github
mastodon
email

OpenResty(nginx擴展)實現防CC攻擊

本文介紹使用 openresty 來實現防 cc 攻擊的功能。openresty 官網http://openresty.org/cn/index.html。下面是防 cc 攻擊的流程圖。
根據流程圖,我們知道防 cc 攻擊主要包括兩部分,一是限制請求速度,二是給用戶發送 js 跳轉代碼進行驗證請求是否合法。

安裝依賴#

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
wget 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.moeyy.cn;
     
            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["moeyy_anticc"] and type(args["moeyy_anticc"])=='table' then
                             p_jskey=args["moeyy_anticc"][table.getn(args["moeyy_anticc"])]
                    else
                             p_jskey=args["moeyy_anticc"]
                    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.."&moeyy_anticc="..jspara
                            else
                                   url=ngx.var.scheme.."://"..ngx.var.host..uri.."?moeyy_anticc="..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.."&moeyy_anticc="..random
        else
            url=ngx.var.scheme.."://"..ngx.var.host..uri.."?moeyy_anticc="..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 小時。

載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。