为什么这个 shellcode 会导致分段错误?

Why is this shellcode causing a segmentation fault?

当我运行以下代码时:

#include <stdio.h>
#include <string.h>

char code[] = "\x31\xc0\x48\xbb\xd1\x9d\x96\x91\xd0\x8c\x97\xff\x48\xf7\xdb\x53\x54\x5f\x99\x52\x57\x54\x5e\xb0\x3b\x0f\x05";

int main()
{
    printf("len:%d bytes\n", strlen(code));
    (*(void(*)()) code)();
    return 0;
}

使用gcc编译器,我先简单编译使用

 gcc program.c -o program

当我 运行 遇到分段错误时。接下来,我尝试使用

进行编译
gcc -fno-stack-protector -z execstack -o test test.c

成功了,我得到了 shell。我想知道的是为什么我需要在编译时传递这些命令才能工作。我的目标是让它工作而不必传递这些命令。我怎样才能实现这个目标?

静态存储中的可写数据(例如您的 code 数组)通常在 .data 部分结束,该部分通常标记为“不可执行”。将数组标记为 const,因此它最终以 .rodata 结尾。 .text并且可以执行。