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

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

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

一、背景

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

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

二、具體步驟

1、確認(rèn)Nginx安裝LuaJIT (不會(huì)自己百度,有詳細(xì)教程)

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、編寫一個(gè)攔截的返回頁面(網(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; /* 按鈕文本居中對(duì)齊 */
  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)容包含危險(xiǎn)的攻擊請求</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、驗(yàn)證

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

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

相關(guān)文章

最新評(píng)論