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

问题描述:

当我运行下面的代码:这个shellcode为什么会导致分段错误?

#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 

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

+0

如果将其更改为“const char code []',它会工作吗? – tangrs

+0

ahh @tangrs的窍门 – yasgur99

可写数据在静态存储(如您code阵列)通常在.data部,其通常标结束“不可执行”。标记阵列const所以它在.rodata RESP结束。 .text并且可以执行。

+0

工作。谢谢! – yasgur99

+0

@ yasgur99不要忘记将答案标记为已接受。 – fuz