一, ModSecurity3.0 介绍
ModSecurity 是一个开源的跨平台 web 应用程序防火墙 (WAF) 引擎, 用于 Apache,IIS 和 Nginx, 由 Trustwave 的 SpiderLabs 开发. 作为 WAF 产品, ModSecurity 专门关注 HTTP 流量, 当发出 HTTP 请求时, ModSecurity 检查请求的所有部分, 如果请求是恶意的, 它会被阻止和记录.
优势:
完美兼容 nginx, 是 nginx 官方推荐的 WAF
支持 OWASP 规则
3.0 版本比老版本更新更快, 更加稳定, 并且得到了 nginx,Inc 和 Trustwave 等团队的积极支持
免费
ModSecurity 的功能:
SQL Injection (SQLi): 阻止 SQL 注入
Cross Site Scripting (XSS): 阻止跨站脚本攻击
Local File Inclusion (LFI): 阻止利用本地文件包含漏洞进行攻击
Remote File Inclusione(RFI): 阻止利用远程文件包含漏洞进行攻击
Remote Code Execution (RCE): 阻止利用远程命令执行漏洞进行攻击
PHP Code Injectiod: 阻止 PHP 代码注入
HTTP Protocol Violations: 阻止违反 HTTP 协议的恶意访问
HTTPoxy: 阻止利用远程代理感染漏洞进行攻击
Shellshock: 阻止利用 Shellshock 漏洞进行攻击
Session Fixation: 阻止利用 Session 会话 ID 不变的漏洞进行攻击
Scanner Detection: 阻止黑客扫描网站
Metadata/Error Leakages: 阻止源代码 / 错误信息泄露
Project Honey Pot Blacklist: 蜜罐项目黑名单
GeoIP Country Blocking: 根据判断 IP 地址归属地来进行 IP 阻断
劣势:
不支持检查响应体的规则, 如果配置中包含这些规则, 则会被忽略, nginx 的的 sub_filter 指令可以用来检查状语从句: 重写响应数据, OWASP 中相关规则是 95X.
不支持 OWASP 核心规则集 DDoS 规则 REQUEST-912-DOS- PROTECTION.conf,nginx 本身支持配置 DDoS 限制
不支持在审计日志中包含请求和响应主体
二, 安装部署
测试环境: centOS7.6 阿里云镜像
升级软件和内核
yum update
安装 nginx: http://nginx.org/en/linux_packages.html#mainline
- yum install yum-utils
- VIM /etc/yum.repos.d/nginx.repo
- [nginx-stable]
- name=nginx stable repo
- baseurl=
- gpgcheck=1
- enabled=1
- gpgkey= https://nginx.org/keys/nginx_signing.key
- [nginx-mainline]
- name=nginx mainline repo
- baseurl=
- gpgcheck=1
- enabled=0
- gpgkey= https://nginx.org/keys/nginx_signing.key
- yum install nginx
- yum install epel-release
- yum install gcc-c++ flex bison yajl yajl-devel curl-devel curl GeoIP-devel doxygen zlib-devel pcre pcre-devel libxml2 libxml2-devel autoconf automake lmdb-devel ssdeep-devel ssdeep-libs lua-devel libmaxminddb-devel Git apt-utils autoconf automake build-essential Git libcurl4-openssl-dev libgeoip-dev liblmdb-dev ibpcre++-dev libtool libxml2-dev libyajl-dev pkgconf wget zlib1g-dev
报错解决: Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again
解决办法: 一句话: 把 / etc/yum.repos.d/epel.repo, 文件第 3 行注释去掉, 把第四行注释掉, 修改为
- [epel]
- name=Extra Packages for Enterprise Linux 6 - $basearch
- baseurl= http://download.fedoraproject.org/pub/epel/6/ $basearch
- #mirrorlist= $basearch
克隆 GitHub 存储库:
Git clone --depth 1 -b v3/master --single-branch <a href="https://github.com/SpiderLabs/ModSecurity">https://github.com/SpiderLabs/ModSecurity</a>
编译源代码:
- $ cd ModSecurity
- $ Git submodule init
- $ Git submodule update
- $ ./build.sh
- $ ./configure
- $ make
- $ make install
注意: 安装中有报错 fatal: No names found, cannot describe anything. 是正常现象
下载用于 ModSecurity 的 NGINX 连接器:
Git clone --depth 1 <a href="https://github.com/SpiderLabs/ModSecurity-nginx.git">https://github.com/SpiderLabs/ModSecurity-nginx.git</a>
确定哪个版本的 NGINX 是运行在主机上的 ModSecurity 模块将加载:
- [root@guigu ModSecurity]# nginx -v
- nginx version: nginx/1.17.3
下载与安装版本对应的源代码:
- wget http://nginx.org/download/nginx-1.17.3.tar.gz
- tar zxvf nginx-1.17.3.tar.gz
编译动态模块, 复制到模块标准目录:
- cd nginx-1.17.3
- #./configure --with-compat --add-dynamic-module=../ModSecurity-nginx
- $ make modules
- cp objs/ngx_http_modsecurity_module.so /etc/nginx/modules/
将以下 load_module 指令添加到 / etc/nginx/nginx.conf 的 main 中:
load_module modules/ngx_http_modsecurity_module.so;
确定 nginx 模块加载成功:
nginx -t
三, 防护效果测试
ModSecurity 3 简单示例
创建 Demo Web 应用 VIM /etc/nginx/nginx.conf
- server {listen 8085;
- location / { default_type text/plain; return 200 "Thank you for requesting ${request_uri}\n"; }
- }
重新加载 nginx:nginx -s reload
确认 nginx 正常工作: curl -D - http://localhost/
保护 Demo Web 应用
创建 / etc/nginx/modsec 文件夹: mkdir /etc/nginx/modsec
下载推荐的 ModSecurity 配置文件
- wget
- mv modsecurity.conf-recommended modsecurity.conf
- VIM modsecurity.conf #在些文件中编辑以下配置
- SecRuleEngine DetectionOnly
- SecRuleEngine On
创建 ModSecurity 的主配置文件
- VIM /etc/nginx/modsec/main.conf
- Include the recommended configuration
- Include /etc/nginx/modsec/modsecurity.conf
- A test rule
- SecRule ARGS:testparam "@contains test" "id:1234,deny,log,status:403"
报错解决:[emerg] "modsecurity_rules_file" directive Rules error.
- VIM /etc/nginx/modsec/modsecurity.conf
- #SecUnicodeMapFile unicode.mapping 20127
配置 nginx 反向代理, VIM /etc/nginx/conf.d/proxy.conf
- #include /etc/nginx/conf.d/*.conf; #把这一行注释掉, 不然 80 端口会有冲突
- server { listen 80;
- modsecurity on; modsecurity_rules_file /etc/nginx/modsec/main.conf; location / { proxy_pass [http://0.0.0.0:8085;](http://0.0.0.0:8085/) proxy_set_header Host $host; }
- nginx -s reload #重新加载 nginx
- curl -D - http://localhost/foo?testparam=123 #能正常返回 "Thank you for requesting /foo?testparam=123"
- curl -D - http://localhost/foo?testparam=123 test #则返回 "403 Forbidden", 说明前面配置的那条 modsecuriy 规则生效了, 并阻拦了 testparam 参数中带 test 的请求
在 / var/log/nginx/error.log 中可以看到拦截的详细日志
部署 OWASP 规则 - CRS(Core Rule Set)
安装运行 nikto 漏洞扫描工具, 用于测试 CRS 的防御效果
- Git clone https://github.com/sullo/nikto #下载 nikto
- cd nikto
- perl program/nikto.pl -h localhost #用 nikto 扫描 nginx 搭建的 Web 系统(反向代理)
扫描结果是 + 7687 requests: 0 error(s) and 308 item(s) reported on remote host #扫描出 308 个问题
启用 OWASP CRS
- cd /etc/nginx/modsec/
- wget #下载 OWASP CRS
- cd owasp-modsecurity-crs-3.0.2/
- cp crs-setup.conf.example crs-setup.conf
在 modsecurity 主配置文件中 include CRS 的配置和规则
- VIM /etc/nginx/modsec/main.conf
- Include the recommended configuration
- Include /etc/nginx/modsec/modsecurity.conf
- OWASP CRS v3 rules
- Include /usr/local/owasp-modsecurity-crs-3.0.2/crs-setup.conf
- Include /usr/local/owasp-modsecurity-crs-3.0.2/rules/*.conf
测试 CRS
- nginx -s reload #重新加载 nginx 配置
- curl http://localhost/ #返回 Thank you for requesting /
- curl -H "User-Agent: Nikto" http://localhost/ #返回 403 Forbidden, 说明 WAF 防护已经生效, 此处匹配的规则是 user-agent 中不能包含漏洞扫描器名字
- perl nikto/program/nikto.pl -h localhost #再次用 nikto 扫描 nginx 搭建的 Web 系统
扫描结果是 + 7687 requests: 0 error(s) and 83 item(s) reported on remote host #扫描出 83 个问题, 比 308 个少了很多
在安装 ModSecurity 时, 我们将演示应用程序配置为为每个请求返回状态代码 200, 但实际上并没有返回这些文件, Nikto 将这 200 个状态码解释为它请求的文件确实存在, 所以报告出 83 个问题, 为了优化 nikto, 去除误报, 我们做如下配置
- cp nikto/program/nikto.conf.default nikto/program/nikto.conf
- VIM nikto/program/nikto.conf #在第 76 行最后加上;-sitefiles, 如下所示
- @@DEFAULT=@@ALL;-@@EXTRAS;tests(report:500);-sitefiles
之后再次用 nikto 扫描
perl program/nikto.pl -h localhost
扫描结果是 + 7583 requests: 0 error(s) and 7 item(s) reported on remote host
可以看出问题只有 7 个问题, 由于 ModSecurity 不支持响应 (response) 的检查, 所以涉及此类的漏洞无法防御. 但总体还是抵御了绝大部分的 nikto 的漏洞扫描.
参考链接:
- [https://github.com/SpiderLabs/ModSecurity](https://github.com/SpiderLabs/ModSecurity)
- [https://github.com/SpiderLabs/ModSecurity/tree/v3/master](https://github.com/SpiderLabs/ModSecurity/tree/v3/master)
- [http://www.modsecurity.cn/chm/index.html](http://www.modsecurity.cn/chm/index.html)
- [https://www.jianshu.com/p/d22f3914d15](https://www.jianshu.com/p/d22f3914d153)
来源: http://www.tuicool.com/articles/NriieiU