k8s Nginx Ingress ControllerでWAF

セキュリティを考慮したKubernetesのNginx Ingress ControllerのIngress設定例です。
※ これが完璧な設定というわけではなく、あくまで一設定例です。

難しい設定はspecに書けないので、以下のようにannotationsに書きます。server_tokens でNginxのバージョン情報を隠してます。

さらにModSecurityを用いてWAFを構築しています。WAFのルールにはOWASPを使いつつ、自分のルールも書けるようにしています。

tlsやrulesの設定は環境に合わせて適宜修正してください。

---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: my-ingress
  namespace: myapp
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/configuration-snippet: |
    server_tokens off;
    nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
    nginx.ingress.kubernetes.io/enable-modsecurity: "true"
    nginx.ingress.kubernetes.io/modsecurity-transaction-id: "$request_id"
    nginx.ingress.kubernetes.io/modsecurity-snippet: |
      SecRuleEngine On
      SecRequestBodyAccess On
      SecAuditEngine RelevantOnly
      SecAuditLogParts ABIJDEFHKZ
      SecAuditLog /var/log/modsec_audit.log
      SecDebugLog /var/log/modsec_audit_debug.log
      Include /etc/nginx/owasp-modsecurity-crs/nginx-modsecurity.conf
spec:
  tls:
  - hosts:
    - example.com
    secretName: example-com-tls-secret
  rules:
  - host: example.com
    http:
      paths:
        - path: /
          backend:
            serviceName: my-service
            servicePort: 80

自分のルールを書きたい場合は、Include /etc/nginx/owasp-modsecurity-crs/nginx-modsecurity.conf の前後辺りに書くと良いでしょう。例えば、myconf.php へのアクセスを防ぎたいなら以下のような設定になると思います。

      Include /etc/nginx/owasp-modsecurity-crs/nginx-modsecurity.conf
      SecRule REQUEST_URI "myconf.php" "id:500001,phase:2,deny,log,status:405"
タイトルとURLをコピーしました