Merge 513b01d087
into 314a2f62ec
commit
4bfd00ca56
276
README.md
276
README.md
|
@ -1,138 +1,138 @@
|
|||
##ngx_lua_waf
|
||||
|
||||
ngx_lua_waf是我刚入职趣游时候开发的一个基于ngx_lua的web应用防火墙。
|
||||
|
||||
代码很简单,开发初衷主要是使用简单,高性能和轻量级。
|
||||
|
||||
现在开源出来,遵从MIT许可协议。其中包含我们的过滤规则。如果大家有什么建议和想fa,欢迎和我一起完善。
|
||||
|
||||
###用途:
|
||||
|
||||
防止sql注入,本地包含,部分溢出,fuzzing测试,xss,SSRF等web攻击
|
||||
防止svn/备份之类文件泄漏
|
||||
防止ApacheBench之类压力测试工具的攻击
|
||||
屏蔽常见的扫描黑客工具,扫描器
|
||||
屏蔽异常的网络请求
|
||||
屏蔽图片附件类目录php执行权限
|
||||
防止webshell上传
|
||||
|
||||
###推荐安装:
|
||||
|
||||
推荐使用lujit2.1做lua支持
|
||||
|
||||
ngx_lua如果是0.9.2以上版本,建议正则过滤函数改为ngx.re.find,匹配效率会提高三倍左右。
|
||||
|
||||
|
||||
###使用说明:
|
||||
|
||||
nginx安装路径假设为:/usr/local/nginx/conf/
|
||||
|
||||
把ngx_lua_waf下载到conf目录下,解压命名为waf
|
||||
|
||||
在nginx.conf的http段添加
|
||||
|
||||
lua_package_path "/usr/local/nginx/conf/waf/?.lua";
|
||||
lua_shared_dict limit 10m;
|
||||
init_by_lua_file /usr/local/nginx/conf/waf/init.lua;
|
||||
access_by_lua_file /usr/local/nginx/conf/waf/waf.lua;
|
||||
|
||||
配置config.lua里的waf规则目录(一般在waf/conf/目录下)
|
||||
|
||||
RulePath = "/usr/local/nginx/conf/waf/wafconf/"
|
||||
|
||||
绝对路径如有变动,需对应修改
|
||||
|
||||
然后重启nginx即可
|
||||
|
||||
|
||||
###配置文件详细说明:
|
||||
|
||||
RulePath = "/usr/local/nginx/conf/waf/wafconf/"
|
||||
--规则存放目录
|
||||
attacklog = "off"
|
||||
--是否开启攻击信息记录,需要配置logdir
|
||||
logdir = "/usr/local/nginx/logs/hack/"
|
||||
--log存储目录,该目录需要用户自己新建,切需要nginx用户的可写权限
|
||||
UrlDeny="on"
|
||||
--是否拦截url访问
|
||||
Redirect="on"
|
||||
--是否拦截后重定向
|
||||
CookieMatch = "on"
|
||||
--是否拦截cookie攻击
|
||||
postMatch = "on"
|
||||
--是否拦截post攻击
|
||||
whiteModule = "on"
|
||||
--是否开启URL白名单
|
||||
black_fileExt={"php","jsp"}
|
||||
--填写不允许上传文件后缀类型
|
||||
ipWhitelist={"127.0.0.1"}
|
||||
--ip白名单,多个ip用逗号分隔
|
||||
ipBlocklist={"1.0.0.1"}
|
||||
--ip黑名单,多个ip用逗号分隔
|
||||
CCDeny="on"
|
||||
--是否开启拦截cc攻击(需要nginx.conf的http段增加lua_shared_dict limit 10m;)
|
||||
CCrate = "100/60"
|
||||
--设置cc攻击频率,单位为秒.
|
||||
--默认1分钟同一个IP只能请求同一个地址100次
|
||||
html=[[Please go away~~]]
|
||||
--警告内容,可在中括号内自定义
|
||||
备注:不要乱动双引号,区分大小写
|
||||
|
||||
###检查规则是否生效
|
||||
|
||||
部署完毕可以尝试如下命令:
|
||||
|
||||
curl http://xxxx/test.php?id=../etc/passwd
|
||||
返回"Please go away~~"字样,说明规则生效。
|
||||
|
||||
注意:默认,本机在白名单不过滤,可自行调整config.lua配置
|
||||
|
||||
|
||||
###效果图如下:
|
||||
|
||||

|
||||
|
||||

|
||||
|
||||
###规则更新:
|
||||
|
||||
考虑到正则的缓存问题,动态规则会影响性能,所以暂没用共享内存字典和redis之类东西做动态管理。
|
||||
|
||||
规则更新可以把规则文件放置到其他服务器,通过crontab任务定时下载来更新规则,nginx reload即可生效。以保障ngx lua waf的高性能。
|
||||
|
||||
只记录过滤日志,不开启过滤,在代码里在check前面加上--注释即可,如果需要过滤,反之
|
||||
|
||||
###一些说明:
|
||||
|
||||
过滤规则在wafconf下,可根据需求自行调整,每条规则需换行,或者用|分割
|
||||
|
||||
args里面的规则get参数进行过滤的
|
||||
url是只在get请求url过滤的规则
|
||||
post是只在post请求过滤的规则
|
||||
whitelist是白名单,里面的url匹配到不做过滤
|
||||
user-agent是对user-agent的过滤规则
|
||||
|
||||
|
||||
默认开启了get和post过滤,需要开启cookie过滤的,编辑waf.lua取消部分--注释即可
|
||||
|
||||
日志文件名称格式如下:虚拟主机名_sec.log
|
||||
|
||||
|
||||
## Copyright
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<td>Weibo</td><td>神奇的魔法师</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Forum</td><td>http://bbs.linuxtone.org/</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Copyright</td><td>Copyright (c) 2013- loveshell</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>License</td><td>MIT License</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
感谢ngx_lua模块的开发者[@agentzh](https://github.com/agentzh/),春哥是我所接触过开源精神最好的人
|
||||
##ngx_lua_waf
|
||||
|
||||
ngx_lua_waf是我刚入职趣游时候开发的一个基于ngx_lua的web应用防火墙。
|
||||
|
||||
代码很简单,开发初衷主要是使用简单,高性能和轻量级。
|
||||
|
||||
现在开源出来,遵从MIT许可协议。其中包含我们的过滤规则。如果大家有什么建议和想fa,欢迎和我一起完善。
|
||||
|
||||
###用途:
|
||||
|
||||
防止sql注入,本地包含,部分溢出,fuzzing测试,xss,SSRF等web攻击
|
||||
防止svn/备份之类文件泄漏
|
||||
防止ApacheBench之类压力测试工具的攻击
|
||||
屏蔽常见的扫描黑客工具,扫描器
|
||||
屏蔽异常的网络请求
|
||||
屏蔽图片附件类目录php执行权限
|
||||
防止webshell上传
|
||||
|
||||
###推荐安装:
|
||||
|
||||
推荐使用lujit2.1做lua支持
|
||||
|
||||
ngx_lua如果是0.9.2以上版本,建议正则过滤函数改为ngx.re.find,匹配效率会提高三倍左右。
|
||||
|
||||
|
||||
###使用说明:
|
||||
|
||||
nginx安装路径假设为:/usr/local/nginx/conf/
|
||||
|
||||
把ngx_lua_waf下载到conf目录下,解压命名为waf
|
||||
|
||||
在nginx.conf的http段添加
|
||||
|
||||
lua_package_path "/usr/local/nginx/conf/waf/?.lua";
|
||||
lua_shared_dict limit 10m;
|
||||
init_by_lua_file /usr/local/nginx/conf/waf/init.lua;
|
||||
access_by_lua_file /usr/local/nginx/conf/waf/waf.lua;
|
||||
|
||||
配置config.lua里的waf规则目录(一般在waf/conf/目录下)
|
||||
|
||||
RulePath = "/usr/local/nginx/conf/waf/wafconf/"
|
||||
|
||||
绝对路径如有变动,需对应修改
|
||||
|
||||
然后重启nginx即可
|
||||
|
||||
|
||||
###配置文件详细说明:
|
||||
|
||||
RulePath = "/usr/local/nginx/conf/waf/wafconf/"
|
||||
--规则存放目录
|
||||
attacklog = "off"
|
||||
--是否开启攻击信息记录,需要配置logdir
|
||||
logdir = "/usr/local/nginx/logs/hack/"
|
||||
--log存储目录,该目录需要用户自己新建,切需要nginx用户的可写权限
|
||||
UrlDeny="on"
|
||||
--是否拦截url访问
|
||||
Redirect="on"
|
||||
--是否拦截后重定向
|
||||
CookieMatch = "on"
|
||||
--是否拦截cookie攻击
|
||||
postMatch = "on"
|
||||
--是否拦截post攻击
|
||||
whiteModule = "on"
|
||||
--是否开启URL白名单
|
||||
black_fileExt={"php","jsp"}
|
||||
--填写不允许上传文件后缀类型
|
||||
ipWhitelist={"127.0.0.1"}
|
||||
--ip白名单,多个ip用逗号分隔
|
||||
ipBlocklist={"1.0.0.1"}
|
||||
--ip黑名单,多个ip用逗号分隔
|
||||
CCDeny="on"
|
||||
--是否开启拦截cc攻击(需要nginx.conf的http段增加lua_shared_dict limit 10m;)
|
||||
CCrate = "100/60"
|
||||
--设置cc攻击频率,单位为秒.
|
||||
--默认1分钟同一个IP只能请求同一个地址100次
|
||||
html=[[Please go away~~]]
|
||||
--警告内容,可在中括号内自定义
|
||||
备注:不要乱动双引号,区分大小写
|
||||
|
||||
###检查规则是否生效
|
||||
|
||||
部署完毕可以尝试如下命令:
|
||||
|
||||
curl http://xxxx/test.php?id=../etc/passwd
|
||||
返回"Please go away~~"字样,说明规则生效。
|
||||
|
||||
注意:默认,本机在白名单不过滤,可自行调整config.lua配置
|
||||
|
||||
|
||||
###效果图如下:
|
||||
|
||||

|
||||
|
||||

|
||||
|
||||
###规则更新:
|
||||
|
||||
考虑到正则的缓存问题,动态规则会影响性能,所以暂没用共享内存字典和redis之类东西做动态管理。
|
||||
|
||||
规则更新可以把规则文件放置到其他服务器,通过crontab任务定时下载来更新规则,nginx reload即可生效。以保障ngx lua waf的高性能。
|
||||
|
||||
只记录过滤日志,不开启过滤,在代码里在check前面加上--注释即可,如果需要过滤,反之
|
||||
|
||||
###一些说明:
|
||||
|
||||
过滤规则在wafconf下,可根据需求自行调整,每条规则需换行,或者用|分割
|
||||
|
||||
args里面的规则get参数进行过滤的
|
||||
url是只在get请求url过滤的规则
|
||||
post是只在post请求过滤的规则
|
||||
whitelist是白名单,里面的url匹配到不做过滤
|
||||
user-agent是对user-agent的过滤规则
|
||||
|
||||
|
||||
默认开启了get和post过滤,需要开启cookie过滤的,编辑waf.lua取消部分--注释即可
|
||||
|
||||
日志文件名称格式如下:虚拟主机名_sec.log
|
||||
|
||||
|
||||
## Copyright
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<td>Weibo</td><td>神奇的魔法师</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Forum</td><td>http://bbs.linuxtone.org/</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Copyright</td><td>Copyright (c) 2013- loveshell</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>License</td><td>MIT License</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
感谢ngx_lua模块的开发者[@agentzh](https://github.com/agentzh/),春哥是我所接触过开源精神最好的人
|
||||
|
|
66
config.lua
66
config.lua
|
@ -1,45 +1,45 @@
|
|||
RulePath = "/usr/local/nginx/conf/waf/wafconf/"
|
||||
RulePath = "/etc/nginx/waf/wafconf/"
|
||||
attacklog = "on"
|
||||
logdir = "/usr/local/nginx/logs/hack/"
|
||||
logdir = "/var/log/nginx/"
|
||||
UrlDeny="on"
|
||||
Redirect="on"
|
||||
CookieMatch="on"
|
||||
postMatch="on"
|
||||
whiteModule="on"
|
||||
postMatch="on"
|
||||
whiteModule="on"
|
||||
black_fileExt={"php","jsp"}
|
||||
ipWhitelist={"127.0.0.1"}
|
||||
ipBlocklist={"1.0.0.1"}
|
||||
CCDeny="off"
|
||||
CCDeny="on"
|
||||
CCrate="100/60"
|
||||
html=[[
|
||||
<html xmlns="http://www.w3.org/1999/xhtml"><head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
||||
<title>网站防火墙</title>
|
||||
<style>
|
||||
p {
|
||||
line-height:20px;
|
||||
}
|
||||
ul{ list-style-type:none;}
|
||||
li{ list-style-type:none;}
|
||||
</style>
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
||||
<title>网站防火墙</title>
|
||||
<style>
|
||||
p {
|
||||
line-height:20px;
|
||||
}
|
||||
ul { list-style-type:none;}
|
||||
li { list-style-type:none;}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body style=" padding:0; margin:0; font:14px/1.5 Microsoft Yahei, 宋体,sans-serif; color:#555;">
|
||||
|
||||
<div style="margin: 0 auto; width:1000px; padding-top:70px; overflow:hidden;">
|
||||
|
||||
|
||||
<div style="width:600px; float:left;">
|
||||
<div style=" height:40px; line-height:40px; color:#fff; font-size:16px; overflow:hidden; background:#6bb3f6; padding-left:20px;">网站防火墙 </div>
|
||||
<div style="border:1px dashed #cdcece; border-top:none; font-size:14px; background:#fff; color:#555; line-height:24px; height:220px; padding:20px 20px 0 20px; overflow-y:auto;background:#f3f7f9;">
|
||||
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-weight:600; color:#fc4f03;">您的请求带有不合法参数,已被网站管理员设置拦截!</span></p>
|
||||
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">可能原因:您提交的内容包含危险的攻击请求</p>
|
||||
<p style=" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:1; text-indent:0px;">如何解决:</p>
|
||||
<ul style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; -qt-list-indent: 1;"><li style=" margin-top:12px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">1)检查提交内容;</li>
|
||||
<li style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">2)如网站托管,请联系空间提供商;</li>
|
||||
<li style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">3)普通网站访客,请联系网站管理员;</li></ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body></html>
|
||||
<div style="margin: 0 auto; width:600px; padding-top:70px; overflow:hidden;">
|
||||
<div style="width:600px; float:left;">
|
||||
<div style=" height:40px; line-height:40px; color:#fff; font-size:16px; overflow:hidden; background:#6bb3f6; padding-left:20px;">网站防火墙 </div>
|
||||
<div style="border:1px dashed #cdcece; border-top:none; font-size:14px; background:#fff; color:#555; line-height:24px; height:220px; padding:20px 20px 0 20px; overflow-y:auto;background:#f3f7f9;">
|
||||
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-weight:600; color:#fc4f03;">您的请求带有不合法参数,已被网站管理员设置拦截!</span></p>
|
||||
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">可能原因:您提交的内容包含危险的攻击请求</p>
|
||||
<p style=" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:1; text-indent:0px;">如何解决:</p>
|
||||
<ul style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; -qt-list-indent: 1;">
|
||||
<li style=" margin-top:12px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">1)检查提交内容;</li>
|
||||
<li style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">2)如网站托管,请联系空间提供商;</li>
|
||||
<li style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">3)普通网站访客,请联系网站管理员;</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
]]
|
||||
|
|
16
init.lua
16
init.lua
|
@ -1,10 +1,10 @@
|
|||
require 'config'
|
||||
local match = string.match
|
||||
local ngxmatch=ngx.re.match
|
||||
local ngxmatch=ngx.re.find
|
||||
local unescape=ngx.unescape_uri
|
||||
local get_headers = ngx.req.get_headers
|
||||
local optionIsOn = function (options) return options == "on" and true or false end
|
||||
logpath = logdir
|
||||
logpath = logdir
|
||||
rulepath = RulePath
|
||||
UrlDeny = optionIsOn(UrlDeny)
|
||||
PostCheck = optionIsOn(postMatch)
|
||||
|
@ -15,11 +15,11 @@ attacklog = optionIsOn(attacklog)
|
|||
CCDeny = optionIsOn(CCDeny)
|
||||
Redirect=optionIsOn(Redirect)
|
||||
function getClientIp()
|
||||
IP = ngx.var.remote_addr
|
||||
if IP == nil then
|
||||
IP = "unknown"
|
||||
end
|
||||
return IP
|
||||
IP = ngx.var.remote_addr
|
||||
if IP == nil then
|
||||
IP = "unknown"
|
||||
end
|
||||
return IP
|
||||
end
|
||||
function write(logfile,msg)
|
||||
local fd = io.open(logfile,"ab")
|
||||
|
@ -79,7 +79,7 @@ function whiteurl()
|
|||
if wturlrules ~=nil then
|
||||
for _,rule in pairs(wturlrules) do
|
||||
if ngxmatch(ngx.var.uri,rule,"isjo") then
|
||||
return true
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
4
waf.lua
4
waf.lua
|
@ -1,6 +1,6 @@
|
|||
local content_length=tonumber(ngx.req.get_headers()['content-length'])
|
||||
local method=ngx.req.get_method()
|
||||
local ngxmatch=ngx.re.match
|
||||
local ngxmatch=ngx.re.find
|
||||
if whiteip() then
|
||||
elseif blockip() then
|
||||
elseif denycc() then
|
||||
|
@ -14,7 +14,7 @@ elseif url() then
|
|||
elseif args() then
|
||||
elseif cookie() then
|
||||
elseif PostCheck then
|
||||
if method=="POST" then
|
||||
if method=="POST" then
|
||||
local boundary = get_boundary()
|
||||
if boundary then
|
||||
local len = string.len
|
||||
|
|
Loading…
Reference in New Issue