ModSecurity
ModSecurity 是一个开源的 Web 应用防火墙(WAF),旨在保护 Web 应用免受恶意攻击。它由开源社区维护,支持多种编程语言和 Web 服务器。平台负载均衡器(ALB)支持配置 ModSecurity,允许在 Ingress 级别进行单独配置。
目录
术语
术语 | 说明 |
---|
owasp-core-rules | OWASP Core Rule Set 是一个开源规则集,用于检测和防止常见的 Web 应用攻击。 |
操作步骤
通过向对应资源的 YAML 文件添加注解或配置 CR 来配置 ModSecurity。
方法一:添加注解
在对应 YAML 文件的 metadata.annotations 字段中添加以下注解以配置 ModSecurity。
-
Ingress-Nginx 兼容注解
注解 | 类型 | 适用对象 | 说明 |
---|
nginx.ingress.kubernetes.io/enable-modsecurity | bool | Ingress | 启用 ModSecurity。 |
nginx.ingress.kubernetes.io/enable-owasp-core-rules | bool | Ingress | 启用 OWASP Core Rule Set。 |
nginx.ingress.kubernetes.io/modsecurity-transaction-id | 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,定义了 /waf-enable
路由并配置了 ModSecurity 规则。任何包含查询参数 test
且其值包含字符串 test
的请求都会被阻止。
cat <<EOF | kubectl apply -f -
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
EOF