nginx配置OWASP CRS 防止跨站,sql注入,远程执行命令等
1. 基本定义与作用
OWASP CRS 是一套通用的攻击检测规则集,专为与 ModSecurity 或兼容的 Web 应用防火墙(WAF)配合使用而设计。它旨在作为 Web 应用的第一道防线,以最少的误报来保护 Web 应用免受各种攻击。
2. 核心防护能力
CRS 采用分层防御策略,覆盖了 OWASP Top 10 中列出的大多数安全威胁。除了您提到的跨站脚本(XSS)外,它还能有效防御:
SQL 注入攻击
远程命令执行(RCE)
本地/远程文件包含(LFI/RFI)
PHP 注入攻击
会话固定攻击
协议合规性检查与数据泄露防护等
 
3 安装 OWASP CRS
# 到本地源目录下 我这里使用的ubuntu 系统。使用的是root用户
cd /usr/local/src
git clone --depth 1 -b v3/master --single-branch https://github.com/SpiderLabs/ModSecurity
cd ModSecurity/
git submodule update --init --recursive
./configure 
make && make install 
# 检查安装是否成功
ldconfig -p | grep modsecurity
# 如果没有提示
echo "/usr/local/modsecurity/lib" > /etc/ld.so.conf.d/modsecurity.conf
# 如果有提示  则表示安装成功
libmodsecurity.so.3 (libc6,x86-64) => /usr/local/modsecurity/lib/libmodsecurity.so.3
libmodsecurity.so (libc6,x86-64) => /usr/local/modsecurity/lib/libmodsecurity.so
3. nginx 安装扩展
(1)下载 扩展
git clone --depth 1 https://github.com/SpiderLabs/ModSecurity-nginx.git
(2) 如果已经安装了nginx
#  要看一下现有的配置
Nginx -V
 ./configure  --user=www --group=www --prefix=/usr/local/nginx   --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_v3_module --with-http_gzip_static_module --with-http_sub_module --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_realip_module --with-openssl=/usr/local/openssl-3.5.6 --with-ld-opt="-L/usr/local/openssl-3.5.6/lib -Wl,-rpath,/usr/local/openssl-3.5.6/lib"
# 补充
--with-compat --add-dynamic-module=/usr/local/src/ModSecurity-nginx 
到对应nginx 版本安装目录下运行 ./configure ....【加上面所有参数】
(2) 如果是新安装 
直接对应configure 根据需要调整并添加 --with-compat --add-dynamic-module=/usr/local/src/ModSecurity-nginx 
./make
# 如果不是新安装就不要你 make install
cd objs/
mkdir -p /usr/local/nginx/moduls/
cp ngx_http_modsecurity_module.so /usr/local/nginx/moduls/
chmod +x /usr/local/nginx/moduls/ngx_http_modsecurity_module.so
 
(3) 增加nginx 配置。在nginx.conf 配置  与http,user 同级别 
load_module /usr/local/nginx/moduls/ngx_http_modsecurity_module.so;
 
(4) 测试nginx 配置,看是否报错。如果没有报错,则load 对应模块正常。
 
4. 下载 OWASP CRS Rules,配置安全模块的规则
(1)下载Rules https://github.com/coreruleset/coreruleset/releases 下载的是coreruleset-4.27.0-minimal.tar.gz
wget https://github.com/coreruleset/coreruleset/releases/download/v4.27.0/coreruleset-4.27.0-minimal.tar.gz
tar -xvf coreruleset-4.27.0-minimal.tar.gz
mv coreruleset-4.27.0/ [path]/nginx/conf/coreruleset
cd coreruleset/
cp crs-setup.conf.example crs-setup.conf
# 创建配置文件 modsecurity.conf 配置入下,主要是日志位置。如果遇到访问不到需要解决对应url。
引入的文件建议用绝度路径,如果是相对路径注意启动nginx命令时所在目录。
SecRuleEngine On
SecAuditLog /home/wwwlogs/modsec_audit.log
SecAuditEngine RelevantOnly
SecAuditLogType Serial
SecAuditLogParts ABCEFHZ
SecRuleRemoveById 911100

# 为 /static/js/ 路径下的文件忽略 LFI 规则 930130 的检查
SecRule REQUEST_URI "@beginsWith /static/js/" \
    "id:1005,\
    phase:1,\
    pass,\
    nolog,\
    ctl:ruleRemoveById=930130"

Include coreruleset/crs-setup.conf
Include coreruleset/rules/*.conf
~
 
5. 配置nginx.conf 配置文件,重启应用。
在nginx.conf 配置 http 模块下 新增
引入的文件建议用绝度路径,如果是相对路径注意启动nginx命令时所在目录。
modsecurity on;
modsecurity_rules_file ./modsecurity.conf;
测试配置,重启nginx 没有报错即可。
 
6. 测试 
 curl  -I "https://www.forasp.cn/?id=<script>alert(1)</script>"
提示 403 Forbidden 即成功
 
7.  如果遇到无法访问,或者访问不到的,需要通过查看日志,确定是正常url 则将uri 放开