##test.lua脚本
# 先执行下面两条命令,生成lua_log.txt的日志文件
# touch /usr/local/nginx/conf/lua_log.txt
# chmod 777 /usr/local/nginx/conf/lua_log.txt
=====================================================================
local f, err = io.open("/usr/local/nginx/conf/lua_log.txt", "a+") -- 通常没必要用b二进制,也要确认是否有写权限
-- ngx.log(ngx.ERR, " lua_err:", err) -- 调试日志
resp_body = string.sub(ngx.arg[1],1,10000)
ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body
if ngx.arg[2] then
ngx.var.resp_body = ngx.ctx.buffered
end
str_resp_body = tostring(ngx.var.resp_body) -- 转换成string
-- 是否包含字符串
hacker_keyword = "rob_link.com"
check_str = string.find(str_resp_body, hacker_keyword) -- 空值为nil,其它为数字
-- 输出页面内容,自动刷新当前页面,跟劫持页面重复刷新,覆盖执行
return_html=[[
<meta http-equiv="refresh" content="0">
]]
-- 判断是否包含劫持关键字
if(check_str ~= nil)
then
-- 包字符串,需要重新跳转
ngx.arg[1] = return_html
-- ngx.arg[2] = true
f:write("被劫持了:") f:write(ngx.var.time_iso8601) f:write("|") f:write(ngx.var.host) f:write("\r\n") f:flush()
end
f:close()
===================================================================== |