Nginx HTTP/2和mp4模块拒绝服务漏洞预警
2018-11-09 18:00

Nginx HTTP/2和mp4模块拒绝服务预警

报告编号:B6-2018-110901

报告来源:360-CERT

报告作者:360-CERT

更新日期:2018-11-09

0x00 事件背景

Nginx 11月6日的安全更新中,修补了三个可导致拒绝服务的漏洞:CVE-2018-16843,CVE-2018-16844和CVE-2018-16845。位于nginx HTTP/2 模块和流媒体MP4模块。

CVE-2018-16843,CVE-2018-16844漏洞存在于ngx_http_v2模块之中(默认情况下不编译,编译时需要开启--with-http_v2_module,同时将listen http2添加到配置文件中),当用户添加http2支持时,攻击者可以发送特制的HTTP/2请求,消耗CPU和内存资源,最终导致DoS。

CVE-2018-16845漏洞存在于ngx_http_mp4_module模块中,当用户对Nginx添加MP4流媒体支持,恶意的MP4文件会导致处理进程无限循环、崩溃或者内存泄露。

0x01 影响范围

CVE-2018-16843,CVE-2018-16844影响版本:

  • Mainline version :1.9.5 - 1.15.5

CVE-2018-16845 影响版本:

  • Mainline version :1.1.3+, 1.0.7+

0x02 补丁分析

file:src/http/v2/ngx_http_v2.c src/http/v2/ngx_http_v2.h

--- a/src/http/v2/ngx_http_v2.c    Tue Nov 06 16:29:35 2018 +0300
+++ b/src/http/v2/ngx_http_v2.c    Tue Nov 06 16:29:49 2018 +0300
@@ -4481,12 +4481,19 @@

 #endif

+    h2scf = ngx_http_get_module_srv_conf(h2c->http_connection->conf_ctx,
+                                         ngx_http_v2_module);
+
+    if (h2c->idle++ > 10 * h2scf->max_requests) {
+        ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
+                      "http2 flood detected");
+        ngx_http_v2_finalize_connection(h2c, NGX_HTTP_V2_NO_ERROR);
+        return;
+    }
+
     c->destroyed = 0;
     ngx_reusable_connection(c, 0);
-    h2scf = ngx_http_get_module_srv_conf(h2c->http_connection->conf_ctx,
-                                         ngx_http_v2_module);
-
     h2c->pool = ngx_create_pool(h2scf->pool_size, h2c->connection->log);
     if (h2c->pool == NULL) {
         ngx_http_v2_finalize_connection(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
--- a/src/http/v2/ngx_http_v2.c    Tue Nov 06 16:29:18 2018 +0300
+++ b/src/http/v2/ngx_http_v2.c    Tue Nov 06 16:29:35 2018 +0300
@@ -664,6 +664,7 @@
     h2c->pool = NULL;
     h2c->free_frames = NULL;
+    h2c->frames = 0;
     h2c->free_fake_connections = NULL;
 #if (NGX_HTTP_SSL)
@@ -2895,7 +2896,7 @@
         frame->blocked = 0;
-    } else {
+    } else if (h2c->frames < 10000) {
         pool = h2c->pool ? h2c->pool : h2c->connection->pool;
         frame = ngx_pcalloc(pool, sizeof(ngx_http_v2_out_frame_t));
@@ -2919,6 +2920,15 @@
         frame->last = frame->first;
         frame->handler = ngx_http_v2_frame_handler;
+
+        h2c->frames++;
+
+    } else {
+        ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
+                      "http2 flood detected");
+
+        h2c->connection->error = 1;
+        return NULL;
     }
 #if (NGX_DEBUG)

为了修补CVE-2018-16843,CVE-2018-16844漏洞,新增了idle和frames变量统计请求数,每个子进程处理的请求数远大于配置的max_requests或者超出10000,将不进行处理。

CVE-2018-16845:

file:src/http/modules/ngx_http_mp4_module.c

--- src/http/modules/ngx_http_mp4_module.c
+++ src/http/modules/ngx_http_mp4_module.c
@@ -942,6 +942,13 @@ ngx_http_mp4_read_atom(ngx_http_mp4_file
                 atom_size = ngx_mp4_get_64value(atom_header + 8);
                 atom_header_size = sizeof(ngx_mp4_atom_header64_t);

+                if (atom_size < sizeof(ngx_mp4_atom_header64_t)) {
+                    ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
+                                  "\"%s\" mp4 atom is too small:%uL",
+                                  mp4->file.name.data, atom_size);
+                    return NGX_ERROR;
+                }
+
             } else {
                 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
                               "\"%s\" mp4 atom is too small:%uL",

MP4文件由若干称为Atom(或称为box)的数据对象组成,每个Atom的头部为四个字节的数据长度(Big Endian)和四个字节的类型标识,数据长度和类型标志都可以扩展。Atom可以嵌套,即其数据域可以由若干其它Atom组成,从而实现结构化的数据。为了修补CVE-2018-16845,在MP4模块中对文件的Atom头部结构进行检查,过滤掉恶意的MP4文件。

0x03 修复建议

关闭http/2请求处理和MP4流媒体支持,强烈建议将Nginx 升级至1.15.6,或1.14.1 stable 最新版本。

0x04 时间线

2018-11-06 Nginx 发布版本更新

2018-11-09 360CERT 发布预警公告

0x05 参考链接

  1. nginx security advisory (CVE-2018-16845)
  2. nginx security advisory (CVE-2018-16843, CVE-2018-16844)