使用 Traefik 中間件處理 Log4J 漏洞

Traefik 的中間件是最讓人喜歡的一個功能,爲了能夠擴展中間件,Traefik 官方還推出了 Pilot(https://pilot.traefik.io/) 這樣的中間件 SaaS 服務,和 Traefik 深度集成,我們可以非常方便的使用平臺上提供的各種中間件,在 Dashboard 上就可以無縫進行對接:

Log4Shell(https://github.com/traefik/plugin-log4shell) 就是一個解決最近很火的 Log4J 漏洞的 Traefik 插件,相關介紹:https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2021-44228。不過要使用該中間件需要 Traefik >= v.2.5.5 版本。

使用也是非常簡單的,首先通過靜態配置啓用該插件,可以通過 Traefik 啓動參數配置:

--pilot.token=xxx  # 去 pilot 註冊實例獲取的 token
--experimental.plugins.log4shell.modulename=github.com/traefik/plugin-log4shell
--experimental.plugins.log4shell.version=v0.1.2

或者配置文件:

pilot:
  token: xxx

experimental:
  plugins:
    log4shell:
      modulename: github.com/traefik/plugin-log4shell
      version: v0.1.2

爲了使用 Log4Shell 插件我們首先需要創建一箇中間件,比如在 Kubernetes 系統中,只需要創建如下所示的資源對象即可:

apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  name: log4shell-foo
spec:
  plugin:
    log4shell:
      errorCode: 200

然後在 IngressRoute 中關聯上上面的中間件即可修復:

apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
  name: whoami
spec:
  entryPoints:
    - web
  routes:
    - kind: Rule
      match: Host(`whoami.localhost`)
      middlewares:
        - name: log4shell-foo
      services:
        - kind: Service
          name: whoami-svc
          port: 80

當然如果使用的是默認的 Ingress 資源對象,則需要通過 annotation 註解來配置:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: myingress
  annotations:
    traefik.ingress.kubernetes.io/router.middlewares: default-log4shell-foo@kubernetescrd
spec:
  ingressClassName: traefik
  rules:
    - host: whoami.localhost
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name:  whoami
                port:
                  number: 80

同樣如果是使用 Docker 環境則需要通過 labels 標籤進行配置:

version: '3.7'

services:
  whoami:
    image: traefik/whoami:v1.7.1
    labels:
      traefik.enable: 'true'
      traefik.http.routers.app.rule: Host(`whoami.localhost`)
      traefik.http.routers.app.entrypoints: websecure
      traefik.http.routers.app.middlewares: log4shell-foo
      traefik.http.middlewares.log4shell-foo.plugin.log4shell.errorcode: 200
本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/CbGo7xNJkBkmu39voom77g