漏洞评级
高危漏洞
确认受影响版本
0.6.18 – 1.20.0
确认修复版本
1.21.0、1.20.1
厂商
F5, Inc
厂商官网
厂商参考资料
https://mailman.nginx.org/pipermail/nginx-announce/2021/000300.html
漏洞CVE
CVE-2021-23017
CWE
193
CVSS评分
8.1
漏洞概述及影响
在处理DNS响应时,ngx_resolver_copy()中的一个off-by-one错误将允许网络攻击者在堆分配的缓冲区中写入超出边界的点字符(‘.’, 0x2E)。配置解析程序原语时,响应nginx服务器DNS请求的DNS响应可能会触发该漏洞。
精心构造的数据包可以通过使用0x2E覆盖下一个堆块元数据的最低有效字节,此时,能够向nginx服务器提供DNS响应的网络攻击者可以实现拒绝服务攻击或远程代码执行攻击。
由于nginx中缺少DNS欺骗防御措施,并且在检查DNS事务ID之前调用了易受攻击的函数,因此远程攻击者可以通过在可行的时间内向目标服务器发送恶意DNS响应来利用该漏洞实施攻击。
漏洞成因分析
nginx的DNS解析器(core/ngx_resolver.c)可以在设置解析器原语时,通过DNS解析多个模块的主机名。
ngx_resolver_copy()会被调用以验证和解压缩DNS响应中包含的每个DNS域名,接收作为输入的网络包和指向正在处理的域名的指针,并在成功时返回指向包含未压缩域名的新分配缓冲区的指针。整个过程分为两步执行:
计算未压缩域名的大小len并验证输入数据包,丢弃包含128个以上指针或超出输入缓冲区边界指针的域名。
分配一个输出缓冲区,并将未压缩的域名复制到其中。
第1部分中的大小计算和第2部分中的域名解压之间的不匹配会导致len中的off-by-one错误,从而允许在name->data数据边界之外写入一个点字符。
当压缩域名的最后一部分包含指向NULL字节的指针时,就会发生计算错误的情况。虽然计算步骤只考虑标签之间的点,但每次处理标签并且下一个字符不是NULL时,解压缩步骤都会写入一个点字符。当标签后跟指向NULL字节的指针时,解压缩过程将如下:
// 1) copy the label to the output buffer, ngx_strlow(dst, src, n); dst += n; src += n; // 2) read next character, n = *src++; // 3) as its a pointer, its not NUL, if (n != 0) { // 4) so a dot character that was not accounted for is written out of bounds *dst++ = '.'; } // 5) Afterwards, the pointer is followed, if (n & 0xc0) { n = ((n & 0x3f) << 8) + *src; src = &buf[n]; n = *src++; } // 6) and a NULL byte is found, signaling the end of the function if (n == 0) { name->len = dst - name->data; return NGX_OK; }
如果计算出的大小正好与堆块大小对齐,则写入的点字符超出边界,将覆盖下一个堆块大小元数据的最低有效字节。这可能会修改下一个堆块的大小,但也会覆盖3个标志,从而清除PREV_INUSE并设置IS_MMAPPED。
==7863== Invalid write of size 1 ==7863== at 0x137C2E: ngx_resolver_copy (ngx_resolver.c:4018) ==7863== by 0x13D12B: ngx_resolver_process_a (ngx_resolver.c:2470) ==7863== by 0x13D12B: ngx_resolver_process_response (ngx_resolver.c:1844) ==7863== by 0x13D46A: ngx_resolver_udp_read (ngx_resolver.c:1574) ==7863== by 0x14AB19: ngx_epoll_process_events (ngx_epoll_module.c:901) ==7863== by 0x1414D4: ngx_process_events_and_timers (ngx_event.c:247) ==7863== by 0x148E57: ngx_worker_process_cycle (ngx_process_cycle.c:719) ==7863== by 0x1474DA: ngx_spawn_process (ngx_process.c:199) ==7863== by 0x1480A8: ngx_start_worker_processes (ngx_process_cycle.c:344) ==7863== by 0x14952D: ngx_master_process_cycle (ngx_process_cycle.c:130) ==7863== by 0x12237F: main (nginx.c:383) ==7863== Address 0x4bbcfb8 is 0 bytes after a block of size 24 alloc'd ==7863== at 0x483E77F: malloc (vg_replace_malloc.c:307) ==7863== by 0x1448C*4: ngx_alloc (ngx_alloc.c:22) ==7863== by 0x137AE4: ngx_resolver_alloc (ngx_resolver.c:4119) ==7863== by 0x137B26: ngx_resolver_copy (ngx_resolver.c:3994) ==7863== by 0x13D12B: ngx_resolver_process_a (ngx_resolver.c:2470) ==7863== by 0x13D12B: ngx_resolver_process_response (ngx_resolver.c:1844) ==7863== by 0x13D46A: ngx_resolver_udp_read (ngx_resolver.c:1574) ==7863== by 0x14AB19: ngx_epoll_process_events (ngx_epoll_module.c:901) ==7863== by 0x1414D4: ngx_process_events_and_timers (ngx_event.c:247) ==7863== by 0x148E57: ngx_worker_process_cycle (ngx_process_cycle.c:719) ==7863== by 0x1474DA: ngx_spawn_process (ngx_process.c:199) ==7863== by 0x1480A8: ngx_start_worker_processes (ngx_process_cycle.c:344) ==7863== by 0x14952D: ngx_master_process_cycle (ngx_process_cycle.c:130)
考虑到nginx中与用户控制器数据的丰富交互机会以及记录在案的先例,这个漏洞将有可能允许攻击者在某些操作系统和体系结构上执行远程代码。
漏洞利用PoC
漏洞利用PoC下载地址:【poc.py】
广大研究人员可以通过valgrind并运行nginx来对该漏洞进行测试:
valgrind --trace-children=yes objs/nginx -p ../runtime -c conf/reverse-proxy.conf
接下来,运行DNS服务器(默认监听端口1053):
python poc.py
触发请求并发送至目标服务器:
curl http://127.0.0.1:8080/
根据漏洞被触发时的堆内存布局,可能会出现几种不同形式的日志:
corrupted size vs. prev_size 2021/04/16 13:35:15 [alert] 2501#0: worker process 2502 exited on signal 6 (core dumped) malloc(): invalid next size (unsorted) 2021/04/16 13:35:34 [alert] 2525#0: worker process 2526 exited on signal 6 (core dumped)
不过,valgrind和AdressSanitizer都是能够检测到这种内存崩溃事件的。
所使用的nginx配置
daemon off; http{ access_log logs/access.log; server{ listen 8080; location / { resolver 127.0.0.1:1053; set $dns http://example.net; proxy_pass $dns; } } } events { worker_connections 1024; }
参考资料
https://googleprojectzero.blogspot.com/2016/12/chrome-os-exploit-one-byte-overflow-and.html
https://googleprojectzero.blogspot.com/2014/08/the-poisoned-nul-byte-2014-edition.html
https://www.slideshare.net/codeblue_jp/cb16-matsukuma-en-68459606
来源:freebuf.com 2021-06-07 13:47:33 by: Alpha_h4ck
请登录后发表评论
注册