代码拉取完成,页面将自动刷新
同步操作将从 欧流全/x-waf 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
--[[
Copyright (c) 2016 xsec.io
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEq
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
]]
local io = require("io")
local cjson = require("cjson.safe")
local string = require("string")
local config = require("config")
local _M = {
version = "0.1",
RULE_TABLE = {},
RULE_FILES = {
"args.rule",
"blackip.rule",
"cookie.rule",
"post.rule",
"url.rule",
"useragent.rule",
"whiteip.rule",
"whiteUrl.rule"
}
}
-- Get all rule file name
function _M.get_rule_files(rules_path)
local rule_files = {}
for _, file in ipairs(_M.RULE_FILES) do
if file ~= "" then
local file_name = rules_path .. '/' .. file
ngx.log(ngx.DEBUG, string.format("rule key:%s, rule file name:%s", file, file_name))
rule_files[file] = file_name
end
end
return rule_files
end
-- Load WAF rules into table when on nginx's init phase
function _M.get_rules(rules_path)
local rule_files = _M.get_rule_files(rules_path)
if rule_files == {} then
return nil
end
for rule_name, rule_file in pairs(rule_files) do
local t_rule = {}
local file_rule_name = io.open(rule_file)
local json_rules = file_rule_name:read("*a")
file_rule_name:close()
local table_rules = cjson.decode(json_rules)
if table_rules ~= nil then
ngx.log(ngx.INFO, string.format("%s:%s", table_rules, type(table_rules)))
for _, table_name in pairs(table_rules) do
-- ngx.log(ngx.INFO, string.format("Insert table:%s, value:%s", t_rule, table_name["RuleItem"]))
table.insert(t_rule, table_name["RuleItem"])
end
end
ngx.log(ngx.INFO, string.format("rule_name:%s, value:%s", rule_name, t_rule))
_M.RULE_TABLE[rule_name] = t_rule
end
return (_M.RULE_TABLE)
end
-- Get the client IP
function _M.get_client_ip()
local CLIENT_IP = ngx.req.get_headers()["X_real_ip"]
if CLIENT_IP == nil then
CLIENT_IP = ngx.req.get_headers()["X_Forwarded_For"]
end
if CLIENT_IP == nil then
CLIENT_IP = ngx.var.remote_addr
end
if CLIENT_IP == nil then
CLIENT_IP = ""
end
return CLIENT_IP
end
-- Get the client user agent
function _M.get_user_agent()
local USER_AGENT = ngx.var.http_user_agent
if USER_AGENT == nil then
USER_AGENT = "unknown"
end
return USER_AGENT
end
-- get server's host
function _M.get_server_host()
local host = ngx.req.get_headers()["Host"]
return host
end
-- Get all rule file name by lfs
--function _M.get_rule_files(rules_path)
--local lfs = require("lfs")
-- local rule_files = {}
-- for file in lfs.dir(rules_path) do
-- if file ~= "." and file ~= ".." then
-- local file_name = rules_path .. '/' .. file
-- ngx.log(ngx.DEBUG, string.format("rule key:%s, rule file name:%s", file, file_name))
-- rule_files[file] = file_name
-- end
-- end
-- return rule_files
--end
-- WAF log record for json
function _M.log_record(config_log_dir, attack_type, url, data, ruletag)
local log_path = config_log_dir
local client_IP = _M.get_client_ip()
local user_agent = _M.get_user_agent()
local server_name = ngx.var.server_name
local local_time = ngx.localtime()
local log_json_obj = {
client_ip = client_IP,
local_time = local_time,
server_name = server_name,
user_agent = user_agent,
attack_type = attack_type,
req_url = url,
req_data = data,
rule_tag = ruletag,
}
local log_line = cjson.encode(log_json_obj)
local log_name = string.format("%s/%s_waf.log", log_path, ngx.today())
local file, err = io.open(log_name, "a+")
if err ~= nil then ngx.log(ngx.DEBUG, "file err:" .. err) end
if file == nil then
return
end
file:write(string.format("%s\n", log_line))
file:flush()
file:close()
end
-- WAF response
function _M.waf_output()
if config.config_waf_model == "redirect" then
ngx.redirect(config.config_waf_redirect_url, 301)
elseif config.config_waf_model == "jinghuashuiyue" then
local bad_guy_ip = _M.get_client_ip()
_M.set_bad_guys(bad_guy_ip, config.config_expire_time)
else
ngx.header.content_type = "text/html"
ngx.status = ngx.HTTP_FORBIDDEN
ngx.say(string.format(config.config_output_html, _M.get_client_ip()))
ngx.exit(ngx.status)
end
end
-- set bad guys ip to ngx.shared dict
function _M.set_bad_guys(bad_guy_ip, expire_time)
local badGuys = ngx.shared.badGuys
local req, _ = badGuys:get(bad_guy_ip)
if req then
badGuys:incr(bad_guy_ip, 1)
else
badGuys:set(bad_guy_ip, 1, expire_time)
end
end
return _M
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。