如何打造好用的ModSecurity系列 Part 1
字数 1106 2025-08-09 22:00:43

ModSecurity 安装与配置指南

一、ModSecurity 简介

ModSecurity 是一个开源的 Web 应用程序防火墙 (WAF),具有以下核心优势:

  1. 实时监控与攻击检测:实时监控 HTTP 流量,提供可自定义的日志记录功能
  2. 攻击防护与虚拟补丁:拦截已知漏洞攻击,通过行为评分系统跟踪可疑IP
  3. 灵活规则引擎:基于强大的 CRS (Core Rule Set) 规则体系
  4. 多种部署模式
    • 嵌入式模式(最小性能开销,直接处理HTTPS流量)
    • 基于网络的部署(可作为反向代理保护后端服务)
  5. 跨平台支持:可在多种操作系统上运行

二、安装准备

环境要求

  • 操作系统:Ubuntu 18.04(其他Linux发行版也可)
  • 基础依赖:
    apt-get install g++ flex bison curl doxygen libyajl-dev libgeoip-dev libtool dh-autoreconf libcurl4-gnutls-dev libxml2 libpcre++-dev libxml2-dev
    

三、Libmodsecurity 安装

  1. 获取源代码:

    cd /opt/
    git clone https://github.com/SpiderLabs/ModSecurity
    cd ModSecurity/
    git checkout -b v3/master origin/v3/master
    
  2. 编译安装:

    sh build.sh
    git submodule init
    git submodule update  # 初始化子模块
    ./configure
    make
    make install
    

安装完成后,库文件默认位于:/usr/local/modsecurity

四、Nginx 集成 ModSecurity

1. 安装带ModSecurity模块的Nginx

  1. 下载Nginx和ModSecurity-Nginx连接器:

    wget http://nginx.org/download/nginx-1.20.1.tar.gz
    git clone https://github.com/SpiderLabs/ModSecurity-nginx.git
    tar -zxvf nginx-1.20.1.tar.gz
    
  2. 编译安装:

    cd nginx-1.20.1/
    ./configure --add-module=/root/modsecurity-nginx/ --prefix=/usr/local/nginx 
    make && make install
    /usr/local/nginx/sbin/nginx -t  # 测试配置
    

2. 配置规则集

  1. 获取OWASP CRS规则:

    git clone https://github.com/coreruleset/coreruleset/
    cd coreruleset/
    cp crs-setup.conf.example crs-setup.conf
    cp /root/ModSecurity/modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
    
  2. 修改modsecurity.conf,添加规则引用:

    Include /root/coreruleset/crs-setup.conf
    Include /root/coreruleset/rules/*.conf
    
  3. 启用拦截功能:

    SecRuleEngine On
    
  4. 解决unicode.mapping缺失问题:

    cp /root/ModSecurity/unicode.mapping /usr/local/nginx/conf/
    

3. Nginx 主配置

修改/usr/local/nginx/conf/nginx.conf

server {
    modsecurity on;
    listen       1001;
    server_name  localhost;

    location / {
        modsecurity_rules_file /usr/local/nginx/conf/modsecurity.conf;
        root   html;
        index  index.html index.htm;
    }
}

启动Nginx:

/usr/local/nginx/sbin/nginx

五、Apache 集成 ModSecurity

1. 安装

apt update
apt install apache2
apt-get install libapache2-mod-security2

2. 配置文件

  • 模块加载配置:/etc/apache2/mods-enabled/security2.load
  • 主配置文件:/etc/apache2/mods-enabled/security2.conf

默认配置内容:

<IfModule security2_module>
    # 持久化数据目录
    SecDataDir /var/cache/modsecurity

    # 包含ModSecurity配置
    IncludeOptional /etc/modsecurity/*.conf

    # 包含OWASP CRS规则
    IncludeOptional /usr/share/modsecurity-crs/owasp-crs.load
</IfModule>

3. 自定义规则配置

  1. 获取最新CRS规则(同Nginx部分)

  2. 修改security2.conf引用新规则:

    IncludeOptional /root/coreruleset/*.conf
    IncludeOptional /root/coreruleset/rules/*.conf
    
  3. 重启Apache:

    systemctl restart apache2
    

六、日志分析

1. Nginx 日志

  • 错误日志位置:/usr/local/nginx/logs/error.log
  • 示例拦截日志:
    2021/06/03 15:50:25 [error] 29019#0: *13 
    [client 1.1.1.1] 
    ModSecurity: Access denied with code 403 (phase 2). 
    Matched "Operator `Ge' with parameter `5' against variable `TX:ANOMALY_SCORE' (Value: `8' ) 
    [file "/root/coreruleset/rules/REQUEST-949-BLOCKING-EVALUATION.conf"] 
    [line "138"] 
    [id "949110"]
    [msg "Inbound Anomaly Score Exceeded (Total Score: 8)"] 
    [severity "2"] 
    [ver "OWASP_CRS/3.4.0-dev"]
    

2. Apache 日志

  • 错误日志位置:/var/log/apache2/error.log
  • 详细审计日志默认位置:/var/log/modsec_audit.log

七、测试验证

发送包含恶意内容的POST请求测试拦截功能:

POST / HTTP/1.1
Host: example.com
Content-Type: application/x-www-form-urlencoded

a=/bin/bash

正常情况应返回403拦截响应,并在日志中记录相应条目。

八、参考资源

  1. ModSecurity官方参考手册
  2. ModSecurity中文文档
  3. OWASP Core Rule Set仓库
ModSecurity 安装与配置指南 一、ModSecurity 简介 ModSecurity 是一个开源的 Web 应用程序防火墙 (WAF),具有以下核心优势: 实时监控与攻击检测 :实时监控 HTTP 流量,提供可自定义的日志记录功能 攻击防护与虚拟补丁 :拦截已知漏洞攻击,通过行为评分系统跟踪可疑IP 灵活规则引擎 :基于强大的 CRS (Core Rule Set) 规则体系 多种部署模式 : 嵌入式模式(最小性能开销,直接处理HTTPS流量) 基于网络的部署(可作为反向代理保护后端服务) 跨平台支持 :可在多种操作系统上运行 二、安装准备 环境要求 操作系统:Ubuntu 18.04(其他Linux发行版也可) 基础依赖: 三、Libmodsecurity 安装 获取源代码: 编译安装: 安装完成后,库文件默认位于: /usr/local/modsecurity 四、Nginx 集成 ModSecurity 1. 安装带ModSecurity模块的Nginx 下载Nginx和ModSecurity-Nginx连接器: 编译安装: 2. 配置规则集 获取OWASP CRS规则: 修改 modsecurity.conf ,添加规则引用: 启用拦截功能: 解决unicode.mapping缺失问题: 3. Nginx 主配置 修改 /usr/local/nginx/conf/nginx.conf : 启动Nginx: 五、Apache 集成 ModSecurity 1. 安装 2. 配置文件 模块加载配置: /etc/apache2/mods-enabled/security2.load 主配置文件: /etc/apache2/mods-enabled/security2.conf 默认配置内容: 3. 自定义规则配置 获取最新CRS规则(同Nginx部分) 修改 security2.conf 引用新规则: 重启Apache: 六、日志分析 1. Nginx 日志 错误日志位置: /usr/local/nginx/logs/error.log 示例拦截日志: 2. Apache 日志 错误日志位置: /var/log/apache2/error.log 详细审计日志默认位置: /var/log/modsec_audit.log 七、测试验证 发送包含恶意内容的POST请求测试拦截功能: 正常情况应返回403拦截响应,并在日志中记录相应条目。 八、参考资源 ModSecurity官方参考手册 ModSecurity中文文档 OWASP Core Rule Set仓库