nginx添加modsecurity插件
文章目录
- 准备工作
- 安装依赖
- 下载ModSecurity并安装
- 安装nginx的插件/安装nginx
- 安装规则
- 测试
- 遇到的问题
ModSecurity的git地址
准备工作
安装依赖
-
yum install -y gcc make pcre-devel libxml2 libxml2-devel curl-devel httpd-devel libtool
如果安装过程中还有其他依赖百度安装即可
-
查看g++的版本:版本需要大于等于7.3,否则不支持C++17标准
[root@192 rules]# g++ --version g++ (GCC) 9.3.1 20200408 (Red Hat 9.3.1-2) Copyright (C) 2019 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
如果g++版本过低,则升级
升级g++方式
下载ModSecurity并安装
[root@localhost source]# git clone --recursive https://github.com/owasp-modsecurity/ModSecurity ModSecurity
完成后进入源码目录:
[root@localhost modsecurity]# cd ModSecurity/
安装
[root@localhost ModSecurity]# git submodule init
[root@localhost ModSecurity]# git submodule update
[root@localhost ModSecurity]# ./build.sh
[root@localhost ModSecurity]# ./configure
[root@localhost ModSecurity]# make
[root@localhost ModSecurity]# make install
安装nginx的插件/安装nginx
-
ModSecurity-nginx的代码站地址,它是nginx和modsecurity之间联动的桥梁
-
下载ModSecurity-nginx
[root@localhost modsecurity]# git clone https://github.com/owasp-modsecurity/ModSecurity-nginx.git
-
安装nginx
在执行配置文件的时候添加上ModSecurity-nginx
插件./configure --with-http_stub_status_module --with-http_ssl_module --add-module=/soft/ModSecurity-nginx # 在nginx的目录中执行,/soft/ModSecurity-nginx为ModSecurity-nginx的下载目录
安装规则
规则的代码站
-
下载规则
[root@localhost modsecurity]# git clone https://github.com/coreruleset/coreruleset.git
-
进入coreruleset下载目录复制规则的配置文件
cp crs-setup.conf.example crs-setup.conf
-
生成ModSecurity的配置文件并修改
cp /soft/ModSecurity/modsecurity.conf-recommended /soft/ModSecurity/modsecurity.conf
vi modsecurity.conf
# 打开引擎
-
配置modsecurity
vi /soft/ModSecurity/modsecurity.conf # 打开引擎 SecRuleEngine On# 在modsecurity.conf末尾处添加 include /soft/coreruleset/crs-setup.conf # 规则的配置文件 include /soft/coreruleset/rules/*.conf # 各项规则的配置文件
-
修改nginx配置文件
在nginx下的server文件中增加两行modsecurity on; modsecurity_rules_file /soft/ModSecurity/modsecurity.conf;
-
启动nginx,或者重启nginx
测试
访问http://192.168.96.128:81/%E4%BA%8C%E7%BA%A7%E8%8F%9C%E5%8D%95.html是可以的
在路径后面添加上?param=后访问http://192.168.96.128:81/%E4%BA%8C%E7%BA%A7%E8%8F%9C%E5%8D%95.html?param=%3Cscript%3Ealert(1);%3C/script%3E
返回403
遇到的问题
- 需要配置阿里的镜像地址,否则下载依赖时报错或者很慢
- 我是先安装的nginx,在安装的modsecurity,则需要重新配置nginx和重新编译,重新编译后会生成新的nginx启动文件,由于我把先安装的nginx启动文件复制到的PATH目录下,为了在任何目录执行,所以即使重新编译了nginx,重启nginx后通过
nginx -V
查看nginx的编译参数还是没有新的插件,因为是用的旧的nginx启动文件启动的,将新的启动文件复制到PATH目录下覆盖掉之前的即可,nginx源码安装的默认生成路径是/usr/local/nginx/sbin下
参考: https://www.cnblogs.com/architectforest/p/18396169