#modsecurity #nginx
ModSecurity 简介
ModSecurity 本质上就是 网络应用防火墙web application firewall(WAF)引擎。它与 Apache、IIS 和 Nginx 兼容,并由第三方公司维护。该防火墙会将一份规则列表与由 Web 服务器/代理提供的 HTTP 头流进行交叉引用。目前这个仓库已经被简化,只包含主要的 LibModSecurity 库。你可以直接在自己的服务器实现中调用这个库,或通过特定编程语言的封装进行调用。
其母公司的支持计划于 2024 年 7 月 1 日结束,之后这个项目将由开源社区维护。
功能介绍
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:阻止利用远程代理感染漏洞进行攻击
Sshllshock:阻止利用Shellshock漏洞进行攻击
Session Fixation:阻止利用Session会话ID不变的漏洞进行攻击
Scanner Detection:阻止黑客扫描网站
Metadata/Error Leakages:阻止源代码/错误信息泄露
Project Honey Pot Blacklist:蜜罐项目黑名单
GeoIP Country Blocking:根据判断IP地址归属地来进行IP阻断 #这个3.0.4好像不支持
REQUEST-910-IP-REPUTATION.conf. Line: 75. Column: 22. This version of ModSecurity was not compiled with GeoIP or MaxMind support. in /etc/nginx/nginx.conf:32
安装步骤
- YUM安装nginx
- 下载安装modsecurity
- 下载安装modsecurity-nginx 连接器
- 下载新版nginx源码包
- 编译nginx并添加modsecurity-nginx模块
- 下载规则包
- 复制modsecurity.conf-recommended,unicode.mapping,crs-setup.conf和rules规则文件到nginx配置目录
- 配置nginx.conf,modsecurity.conf启用安全防护
安装modsecurity
- 安装所需依赖
yum install -y gcc-c++ flex bison yajl yajl-devel curl-devel curl GeoIP-devel doxygen zlib-devel pcre-devel lmdb-devel libxml2-devel ssdeep-devel lua-devel libtool autoconf automake wget epel-release openssl-devel
- 安装源码包
mkdir /data/modsecurity
cd /data/modsecurity
wget http://www.modsecurity.cn/download/modsecurity/modsecurity-v3.0.4.tar.gz
tar -zxvf modsecurity-v3.0.4.tar.gz
cp -r modsecurity-v3.0.4 /usr/local/modsecurity
cd /usr/local/modsecurity/
./configure
make && make install
安装modsecurity-nginx
modsecurity-nginx是 nginx 和 libmodsecurity 之间的连接器,需要安装。
- 下载安装
cd /usr/local/
wget https://codeload.github.com/SpiderLabs/ModSecurity-nginx/zip/refs/heads/master
unzip master
mv ModSecurity-nginx-master /usr/local/modsecurity-nginx // 改名
升级nginx并添加modsecurity-nginx模块
Nginx 不是一个模块化的设计,它使用的是模块化的方式来添加额外的功能。这意味着,如果你想要给 Nginx 添加一个新的模块,你需要重新编译 Nginx,并在编译过程中包含你想要的模块。
1. 下载新版本源码包
2. nginx -V 查看对应参数--prefix,--sbin-path,--modules-path,--conf-path等,尽量保持一致,或只保留必要的模块
3. 并添加模块
./configure --prefix=/usr/local/nginx --add-module=/usr/local/modsecurity-nginx
3. 只编译不安装
make #只执行make命令就行,不需要执行make install,会生成模块,把旧模块进行替换
4. 替换nginx执行文件
# 备份原来的文件
[root@master nginx-1.22.0]# mv /usr/sbin/nginx /usr/sbin/nginx-old-1.12.2
# 将新文件拷贝到可执行目录下
[root@master nginx-1.22.0]# cp ./objs/nginx /usr/sbin/
5.启动nginx
[root@master nginx-1.22.0]# systemctl restart nginx
6. 重新查看nginx版本
[root@master nginx-1.22.0]# nginx -v
nginx version: nginx/1.22.0
###隐藏nginx中的Server版本信息
在nginx.conf中的http模块中加入以下代码即可。
server_tokens off;**
[[nginx升级更新配置步骤]]
问题:
报错1:
nginx: [emerg] module "/usr/lib64/nginx/modules/ngx_http_image_filter_module.so" version 1014001 instead of 1025004 in /usr/share/nginx/modules/mod-http-image-filter.conf:1
修复:重新安装一下模块,版本不兼容导致
cp objs/ngx_http_image_filter_module.so /usr/lib64/nginx/modules/
报错2:
./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.
修复:
yum install openssl openssl-devel -y
报错3:
./configure: error: the HTTP XSLT module requires the libxml2/libxslt
libraries. You can either do not enable the module or install the libraries.
修复:
yum install -y libxml2-devel libxslt-devel
报错4:
./configure: error: the HTTP image filter module requires the GD library.
You can either do not enable the module or install the libraries.
修复:
yum install -y gd-devel
报错5:
+ perl version: This is perl 5, version 26, subversion 3 (v5.26.3) built for x86_64-linux-thread-multi
Can't locate ExtUtils/Embed.pm in @INC (you may need to install the ExtUtils::Embed module) (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5).
BEGIN failed--compilation aborted.
./configure: error: perl module ExtUtils::Embed is required
修复:
yum install -y perl
yum install perl-ExtUtils-Embed
报错6:
./configure: warning: the "--with-ipv6" option is deprecated
修复 :
系统已经关闭了ipv6
[root@Centos7-aliyunECS nginx-1.14.0]# sysctl -p
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1
报错7:
nginx: [alert] version 1.25.4 of nginx.pm is required, but 1.14.1 was found
修复:
cp /home/dimx/nginx/nginx-1.25.4/objs/src/http/modules/perl/blib/arch/auto/nginx/nginx.so /usr/lib64/perl5/vendor_perl/auto/nginx/
报错8:
nginx object version 1.14.1 does not match bootstrap parameter 1.25.4 at /usr/share/perl5/XSLoader.pm line 114.
修复:
mv /usr/lib64/perl5/vendor_perl/nginx.pm /usr/lib64/perl5/vendor_perl/nginx.pm.old
cp src/http//modules/perl/nginx.pm /usr/lib64/perl5/vendor_perl/
下载规则包
可以在官网下载最新规则包:[ModSecurity中文社区](http://www.modsecurity.cn/)
cd /data/modsecurity/
wget http://www.modsecurity.cn/download/corerule/owasp-modsecurity-crs-3.3-dev.zip
unzip owasp-modsecurity-crs-3.3-dev
mkdir /etc/nginx/conf.d/modsecurity
cp crs-setup.conf.example /etc/nginx/conf.d/modsecurity/crs-setup.conf
cp -r rules /etc/nginx/conf.d/modsecurity/
复制配置文件
以上安装均全完成,现在进入配置环节,将ModSecurity 下的配置文件 复制到nginx.config 目录,方便后期更改配置。
cp /usr/local/modsecurity/modsecurity.conf-recommended /etc/nginx/conf.d/modsecurity/modsecurity.conf // 重命名
cp /usr/local/modsecurity/unicode.mapping /etc/nginx/conf.d/modsecurity/ // 复制文件
1. 配置nginx.conf
vim /etc/nginx/nginx.conf
在http或server节点中添加以下内容(在http节点添加表示全局配置,在server节点添加表示为指定网站配置)。
modsecurity on;
modsecurity_rules_file /etc/nginx/conf.d/modsecurity/modsecurity.conf;
2. 配置modsecurity.conf
打开规则引擎SecRuleEngine On
#SecRuleEngine DetectionOnly
SecRuleEngine On
确保ModSecurity在记录审计日志时保存请求体IJ 改为 C
#SecAuditLogParts ABIJDEFHZ
SecAuditLogParts ABCDEFHZ
添加防护规则文件
Include /etc/nginx/conf.d/modsecurity/crs-setup.conf
Include /etc/nginx/conf.d/modsecurity/rules/*.conf
# 修改REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example与RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example两个文件的文件名,将".example"删除,可将自己写的规则放置于此两个文件中;
cd /etc/nginx/conf.d/modsecurity/rules/
mv REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
mv RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
测试规则
模拟攻击,测试未启动ModSecurity时的访问效果,访问URL为:http://服务器IP/?param="><script>alert(1);</script>
效果如下:
重新加载Nginx测试效果
/usr/local/nginx/sbin/nginx -s reload