45
为 Nginx 配置 ModSecurity 网络应用防火墙

OSI(开放系统互联) 是最常被网络相关讨论引用的网络流量框架之一。当数据包通过第 6 层(表示层)移动到第 7 层(应用层)时,它会进行解密或解码操作。这些操作可能会因异常解码和解释而产生漏洞,而这些漏洞可能被利用来打破标准应用上下文。注入就是这种漏洞的一种类型,而且因为传统的 IDS/IPS 设备无法应对这些威胁,所以其长时间以来一直是人们特别关注的问题。

ModSecurity 简介

ModSecurity 本质上就是 网络应用防火墙(web application firewall)WAF)引擎。它与 Apache、IIS 和 Nginx 兼容,并由第三方公司维护。该防火墙会将一份规则列表与由 Web 服务器/代理提供的 HTTP 头流进行交叉引用。目前这个仓库已经被简化,只包含主要的 LibModSecurity 库。你可以直接在自己的服务器实现中调用这个库,或通过特定编程语言的封装进行调用。

其母公司的支持计划于 2024 年 7 月 1 日结束,之后这个项目将由开源社区维护。

安装 Nginx 连接器

Nginx 连接器 是一个 Nginx 动态模块,可以通过 Fedora 包 nginx-mod-modsecurity 进行安装。它依赖于 libmodsecurity.so,所以在这个使用场景中,这个包本身就是防火墙。

1. [user@fedora ~]$ sudo dnf install -y nginx nginx-mod-modsecurity

2. [user@fedora ~]$ rpm -qR nginx-mod-modsecurity

3. config(nginx-mod-modsecurity) = 1.0.3-3.fc38

4. libc.so.6(GLIBC_2.4)(64bit)

5. libmodsecurity.so.3()(64bit)

6. nginx(abi) = 1.24.0

7. nginx-filesystem

8. ...

安装完成后,你会见到连接器在 /etc/nginx 中添加了一些重要的文件。

1. [user@fedora ~]$ rpm -ql nginx-mod-modsecurity

2. /etc/nginx/modsecurity.conf                   # waf 配置

3. /etc/nginx/nginx.conf.modsecurity             # nginx 示例配置

4. /usr/lib64/nginx/modules/ngx_http_modsecurity_module.so

5. /usr/share/nginx/modules/mod-modsecurity.conf

6. /usr/share/doc/nginx-mod-modsecurity/README.md

7. ...

通过提供一些额外的配置指令,连接器对 Nginx 进行了扩展。下面的部分将演示 nginx.conf.modsecurity 文件中一些示例指令。指令的完整列表可以在 README.md 文件或项目的 GitHub 页面找到。

启动网络应用防火墙

nginx.conf.modsecurity 是我们将要运行的 Nginx 配置。解开如下所示的 modsecurity 行注释:

1. [user@fedora ~]$ sudo sed -i 's/#modsec/modsec/g' /etc/nginx/nginx.conf.modsecurity

2. [user@fedora ~]$ grep -C2 modsecurity /etc/nginx/nginx.conf.modsecurity

3.         # 如有需要,启用 ModSecurity WAF

4.         modsecurity on;

5.         # 如有需要,加载 ModSecurity CRS

6.         modsecurity_rules_file /etc/nginx/modsecurity.conf;

shell 中启动服务器并查看日志,确保在 modsecurity.conf 加载了七个默认规则。

1. [user@fedora ~]$ sudo nginx -c /etc/nginx/nginx.conf.modsecurity

2. [user@fedora ~]$ head /var/log/nginx/error.log

3. 2023/10/21 23:55:09 [notice] 46218#46218: ModSecurity-nginx v1.0.3 (rules loaded inline/local/remote: 0/7/0)

4. 2023/10/21 23:55:09 [notice] 46218#46218: using the "epoll" event method

5. 2023/10/21 23:55:09 [notice] 46218#46218: nginx/1.24.0

6. 2023/10/21 23:55:09 [notice] 46218#46218: OS: Linux 6.5.7-200.fc38.x86_64

通过发送一些不符合 Content-Type 头格式的数据来测试默认规则。

1. [user@fedora ~]$ curl -X POST http://localhost -H "Content-Type: application/json" --data "<xml></xml>"

2. [user@fedora ~]$ tail /var/log/modsec_audit.log

3. ...

4. ---rH5bFain---H--

5. ModSecurity: Warning. Matched "Operator `Eq' with parameter `0' against variable `REQBODY_ERROR' (Value: `1' ) [file "/etc/nginx/modsecurity.conf"] [line "75"] [id "200002"] [rev ""] [msg "Failed to parse request body."] [data "JSON parsing error: lexical error: invalid char in json text.\n"] [severity "2"] [ver ""] [maturity "0"] [accuracy "0"] [hostname "10.0.2.100"] [uri "/"] [unique_id "169795900388.487044"] [ref "v121,1"]

OWASP 核心规则集扩展你的网络应用防火墙

默认的 Nginx 连接器带有七条规则。OWASP Core Rule Set v3.3.5 则更为详尽,涵盖了许多场景。

复制并提取规则的存档。

1. [user@fedora ~]$ curl -fSL https://github.com/coreruleset/coreruleset/archive/refs/tags/v3.3.5.tar.gz --output /tmp/v3.3.5.tar.gz

2. [user@fedora ~]$ sudo tar -C /etc/nginx -xvf /tmp/v3.3.5.tar.gz

3. [user@fedora ~]$ tree -L 1 /etc/nginx/

4. /etc/nginx/

5. ├── conf.d

6. ├── default.d

7. ├── modsecurity.conf          # waf 配置

8. ├── nginx.conf

9. ├── nginx.conf.modsecurity    # nginx 启用 waf

10. ├── coreruleset-3.3.5

11. ├   ├── rules                 # 规则目录

12. ├       ...

13. ├   ...

现在,你在 Nginx 配置文件夹中有了一个包含所有当前 OWASP 规则的 rules 目录。接下来,让 Nginx 知道这些规则。以下操作指南来源于 OWASP INSTALL 文件。

创建一个 crs.conf 文件,并在全局网络应用防火墙配置文件( modsecurity.conf )中包含所有相关的配置文件。

1. [user@fedora ~]$ sudo cp /etc/nginx/coreruleset-3.3.5/crs-setup.conf.example /etc/nginx/coreruleset-3.3.5/crs.conf

2. [user@fedora ~]$ echo -e "\nInclude /etc/nginx/coreruleset-3.3.5/crs.conf"  | sudo tee -a /etc/nginx/modsecurity.conf

3. [user@fedora ~]$ echo -e "\nInclude /etc/nginx/coreruleset-3.3.5/rules/*.conf" | sudo tee -a /etc/nginx/modsecurity.conf

4. [user@fedora ~]$ tail /etc/nginx/modsecurity.conf

5. Include /etc/nginx/coreruleset-3.3.5/crs.conf

6. Include /etc/nginx/coreruleset-3.3.5/rules/*.conf

根据文档,包含这些文件的顺序很重要。上面的 tee 的命令将新的 Include 行放在了 modsecurity.conf 文件的末尾。现在,用这个新配置重启 Nginx。

1. [user@fedora ~]$ sudo nginx -s stop && sudo nginx -c /etc/nginx/nginx.conf.modsecurity

2. [user@fedora ~]$ tail /var/log/nginx/error.log

3. 2023/10/22 10:53:23 [notice] 202#202: exit

4. 2023/10/22 10:53:50 [notice] 230#230: ModSecurity-nginx v1.0.3 (rules loaded inline/local/remote: 0/921/0)

5. 2023/10/22 10:53:50 [notice] 230#230: using the "epoll" event method

6. 2023/10/22 10:53:50 [notice] 230#230: nginx/1.24.0

7. 2023/10/22 10:53:50 [notice] 230#230: OS: Linux 6.5.7-200.fc38.x86_64

8. 2023/10/22 10:53:50 [notice] 230#230: getrlimit(RLIMIT_NOFILE): 524288:524288

9. 2023/10/22 10:53:50 [notice] 231#231: start worker processes

注意,Nginx 成功加载了 921 条规则。还需要做一些测试来确保规则实际上是被网络应用防火墙检查过的。这里再次引用 INSTALL 文件中的 “Testing the Installation” 片段。

1. [user@fedora ~]$ curl 'http://localhost/?param=''><script>alert(1);</script>'

2. [user@fedora ~]$ tail /var/log/modsec_audit.log

3. ...

4. ---8NSpdnLe---H--

5. ModSecurity: Warning. detected XSS using libinjection. [file "/etc/nginx/coreruleset-3.3.5/rules/REQUEST-941-APPLICATION-ATTACK-XSS.conf"] [line "38"] [id "941100"] [rev ""] [msg "XSS Attack Detected via libinjection"] [data "Matched Data: XSS data found within ARGS:param: ><script>alert(1);</script>"] [severity "2"] [ver "OWASP_CRS/3.3.5"]

6. ...

结论

本文演示了如何为 Nginx 服务器配置网络应用防火墙。这个部署使用了标准规则和 OWASP Core Rule Set v3.3.5。演示的防火墙在检测模式中运行并记录不寻常的行为。将防火墙运行在防御模式要对 modsecurity.conf 进行更多改动。请参考 ModSecurity Reference Manual v3.x 获取如何启用防御模式和更多信息。

 


这条帮助是否解决了您的问题? 已解决 未解决

提交成功!非常感谢您的反馈,我们会继续努力做到更好! 很抱歉未能解决您的疑问。我们已收到您的反馈意见,同时会及时作出反馈处理!