Update init.lua
parent
fb313dac15
commit
8b74143a0d
99
init.lua
99
init.lua
|
@ -1,6 +1,6 @@
|
||||||
--配置部分
|
--配置部分
|
||||||
logpath='/data/logs/hack/'
|
logpath='/data/logs/hack/'
|
||||||
rulepath='/usr/local/nginx/conf/wafconf/'
|
rulepath='/usr/local/openresty/nginx/conf/ngx_lua_waf/wafconf/'
|
||||||
syslogserver='127.0.0.1'
|
syslogserver='127.0.0.1'
|
||||||
filext=''
|
filext=''
|
||||||
--如果需要开启syslog传输,请取消掉log函数部分的注释
|
--如果需要开启syslog传输,请取消掉log函数部分的注释
|
||||||
|
@ -72,43 +72,84 @@ sign=level+facility*8
|
||||||
ok, err = sock:send('<'..sign..'>'..msg)
|
ok, err = sock:send('<'..sign..'>'..msg)
|
||||||
sock:close()
|
sock:close()
|
||||||
end
|
end
|
||||||
function log(method,url,data)
|
function log()
|
||||||
if data then
|
|
||||||
if ngx.var.http_user_agent then
|
|
||||||
-- syslog(ngx.var.remote_addr.." ".." ["..ngx.localtime().."] \""..method.." "..url.."\" \""..data.."\" \""..ngx.status.."\" \""..ngx.var.http_user_agent.."\"\n")
|
|
||||||
write(logpath..'/'..ngx.var.server_name.."_sec.log",ngx.var.remote_addr.." ".." ["..ngx.localtime().."] \""..method.." "..url.."\" \""..data.."\" \""..ngx.status.."\" \""..ngx.var.http_user_agent.."\"\n")
|
|
||||||
else
|
|
||||||
-- syslog(ngx.var.remote_addr.." ".." ["..ngx.localtime().."] \""..method.." "..url.."\" \""..data.."\" \"-\"\n")
|
|
||||||
write(logpath..'/'..ngx.var.server_name.."_sec.log",ngx.var.remote_addr.." ".." ["..ngx.localtime().."] \""..method.." "..url.."\" \""..data.."\" \"-\"\n")
|
|
||||||
end
|
|
||||||
else
|
|
||||||
if ngx.var.http_user_agent then
|
if ngx.var.http_user_agent then
|
||||||
-- syslog(ngx.var.remote_addr.." ".." ["..ngx.localtime().."] \""..method.." "..url.."\" \"-\" \""..ngx.var.http_user_agent.."\"\n")
|
-- syslog(ngx.var.remote_addr.." ".." ["..ngx.localtime().."] \""..method.." "..url.."\" \"-\" \""..ngx.var.http_user_agent.."\"\n")
|
||||||
write(logpath..'/'..ngx.var.server_name.."_sec.log",ngx.var.remote_addr.." ".." ["..ngx.localtime().."] \""..method.." "..url.."\" \"-\" \""..ngx.var.http_user_agent.."\"\n")
|
write(logpath..'/'..ngx.var.server_name.."_sec.log",ngx.var.remote_addr.." ".." ["..ngx.localtime().."] \""..ngx.req.get_method().." "..ngx.var.request_uri.."\" \"-\" \""..ngx.var.http_user_agent.."\"\n")
|
||||||
else
|
else
|
||||||
-- syslog(ngx.var.remote_addr.." ".." ["..ngx.localtime().."] \""..method.." "..url.."\" \"-\" \"".."-\"\n")
|
-- syslog(ngx.var.remote_addr.." ".." ["..ngx.localtime().."] \""..method.." "..url.."\" \"-\" \"".."-\"\n")
|
||||||
write(logpath..'/'..ngx.var.server_name.."_sec.log",ngx.var.remote_addr.." ".." ["..ngx.localtime().."] \""..method.." "..url.."\" \"-\" \"".."-\"\n")
|
write(logpath..'/'..ngx.var.server_name.."_sec.log",ngx.var.remote_addr.." ".." ["..ngx.localtime().."] \""..ngx.req.get_method().." "..ngx.var.request_uri.."\" \"-\" \"".."-\"\n")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
--------------------------------------响应函数--------------------------------------------------------------------------------
|
|
||||||
function check()
|
|
||||||
ngx.header.content_type = "text/html"
|
|
||||||
ngx.print("just a joke hehe~ !!")
|
|
||||||
ngx.exit(200)
|
|
||||||
end
|
|
||||||
------------------------------------规则读取函数-------------------------------------------------------------------
|
------------------------------------规则读取函数-------------------------------------------------------------------
|
||||||
function read_rule(var)
|
function getrule(method,dict)
|
||||||
file = io.open(rulepath..'/'..var,"r")
|
local waf = dict;
|
||||||
|
file = io.open(rulepath..'/'..method,"r")
|
||||||
t = {}
|
t = {}
|
||||||
for line in file:lines() do
|
for line in file:lines() do
|
||||||
table.insert(t,line)
|
waf.set(waf,line,true)
|
||||||
end
|
end
|
||||||
file:close()
|
file:close()
|
||||||
return(table.concat(t,"|"))
|
|
||||||
end
|
end
|
||||||
regex=read_rule('global')
|
local update = ngx.shared.update;
|
||||||
get=read_rule('get')
|
local updated_at = update:get("updated_at");
|
||||||
post=read_rule('post')
|
if updated_at == nil or updated_at < ( ngx.now() - 10 ) then
|
||||||
agent=read_rule('user-agent')
|
getrule('urlpath',ngx.shared.urlpath)
|
||||||
whitelist=read_rule('whitelist')
|
getrule('post',ngx.shared.post)
|
||||||
|
getrule('user-agent',ngx.shared.ua)
|
||||||
|
getrule('args',ngx.shared.args)
|
||||||
|
|
||||||
|
else
|
||||||
|
update:set("updated_at", ngx.now());
|
||||||
|
end
|
||||||
|
function say_html(ruleid)
|
||||||
|
ngx.header.content_type = "text/html"
|
||||||
|
ngx.say('Please go away~~~')
|
||||||
|
log()
|
||||||
|
ngx.exit(200)
|
||||||
|
end
|
||||||
|
function args()
|
||||||
|
for k,v in pairs(ngx.shared.args:get_keys()) do
|
||||||
|
local args = ngx.req.get_uri_args()
|
||||||
|
for key, val in pairs(args) do
|
||||||
|
if ngx.re.match(val,v,"isjo") then
|
||||||
|
say_html(k)
|
||||||
|
log('GET')
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
function url()
|
||||||
|
for k,v in pairs(ngx.shared.urlpath:get_keys()) do
|
||||||
|
if ngx.re.match(ngx.var.request_uri,v,"isjo") then
|
||||||
|
say_html(k)
|
||||||
|
log('GET')
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
function ua()
|
||||||
|
for k,v in pairs(ngx.shared.ua:get_keys()) do
|
||||||
|
if ngx.re.match(ngx.var.http_user_agent,v,"isjo") then
|
||||||
|
say_html(k)
|
||||||
|
log('User-agent')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
function body()
|
||||||
|
for k,v in pairs(ngx.shared.post:get_keys()) do
|
||||||
|
if ngx.req.get_body_data() and ngx.re.match(ngx.req.get_body_data(),v,"isjo") then
|
||||||
|
say_html(k)
|
||||||
|
log('POST')
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
Loading…
Reference in New Issue