匿名
未登录
登录
Linux78|wiki
搜索
查看“Nginx配置naxsi waf”的源代码
来自Linux78|wiki
名字空间
页面
讨论
更多
更多
页面选项
查看
查看源代码
历史
←
Nginx配置naxsi waf
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
wiki:用户|用户
您可以查看与复制此页面的源代码。
Naxsi 和 Modsecurity 对比 WAF Naxsi Modsecurity Nginx兼容性 好(第三方 nginx 模块) 不好(原 Apache 下的模块,Nginx 下bug较多) 防御模式 简单(不依赖签名库) 复杂(依赖更新规则) 白名单规则 支持 支持 安装难度 容易 一般(需要安装依赖库) 社区支持 一般 较好 内存占用 小 大?不到哪去 特色 支持学习模式,可借助 nxapi/nxtool 自动分析白名单 可开启只记录不阻止,但是官方没有提供分析工具。 总结 Naxsi 比较灵活,所以学习成本较大,白名单规则需要自己完成。对 Nginx 支持良好,快速轻便。 Modsecurity 操作简单,规则更新较快,比较占用服务器资源,对于生产环境下的 Nginx 来说是个噩梦 在日常使用中,可以发现 Modsecurity 具有非常严格的防御规则(误报挺多的),并且规则支持较好(有强大的后台?)。如果使用 Apache 服务器,推荐使用 Modsecurity WAF。如果你使用的是 Nginx 服务器,建议先尝试使用 Naxsi 。 在 Centos 下编译安装 Nginx + Naxsi WAF 。 编译 Nginx + Naxsi 首先先运行: # nginx -V 然后可以看到现有的模块,复制保存一下备用。 configure arguments: --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --with-http_flv_module --with-http_mp4_module --with-openssl=../openssl-1.0.2l --with-pcre=../pcre-8.40 --with-pcre-jit --with-ld-opt=-ljemalloc 下载 Nginx 和 Naxsi Naxsi 版本可以在 https://github.com/nbs-system/naxsi 这里,选择 Branch –> Tags 查看版本号。 下载 Nginx 和 Naxsi ,并解压,然后进入解压后的 Nginx 目录: wget http://nginx.org/download/nginx-1.12.1.tar.gz wget https://github.com/nbs-system/naxsi/archive/0.55.3.tar.gz tar xvzf nginx-1.12.1.tar.gz tar xvzf naxsi-0.55.3.tar.gz cd nginx-1.12.1/ Naxsi 不要求任何特定的依赖,它需要的 libpcre ,libssl ,zlib ,gzip 这些 Nginx 已经集成了。 然后编译(记得在 ./configure 后面加上 —add-module=../naxsi-0.55.3/naxsi_src/ 和你之前备份的模块): ./configure --prefix=/usr/local/nginx --user=www --group=www --add-module=../naxsi-0.55.3/naxsi_src/ --with-http_stub_status_module --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --with-http_flv_module --with-http_mp4_module --with-openssl=../openssl-1.0.2l --with-pcre=../pcre-8.40 --with-pcre-jit --with-ld-opt=-ljemalloc make //不要 make install,不然就真的覆盖了 等待编译完成,Naxsi 安装完成。 替换nginx二进制文件 cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak cp ./objs/nginx /usr/local/nginx/sbin/ 注:如果提示 cp: cannot create regular file ‘/usr/local/nginx/sbin/nginx’: Text file busy ,请先 service nginx stop 检查 nginx 模块 # nginx -V 看到有 –add-module=../naxsi-0.55.3/naxsi_src/ 就成功了。 nginx/naxsi 基本配置 首先将 naxsi 目录下的 naxsi_core.rules 拷贝至 nginx.conf 所在目录。 http 部分配置 打开 nginx.conf 在 http 部分配置: http { include naxsi_core.rules; #导入 naxsi 核心规则 ... } server 部分配置 在 nginx.conf 的 server 部分配置: location / { #开启 naxsi SecRulesEnabled; #开启学习模式 LearningMode; #定义阻止请求的位置 DeniedUrl "/50x.html"; #CheckRules, 确定 naxsi 何时采取行动 CheckRule "$SQL >= 8" BLOCK; CheckRule "$RFI >= 8" BLOCK; CheckRule "$TRAVERSAL >= 4" BLOCK; CheckRule "$EVADE >= 4" BLOCK; CheckRule "$XSS >= 8" BLOCK; #naxsi 日志文件 error_log /.../foo.log; ... } error_page 500 502 503 504 /50x.html; #This is where the blocked requests are going location = /50x.html { return 418; #I'm a teapot \o/ } server 完整示例配置 server { listen 80 default; access_log /wwwlogs/access_nginx.log combined; root /www/site; index index.html index.htm index.php; location ~ [^/]\.php(/|$) { SecRulesEnabled; #LearningMode; DeniedUrl "/RequestDenied"; CheckRule "$SQL >= 8" BLOCK; CheckRule "$RFI >= 8" BLOCK; CheckRule "$TRAVERSAL >= 4" BLOCK; CheckRule "$EVADE >= 4" BLOCK; CheckRule "$XSS >= 8" BLOCK; error_log /wwwlogs/foo.log; fastcgi_pass unix:/dev/shm/php-cgi.sock; fastcgi_index index.php; include fastcgi.conf; } location /RequestDenied { return 403; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ { expires 30d; access_log off; } location ~ .*\.(js|css)?$ { expires 7d; access_log off; } } 测试 测试 nginx 配置 /nginx/sbin/nginx -t nginx: the configuration file /nginx/conf/nginx.conf syntax is ok nginx: configuration file /nginx/conf/nginx.conf test is successful 重启 nginx 防御测试 浏览器中打开 http://www.test.com/?a=<>‘ ,出现 403 错误,并且在 foo.log 中出现 NAXSI_FMT 开头的日志。恭喜你 Naxsi 启用成功。 白名单规则 Naxsi 社区提供了一些常用的白名单规则,例如 wordpress 。可以在 https://github.com/nbs-system/naxsi-rules 下载白名单规则。 然后将规则 include 到 server 内的 location 中。重启 nginx 即可。不过目前这些白名单最近的修改日期显示是1年前~.~ ,可根据自身需要添加白名单规则。 详细的白名单规则以及 Naxsi 其他支持,可参考 Naxsi WIKI。
返回至
Nginx配置naxsi waf
。
导航
导航
首页
最近更改
随机页面
栏目
Nginx
Kubernetes
Spring Cloud
Wiki工具
Wiki工具
特殊页面
页面工具
页面工具
用户页面工具
更多
链入页面
相关更改
页面信息
页面日志