配置 ModSecurity
ModSecurity 是一个开源的 Web 应用防火墙 (WAF),旨在保护 Web 应用免受恶意攻击,它由开源社区维护,支持多种编程语言和 Web 服务器,平台负载均衡器(ALB)支持配置 ModSecurity,允许在 Ingress 级别单独进行配置。
名词解释
| 名词 | 解释 |
|---|---|
| owasp-core-rules | OWASP 核心规则集是一个开源规则集,用于检测和防止常见的 Web 应用攻击。 |
操作步骤
通过为对应资源的 YAML 文件添加注解或配置 CR 的方式配置 ModSecurity。
方式一:添加注解
在对应 YAML 文件的 metadata.annotations 字段中添加如下注解配置 ModSecurity。
-
Igress-Nginx 兼容注解
注解 类型 适用对象 说明 nginx.ingress.kubernetes.io/enable-modsecurity bool Ingress 启用 ModSecurity。 nginx.ingress.kubernetes.io/enable-owasp-core-rules bool Ingress 启用 OWASP 核心规则集。 nginx.ingress.kubernetes.io/modsecurity-snippet string Ingress 用于标识每个请求的唯一事务 ID,便于日志记录和调试。 nginx.ingress.kubernetes.io/modsecurity-snippet string Ingress、ALB、FT、Rule 允许用户插入自定义的 ModSecurity 配置,以满足特定的安全需求。 -
ALB 特殊注解
注解 类型 适用对象 说明 alb.modsecurity.cpaas.io/use-recommend bool Ingress 启用或禁用推荐的 ModSecurity 规则,设置为 true时,将应用一组预定义的安全规则。alb.modsecurity.cpaas.io/cmref string Ingress 引用特定的配置,例如:可以通过指定 ConfigMap 的引用路径( $ns/$name#$section)来加载自定义的安全配置。
方式二:配置 CR
-
打开需要配置的 ALB、FT 或 Rule 配置文件。
-
在 spec.config 下按需增加下述字段。
{ "modsecurity": { "enable": true, # 启用或禁用 ModSecurity "transactionId": "$xx", # 使用来自 Nginx 的 ID "useCoreRules": true, # 添加 modsecurity_rules_file /etc/nginx/owasp-modsecurity-crs/nginx-modsecurity.conf "useRecommend": true, # 添加 modsecurity_rules_file /etc/nginx/modsecurity/modsecurity.conf "cmRef": "$ns/$name#$section" # 从 ConfigMap 添加配置 } } -
保存并应用配置文件。
相关说明
覆盖
若 Rule 中没有配置 ModSecurity,将尝试在 FT 中查找配置;若 FT 中也没有配置,那么将使用 ALB 中的配置。
配置示例
下述示例中部署了一个名为 waf-alb 的 ALB,以及一个名为 hello 的演示后端应用。同时,还部署了一个名为 ing-waf-enable 的 Ingress ,该 Ingress 定义了 /waf-enable 路由,并配置了 ModSecurity 规则,任何请求如果包含查询参数 test,并且这个参数的值中包含字符串 test,那么该请求将被阻止。
apiVersion: crd.alauda.io/v2
kind: ALB2
metadata:
name: waf-alb
namespace: cpaas-system
spec:
config:
loadbalancerName: waf-alb
projects:
- ALL_ALL
replicas: 1
type: nginx
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/enable-modsecurity: "true"
nginx.ingress.kubernetes.io/modsecurity-transaction-id: "$request_id"
nginx.ingress.kubernetes.io/modsecurity-snippet: |
SecRuleEngine On
SecRule ARGS:test "@contains test" "id:1234,deny,log"
name: ing-waf-enable
spec:
ingressClassName: waf-alb
rules:
- http:
paths:
- backend:
service:
name: hello
port:
number: 80
path: /waf-enable
pathType: ImplementationSpecific
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ing-waf-normal
spec:
ingressClassName: waf-alb
rules:
- http:
paths:
- backend:
service:
name: hello
port:
number: 80
path: /waf-not-enable
pathType: ImplementationSpecific
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello
spec:
replicas: 1
selector:
matchLabels:
service.cpaas.io/name: hello
service_name: hello
template:
metadata:
labels:
service.cpaas.io/name: hello
service_name: hello
spec:
containers:
- name: hello-world
image: docker.io/hashicorp/http-echo
imagePullPolicy: IfNotPresent
---
apiVersion: v1
kind: Service
metadata:
name: hello
spec:
internalTrafficPolicy: Cluster
ipFamilies:
- IPv4
ipFamilyPolicy: SingleStack
ports:
- name: http
port: 80
protocol: TCP
targetPort: 5678
selector:
service_name: hello
sessionAffinity: None
type: ClusterIP