C 运行外壳代码时出现分段错误

C 运行外壳代码时出现分段错误,c,gcc,buffer-overflow,shellcode,C,Gcc,Buffer Overflow,Shellcode,在深入研究外壳代码之前,我正在尝试外壳代码,因此我从外壳代码手册中找到了一个示例。示例如下: char shellcode[] = "\xeb\x1a\x5e\x31\xc0\x88\x46\x07\x8d\x1e\x89\x5e\x08\x89\x4 \x0c\xb0\x0b\x89\xf3\x8d\x4e\x08\x8d\x56\x0c\xcd\x80\xe8\xe1\xff\xff\xff\x2f\x62\x69 \x6e\x2f\x73\x68"; int main() { int

在深入研究外壳代码之前,我正在尝试外壳代码,因此我从外壳代码手册中找到了一个示例。示例如下:

char shellcode[] = "\xeb\x1a\x5e\x31\xc0\x88\x46\x07\x8d\x1e\x89\x5e\x08\x89\x4
\x0c\xb0\x0b\x89\xf3\x8d\x4e\x08\x8d\x56\x0c\xcd\x80\xe8\xe1\xff\xff\xff\x2f\x62\x69
\x6e\x2f\x73\x68";

int main() {

int *ret;
ret = (int *)&ret + 2;
(*ret) = (int)shellcode;
}
外壳代码应该产生一个外壳。但是我得到了一个分段错误。 我使用带有
-fno stack protector
-z execstack
选项的
gcc
编译器编译了该程序。我快速查看了
readelf
命令,很明显堆栈是可执行的

 GNU_STACK      0x000000 0x00000000 0x00000000 0x00000 0x00000 RWE 0x4

ret
是一个指针,当您声明它时,它不指向任何内存位置。 稍后,您试图通过向指针指向的位置添加2来为其赋值。(这是一个矛盾的语句)


我使用“gcc filename.cpp”命令编译以下代码。Ti的编译没有错误。我希望这将帮助您解决您的疑问

#include<stdio.h>

char shellcode[] = "\xeb\x1a\x5e\x31\xc0\x88\x46\x07\x8d\x1e\x89\x5e\x08\x89\x4\x0c\xb0\x0b\x89\xf3\x8d\x4e\x08\x8d\x56\x0c\xcd\x80\xe8\xe1\xff\xff\xff\x2f\x62\x69\x6e\x2f\x73\x68";

int main() {

int *ret;
ret = (int *) ret + 2; //I don't know why you had written this
ret = (int *)shellcode;

}
#包括
字符外壳代码[]=“\xeb\x1a\x5e\x31\xc0\x88\x46\x07\x8d\x1e\x89\x5e\x08\x89\x4\x0c\xb0\x0b\x89\xf3\x8d\x4e\x08\x8d\x56\x0c\xcd\x80\xe8\xe1\xff\xff\xff\xff\xff\x2f\x2f\x62\x69\x6e\x2f\x73\x68”;
int main(){
int*ret;
ret=(int*)ret+2;//我不知道你为什么写这个
ret=(int*)外壳代码;
}

True。现在清楚了。这就是为什么你总是需要别人来读你写的东西,因为他只会看到你的错误,而你每次重读代码都会忽略这些错误。问题代码旨在用
shellcode
的地址覆盖返回地址;这不是偶然的未定义的行为。(很明显,它不应该是可移植的或符合标准的C)@immibis你想回答这个问题吗?@T-D我们无法说服所有人我希望我回答了你的问题:)反勾用于引用代码,而不是增加强调。
#include<stdio.h>

char shellcode[] = "\xeb\x1a\x5e\x31\xc0\x88\x46\x07\x8d\x1e\x89\x5e\x08\x89\x4\x0c\xb0\x0b\x89\xf3\x8d\x4e\x08\x8d\x56\x0c\xcd\x80\xe8\xe1\xff\xff\xff\x2f\x62\x69\x6e\x2f\x73\x68";

int main() {

int *ret;
ret = (int *) ret + 2; //I don't know why you had written this
ret = (int *)shellcode;

}