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


當然,他這個是用的阿里云的WAF服務,不用阿里云的服務也能做。
二、具體步驟
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)
end3、違禁詞記錄到 /application/lua/blocked_keywords.txt中

4、編寫一個攔截的返回頁面(網上抄的)
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>網站防火墻</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; /* 調整按鈕內邊距 */
background: #6bb3f6;
color: white;
text-decoration: none;
border-radius: 5px;
display: inline-block; /* 改為行內塊元素 */
text-align: center; /* 按鈕文本居中對齊 */
font-size: 14px; /* 調整字體大小 */
}
.button:hover {
background: #5aa1e3;
cursor: pointer;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
網站防火墻
</div>
<div class="content">
<div class="text-content">
<p><span style="font-weight:600; color:#fc4f03;">您的請求帶有不合法參數,已被網站管理員設置攔截!</span></p>
<p>可能原因:您提交的內容包含危險的攻擊請求</p>
<p>如何解決:</p>
<ul>
<li>1)檢查提交內容;</li>
<li>2)如網站托管,請聯系空間提供商;</li>
<li>3)普通網站訪客,請聯系網站管理員;</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;
- 網站配置文件引用腳本
access_by_lua_file /application/lua/url_filter.lua;

6、reload Nginx
nginx -s reload
7、驗證

8、后續(xù)可添加其他功能。
到此這篇關于Nginx + lua 實現WAF的文章就介紹到這了,更多相關Nginx lua 實現WAF內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Nginx+cpolar實現內網穿透多個Windows Web站點端口的步驟詳解
這篇文章主要給大家介紹了Nginx+cpolar實現內網穿透多個Windows Web站點端口的詳細步驟,文章通過圖文介紹的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下2023-10-10
Nginx服務器作反向代理實現內部局域網的url轉發(fā)配置
這篇文章主要介紹了Nginx服務器作反向代理實現內部局域網的url轉發(fā)實例,文中提到需要注意proxy_read_timeout參數的相關調整,需要的朋友可以參考下2016-01-01

