首页 / 平台管理 / 网络管理 / 负载均衡器 / 配置 ModSecurity

配置 ModSecurity

ModSecurity 是一个开源的 Web 应用防火墙 (WAF),旨在保护 Web 应用免受恶意攻击,它由开源社区维护,支持多种编程语言和 Web 服务器,平台负载均衡器(ALB)支持配置 ModSecurity,允许在 Ingress 级别单独进行配置。

名词解释

名词 解释
owasp-core-rules OWASP 核心规则集是一个开源规则集,用于检测和防止常见的 Web 应用攻击。

操作步骤

通过为对应资源的 YAML 文件添加注解或配置 CR 的方式配置 ModSecurity。

方式一:添加注解

在对应 YAML 文件的 metadata.annotations 字段中添加如下注解配置 ModSecurity。

方式二:配置 CR

  1. 打开需要配置的 ALB、FT 或 Rule 配置文件。

  2. 在 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 添加配置
     }
    }
  3. 保存并应用配置文件。

相关说明

覆盖

若 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