11local match = string.match
2- local ngxmatch = ngx .re .match
2+ local ngxMatch = ngx .re .match
33local unescape = ngx .unescape_uri
44local get_headers = ngx .req .get_headers
55local cjson = require " cjson"
@@ -11,19 +11,9 @@ local function optionIsOn(options)
1111 return options == " on" or options == " On" or options == " ON"
1212end
1313
14- local logpath = ngx .var .logdir
15- local rulepath = ngx .var .RulePath
16- local attacklog = optionIsOn (ngx .var .attackLog )
17- local Redirect = optionIsOn (ngx .var .redirect )
18- local CCDeny = optionIsOn (ngx .var .CCDeny )
19- local UrlBlockDeny = optionIsOn (ngx .var .urlBlockDeny )
20- local UrlWhiteAllow = optionIsOn (ngx .var .urlWhiteAllow )
21- local IpBlockDeny = optionIsOn (ngx .var .ipBlockDeny )
22- local IpWhiteAllow = optionIsOn (ngx .var .ipWhiteAllow )
14+ local logPath = ngx .var .logdir
15+ local rulePath = ngx .var .RulePath
2316local PostDeny = optionIsOn (ngx .var .postDeny )
24- local ArgsDeny = optionIsOn (ngx .var .argsDeny )
25- local CookieDeny = optionIsOn (ngx .var .cookieDeny )
26- local FileExtDeny = optionIsOn (ngx .var .fileExtDeny )
2717
2818local function getClientIp ()
2919 IP = ngx .var .remote_addr
@@ -40,7 +30,8 @@ local function write(logfile,msg)
4030 fd :close ()
4131end
4232local function log (method ,url ,data ,ruletag )
43- if attacklog then
33+ local attackLog = optionIsOn (ngx .var .attackLog )
34+ if attackLog then
4435 local realIp = getClientIp ()
4536 local ua = ngx .var .http_user_agent
4637 local servername = ngx .var .server_name
@@ -51,13 +42,13 @@ local function log(method,url,data,ruletag)
5142 else
5243 line = realIp .. " [" .. time .. " ] \" " .. method .. " " .. servername .. url .. " \" \" " .. data .. " \" - \" " .. ruletag .. " \"\n "
5344 end
54- local filename = logpath .. ' /' .. servername .. " _" .. ngx .today ().. " _sec.log"
45+ local filename = logPath .. ' /' .. servername .. " _" .. ngx .today ().. " _sec.log"
5546 write (filename ,line )
5647 end
5748end
5849---- --------------------------------规则读取函数-------------------------------------------------------------------
5950local function read_json (var )
60- file = io.open (rulepath .. ' /' .. var .. ' .json' ," r" )
51+ file = io.open (rulePath .. ' /' .. var .. ' .json' ," r" )
6152 if file == nil then
6253 return
6354 end
@@ -79,7 +70,7 @@ local function select_rules(rules)
7970end
8071
8172local function read_str (var )
82- file = io.open (rulepath .. ' /' .. var ," r" )
73+ file = io.open (rulePath .. ' /' .. var ," r" )
8374 if file == nil then
8475 return
8576 end
@@ -88,43 +79,37 @@ local function read_str(var)
8879 return str
8980end
9081
91- local argsCheckList = select_rules (read_json (' args_check' ))
92- local postCheckList = select_rules (read_json (' post_check' ))
93- local cookieBlockList = select_rules (read_json (' cookie_block' ))
94- local uarules = select_rules (read_json (' user_agent' ))
95-
96- local urlWhiteList = read_json (' url_white' )
97- local urlBlockList = read_json (' url_block' )
98- local ipWhiteList = read_json (' ip_white' )
99- local ipBlockList = read_json (' ip_block' )
100- local fileExtBlockList = read_json (' file_ext_block' )
101-
102- local ccRate = read_str (' cc.json' )
10382local html = read_str (' html' )
10483
10584local function say_html ()
106- if Redirect then
85+ local redirect = optionIsOn (ngx .var .redirect )
86+ if redirect then
10787 ngx .header .content_type = " text/html"
10888 ngx .status = ngx .HTTP_FORBIDDEN
10989 ngx .say (html )
11090 ngx .exit (ngx .status )
11191 end
11292end
11393
114- local function whiteurl ()
115- if UrlWhiteAllow then
116- if urlWhiteList ~= nil then
117- for _ ,rule in pairs (urlWhiteList ) do
118- if ngxmatch (ngx .var .uri ,rule ," isjo" ) then
94+ local function whiteUrlCheck ()
95+ local urlWhiteAllow = optionIsOn (ngx .var .urlWhiteAllow )
96+ if urlWhiteAllow then
97+ local urlWhiteList = read_json (' url_white' )
98+ if urlWhiteList ~= nil then
99+ for _ , rule in pairs (urlWhiteList ) do
100+ if ngxMatch (ngx .var .uri , rule , " isjo" ) then
119101 return true
120102 end
121103 end
122104 end
123105 end
124106 return false
125107end
108+
126109local function fileExtCheck (ext )
127- if FileExtDeny then
110+ local fileExtDeny = optionIsOn (ngx .var .fileExtDeny )
111+ if fileExtDeny then
112+ local fileExtBlockList = read_json (' fileExtBlockList' )
128113 local items = Set (fileExtBlockList )
129114 ext = string.lower (ext )
130115 if ext then
@@ -144,8 +129,10 @@ function Set (list)
144129 return set
145130end
146131
147- local function args ()
148- if ArgsDeny then
132+ local function getArgsCheck ()
133+ local argsDeny = optionIsOn (ngx .var .argsDeny )
134+ if argsDeny then
135+ local argsCheckList = select_rules (read_json (' args_check' ))
149136 if argsCheckList then
150137 for _ ,rule in pairs (argsCheckList ) do
151138 local uriArgs = ngx .req .get_uri_args ()
@@ -162,7 +149,7 @@ local function args()
162149 else
163150 data = val
164151 end
165- if data and type (data ) ~= " boolean" and rule ~= " " and ngxmatch (unescape (data ),rule ," isjo" ) then
152+ if data and type (data ) ~= " boolean" and rule ~= " " and ngxMatch (unescape (data ),rule ," isjo" ) then
166153 log (' GET' ,ngx .var .request_uri ," -" ,rule )
167154 say_html ()
168155 return true
@@ -175,11 +162,13 @@ local function args()
175162end
176163
177164
178- local function url ()
179- if UrlBlockDeny then
180- for _ ,rule in pairs (urlBlockList ) do
181- if rule ~= " " and ngxmatch (ngx .var .request_uri ,rule ," isjo" ) then
182- log (' GET' ,ngx .var .request_uri ," -" ,rule )
165+ local function blockUrlCheck ()
166+ local urlBlockDeny = optionIsOn (ngx .var .urlBlockDeny )
167+ if urlBlockDeny then
168+ local urlBlockList = read_json (' url_block' )
169+ for _ , rule in pairs (urlBlockList ) do
170+ if rule ~= " " and ngxMatch (ngx .var .request_uri , rule , " isjo" ) then
171+ log (' GET' , ngx .var .request_uri , " -" , rule )
183172 say_html ()
184173 return true
185174 end
191180function ua ()
192181 local ua = ngx .var .http_user_agent
193182 if ua ~= nil then
194- for _ ,rule in pairs (uarules ) do
195- if rule ~= " " and ngxmatch (ua ,rule ," isjo" ) then
183+ local uaRules = select_rules (read_json (' user_agent' ))
184+ for _ ,rule in pairs (uaRules ) do
185+ if rule ~= " " and ngxMatch (ua ,rule ," isjo" ) then
196186 log (' UA' ,ngx .var .request_uri ," -" ,rule )
197187 say_html ()
198188 return true
@@ -202,20 +192,23 @@ function ua()
202192 return false
203193end
204194function body (data )
195+ local postCheckList = select_rules (read_json (' post_check' ))
205196 for _ ,rule in pairs (postCheckList ) do
206- if rule ~= " " and data ~= " " and ngxmatch (unescape (data ),rule ," isjo" ) then
197+ if rule ~= " " and data ~= " " and ngxMatch (unescape (data ),rule ," isjo" ) then
207198 log (' POST' ,ngx .var .request_uri ,data ,rule )
208199 say_html ()
209200 return true
210201 end
211202 end
212203 return false
213204end
214- local function cookie ()
205+ local function cookieCheck ()
215206 local ck = ngx .var .http_cookie
216- if CookieDeny and ck then
207+ local cookieDeny = optionIsOn (ngx .var .cookieDeny )
208+ if cookieDeny and ck then
209+ local cookieBlockList = select_rules (read_json (' cookie_block' ))
217210 for _ ,rule in pairs (cookieBlockList ) do
218- if rule ~= " " and ngxmatch (ck ,rule ," isjo" ) then
211+ if rule ~= " " and ngxMatch (ck ,rule ," isjo" ) then
219212 log (' Cookie' ,ngx .var .request_uri ," -" ,rule )
220213 say_html ()
221214 return true
@@ -225,23 +218,25 @@ local function cookie()
225218 return false
226219end
227220
228- local function denycc ()
229- if CCDeny and ccRate then
221+ local function denyCC ()
222+ local ccRate = read_str (' cc.json' )
223+ local ccDeny = optionIsOn (ngx .var .CCDeny )
224+ if ccDeny and ccRate then
230225 local uri = ngx .var .uri
231- CCcount = tonumber (string.match (ccRate ,' (.*)/' ))
232- CCseconds = tonumber (string.match (ccRate ,' /(.*)' ))
233- local uri = getClientIp ().. uri
226+ ccCount = tonumber (string.match (ccRate ,' (.*)/' ))
227+ ccSeconds = tonumber (string.match (ccRate ,' /(.*)' ))
228+ local access_uri = getClientIp ().. uri
234229 local limit = ngx .shared .limit
235- local req ,_ = limit :get (uri )
230+ local req ,_ = limit :get (access_uri )
236231 if req then
237- if req > CCcount then
232+ if req > ccCount then
238233 ngx .exit (503 )
239234 return true
240235 else
241- limit :incr (token ,1 )
236+ limit :incr (access_uri ,1 )
242237 end
243238 else
244- limit :set (uri ,1 ,CCseconds )
239+ limit :set (access_uri ,1 ,ccSeconds )
245240 end
246241 end
247242 return false
@@ -265,8 +260,10 @@ local function get_boundary()
265260 return match (header , " ;%s*boundary=([^\" ,;]+)" )
266261end
267262
268- local function whiteip ()
269- if IpWhiteAllow then
263+ local function whiteIpCheck ()
264+ local ipWhiteAllow = optionIsOn (ngx .var .ipWhiteAllow )
265+ if ipWhiteAllow then
266+ local ipWhiteList = read_json (' ip_white' )
270267 if next (ipWhiteList ) ~= nil then
271268 for _ ,ip in pairs (ipWhiteList ) do
272269 if getClientIp ()== ip then
@@ -278,8 +275,10 @@ local function whiteip()
278275 return false
279276end
280277
281- local function blockip ()
282- if IpBlockDeny then
278+ local function blockIpCheck ()
279+ local ipBlockDeny = optionIsOn (ngx .var .ipBlockDeny )
280+ if ipBlockDeny then
281+ local ipBlockList = read_json (' ip_block' )
283282 if next (ipBlockList ) ~= nil then
284283 for _ ,ip in pairs (ipBlockList ) do
285284 if getClientIp ()== ip then
@@ -292,39 +291,41 @@ local function blockip()
292291 return false
293292end
294293
294+ local function handleBodyKeyOrVal (kv )
295+ if type (kv ) == " table" then
296+ if type (kv [1 ]) == " boolean" then
297+ return
298+ end
299+ data = table.concat (kv , " , " )
300+ else
301+ data = kv
302+ end
303+ if data then
304+ if type (data ) ~= " boolean" then
305+ body (data )
306+ end
307+ end
308+ end
295309
296-
297- if whiteip () then
298- elseif blockip () then
299- elseif denycc () then
300- elseif ngx .var .http_Acunetix_Aspect then
301- ngx .exit (444 )
302- elseif ngx .var .http_X_Scan_Memo then
303- ngx .exit (444 )
304- elseif whiteurl () then
305- elseif ua () then
306- elseif url () then
307- elseif args () then
308- elseif cookie () then
309- elseif PostDeny then
310- if method == " POST" then
310+ local function postCheck ()
311+ if method == " POST" then
311312 local boundary = get_boundary ()
312313 if boundary then
313314 local len = string.len
314- local sock , err = ngx .req .socket ()
315+ local sock = ngx .req .socket ()
315316 if not sock then
316317 return
317318 end
318319 ngx .req .init_body (128 * 1024 )
319320 sock :settimeout (0 )
320- local content_length = nil
321- content_length = tonumber (ngx .req .get_headers ()[' content-length' ])
321+ local contentLength = nil
322+ contentLength = tonumber (ngx .req .get_headers ()[' content-length' ])
322323 local chunk_size = 4096
323- if content_length < chunk_size then
324- chunk_size = content_length
324+ if contentLength < chunk_size then
325+ chunk_size = contentLength
325326 end
326327 local size = 0
327- while size < content_length do
328+ while size < contentLength do
328329 local data , err , partial = sock :receive (chunk_size )
329330 data = data or partial
330331 if not data then
@@ -335,15 +336,15 @@ elseif PostDeny then
335336 return true
336337 end
337338 size = size + len (data )
338- local m = ngxmatch (data ,[[ Content-Disposition: form-data;(.+)filename="(.+)\\.(.*)"]] , ' ijo' )
339+ local m = ngxMatch (data , ' Content-Disposition: form-data; (.+)filename="(.+)\\ .(.*)"' , ' ijo' )
339340 if m then
340341 fileExtCheck (m [3 ])
341- filetranslate = true
342+ fileTranslate = true
342343 else
343- if ngxmatch (data ," Content-Disposition:" ,' isjo' ) then
344- filetranslate = false
344+ if ngxMatch (data , " Content-Disposition:" , ' isjo' ) then
345+ fileTranslate = false
345346 end
346- if filetranslate == false then
347+ if fileTranslate == false then
347348 if body (data ) then
348349 return true
349350 end
@@ -357,25 +358,32 @@ elseif PostDeny then
357358 ngx .req .finish_body ()
358359 else
359360 ngx .req .read_body ()
360- local args = ngx .req .get_post_args ()
361- if not args then
361+ local bodyObj = ngx .req .get_post_args ()
362+ if not bodyObj then
362363 return
363364 end
364- for key , val in pairs (args ) do
365- if type (val ) == " table" then
366- if type (val [1 ]) == " boolean" then
367- return
368- end
369- data = table.concat (val , " , " )
370- else
371- data = val
372- end
373- if data and type (data ) ~= " boolean" and body (data ) then
374- body (key )
375- end
365+ for key , val in pairs (bodyObj ) do
366+ handleBodyKeyOrVal (key )
367+ handleBodyKeyOrVal (val )
376368 end
377369 end
378370 end
371+ end
372+
373+ if whiteIpCheck () then
374+ elseif blockIpCheck () then
375+ elseif denyCC () then
376+ elseif ngx .var .http_Acunetix_Aspect then
377+ ngx .exit (444 )
378+ elseif ngx .var .http_X_Scan_Memo then
379+ ngx .exit (444 )
380+ elseif whiteUrlCheck () then
381+ elseif ua () then
382+ elseif blockUrlCheck () then
383+ elseif getArgsCheck () then
384+ elseif cookieCheck () then
385+ elseif PostDeny then
386+ postCheck ()
379387else
380388 return
381389end
0 commit comments