-
Notifications
You must be signed in to change notification settings - Fork 191
/
util.lua
executable file
·196 lines (175 loc) · 5.88 KB
/
util.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
--[[
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",
"headers.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 headers
function _M.get_headers()
local headers = ngx.req.get_headers()
for k, v in pairs(headers) do
ngx.log(ngx.DEBUG, string.format("k:%s, v:%s", k, v))
end
return headers
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, method, 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_method = method,
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 = io.open(log_name, "a")
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
if badGuys then
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
end
return _M