nginx添加轻量级防火墙(宝塔环境)

在Github上找到了一个轻量级的防火墙,主要是用来防sql注入的,这个Github项目本身的防火墙规则不是很完善,需要自己针对自己用的数据库来额外添加规则

下载防火墙项目

Github项目地址:
https://github.com/loveshell/ngx_lua_waf

一开始是克隆到/www/server/nginx 目录下,但是过了一段时间发现整个防火墙项目的目录都不见了,似乎宝塔会定期清理/www/server/nginx 目录?

克隆下来之后需要修改wafconf目录下的config.lua:
把RulePath修改为防火墙项目下wafconf目录的实际位置,logdir设置为日志目录,如 /www/wwwlogs/waf/
注意权限问题,确保www用户能读写上述的目录

修改nginx配置文件并重启nginx

在宝塔面板中进入nginx的配置文件,在合适位置(如果想对所有网站起作用可以添加在http{...}里)添加:
lua_package_path "/www/wwwroot/waf/ngx_lua_waf/?.lua";
lua_package_cpath "/www/wwwroot/waf/ngx_lua_waf/?.so";
init_by_lua_file /www/wwwroot/waf/ngx_lua_waf/init.lua;
access_by_lua_file /www/wwwroot/waf/ngx_lua_waf/waf.lua;

注意把上面代码中的目录换成实际防火墙项目的目录,保存后重启nginx

加强sql防护

原有防火墙规则(位于wafconf目录下)对sql注入的防护不是很全面,由于我用的是mysql所以这里我给出的规则不一定适合你的:
首先是wafconf/args:

\.\./
\:\$
\$\{
select.+(from|limit)
(?:(union(.*?)select))
having|rongjitest
sleep\((\s*)(\d*)(\s*)\)
benchmark\((.*)\,(.*)\)
base64_decode\(
(?:from\W+information_schema\W)
(?:(?:current_)user|database|schema|connection_id)\s*\(
(?:etc\/\W*passwd)
into(\s+)+(?:dump|out)file\s*
group\s+by.+\(
xwork.MethodAccessor
(?:define|eval|file_get_contents|include|require|require_once|shell_exec|phpinfo|system|passthru|preg_\w+|execute|echo|print|print_r|var_dump|(fp)open|alert|showmodaldialog)\(
xwork\.MethodAccessor
(gopher|doc|php|glob|file|phar|zlib|ftp|ldap|dict|ogg|data)\:\/
java\.lang
\$_(GET|post|cookie|files|session|env|phplib|GLOBALS|SERVER)\[
(onmouseover|onerror|onload)\=
(?i)\b(update|drop|truncate|alter|create|exec|prepare|declare|set)\b
(?i)/\*![\s\S]*\*/      # MySQL comment-based attacks
0x[0-9a-f]{4,}           # Hex-encoded payloads
(?i)information_schema
(?i)load_file\s*\(
(?i)into\s+outfile

然后是wafconf/post:

select.+(from|limit)
(?:(union(.*?)select))
having|rongjitest
sleep\((\s*)(\d*)(\s*)\)
benchmark\((.*)\,(.*)\)
base64_decode\(
(?:from\W+information_schema\W)
(?:(?:current_)user|database|schema|connection_id)\s*\(
(?:etc\/\W*passwd)
into(\s+)+(?:dump|out)file\s*
group\s+by.+\(
xwork.MethodAccessor
(?:define|eval|file_get_contents|include|require|require_once|shell_exec|phpinfo|system|passthru|preg_\w+|execute|echo|print|print_r|var_dump|(fp)open|alert|showmodaldialog)\(
xwork\.MethodAccessor
(gopher|doc|php|glob|file|phar|zlib|ftp|ldap|dict|ogg|data)\:\/
java\.lang
\$_(GET|post|cookie|files|session|env|phplib|GLOBALS|SERVER)\[
(onmouseover|onerror|onload)\=
(?i)\b(update|drop|truncate|alter|create|exec|prepare|declare|set)\b
(?i)/\*![\s\S]*\*/      # MySQL comment-based attacks
0x[0-9a-f]{4,}           # Hex-encoded payloads
(?i)information_schema
(?i)load_file\s*\(
(?i)into\s+outfile
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值