Nginx + lua 實現(xiàn)WAF的詳細(xì)過程
一、背景
近期發(fā)現(xiàn)公司網(wǎng)站有SEO攻擊,為了解決這個問題需要有一個違禁詞攔截,例如以下例子(雨果網(wǎng)):


當(dāng)然,他這個是用的阿里云的WAF服務(wù),不用阿里云的服務(wù)也能做。
二、具體步驟
1、確認(rèn)Nginx安裝LuaJIT (不會自己百度,有詳細(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)
end3、違禁詞記錄到 /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 _M5、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+cpolar實現(xiàn)內(nèi)網(wǎng)穿透多個Windows Web站點端口的步驟詳解
這篇文章主要給大家介紹了Nginx+cpolar實現(xiàn)內(nèi)網(wǎng)穿透多個Windows Web站點端口的詳細(xì)步驟,文章通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-10-10
Nginx代理導(dǎo)致請求頭某些內(nèi)容丟失的問題解決
本文主要介紹了在使用NGINX代理時請求頭中的下劃線被自動忽略的問題,通過兩種方法解決了這個問題,具有一定的參考價值,感興趣的可以了解一下2025-02-02
Nginx服務(wù)器作反向代理實現(xiàn)內(nèi)部局域網(wǎng)的url轉(zhuǎn)發(fā)配置
這篇文章主要介紹了Nginx服務(wù)器作反向代理實現(xiàn)內(nèi)部局域網(wǎng)的url轉(zhuǎn)發(fā)實例,文中提到需要注意proxy_read_timeout參數(shù)的相關(guān)調(diào)整,需要的朋友可以參考下2016-01-01
使用Nginx實現(xiàn)根據(jù) IP 匹配指定 URL
最近的一個項目,需要特定的IP訪問某專題頁面的時候跳轉(zhuǎn)到網(wǎng)站首頁,思考了下,直接使用NGINX實現(xiàn),分享給大家。2014-09-09

