欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Nginx + lua 實現(xiàn)WAF的詳細過程

 更新時間:2024年07月08日 11:05:52   作者:粥小羊i  
這篇文章主要介紹了Nginx + lua 實現(xiàn)WAF的詳細過程,本文給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧

一、背景

    近期發(fā)現(xiàn)公司網(wǎng)站有SEO攻擊,為了解決這個問題需要有一個違禁詞攔截,例如以下例子(雨果網(wǎng)):

當(dāng)然,他這個是用的阿里云的WAF服務(wù),不用阿里云的服務(wù)也能做。

二、具體步驟

1、確認Nginx安裝LuaJIT (不會自己百度,有詳細教程)

2、lua腳本如下:

local shared_dict_name = "blocked_keywords"
local file_path = "/application/lua/blocked_keywords.txt"
local template = require "resty.template"
local shared_dict = ngx.shared[shared_dict_name]
local last_modified_time = 0
local function load_keywords_from_file()
    local file, err = io.open(file_path, "r")
    if not file then
        ngx.log(ngx.ERR, "Unable to open " .. file_path .. ": " .. (err or "Unknown error"))
        return nil
    end
    local keywords = {}
    for line in file:lines() do
        local keyword = line:match("^%s*(.-)%s*$")
        if keyword and keyword ~= "" then
            keywords[#keywords + 1] = keyword
            shared_dict:set(keyword, true)
        end
    end
    file:close()
    return keywords
end
local function get_keywords()
    local keywords = shared_dict:get_keys() or {}
    if #keywords == 0 or ngx.time() - last_modified_time > 60 then
        keywords = load_keywords_from_file()
    end
    return keywords
end
local function is_blocked(request_url)
    local keywords = get_keywords()
    for _, keyword in ipairs(keywords) do
        if string.find(request_url, keyword, 1, true) then
            return true
        end
    end
    return false
end
local request_url = ngx.var.uri
if is_blocked(request_url) then
    ngx.status = ngx.HTTP_FORBIDDEN
    ngx.header.content_type = "text/html; charset=utf-8"
    ngx.say(template.CONTENT)
    return ngx.exit(ngx.HTTP_FORBIDDEN)
end

3、違禁詞記錄到 /application/lua/blocked_keywords.txt中

4、編寫一個攔截的返回頁面(網(wǎng)上抄的)

cat /usr/local/openresty/lualib/resty/template.lua

-- resty/template.lua
 
local _M = {}
 
_M.CONTENT = [[
<html xmlns="http://www.xxxx.cn">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>網(wǎng)站防火墻</title>
<style>
body {
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 0;
  padding: 0;
  font: 14px/1.5 Microsoft Yahei, 宋體, sans-serif;
  color: #555;
  background-color: #f3f7f9;
}
 
.container {
  width: 1000px; /* 定義容器的寬度 */
  padding-top: 70px; /* 保持原有的上邊距 */
}
 
.header {
  height: 40px;
  line-height: 40px;
  color: #fff;
  font-size: 16px;
  background: #6bb3f6;
  padding-left: 20px;
}
 
.content {
  border: 1px dashed #cdcece;
  border-top: none;
  font-size: 14px;
  background: #fff;
  color: #555;
  line-height: 24px;
  min-height: 220px; /* 使用最小高度而不是固定高度 */
  padding: 20px;
  background: #f3f7f9;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}
 
.text-content {
  flex: 1;
}
 
.button {
  margin-top: 20px;
  padding: 5px 10px; /* 調(diào)整按鈕內(nèi)邊距 */
  background: #6bb3f6;
  color: white;
  text-decoration: none;
  border-radius: 5px;
  display: inline-block; /* 改為行內(nèi)塊元素 */
  text-align: center; /* 按鈕文本居中對齊 */
  font-size: 14px; /* 調(diào)整字體大小 */
}
 
.button:hover {
  background: #5aa1e3;
  cursor: pointer;
}
</style>
</head>
 
<body>
<div class="container">
  <div class="header">
    網(wǎng)站防火墻
  </div>
  <div class="content">
    <div class="text-content">
      <p><span style="font-weight:600; color:#fc4f03;">您的請求帶有不合法參數(shù),已被網(wǎng)站管理員設(shè)置攔截!</span></p>
      <p>可能原因:您提交的內(nèi)容包含危險的攻擊請求</p>
      <p>如何解決:</p>
      <ul>
        <li>1)檢查提交內(nèi)容;</li>
        <li>2)如網(wǎng)站托管,請聯(lián)系空間提供商;</li>
        <li>3)普通網(wǎng)站訪客,請聯(lián)系網(wǎng)站管理員;</li>
      </ul>
    </div>
    <!-- 返回首頁按鈕 -->
    <a href="https://xxxx" rel="external nofollow"  class="button">返回首頁</a>
  </div>
</div>
</body>
</html>
]]
 
return _M

5、Nginx配置文件引入lua腳本

- 首先nginx.conf主配置文件加入
    lua_shared_dict blocked_keywords 10m;

- 網(wǎng)站配置文件引用腳本
access_by_lua_file /application/lua/url_filter.lua;

6、reload Nginx

nginx -s reload

7、驗證

8、后續(xù)可添加其他功能。

到此這篇關(guān)于Nginx + lua 實現(xiàn)WAF的文章就介紹到這了,更多相關(guān)Nginx lua 實現(xiàn)WAF內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • nginx緩存不起作用問題解決方法

    nginx緩存不起作用問題解決方法

    nginx代理做好了,緩存也配置好了,但是發(fā)現(xiàn)css、js、jpg這些靜態(tài)文件統(tǒng)統(tǒng)都cached成功。但是偏偏頁面文件依舊到源服務(wù)器取
    2014-04-04
  • Nginx丟棄http包體處理實例詳解

    Nginx丟棄http包體處理實例詳解

    這篇文章主要介紹了Nginx丟棄http包體處理實例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • nginx外網(wǎng)訪問內(nèi)網(wǎng)站點配置操作

    nginx外網(wǎng)訪問內(nèi)網(wǎng)站點配置操作

    這篇文章主要介紹了nginx外網(wǎng)訪問內(nèi)網(wǎng)站點配置操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • Nginx中server_name的超詳細使用指南

    Nginx中server_name的超詳細使用指南

    這篇文章主要介紹了Nginx的server_name指令及其在DNS解析和多域名配置中的應(yīng)用,包括如何使用通配符和正則表達式進行匹配,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2025-03-03
  • Nginx在MAC上的安裝、啟動、重啟和關(guān)閉

    Nginx在MAC上的安裝、啟動、重啟和關(guān)閉

    這篇文章主要介紹了Nginx在MAC上的安裝、啟動、重啟和關(guān)閉的相關(guān)資料,需要的朋友可以參考下
    2018-03-03
  • nginx.conf配置兩個前端路徑

    nginx.conf配置兩個前端路徑

    本文主要介紹了nginx.conf配置兩個前端路徑,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • Nginx源碼研究之nginx限流模塊詳解

    Nginx源碼研究之nginx限流模塊詳解

    這篇文章主要介紹了Nginx源碼研究之nginx限流模塊詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-09-09
  • 詳解Nginx服務(wù)器的配置中開啟文件Gzip壓縮的方法

    詳解Nginx服務(wù)器的配置中開啟文件Gzip壓縮的方法

    這篇文章主要介紹了Nginx服務(wù)器的配置中開啟文件Gzip壓縮的方法,可以對CSS和JavaScript以及各種圖片等web傳輸?shù)奈募M行壓縮,需要的朋友可以參考下
    2016-01-01
  • 解決國內(nèi)k8s的ingress-nginx鏡像無法正常pull拉取問題

    解決國內(nèi)k8s的ingress-nginx鏡像無法正常pull拉取問題

    本文主要介紹了解決國內(nèi)k8s的ingress-nginx鏡像無法正常pull拉取問題,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-03-03
  • nginx設(shè)置目錄白名單、ip白名單的實現(xiàn)方法

    nginx設(shè)置目錄白名單、ip白名單的實現(xiàn)方法

    今天小編就為大家分享一篇nginx設(shè)置目錄白名單、ip白名單的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08

最新評論