Naxsi是一个开放源代码、高效、低维护规则的Nginx web应用防火墙(Web Application Firewall)模块。Naxsi的主要目标是加固web应用程序,以抵御SQL注入、跨站脚本、跨域伪造请求、本地和远程文件包含漏洞。
官网地址:https://github.com/nbs-system/naxsi
Naxsi 不要求任何特定的依赖,它需要的 libpcre ,libssl ,zlib ,gzip 这些 Nginx 已经集成了。
- 下载Naxsi模块
[[email protected] soft]# wget https://github.com/nbs-system/naxsi/archive/master.zip
[[email protected] soft]# tar zxvf naxsi-master.zip
- 重新编译Nginx添加Naxsi模块
[[email protected] ~]# cd /opt/openresty/nginx/sbin/ #进入当前运行的目录
[[email protected] sbin]# ./nginx -V #查看之前编译参数
nginx version: openresty/1.11.2.5
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/opt/openresty/nginx --with-cc-opt=-O2 --add-module=../ngx_devel_kit-0.3.0 --add-module=../iconv-nginx-module-0.14 --add-module=../echo-nginx-module-0.61 --add-module=../xss-nginx-module-0.05 --add-module=../ngx_coolkit-0.2rc3 --add-module=../set-misc-nginx-module-0.31 --add-module=../form-input-nginx-module-0.12 --add-module=../encrypted-session-nginx-module-0.06 --add-module=../srcache-nginx-module-0.31 --add-module=../ngx_lua-0.10.10 --add-module=../ngx_lua_upstream-0.07 --add-module=../headers-more-nginx-module-0.32 --add-module=../array-var-nginx-module-0.05 --add-module=../memc-nginx-module-0.18 --add-module=../redis-nginx-module-0.3.7 --add-module=../rds-json-nginx-module-0.14 --add-module=../rds-csv-nginx-module-0.07 --with-ld-opt=-Wl,-rpath,/opt/openresty/luajit/lib --with-http_realip_module --with-pcre --with-http_ssl_module
[[email protected] sbin]# cd /opt/soft/openresty-1.11.2.5/ #进入源码目录
[[email protected] openresty-1.11.2.5]# ./configure --prefix=/opt/openresty/nginx --with-cc-opt=-O2 --add-module=../ngx_devel_kit-0.3.0 --add-module=../iconv-nginx-module-0.14 --add-module=../echo-nginx-module-0.61 --add-module=../xss-nginx-module-0.05 --add-module=../ngx_coolkit-0.2rc3 --add-module=../set-misc-nginx-module-0.31 --add-module=../form-input-nginx-module-0.12 --add-module=../encrypted-session-nginx-module-0.06 --add-module=../srcache-nginx-module-0.31 --add-module=../ngx_lua-0.10.10 --add-module=../ngx_lua_upstream-0.07 --add-module=../headers-more-nginx-module-0.32 --add-module=../array-var-nginx-module-0.05 --add-module=../memc-nginx-module-0.18 --add-module=../redis-nginx-module-0.3.7 --add-module=../rds-json-nginx-module-0.14 --add-module=../rds-csv-nginx-module-0.07 --with-ld-opt=-Wl,-rpath,/opt/openresty/luajit/lib --with-http_realip_module --with-pcre --with-http_ssl_module --add-module=/opt/soft/naxsi-master/naxsi_src/ #重新编译nginx,加入之前编译的参数,再加入naxsi模块,指定到naxsi的源码目录中的naxsi_src目录
......
./configure: error: no /opt/soft/openresty-1.11.2.5/../ngx_devel_kit-0.3.0/config was found #报错,openresty的./nginx -V中模块路径../xxx的都是openresty默认添加的,这些路径是在根目录的build下,编译时候会自动加上
ERROR: failed to run command: sh ./configure --prefix=/opt/openresty/nginx/nginx \...
[[email protected] openresty-1.11.2.5]#
[[email protected] openresty-1.11.2.5]# ./configure --prefix=/opt/openresty/ --add-module=/opt/soft/naxsi-master/naxsi_src/ --with-luajit #只需要加这几项
[[email protected] openresty-1.11.2.5]# gmake
[[email protected] openresty-1.11.2.5]# find . -type f -iname nginx
./build/nginx-1.11.2/objs/nginx
[[email protected] openresty-1.11.2.5]# cp /opt/openresty/nginx/sbin/nginx{,.20180803bak} #这步很重要
[[email protected] openresty-1.11.2.5]# cp ./build/nginx-1.11.2/objs/nginx /opt/openresty/nginx/sbin/nginx
cp: overwrite ‘/opt/openresty/nginx/sbin/nginx’? y
cp: cannot create regular file ‘/opt/openresty/nginx/sbin/nginx’: Text file busy
[[email protected] openresty-1.11.2.5]# killall -9 nginx
[[email protected] openresty-1.11.2.5]# cp build/nginx-1.11.2/objs/nginx /opt/openresty/nginx/sbin/
cp: overwrite ‘/opt/openresty/nginx/sbin/nginx’? y
[[email protected] openresty-1.11.2.5]# cd !$
cd /opt/openresty/nginx/sbin/
[[email protected] sbin]# ./nginx -V
nginx version: openresty/1.11.2.5
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/opt/openresty//nginx --with-cc-opt=-O2 --add-module=../ngx_devel_kit-0.3.0 --add-module=../echo-nginx-module-0.61 --add-module=../xss-nginx-module-0.05 --add-module=../ngx_coolkit-0.2rc3 --add-module=../set-misc-nginx-module-0.31 --add-module=../form-input-nginx-module-0.12 --add-module=../encrypted-session-nginx-module-0.06 --add-module=../srcache-nginx-module-0.31 --add-module=../ngx_lua-0.10.10 --add-module=../ngx_lua_upstream-0.07 --add-module=../headers-more-nginx-module-0.32 --add-module=../array-var-nginx-module-0.05 --add-module=../memc-nginx-module-0.18 --add-module=../redis2-nginx-module-0.14 --add-module=../redis-nginx-module-0.3.7 --add-module=../rds-json-nginx-module-0.14 --add-module=../rds-csv-nginx-module-0.07 --with-ld-opt=-Wl,-rpath,/opt/openresty/luajit/lib --add-module=/opt/soft/naxsi-master/naxsi_src --with-http_ssl_module
[[email protected] sbin]#
复制Naxsi核心配置文件到nginx/conf下
[[email protected] build]# cp /opt/soft/naxsi-master/naxsi_config/naxsi_core.rules /opt/openresty/nginx/conf/
在nginx.conf配置文件中添加Naxsi核心配置文件
[[email protected] build]# cp /opt/openresty/nginx/conf/nginx.conf{,.20190804bak}
[[email protected] build]# vim /opt/openresty/nginx/conf/nginx.conf
......
http {
include mime.types;
include naxsi_core.rules; #加载naxsi 核心规则文件
default_type application/octet-stream;
......
}
......
配置Naxsi规则,新建文件naxsi.rules
[[email protected] build]# vim /opt/openresty/nginx/conf/naxsi.rules
#LearningMode 启用学习模式,即拦截请求后不拒绝访问,只将触发规则的请求写入error_log选项定义的文件中。如果对规则产生的影响不太清楚,可以先设置为学习模式。启用学习模式不能起到拦截非法请求的防御的效果。建议先使用学习模式,规则测试完成后再启用拦截模式。这样可以避免出现对网站、服务器某些不可知的影响。
#SecRulesEnabled|SecRulesDisabled
SecRulesEnabled; #启用Naxsi模块
DeniedUrl "/RequestDenied"; #拒绝的请求发送到内部URL
#check rules 设置各规则不同的触发阈值。 一旦该阈值触发,请求将被阻塞。
CheckRule "$SQL >= 8" BLOCK;
CheckRule "$RFI >= 8" BLOCK;
CheckRule "$TRAVERSAL >= 4" BLOCK;
CheckRule "$EVADE >= 4" BLOCK;
CheckRule "$XSS >= 8" BLOCK;
#naxsi log
error_log logs/naxsi.log; #nginx相对路径都是相对于根目录
[[email protected] build]#
虚拟主机添加支持Naxsi防×××
[[email protected] build]# vim /opt/openresty/nginx/conf/nginx.conf
......
http {
......
server {
......
location / {
......
include naxsi.rules;
......
}
......
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /RequestDenied { #定义naxsi.rules中DeniedUrl返回的代码
return 403;
}
error_page 403 /403.html;
location = /403.html {
root html;
}
......
}
......
}
......
[email protected] build]# vim /opt/openresty/nginx/html/403.html
<html>
<head>
<title>Error 403 Request Denied</title>
</head>
<body>
<h2>Error 403 Request Denied</h2>
For some reasons,you request has been denied.
</body>
<</html>
[[email protected] build]# /opt/openresty/nginx/sbin/nginx -t -c /opt/openresty/nginx/conf/nginx.conf
nginx: the configuration file /opt/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/openresty/nginx/conf/nginx.conf test is successful
[[email protected] build]# cd /opt/openresty/nginx/sbin/
[[email protected] sbin]# ./nginx -s reload
测试Naxsi
在一台测试机上执行:curl http://192.168.100.127/?a=%3C
[[email protected] ~]# curl http://192.168.100.127/?a=%3C
<html>
<head>
<title>Error 403 Request Denied</title>
</head>
<body>
<h2>Error 403 Request Denied</h2>
For some reasons,you request has been denied.
</body>
<</html>
[[email protected] ~]#
nginx服务器日志
[[email protected] sbin]# tail -500f /opt/openresty/nginx/logs/naxsi.log
2018/08/04 21:53:03 [error] 4824#0: *7 NAXSI_FMT: ip=10.10.13.100&server=192.168.100.127&uri=/&learning=0&vers=0.56&total_processed=3&total_blocked=1&block=1&cscore0=$XSS&score0=8&zone0=ARGS&id0=1302&var_name0=a, client: 10.10.13.100, server: localhost, request: "GET /?a=%3C HTTP/1.1", host: "192.168.100.127"
如果错误日志出现NAXSI_FMT的信息说明成功。
还可以添加白名单naxsi_BasicRule.conf
[[email protected] sbin]# vim /opt/openresty/nginx/conf/naxsi_BasicRule.conf
BasicRule wl:0 "mz:$ARGS_VAR:script";
BasicRule wl:0 "mz:$ARGS_VAR:id";
[[email protected] sbin]# ./nginx -s reload
表示xss×××正常是被拦截的,若被添加白名单,则不被拦截:此处是Get 参数名若为id 或者script,则不被拦截。
测试Naxsi
正常输入http://192.168.100.127 可以正常访问
在上面的地址后加上正常参数,例如:http://192.168.100.127/?id=1 或者 http://192.168.100.127/?id=1%20AND%201=1 返回的页面和上面一样,因为配置到白名单了
如果访问http://192.168.100.127/?name=40//and//1=1 会提示Error 403 Request Denied,含有条件注入被拦截了
访问http://192.168.100.127/?name=%28%29 会提示Error 403 Request Denied,含有特殊字符
白名单规则语法:
BasicRule wl:ID [negative] [mz:[$URL:target_url]|[match_zone]|[$ARGS_VAR:varname]|[$BODY_VARS:varname]|[$HEADERS_VAR:varname]|[NAME]]
wl:ID (White List ID) 哪些拦截规则会进入白名单
wl:0:把所有拦截规则加入白名单
wl:42:把ID为42的拦截规则加入白名单
wl:42,41,43:把ID为42, 41和43的拦截规则加入白名单
wl:-42:把所有拦截规则加入白名单,除了ID为42的拦截规则
mz:(Match Zones)
ARGS: GET的整个参数,如: foo=bar&in=%20
$ARGS_VAR: GET参数的参数名, 如:foo=bar&in=%20中的foo和in
$ARGS_VAR_X: 正则匹配的GET参数的参数名
HEADERS: 整个HTTP协议头
$HEADERS_VAR: HTTP协议头的名字
$HEADERS_VAR_X: 正则匹配的HTTP协议头的名字
BODY: POST的整个参数内容
$BODY_VAR: POST参数的参数名
$BODY_VAR_X: 正则匹配的POST参数的参数名
URL: URL(?前的)
URL_X: 正则匹配的URL(?前的)
FILE_EXT: 文件名 (POST上传文件时上传的文件名)
Naxsi 社区提供了一些常用的白名单规则,例如 wordpress 。可以在 https://github.com/nbs-system/naxsi-rules 下载白名单规则。
原文地址:http://blog.51cto.com/ityunwei2017/2154753