【Nginx】Openresty增長waf配置

1. Ngx lua waf 說明

防止sql注入,本地包含,部分溢出,fuzzing測試,xss,SSRF等web攻擊
防止svn/備份之類文件泄漏
防止ApacheBench之類壓力測試工具的攻擊
屏蔽常見的掃描黑客工具,掃描器
屏蔽異常的網絡請求
屏蔽圖片附件類目錄php執行權限
防止webshell上傳php

2. 下載 waf

使用githtml

git clone https://github.com/loveshell/...nginx

使用wgetgit

wget https://github.com/loveshell/...github

3. 安裝

下載解壓後,將整 ngx_lua_waf 放到 nginx conf 目錄中,並命名爲 waf;

4. 配置

nginx安裝路徑假設爲:/usr/local/nginx/conf/ ;如下都將以此配置爲例進行說明web

4.1. 在nginx.conf的http段添加

lua_package_path "/usr/local/nginx/conf/waf/?.lua";
lua_shared_dict limit 10m;
init_by_lua_file  /usr/local/nginx/conf/waf/init.lua;
access_by_lua_file /usr/local/nginx/conf/waf/waf.lua;

4.2. 配置config.lua

--配置waf規則目錄
RulePath = "/usr/local/nginx/conf/waf/wafconf/"

--修改失敗提示
html=[[{"retcode":"21000","messages":"請求失敗,請稍後再試","body":{}}]]

--開啓cc攻擊,須要在nginx.conf http 段配置lua_shared_dict
CCDeny="on"

-- 設置cc攻擊頻率,單位爲秒 (如:同一個ip請求同一個地址,每秒最多請求50次)
CCrate = "50/1"

4.3. 其餘詳細配置說明

RulePath = "/usr/local/nginx/conf/waf/wafconf/"
    --規則存放目錄

    attacklog = "off"
    --是否開啓攻擊信息記錄,須要配置logdir

    logdir = "/usr/local/nginx/logs/hack/"
    --log存儲目錄,該目錄須要用戶本身新建,切須要nginx用戶的可寫權限

    UrlDeny="on"
    --是否攔截url訪問

    Redirect="on"
    --是否攔截後重定向

    CookieMatch = "on"
    --是否攔截cookie攻擊

    postMatch = "on"
    --是否攔截post攻擊

    whiteModule = "on"
    --是否開啓URL白名單

    black_fileExt={"php","jsp"}
    --填寫不容許上傳文件後綴類型

    ipWhitelist={"127.0.0.1"}
    --ip白名單,多個ip用逗號分隔

    ipBlocklist={"1.0.0.1"}
    --ip黑名單,多個ip用逗號分隔

    CCDeny="on"
    --是否開啓攔截cc攻擊(須要nginx.conf的http段增長lua_shared_dict limit 10m;)

    CCrate = "100/60"
    --設置cc攻擊頻率,單位爲秒.
    --默認1分鐘同一個IP只能請求同一個地址100次

    html=[[Please go away~~]]
    --警告內容,可在中括號內自定義

5 修改 waf/init.lua 文件

替換sys_html 函數:sql

function say_html()
    if Redirect then
        ngx.header.content_type = "text/html;charset=UTF-8"
        ngx.status = ngx.HTTP_FORBIDDEN
        ngx.say(html)
        ngx.exit(ngx.status)
    end
end

替換denycc 函數:shell

function denycc()
    if CCDeny then
        local uri=ngx.var.uri
        CCcount=tonumber(string.match(CCrate,'(.*)/'))
        CCseconds=tonumber(string.match(CCrate,'/(.*)'))
        local token = getClientIp()..uri
        local limit = ngx.shared.limit
        local req,_=limit:get(token)
        if req then
            if req > CCcount then
                ngx.header.content_type = "application/json;charset=UTF-8"
                local ret={returncode='22000',messages='請求拒絕,請稍後再試',body={}}
                --ngx.header['Content-Type']="text/html;charset=UTF-8"
                ngx.say(json.encode(ret))
                ngx.exit(200)
                return true
            else
                 limit:incr(token,1)
            end
        else
            limit:set(token,1,CCseconds)
        end
    end
    return false
end

6. 啓用waf

而後重啓nginx,或reload 便可:
/user/local/nginx/sbin/nginx -s reload

7. 測試

curl http://www.test.cn/test/index?id=../etc/passwd

返回:{"retcode":"21000","messages":"請求失敗,請稍後再試","body":{}} 說明生效
相關文章
相關標籤/搜索