nginx中安装和使用waf防火墙教程

Linux命令

nginx中安装和使用waf防火墙教程

2025-03-20 00:03


? NGINX WAF防火墙实战部署指南(2024新版) 一、WAF方案选型 推荐使用ModSecurity开源方案,其特点:

                                            




? NGINX WAF防火墙实战部署指南(2024新版)


一、WAF方案选型

推荐使用ModSecurity开源方案,其特点:

  • 支持OWASP CRS 3.3.4最新规则库(截至2024年7月)
  • 兼容NGINX 1.25.x版本
  • 提供SQL注入/XSS/CC攻击等200+防护策略

二、环境准备

# 安装编译依赖(Ubuntu/Debian示例)
sudo apt install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev libxml2 libxml2-dev libcurl4-openssl-dev

? 作用:安装编译NGINX和ModSecurity所需的开发库


三、源码编译安装

  1. 下载最新组件(2024年版本)
wget https://github.com/SpiderLabs/ModSecurity/releases/download/v3.0.11/modsecurity-v3.0.11.tar.gz
tar -zxvf modsecurity-v3.0.11.tar.gz
  1. 编译ModSecurity
cd modsecurity-v3.0.11
./build.sh
./configure --enable-standalone-module --disable-mlogc
make -j4

? 参数解析:--enable-standalone-module:生成NGINX专用模块-j4:使用4核并行编译加速

  1. 整合NGINX
# 进入NGINX源码目录
./configure --add-module=../modsecurity-v3.0.11/nginx/modsecurity \
            --with-http_ssl_module
make && sudo make install

? 注意:若已安装NGINX需先卸载或使用相同版本源码


四、基础防护配置

  1. 核心配置文件(/usr/local/nginx/conf/modsecurity.conf)
SecRuleEngine On  # 启用防护引擎
SecRequestBodyAccess On  # 允许检查请求体

# 包含OWASP核心规则集
Include /path/to/owasp-modsecurity-crs/crs-setup.conf
Include /path/to/owasp-modsecurity-crs/rules/*.conf
  1. NGINX服务层配置
http {
    modsecurity on;
    modsecurity_rules_file /usr/local/nginx/conf/modsecurity.conf;

    server {
        location / {
            # 启用WAF检测
            modsecurity_rules off;  # 全局开启时局部可关闭
            ...
        }
    }
}

⚠️ 注意:modsecurity_rules off可用于排除特定路径的检测


五、防护规则实战

  1. SQL注入拦截
SecRule ARGS "@detectSQLi" \
    "id:10001,phase:2,deny,status:403,msg:'SQL Injection Detected'"
  1. XSS攻击防护
SecRule REQUEST_HEADERS:User-Agent|REQUEST_HEADERS:Referer|ARGS|ARGS_NAMES|REQUEST_BODY \
    "@detectXSS" \
    "id:10002,phase:2,deny,msg:'XSS Attack Attempt'"
  1. CC攻击防御
# 60秒内超过100次请求触发拦截
SecAction "id:10003,phase:1,nolog,pass,initcol:ip=%{REMOTE_ADDR},setvar:ip.counter=0"

SecRule IP:COUNTER "@gt 100" \
    "id:10004,phase:1,deny,status:503,msg:'CC Attack Detected'"

六、日志分析与调试

  1. 日志格式配置
modsecurity_log /var/log/modsec_audit.log;
modsecurity_debug_log /var/log/modsec_debug.log;
  1. 日志样例解析
[2024-07-15 10:23:45] [id "10001"] [msg "SQL Injection Detected"] 
[data "1' OR '1'='1"] 
[hostname "example.com"] 
[uri "/login"] 
[unique_id "abcd1234"]

? 关键字段:攻击类型、触发规则ID、攻击载荷、来源URI


七、性能优化方案

  1. 规则集精简
# 禁用不相关规则
cd /path/to/owasp-modsecurity-crs/rules
mv REQUEST-900-EXCLUSION.conf.example REQUEST-900-EXCLUSION.conf

编辑文件添加排除规则:SecRuleRemoveById 941100 942100

  1. 启用缓存优化
modsecurity_ssdeep_optimize on;
modsecurity_hash_optimize on;

八、常见问题排查

  1. 误报处理
# 在modsecurity.conf中添加白名单
SecRuleUpdateTargetById 942100 !ARGS:comment_content
  1. 性能瓶颈分析
# 查看规则处理耗时
grep 'Audit log: ' /var/log/modsec_debug.log | awk '{print $8}'
  1. 紧急关闭WAF
modsecurity off;  # 全局关闭
modsecurity_rules off;  # 局部关闭

九、安全加固建议

  1. 定期更新规则库
cd /path/to/owasp-modsecurity-crs
git pull origin v3.3.4
  1. 启用GeoIP防护
SecGeoLookupDB /path/to/GeoLite2-Country.mmdb
SecRule GEO:COUNTRY_CODE "@pm CN" \
    "id:10005,phase:1,allow"

标签:
  • nginx
  • waf
© 蓝易云.