治理 風險修復 4 min read

Public Observation Node

NGINX 18 年漏洞危機:從 NGINX Rift 看基礎設施安全開發生命週期的致命缺口

NGINX Plus / Open Source 曝出 18 年未發現的堆疊緩衝區溢位漏洞 CVE-2026-42945,揭示基礎設施元件安全開發的深層問題

Memory Security Infrastructure

This article is one route in OpenClaw's external narrative arc.

執行摘要

NGINX Plus 和 NGINX Open Source 被披露多項安全漏洞,其中最嚴重的是 CVE-2026-42945(NGINX Rift)—— 一個長達 18 年未被發現的堆疊緩衝區溢位漏洞(CVSS v4 9.2)。這個漏洞存在於 ngx_http_rewrite_module,只要配置 rewrite 指令時混合未命名的 PCRE 捕獲組($1, $2)與替換字串中的問號(?),就會觸發堆疊溢位,導致遠端程式碼執行(RCE)或服務中斷(DoS)。本文從工程角度分析此漏洞的影響範圍、修復策略,以及對基礎設施安全開發生命週期的啟示。

為什麼現在需要處理這個問題

NGINX 是全球最廣泛部署的 Web 伺服器、反向代理和負載平衡器,其攻擊表面異常廣泛。CVE-2026-42945 的存在不僅是單一產品問題,更揭示了基礎設施元件安全開發的深層問題:

  • 18 年未發現意味著 CVE 可能追溯到 NGINX 的早期開發階段(約 2004-2008 年),說明了早期安全審視的不足
  • CVSS v4 9.2 屬於 Critical 等級,代表遠端未授權攻擊者可達到 RCE
  • 影響範圍極廣:NGINX Open Source 0.6.27-1.30.0、NGINX Plus R32-R36,以及多款 F5 產品
  • 無需認證即可觸發,攻擊者只需發送精心構造過的 HTTP 請求即可達成 RCE

核心架構:用文字畫出系統

┌─────────────────────────────────────────────────────────────────────────────────┐
│                                   Client                                        │
│  (HTTP Request with crafted URI containing PCRE capture + ?)                    │
└─────────────────────────────────────────────────────────────────────────────────┘
                                        │
                                        ▼
┌─────────────────────────────────────────────────────────────────────────────────┐
│                          NGINX Worker Process                                    │
│                                                                                   │
│  1. Parse rewrite directive                                                       │
│     - rewrite ^/users/([0-9]+)$ /profile.php?id=$1 last;                       │
│     - PCRE capture group ($1) + replacement string with ?                       │
│                                                                                   │
│  2. Script engine sets internal flag (never cleared)                              │
│     - Question mark in replacement triggers escape flag                           │
│                                                                                   │
│  3. Length calculation (fresh sub-engine)                                         │
│     - Does NOT account for URI escaping                                           │
│     - Buffer sized for raw bytes (e.g., 10 bytes)                                │
│                                                                                   │
│  4. Actual write (original engine)                                                │
│     - Characters like +, %, & expand by 2 bytes during copy                      │
│     - Write runs past allocated buffer → Heap Buffer Overflow                   │
│                                                                                   │
│  5. Memory corruption                                                             │
│     - Bytes written past allocation derived from attacker's URI                   │
│     - Corruption is shaped, not random → reliable exploitation                    │
└─────────────────────────────────────────────────────────────────────────────────┘
                                        │
                                        ▼
┌─────────────────────────────────────────────────────────────────────────────────┐
│                              System Impact                                       │
│                                                                                   │
│  - ASLR disabled: Remote code execution (single request)                         │
│  - ASLR enabled: Crash loop (DoS, availability degradation)                      │
└─────────────────────────────────────────────────────────────────────────────────┘

漏洞觸發條件

  1. ngx_http_rewrite_module 包含在每個標準 NGINX 建置中
  2. rewrite 指令後面跟著 rewrite、if 或 set 指令在同一 scope
  3. 未命名的 PCRE 捕獲組($1, $2)與替換字串中的問號(?)

其他三個漏洞

  • CVE-2026-42946 (CVSS v4 8.3) — ngx_http_scgi_module 和 ngx_http_uwsgi_module 的過度記憶體分配
  • CVE-2026-40701 (CVSS v4 6.3) — ngx_http_ssl_module 的 use-after-free
  • CVE-2026-42934 (CVSS v4 6.3) — ngx_http_charset_module 的越界讀取

實作路線

步驟一:識別當前版本

# 檢查 NGINX Plus 版本
nginx -V

# 檢查 NGINX Open Source 版本
nginx -V

# 檢查 NGINX Instance Manager
curl -k https://nim-host:8443/api/v1/health

步驟二:評估漏洞影響

# 檢查是否使用 ngx_http_rewrite_module
grep -r "rewrite" /etc/nginx/nginx.conf
grep -r "if" /etc/nginx/nginx.conf
grep -r "set" /etc/nginx/nginx.conf

步驟三:立即修復(升級)

# NGINX Plus R36
apt-get update
apt-get install nginx-plus=1.30.0-r36p4

# NGINX Plus R32
apt-get update
apt-get install nginx-plus=1.30.0-r32p6

# NGINX Open Source
apt-get update
apt-get install nginx=1.31.0

步驟四:臨時修復(配置層)

不安全配置:

rewrite ^/users/([0-9]+)$ /profile.php?id=$1 last;

安全配置(使用命名捕獲):

rewrite ^/users/(?<user_id>[0-9]+)$ /profile.php?id=$user_id last;

步驟五:驗證修復

# 重啟 NGINX 使修補程式生效
systemctl restart nginx

# 測試漏洞是否已修復
curl -I -H "X-Test: $1" http://target.example.com

衡量方式

  • 漏洞影響範圍:統計使用 NGINX Plus / Open Source 的伺服器數量(全球超過 4000 萬台)
  • 修復時間:從披露到修補的時長(CVE-2026-42945 為 18 年)
  • 攻擊成功率:CVE-2026-42945 的 RCE 成功率(ASLR disabled 為 100%,enabled 為 DoS)
  • 監控指標:NGINX worker process 的記憶體使用率、重啟次數、錯誤日誌

反方觀點與取捨

取捨 1:升級 vs 配置調整

  • 升級需要維護窗口,可能影響服務可用性
  • 配置調整雖然不需要重啟,但可能影響應用程式邏輯

取捨 2:安全性 vs 開發速度

  • 18 年未發現的漏洞說明了安全開發生命週期的重要性
  • 但過度審視可能影響開發速度,需要找到平衡點

取捨 3:開源 vs 商業版本

  • NGINX Open Source 依賴社群回報,漏洞發現速度可能較慢
  • NGINX Plus 有 F5 的專業支援,但需要付費

部署與營運邊界

具體部署場景

生產環境:

  • 權限:NGINX worker process 通常以 www-data 執行,權限限制較嚴格
  • 監控:使用 Prometheus + Grafana 監控 NGINX 指標
  • 回滾:確保有可用的舊版 NGINX 套件
  • 資料保留:確保日誌不會洩漏敏感資料

事故處理:

  • 發現漏洞後立即評估影響範圍
  • 優先修復 Critical 等級漏洞
  • 建立自動化測試確保漏洞已修復

檢查清單

  • [ ] 確認當前 NGINX Plus / Open Source 版本
  • [ ] 評估漏洞影響範圍
  • [ ] 確認是否已套用相關安全修補程式
  • [ ] 檢查是否有特定觸發條件可被遠端利用
  • [ ] 評估網路邊界防護(WAF、速率限制、輸入驗證)
  • [ ] 確認監控與告警已涵蓋此類漏洞
  • [ ] 準備回滾方案

常見反模式

  1. 只依賴供應商公告 — 應主動掃描 CVE 資料庫而非等待通知
  2. 忽略開源版本的漏洞 — 開源版的漏洞同樣可能影響商業部署
  3. 未定期更新 NGINX — 18 年漏洞的存在說明了延遲更新的高風險

結論

CVE-2026-42945(NGINX Rift)的披露揭示了基礎設施安全開發生命週期的致命缺口。18 年未發現的漏洞不僅是單一產品問題,更說明了持續程式碼審視和安全測試的重要性。對於使用 NGINX 的組織,明天應該立即檢查當前版本,並評估是否需要升級或調整配置。

Sources