ModSecurity 安装编译与连接nginx


ModSecurity 安装编译与连接nginx

ModSecurity一个开源WAF应用,用于web应用防护。

CRS核心规则

网站:https://coreruleset.org/
基本概念:V2版本与apache使用,V3与nginx搭配使用
有两种工作方式:

  • 默认为异常匹配机制,匹配到异常规则,增加异常数值,在异常值到达一定程度就在入流量或者出流量进行拒绝操作,分为阶段一和阶段二,phase:1 and phase:2
  • 独立判断:类似IDS,匹配第一条请求异常就拒绝

日志记录:默认开启日志,记录到web服务器,然后还有一份详细ModSecurity审核日志在在ModSecurity.conf中的SecAuditLog下配置,这边默认/var/log/mod_security.log

默认匹配动作:

SecDefaultAction "phase:1,log,auditlog,pass"
SecDefaultAction "phase:2,log,auditlog,pass"

偏执级别:
1-4级
默认为1级,通用检测
2级,增加了SQL和XSS检测,有经验使用
3级-4级,高级人员使用。
在下面定义:

#SecAction \
#  "id:900000,\
#   phase:1,\
#   nolog,\
#   pass,\
#   t:none,\
#   setvar:tx.paranoia_level=1"

设置modsecurity对请求体进行操作。

每条规则都有异常评分值,一个请求可能匹配多条规则,然后异常值相加进行处理。
级别为:

- CRITICAL severity: Anomaly Score of 5.
#       Mostly generated by the application attack rules (93x and 94x files).
- ERROR severity: Anomaly Score of 4.
#       Generated mostly from outbound leakage rules (95x files).
- WARNING severity: Anomaly Score of 3.
#       Generated mostly by malicious client rules (91x files).
- NOTICE severity: Anomaly Score of 2.
#       Generated mostly by the protocol rules (92x files).
# 示例
#SecAction \
# "id:900100,\
#  phase:1,\
#  nolog,\
#  pass,\
#  t:none,\
#  setvar:tx.critical_anomaly_score=5,\
#  setvar:tx.error_anomaly_score=4,\
#  setvar:tx.warning_anomaly_score=3,\
#  setvar:tx.notice_anomaly_score=2"

设置入站或出站阈值:一般默认是5,根据敏感度可以增加阻塞,达到一定阈值就会拒绝请求。

#SecAction \
# "id:900110,\
#  phase:1,\
#  nolog,\
#  pass,\
#  t:none,\
#  setvar:tx.inbound_anomaly_score_threshold=5,\
#  setvar:tx.outbound_anomaly_score_threshold=4"

应用程序规则排除:wordpress,duwiki,nextcloud
HTTP策略设置:可以设置允许的HTTP版本、HTTP方法、允许的请求内容类型,禁止的文件扩展名(例如.bak、.sql)和请求头(例如Proxy),针对HTTP参数/上传限制,黑名单限制,请求数量限制,IP信誉限制,超时设置

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 https://github.com/SpiderLabs/ModSecurity/releases/download/v3.0.10/modsecurity-v3.0.10.tar.gz
tar -xzvf modsecurity-v3.0.10.tar.gz

# 进入文件夹
cd modsecurity-v3.0.10

# 编译
./configure

# 编译安装,时间可能比较久,看服务器性能
make
make install

#/usr/local/modsecurity/lib

# 安装完成后,修改modsecurity配置文件,在当前目录下找到示例配置文件
mv modsecurity.conf-recommended modsecurity.conf

vim modsecurity.conf
# 修改内容SecRuleEngine DetectionOnly为SecRuleEngine On 开启引擎防护检测

# 下载modsecurity-ngin模块
wget https://github.com/SpiderLabs/ModSecurity-nginx/releases/download/v1.0.3/modsecurity-nginx-v1.0.3.tar.gz
# 解压
tar -xzvf modsecurity-nginx-v1.0.3.tar.gz

# 文件位置
/keli/modsecurity-nginx

# 编译安装nginx,添加模块,指定上面的文件位置
./configure --user=nginx --group=nginx --prefix=/keli/nginx --with-http_stub_status_module --with-http_ssl_module --with-stream --with-http_gzip_static_module --with-http_sub_module --add-module=/keli/modsecurity-nginx/

# 
Configuration summary
  + using system PCRE2 library
  + using system OpenSSL library
  + using system zlib library

  nginx path prefix: "/keli/nginx"
  nginx binary file: "/keli/nginx/sbin/nginx"
  nginx modules path: "/keli/nginx/modules"
  nginx configuration prefix: "/keli/nginx/conf"
  nginx configuration file: "/keli/nginx/conf/nginx.conf"
  nginx pid file: "/keli/nginx/logs/nginx.pid"
  nginx error log file: "/keli/nginx/logs/error.log"
  nginx http access log file: "/keli/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"


# 下载owap规则集
# 签名文件wget https://github.com/coreruleset/coreruleset/releases/download/v3.3.5/coreruleset-3.3.5.tar.gz.asc
wget https://github.com/coreruleset/coreruleset/archive/refs/tags/v3.3.5.tar.gz

# 修改示例文件
mv crs-setup.conf.example crs-setup.conf

# 将文件复制到nginx配置文件目录中
mkdir /keli/nginx/conf/modsecurity
# 将modsecurity配置文件复制
cp modsecurity.conf /keli/nginx/conf/modsecurity
# 将owap规则集配置复制
cp crs-setup.conf /keli/nginx/conf/modsecurity
# 将规则目录也复制过去
cp -R rules /keli/nginx/conf/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
# unicode.mapping
cp unicode.mapping /keli/nginx/conf/modsecurity/

# 现在的目录结构
/keli/nginx/conf/modsecurity/
    crs-setup.conf(crs规则集配置文件)
    modsecurity.conf(modsecurity配置文件)
    unicode.mapping(modsecurity编码格式文件)
    rules/(crs规则)

# 在nginx配置文件中开启防护和导入配置规则
vim /keli/nginx/conf/nginx.conf

# 在http模块添加,rules_file 为rules目录的路径
    modsecurity on;
    modsecurity_rules_file /keli/nginx/conf/modsecurity/modsecurity.conf;

# 修改modsecurity.conf
# 最后添加调用CRS规则
Include /keli/nginx/conf/modsecurity/crs-setup.conf
Include /keli/nginx/conf/modsecurity/rules/*.conf

# 这边使用nginx服务器,思路大概是modsecurity调用crs规则,然后在nginx调用modsecurity,将相关文件统一放在nginx配置目录下,修改后需要重启nginx进程。

# modsecurity审计日志位置
/var/log/modsec_audit.log
# nginx日志
error.log access.log

总结,在nginx调用引入modsecurity,然后在modsecurity调用引入crs,查看日志/var/log/modaudit.log

apache应用

modsecurity可以应用在nginx和apache等web服务器上,调用方式有一些差别。

/usr/local/modsecurity/lib/libmodsecurity.so 应该要动态加载这个模块


Chuck 2023年9月7日 16:29 收藏文档