CentOS7.7下源码安装3.x.x版本ModSecurity
CentOS7.7下源码安装3.x.x版本ModSecurity+Nginx及配置策略
服务器操作系统:CentOS7.7 64位 需要安装依赖环境如下:
1.首先把需要得规则策略包丢到/usr/local目录下
下载链接:https://github.com/SpiderLabs/owasp-modsecurity-crs/tree/v3.3/dev/rules
2.安装依赖工具
yum install -y epel-release
yum install -y readline-devel curl-devel gcc gcc-c++ python-devel lua-devel doxygen perl yajl-devel GeoIP-devel lmdb-devel ssdeep-devel flex bison autoconf automake
3.安装Modsecurity
cd /usr/local
git clone https://github.com/SpiderLabs/ModSecurity
cd ModSecurity
git checkout -b v3/master origin/v3/master
git submodule init
git submodule update
sh build.sh
./configure
make
make install
4.安装Nginx和ModSecurity-nginx
cd /usr/local
git clone https://github.com/SpiderLabs/ModSecurity-nginx
wget http://nginx.org/download/nginx-1.16.1.tar.gz
tar -zxvf nginx-1.16.1.tar.gz
cd /usr/local/nginx-1.16.1
./configure --add-module=/usr/local/ModSecurity-nginx
make
make install
5.测试效果展示
启动nginx
/usr/local/nginx/sbin/nginx
测试访问:http://42.159.83.201/?id=2%27or%201-3
更改配置重载nginx
/usr/local/nginx/sbin/nginx -s reload
6.最终配置
mkdir /usr/local/nginx/conf/modsecurity/rules
cp /usr/local/Modsecurity/modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity/modsecurity.conf
cp /usr/local/Modsecurity/unicode.mapping /usr/local/nginx/conf/modsecurity/
解压上面下载好得策略规则包,将里面的crs-setup.conf.example复制到/usr/local/nginx/conf/modsecurity/下并重命名为crs-setup.conf
cp /usr/local/owasp-modsecurity-crs-3.3-dev/crs-setup.conf.example /usr/local/nginx/conf/modsecurity/crs-setup.conf
最终得目录下面有如下文件
[root@IT3 modsecurity]# ls
crs-setup.conf modsecurity.conf rules unicode.mapping
[root@IT3 modsecurity]# pwd
/usr/local/nginx/conf/modsecurity
上面的rules是目录,其他是文件
将下载的策略规则包解压后的rules文件夹里面的所有规则复制到/usr/local/nginx/conf/modsecurity/rules下,同时修改REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example与RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example两个文件的文件名,将".example"删除,这两个文件用于自定义规则;
编辑nginx.conf
在http或server节点中添加以下内容(在http节点添加表示全局配置,在server节点添加表示为指定网站配置)这里看具体需要,实际生产业务推荐写在server,这样方便控制
modsecurity on;
modsecurity_rules_file /usr/local/nginx/conf/modsecurity/modsecurity.conf;
编辑modsecurity.conf
SecRuleEngine DetectionOnly改为SecRuleEngine On
同时添加以下内容:
Include /usr/local/nginx/conf/modsecurity/crs-setup.conf
Include /usr/local/nginx/conf/modsecurity/rules/*.conf
7.重载nginx配置
/usr/local/nginx/sbin/nginx -s reload
测试访问:http://42.159.83.201/?id=2%27or%201-3
8.其他补充
如果要确保ModSecurity v3.0.3在记录审计日志时保存请求体,SecAuditLogParts需要添加配置C,而不是IJ,否则审计日志将无法记录请求体