forked from rwx------/nginx_waf
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit.lua
executable file
·163 lines (140 loc) · 4.03 KB
/
init.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
--require 'ip_list'
local Config = require("config")
--开关转换为true或false函数
local function optionIsOn(options)
local options = string.lower(options)
if options == "on" then
return true
else
return false
end
end
--生成密码
local function makePassword()
local string="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
math.randomseed(os.time()) --随机种子
local r1 = math.random(1,62) --生成1-62之间的随机数
local r2 = math.random(1,62) --生成1-62之间的随机数
local r3 = math.random(1,62) --生成1-62之间的随机数
local r4 = math.random(1,62) --生成1-62之间的随机数
local r5 = math.random(1,62) --生成1-62之间的随机数
local r6 = math.random(1,62) --生成1-62之间的随机数
local r7 = math.random(1,62) --生成1-62之间的随机数
local r8 = math.random(1,62) --生成1-62之间的随机数
local s1 = string.sub(string,r1,r1)
local s2 = string.sub(string,r2,r2)
local s3 = string.sub(string,r3,r3)
local s4 = string.sub(string,r4,r4)
local s5 = string.sub(string,r5,r5)
local s6 = string.sub(string,r6,r6)
local s7 = string.sub(string,r7,r7)
local s8 = string.sub(string,r8,r8)
return s1..s2..s3..s4..s5..s6..s7..s8
end
--解析文件到正则字符串函数
local function parseRuleFile(filePath)
local list = ''
local rfile = assert(io.open(filePath,'r'))
for line in rfile:lines() do
if not (string.match(line,"^ *$")) then
list = list.."|"..line
end
end
list = string.gsub(list,"^%|",'')
rfile:close()
return list
end
--解析动作
local function actionIsOn1(action)
if action == "captcha" then
return true
else
return false
end
end
local function actionIsOn2(action)
if action == "forbidden" then
return true
else
return false
end
end
local function actionIsOn3(action)
if action == "iptables" then
return true
else
return false
end
end
--读取文件到内存
local function readFile2Mem(file)
local fp = io.open(file,"r")
if fp then
return fp:read("*a")
end
end
--读取验证码到字典
local function readCaptcha2Dict(dir,dict)
local i = 0
for path in io.popen('ls -a '..dir..'*.png'):lines() do
if i < 10000 then
i = i + 1
local fp = io.open(path,"rb")
local img = fp:read("*a")
local captcha = string.gsub(path,".*/(.*)%.png","%1")
captcha = string.lower(captcha)
dict:set(i,captcha)
dict:set(captcha,img)
else
break
end
end
end
_Conf = {
--引入原始设置
limitReqModules = Config.limitReqModules,
whiteIpModules = Config.whiteIpModules,
whiteUaModules = Config.whiteUaModules,
captchaGray2 = Config.captchaToGray2,
debug = Config.debug,
logOnly = Config.logOnly,
logPath = Config.logPath,
gray1Time = Config.gray1Time,
gray2Time = Config.gray2Time,
whiteTime = Config.whiteTime,
keyExpire = Config.keyExpire,
captchaKey = Config.captchaKey,
banMode = Config.banMode,
--解析开关设置
captchaGray2ModulesIsOn = Config.captchaToGray2.state,
limitReqModulesIsOn = Config.limitReqModules.state,
--解析文件到正则
limitUrlProtect = Config.limitReqModules.urlProtect,
whiteIpList = Config.whiteIpModules.ipTables,
blackIpList = Config.blackIpModules.ipTables,
whiteUaList = Config.whiteUaModules.uaTables,
--读取文件到内存
captchaPage = readFile2Mem(Config.captchaPage),
reCaptchaPage = readFile2Mem(Config.reCaptchaPage),
gray2Page = readFile2Mem(Config.gray2Page),
--新建字典(用于记录ip访问次数及灰名单)
dict = ngx.shared.guard_dict,
--新建字典(只用于记录验证码,防止丢失)
dict_captcha = ngx.shared.dict_captcha,
--验证码图片路径
captchaDir = Config.captchaDir,
captchaAction = actionIsOn1(Config.blockAction),
forbiddenAction = actionIsOn2(Config.blockAction),
--解析url匹配模式
urlMatchMode = Config.urlMatchMode,
normalCount = 0,
exceedCount = 0,
}
--读取验证码到字典
if Config.blockAction == "captcha" then
readCaptcha2Dict(_Conf.captchaDir,_Conf.dict_captcha)
end
--判断是否key是动态生成
if Config.keyDefine == "dynamic" then
_Conf.captchaKey = makePassword()
end