-
Notifications
You must be signed in to change notification settings - Fork 1
/
waf.lua
412 lines (376 loc) · 14.2 KB
/
waf.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
local Util = require("util")
local BanAction = require("ban")
local Ddos = require("ddos")
local Waf = {}
-- ip白名单模块
function Waf:ipInWhiteList(ip, uri)
-- 判断是否开启ip白名单模块
if _conf.whiteIpModulesIsOn then
-- 匹配白名单列表
if _conf.whiteIpList ~= "" and _rulematch(ip, _conf.whiteIpList, "jo") then
Util:logDebug("[ipInWhiteList]", ip, ip, uri)
return true
else
return false
end
end
end
-- ip黑名单模块
function Waf:ipInBlackList(ip, uri)
-- 判断是否开启ip黑名单模块
if _conf.blackIpModulesIsOn then
-- 匹配黑名单列表
if _conf.blackIpList ~= "" and _rulematch(ip, _conf.blackIpList, "jo") then
Util:logWaf("[ipInBlackList]", ip, ip, uri)
BanAction:banAction(_conf.blackIpBanAction, ip)
return true
else
return false
end
end
end
-- url白名单模块
function Waf:urlInWhiteList(ip, uri)
-- 判断是否开启url白名单模块
if _conf.whiteUrlModulesIsOn then
-- 匹配白名单列表
if _conf.whiteUrlList ~= "" and _rulematch(uri, _conf.whiteUrlList, "joi") then
Util:logDebug("[urlInWhiteList]", uri, ip, uri)
return true
else
return false
end
end
end
-- url黑名单模块
function Waf:urlInBlackList(ip, uri)
-- 判断是否开启url黑名单模块
if _conf.blackUrlModulesIsOn then
-- 匹配黑名单列表
if _conf.blackUrlList ~= "" and _rulematch(uri, _conf.blackUrlList, "joi") then
Util:logWaf("[urlInBlackList]", uri, ip, uri)
BanAction:banAction(_conf.blackUrlBanAction, ip)
return true
else
return false
end
end
end
-- 域名白名单模块
function Waf:siteInWhiteList(ip, uri)
-- 判断是否开启域名白名单模块
if _conf.whiteSiteModulesIsOn then
-- 匹配白名单列表
local site = ngx.var.host
if _conf.whiteSiteList ~= "" and _rulematch(site, _conf.whiteSiteList, "joi") then
Util:logDebug("[siteInWhiteList]", site, ip, uri)
return true
else
return false
end
end
end
-- 域名黑名单模块
function Waf:siteInBlackList(ip, uri)
-- 判断是否开启域名黑名单模块
if _conf.blackSiteModulesIsOn then
-- 匹配黑名单列表
local site = ngx.var.host
if _conf.blackSiteList ~= "" and _rulematch(site, _conf.blackSiteList, "joi") then
Util:logWaf("[siteInBlackList]", site, ip, uri)
BanAction:banAction(_conf.blackSiteBanAction, ip)
return true
else
return false
end
end
end
-- ua黑名单模块
function Waf:uaInBlackList(ip, uri)
-- 判断是否开启ua黑名单模块
if _conf.blackUaModulesIsOn then
-- 匹配黑名单列表
local ua = ngx.var.http_user_agent
if _conf.blackUaList ~= "" and _rulematch(ua, _conf.blackUaList, "joi") then
Util:logWaf("[uaInBlackList]", ua, ip, uri)
BanAction:banAction(_conf.blackUaBanAction, ip)
return true
else
return false
end
end
end
-- sql注入防御模块
function Waf:sqlInjectList(ip, uri, postArgs)
-- 判断是否开启注入防御模块
if _conf.antiInjectModuleIsOn then
local matched, args = self:isInjectListMatched(uri, _conf.sqlInjectList, postArgs)
if matched then
local banAction = _conf.antiInjectBanAction
-- 如果连续注入试探超配置限制则调用封禁模块进行拦截封禁
if _conf.antiInjectCnt > 0 then
-- 获取该ip已经尝试深入注入了多少次
local ipDomain = ip..ngx.var.host
local injectCnt = _conf.injectStatDict:get(ipDomain)
if injectCnt then
injectCnt = injectCnt + 1
else
injectCnt = 1
end
_conf.injectStatDict:set(ipDomain, injectCnt, _conf.antiInjectTime)
-- 注入超过限制次数,iptables ban
if injectCnt > _conf.antiInjectCnt then
banAction = _conf.antiInjectOverAction
end
end
Util:logWaf("[sqlInjectList]", args, ip, uri)
BanAction:banAction(banAction, ip)
return true
end
return false
end
end
-- xss注入防御模块
function Waf:xssInjectList(ip, uri, postArgs)
-- 判断是否开启注入防御模块
if _conf.antiInjectModuleIsOn then
local matched, args = self:isInjectListMatched(uri, _conf.xssInjectList, postArgs)
if matched then
local banAction = _conf.antiInjectBanAction
-- 如果连续注入试探超配置限制则调用封禁模块进行拦截封禁
if _conf.antiInjectCnt > 0 then
-- 获取该ip已经尝试深入注入了多少次
local ipDomain = ip..ngx.var.host
local injectCnt = _conf.injectStatDict:get(ipDomain)
if injectCnt then
injectCnt = injectCnt + 1
else
injectCnt = 1
end
_conf.injectStatDict:set(ipDomain, injectCnt, _conf.antiInjectTime)
-- 注入超过限制次数,iptables ban
if injectCnt > _conf.antiInjectCnt then
banAction = _conf.antiInjectOverAction
end
end
Util:logWaf("[xssInjectList]", args, ip, uri)
BanAction:banAction(banAction, ip)
return true
end
return false
end
end
-- cmd注入防御模块
function Waf:cmdInjectList(ip, uri, postArgs)
-- 判断是否开启注入防御模块
if _conf.antiInjectModuleIsOn then
local matched, args = self:isInjectListMatched(uri, _conf.cmdInjectList, postArgs)
if matched then
local banAction = _conf.antiInjectBanAction
-- 如果连续注入试探超配置限制则调用封禁模块进行拦截封禁
if _conf.antiInjectCnt > 0 then
-- 获取该ip已经尝试深入注入了多少次
local ipDomain = ip..ngx.var.host
local injectCnt = _conf.injectStatDict:get(ipDomain)
if injectCnt then
injectCnt = injectCnt + 1
else
injectCnt = 1
end
_conf.injectStatDict:set(ipDomain, injectCnt, _conf.antiInjectTime)
-- 注入超过限制次数,iptables ban
if injectCnt > _conf.antiInjectCnt then
banAction = _conf.antiInjectOverAction
end
end
Util:logWaf("[cmdInjectList]", args, ip, uri)
BanAction:banAction(banAction, ip)
return true
end
return false
end
end
-- referer注入防御模块
function Waf:refInjectList(ip, uri)
-- 判断是否开启注入防御模块
if _conf.antiInjectModuleIsOn then
local referer = ngx.var.http_referer
if not referer then
return false
end
local matched, args = self:isInjectListMatched(referer, _conf.refInjectList)
if matched then
local banAction = _conf.antiInjectBanAction
-- 如果连续注入试探超配置限制则调用封禁模块进行拦截封禁
if _conf.antiInjectCnt > 0 then
-- 获取该ip已经尝试深入注入了多少次
local ipDomain = ip..ngx.var.host
local injectCnt = _conf.injectStatDict:get(ipDomain)
if injectCnt then
injectCnt = injectCnt + 1
else
injectCnt = 1
end
_conf.injectStatDict:set(ipDomain, injectCnt, _conf.antiInjectTime)
-- 注入超过限制次数,iptables ban
if injectCnt > _conf.antiInjectCnt then
banAction = _conf.antiInjectOverAction
end
end
Util:logWaf("[refInjectList]", args, ip, uri)
BanAction:banAction(banAction, ip)
return true
end
return false
end
end
-- 注入规则匹配
function Waf:isInjectListMatched(injectCont, injectList, postArgs)
-- 配置规则文件为空,不进行检测
if not injectList or injectList == "" then
return false
end
-- 先url匹配注入规则列表
if _rulematch(injectCont, injectList, "joi") then
return true, injectCont
end
-- 再post匹配注入规则列表
if postArgs then
local args = nil
for key, val in pairs(postArgs) do
if type(val) == "table" then
if type(val[1]) == "boolean" then
return
end
args = table.concat(val,", ")
else
args = val
end
if _rulematch(args, injectList, "joi") then
return true, args
end
end
end
return false
end
-- 域名限速模块,核心模块
function Waf:limitDomainModule(ip, uri)
if _conf.limitDomainModuleIsOn then
local domain = ngx.var.host
local domainReq = _conf.limitReqDict:get(domain)
local domaninIsBan = _conf.limitStatDict:get(domain)
-- 先统计请求次数
if domainReq then
-- 域名请求次数递增
domainReq = domainReq + 1
_conf.limitReqDict:incr(domain, 1)
else
-- 域名第一次请求
domainReq = 1
_conf.limitReqDict:set(domain, 1, _conf.limitDomainTime)
end
Util:logDebug("[limitDomainModule]", "DomainReq="..domainReq, ip, uri)
-- 如果该域名请求已经被拦截,直接退出无需进行下层检测
if domaninIsBan ~= nil then
Util:logWaf("[limitDomainModule]", "DomainReq="..domainReq, ip, uri)
BanAction:banAction(_conf.limitDomainBanAction, ip)
return true
end
-- 检查域名请求是否超过最大请求限制,以防止人肉刷死
if domainReq > _conf.limitDomainReqs then
_conf.limitStatDict:set(domain, 1, _conf.limitDomainBanTime)
Util:logAttack("[limitDomainModule]", "DomainReq="..domainReq, ip, uri)
BanAction:banAction(_conf.limitDomainBanAction, ip)
return true
end
return false
end
end
-- ip限速模块,核心模块
function Waf:limitIpModule(ip, uri)
if _conf.limitIpModuleIsOn then
local ipReq = _conf.limitReqDict:get(ip)
local ipIsBan = _conf.limitStatDict:get(ip)
-- 先统计请求次数
if ipReq then
-- ip请求次数递增
ipReq = ipReq + 1
_conf.limitReqDict:incr(ip, 1)
else
-- ip第一次请求
ipReq = 1
_conf.limitReqDict:set(ip, 1, _conf.limitIpTime)
end
Util:logDebug("[limitIpModule]", "IpReq="..ipReq, ip, uri)
-- 如果该ip请求已经被拦截,直接退出无需进行下层检测
if ipIsBan ~= nil then
Util:logWaf("[limitIpModule]", "IpReq="..ipReq, ip, uri)
BanAction:banAction(_conf.limitIpBanAction, ip)
return true
end
-- 检查ip请求是否超过最大请求限制
if ipReq > _conf.limitIpReqs then
_conf.limitStatDict:set(ip, 1, _conf.limitIpBanTime)
Util:logAttack("[limitIpModule]", "IpReq="..ipReq, ip, uri)
BanAction:banAction(_conf.limitIpBanAction, ip)
return true
end
return false
end
end
-- cc防御模块,核心模块
function Waf:limitReqModule(ip, uri)
if _conf.limitReqModuleIsOn then
local ipDomain = ip..ngx.var.host
local ipDomainReq = _conf.limitReqDict:get(ipDomain)
local validPhase, validFailCnt = _conf.limitStatDict:get(ipDomain)
if not validFailCnt then
validFailCnt = 0
end
local ck = require "cookie"
local cookie = ck:new()
-- 先统计请求次数
if ipDomainReq then
-- 域名请求次数递增
ipDomainReq = ipDomainReq + 1
_conf.limitReqDict:incr(ipDomain, 1)
else
-- 域名第一次请求
ipDomainReq = 1
_conf.limitReqDict:set(ipDomain, 1, _conf.limitReqTime)
end
Util:logDebug("[limitReqModule]", "IpDomainReq="..ipDomainReq, ip, uri)
-- 该ip已验证通过,放行请求一段时间
if validPhase == _conf.limitReqPhasePass then
ngx.var.waf_policy = "pass"
Util:logDebug("[limitReqModule]", "Pass", ip, uri)
return false
end
-- 如果该ip请求已经被cc防御拦截,直接退出无需进行下层检测
if validPhase == _conf.limitReqPhaseBan then
Util:logWaf("[limitReqModule]", "Baned", ip, uri)
BanAction:banAction(_conf.limitReqBanAction, ip)
return true
end
-- 如果该ip请求拦截验证超过配置失败次数,直接退出无需进行下层检测
if _conf.limitReqValidCnt and validFailCnt >= _conf.limitReqValidCnt then
Util:logAttack("[limitReqModule]", "Baned", ip, uri)
_conf.limitStatDict:set(ipDomain, _conf.limitReqPhaseBan, _conf.limitReqBanTime)
BanAction:banAction(_conf.limitReqBanAction, ip)
return true
end
-- 根据配置的模块进行防御检测
local reqModule = Ddos:getLimitReqModule(validPhase)
if reqModule then
if reqModule == "limitCookieModule" then
return Ddos:limitCookieModule(ipDomain, cookie, ipDomainReq, ip, uri, validPhase, validFailCnt)
elseif reqModule == "limitJsJumpModule" then
return Ddos:limitJsJumpModule(ipDomain, cookie, ipDomainReq, ip, uri, validPhase, validFailCnt)
elseif reqModule == "limitCaptchaModule" then
return Ddos:limitCaptchaModule(ipDomain, cookie, ipDomainReq, ip, uri, validPhase, validFailCnt)
end
end
return false
end
end
return Waf